- 金錢
- 45
- 威望
- 3183
- 貢獻值
- 0
- 推廣值
- 0
- 性別
- 保密
- 在線時間
- 38 小時
- 最後登錄
- 2024-2-25
- 主題
- 0
- 精華
- 0
- 閱讀權限
- 70
- 註冊時間
- 2012-3-17
- 帖子
- 553
 
該用戶從未簽到 - 推廣值
- 0
- 貢獻值
- 0
- 金錢
- 45
- 威望
- 3183
- 主題
- 0
|
import numpy as np4 }; ]: \8 @/ ~
import matplotlib.pyplot as plt
8 i9 p- q+ o8 Z( y# ^. H" j) Q( O; g3 D+ y+ g
import utilities
1 F3 U4 Y' W$ E) j% D4 [* L
U! s- B6 w& l: i5 y# x# Load input data( ^8 Z$ z/ A2 ?
input_file = 'D:\\1.Modeling material\\Py_Study\\2.code_model\\Python-Machine-Learning-Cookbook\\Python-Machine-Learning-Cookbook-master\\Chapter03\\data_multivar.txt'
' G! _0 Y2 O- x" }& X; s4 nX, y = utilities.load_data(input_file); Z* L$ @9 I, ~3 u+ U* V
0 |5 R' d! X/ P6 I I###############################################& W. H% u. @ o! k: t1 r
# Separate the data into classes based on 'y'
8 X: ]6 j; U3 {class_0 = np.array([X[i] for i in range(len(X)) if y[i]==0]). l5 i8 i) z& S5 ?% n
class_1 = np.array([X[i] for i in range(len(X)) if y[i]==1])$ |3 k7 Q6 }& r( A8 w1 S
7 z! } H0 W ^0 A# Q& \2 ]# Plot the input data3 d5 v) y0 V6 ?' z
plt.figure()& t2 p4 J5 m8 S+ q P8 R- `
plt.scatter(class_0[:,0], class_0[:,1], facecolors='black', edgecolors='black', marker='s')
& ]7 n" e8 x# r: _! s' B& kplt.scatter(class_1[:,0], class_1[:,1], facecolors='None', edgecolors='black', marker='s')
2 d7 O: a5 J5 Y5 r, f7 W* Yplt.title('Input data')# J: K; n* r) ^7 ]% k( s8 S
% \2 O! N+ _' M& g: b W
###############################################
0 {5 Y4 h0 g$ r. ]9 ~- v( w" U# Train test split and SVM training! U' s4 ?+ X( `# }0 D$ s: x5 w* g* {
from sklearn import cross_validation7 T) m# x% S$ j4 K
from sklearn.svm import SVC
6 l5 Z1 X# n) D% Q" v
6 D9 Y$ X- P* P& fX_train, X_test, y_train, y_test = cross_validation.train_test_split(X, y, test_size=0.25, random_state=5)
3 N4 v n' s) Y1 A K# i% s. U
5 X# |( o* `9 q& H4 U9 l$ _#params = {'kernel': 'linear'}# q7 G* Z# X. X) `1 q; Z! L
#params = {'kernel': 'poly', 'degree': 3}
+ H- C- K* z6 i/ a Y) qparams = {'kernel': 'rbf'}
/ _1 i( l' R; @- Y( Nclassifier = SVC(**params)# ~$ a0 z9 I, V0 l$ A
classifier.fit(X_train, y_train)1 |# u7 F$ T6 Q( H
utilities.plot_classifier(classifier, X_train, y_train, 'Training dataset')
. ?/ W: m) A5 ]& Y! T7 L1 p" w+ ]; V
y_test_pred = classifier.predict(X_test)6 ^2 d4 J% e: n, j
utilities.plot_classifier(classifier, X_test, y_test, 'Test dataset')% L2 l2 J- \+ D" R5 G+ N
$ d2 n" D$ f3 ^) Q0 {###############################################5 w2 G- ?9 e7 K
# Evaluate classifier performance# M+ \/ a% V- U! l, Q- z* @' ?
: n. Y C3 [* f# i: ^4 i4 j; k9 D+ Wfrom sklearn.metrics import classification_report% }3 Z: ]0 X( Z/ u. I) e6 ?
3 j6 f; d- x3 r" @# T8 s% y# v* c5 e
target_names = ['Class-' + str(int(i)) for i in set(y)]& n- D+ b4 K7 R$ [
print "\n" + "#"*30: ?! u/ V0 `* V: _( G- _2 X, n: r7 W
print "\nClassifier performance on training dataset\n"
+ h9 X: A. b/ q" cprint classification_report(y_train, classifier.predict(X_train), target_names=target_names)7 E0 o( g2 O, \ F+ N
print "#"*30 + "\n"
+ C9 o4 _: {, a* X
1 c' o( |0 L" n5 j* R( z0 [1 Z/ T! `print "#"*30: Q% w% A5 e- `
print "\nClassification report on test dataset\n"
( \. K/ E$ ]3 D& L" x' r1 bprint classification_report(y_test, y_test_pred, target_names=target_names)' I8 {7 \' z' J6 N- \* W
print "#"*30 + "\n"
# u8 F- V r& s5 d- }
! }3 A3 d2 d, d" u% [9 V; p6 H |
|