- 金錢
- 45
- 威望
- 3183
- 貢獻值
- 0
- 推廣值
- 0
- 性別
- 保密
- 在線時間
- 38 小時
- 最後登錄
- 2024-2-25
- 主題
- 0
- 精華
- 0
- 閱讀權限
- 70
- 註冊時間
- 2012-3-17
- 帖子
- 553
 
該用戶從未簽到 - 推廣值
- 0
- 貢獻值
- 0
- 金錢
- 45
- 威望
- 3183
- 主題
- 0
|
import numpy as np: `. p7 p; }5 _7 `" ?" ~$ ^2 s
import matplotlib.pyplot as plt
: c% P T4 b+ ]; T* H3 {
" N) P1 A6 B" i; y5 R+ m# a! v8 O3 Yimport utilities
8 O8 }: ]) B5 i8 l0 `+ F6 _ `4 h! {( n& L2 a
# Load input data: l2 d" l' _ j- M
input_file = 'D:\\1.Modeling material\\Py_Study\\2.code_model\\Python-Machine-Learning-Cookbook\\Python-Machine-Learning-Cookbook-master\\Chapter03\\data_multivar.txt'
: X# X5 A) Z. j$ g" \# @( } t. qX, y = utilities.load_data(input_file)
$ t. j6 z$ D+ a/ [0 x+ J8 F0 m8 R! ?# b1 \* h3 f
###############################################
9 x3 H% I8 o2 I/ H# Separate the data into classes based on 'y'7 g0 V( x' y8 Y" Y
class_0 = np.array([X[i] for i in range(len(X)) if y[i]==0])
" n. C G+ A( Yclass_1 = np.array([X[i] for i in range(len(X)) if y[i]==1]): u Y; o; Z3 U6 m b
- a8 _6 K* S2 f- [# Plot the input data# J `9 ^( \2 `9 N4 `% z6 ]
plt.figure()
; c3 z5 l% Q8 m4 Dplt.scatter(class_0[:,0], class_0[:,1], facecolors='black', edgecolors='black', marker='s')4 x# ]3 f9 p) Q" t$ T6 ~0 K: q" P! e$ B
plt.scatter(class_1[:,0], class_1[:,1], facecolors='None', edgecolors='black', marker='s')3 R# w- |- f, g" J6 U1 H! H: z
plt.title('Input data')
0 T2 p+ W- I9 M3 ^! Y: L
{; X! ^" b0 s+ ?###############################################
$ @4 @- d9 ]+ K# n: b" C' F8 @# Train test split and SVM training
5 E" f. f6 H# \& F1 C" R0 c$ sfrom sklearn import cross_validation
1 g7 O7 X! L' q$ l' [from sklearn.svm import SVC- d. v: P: s7 l' y% V: U; J
2 s5 `1 }$ j" w4 |# z
X_train, X_test, y_train, y_test = cross_validation.train_test_split(X, y, test_size=0.25, random_state=5)
9 u( q( g' G3 A0 j3 M; p3 ]* c9 j2 z! r- H+ u: t2 X! W
#params = {'kernel': 'linear'}
. G9 `, n, W* b ]0 h#params = {'kernel': 'poly', 'degree': 3}. r4 g9 Z6 \9 B1 s, G2 V: e) y
params = {'kernel': 'rbf'}7 N! n0 |6 M5 S5 }* ?) r. o4 y( O
classifier = SVC(**params)1 Q) n$ y, Q# H. g1 U( b. Y9 r, T
classifier.fit(X_train, y_train)9 B. w. A' Q$ @ [" o7 R9 z
utilities.plot_classifier(classifier, X_train, y_train, 'Training dataset')
% F/ C% W& ]# S& N# a0 Q f
- X1 }% f: D7 l2 ^+ |0 Ey_test_pred = classifier.predict(X_test)
* w$ c3 F! c1 n: Tutilities.plot_classifier(classifier, X_test, y_test, 'Test dataset'), H2 B1 P- K v J; i
4 h! I6 B0 O# ]) _6 Y% b1 \
###############################################: J* l& a$ x' i3 t
# Evaluate classifier performance& w' R& _( ~) |, c; f$ C
( ?- @+ r- G4 |; J
from sklearn.metrics import classification_report
, t4 F" h) c; M
# a4 N( z, r6 T1 v( ~4 W0 [target_names = ['Class-' + str(int(i)) for i in set(y)]8 R" t; b6 \+ s! {
print "\n" + "#"*30
4 F5 Q8 t6 q, w& a+ B1 H1 `" W, D+ Eprint "\nClassifier performance on training dataset\n"
/ R0 l" y+ p6 `3 k( Bprint classification_report(y_train, classifier.predict(X_train), target_names=target_names)5 s* O7 ^8 T: s( N4 r
print "#"*30 + "\n"$ }# q# H4 {( m- B0 @
+ J1 m5 ?5 H* Q- I* b5 P
print "#"*30
7 C: X, Y; U, P' h& ?+ Zprint "\nClassification report on test dataset\n"
) |: c/ b' H1 C u: a; Lprint classification_report(y_test, y_test_pred, target_names=target_names) S; d! u3 y7 w/ _: |1 A" D! L
print "#"*30 + "\n"" {: q3 C8 U6 H6 M" u
7 I" r$ L5 K2 K* i$ T
|
|