- 金錢
- 46
- 威望
- 3183
- 貢獻值
- 0
- 推廣值
- 0
- 性別
- 保密
- 在線時間
- 38 小時
- 最後登錄
- 2024-2-25
- 主題
- 0
- 精華
- 0
- 閱讀權限
- 70
- 註冊時間
- 2012-3-17
- 帖子
- 556
 
該用戶從未簽到 - 推廣值
- 0
- 貢獻值
- 0
- 金錢
- 46
- 威望
- 3183
- 主題
- 0
|
import numpy as np0 m. D$ Q k5 c6 q$ z- S3 K
import matplotlib.pyplot as plt' W9 N+ I% y: Z6 [' F" h
) L; ~& ?1 m) h6 J7 |* N
import utilities
' o& Y- `6 I1 a& K, }. g! n* ?
, I2 n: @# P3 R5 V! I" n# Load input data
8 }) |! s* P3 i3 D; Oinput_file = 'D:\\1.Modeling material\\Py_Study\\2.code_model\\Python-Machine-Learning-Cookbook\\Python-Machine-Learning-Cookbook-master\\Chapter03\\data_multivar.txt'5 Z# q/ i" P) M r+ S
X, y = utilities.load_data(input_file)1 k9 y9 q- x8 j8 f' P! ?0 F
0 p( ^" N5 [, S, {/ S+ N! M/ A
###############################################4 U/ R2 @) l" f) B' E! N& g
# Separate the data into classes based on 'y'
, g! x1 q. h. Jclass_0 = np.array([X[i] for i in range(len(X)) if y[i]==0])6 q2 C7 S! v) r9 S6 S9 ?8 {( f
class_1 = np.array([X[i] for i in range(len(X)) if y[i]==1])9 x" s' ]$ {8 P+ O- g
7 J. O+ `- v }( q4 K$ J
# Plot the input data
y' X Q! o& U+ }4 Y- Fplt.figure()
) Z" a0 s C% |( O. ^& B/ t$ Iplt.scatter(class_0[:,0], class_0[:,1], facecolors='black', edgecolors='black', marker='s')
8 `8 ~0 Z1 y3 }+ z; }plt.scatter(class_1[:,0], class_1[:,1], facecolors='None', edgecolors='black', marker='s'), q$ p$ {3 k0 N7 c
plt.title('Input data'): @5 N. G2 \: [2 m
# M! c0 t, S: [, |2 F8 A###############################################/ \6 X6 a6 d3 |" `* v2 [8 V0 e
# Train test split and SVM training- T' _/ L( S% X; I, X3 `
from sklearn import cross_validation
& Q0 w# o% N2 I3 ^; K4 Kfrom sklearn.svm import SVC; B$ n1 y- S1 @, }6 E" F- H9 b
! }, v1 o( h, j5 W% o/ iX_train, X_test, y_train, y_test = cross_validation.train_test_split(X, y, test_size=0.25, random_state=5)" z* j+ I1 I3 C) }5 [; C8 g
$ o" Y9 J" t9 E. L6 _#params = {'kernel': 'linear'}, K/ m8 m* e% ]' s: G: V$ ~8 D
#params = {'kernel': 'poly', 'degree': 3}
3 I& l& T" w* eparams = {'kernel': 'rbf'}. b1 _- x- z+ f7 Y( x
classifier = SVC(**params)
8 s3 @8 }, A1 n+ oclassifier.fit(X_train, y_train)3 }7 S1 p$ \3 y: g m. c$ }
utilities.plot_classifier(classifier, X_train, y_train, 'Training dataset')2 Z9 J2 @9 e, C1 r
; w0 z" b& c7 T/ s7 ? T- vy_test_pred = classifier.predict(X_test)+ o: M- e1 S, Z6 t% _
utilities.plot_classifier(classifier, X_test, y_test, 'Test dataset')
' Y9 D* V% _7 w# H/ A1 \. e) T+ {9 Y- _. A- h, l8 n/ S% O
###############################################
: y' i( b3 ` A& O+ ^ n# Evaluate classifier performance2 \5 D/ W: `. I8 V
0 j( _6 |* J8 C! k8 mfrom sklearn.metrics import classification_report
! L5 b2 m; Q4 r! O
/ S) I$ L3 q0 p: Q5 Ctarget_names = ['Class-' + str(int(i)) for i in set(y)]5 N. V2 Z4 ~, \3 n& o3 W: j
print "\n" + "#"*30
# h* L0 P/ @6 W7 y, Q5 h" @- v6 gprint "\nClassifier performance on training dataset\n"% ~4 g6 K6 L7 r' f2 ~/ v
print classification_report(y_train, classifier.predict(X_train), target_names=target_names)0 R) N' b, B x- r+ o' D
print "#"*30 + "\n"
# W4 P: P" u# H: t4 L% J! |3 g
) f7 `8 [( U$ G. m9 Bprint "#"*30
) B5 \. M( T3 X5 s) Nprint "\nClassification report on test dataset\n"9 k' a% x% u5 A. N5 U$ Q; ~
print classification_report(y_test, y_test_pred, target_names=target_names); |% x' W: l- ~
print "#"*30 + "\n"5 w3 |& J: X2 B. o
) }; J3 q. u' S8 W3 R/ X
|
|