- 金錢
- 46
- 威望
- 3183
- 貢獻值
- 0
- 推廣值
- 0
- 性別
- 保密
- 在線時間
- 38 小時
- 最後登錄
- 2024-2-25
- 主題
- 0
- 精華
- 0
- 閱讀權限
- 70
- 註冊時間
- 2012-3-17
- 帖子
- 556
 
該用戶從未簽到 - 推廣值
- 0
- 貢獻值
- 0
- 金錢
- 46
- 威望
- 3183
- 主題
- 0
|
import numpy as np
( V2 M- f3 U+ o2 Eimport matplotlib.pyplot as plt
' ~! } w+ e! ?5 `: s& p% R9 \3 ` @- S( Y+ v+ m- \
import utilities ( |* y; T4 G( d: C' H
# b! P. _" c, u; B* u# Load input data
4 ~: f* | 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'3 h# H& a7 }: u# D
X, y = utilities.load_data(input_file)
0 ]+ o4 [+ |6 p" ?* I3 B
9 Y3 K! Q; A! H5 k/ y4 N###############################################$ M; Y7 B# a% V0 s$ S1 H# G
# Separate the data into classes based on 'y'
( E8 B4 W& K- u( Y, P+ z- x: X6 u1 xclass_0 = np.array([X[i] for i in range(len(X)) if y[i]==0])
W$ p+ @) U0 j1 B/ P; fclass_1 = np.array([X[i] for i in range(len(X)) if y[i]==1])/ x( g5 m% r- S
2 h2 t( U3 j6 H/ J, @5 S: M# Plot the input data
# ~( M) h9 g" b' Nplt.figure()/ ]5 C- y+ I1 |" ?: F
plt.scatter(class_0[:,0], class_0[:,1], facecolors='black', edgecolors='black', marker='s')) g5 I+ H$ V9 m5 x7 w+ N
plt.scatter(class_1[:,0], class_1[:,1], facecolors='None', edgecolors='black', marker='s')
7 S8 v. Y& p# O" R, x, } s5 mplt.title('Input data')
8 X1 f7 O9 D. O+ W: V
J8 r% ?: Y" @' n/ }+ J. S###############################################
" u6 @# j: R3 s# x$ B# a# Train test split and SVM training
) _& i( h, L6 w5 x/ |# O4 n+ Ffrom sklearn import cross_validation
9 }, }9 R) z5 Ufrom sklearn.svm import SVC
5 x2 e8 E' n9 Q N4 m& M7 o' ?- T) f
0 g4 A) w+ I% g% n' tX_train, X_test, y_train, y_test = cross_validation.train_test_split(X, y, test_size=0.25, random_state=5). h6 d6 w/ `1 D2 i" S, L
8 N! F6 R& ]4 }7 O#params = {'kernel': 'linear'}$ m, b) F! K, h) y7 S* }
#params = {'kernel': 'poly', 'degree': 3}
( f3 U1 A" p; z# U) d8 j/ lparams = {'kernel': 'rbf'}8 ^" v, H/ H+ L4 q6 k: ~
classifier = SVC(**params)
/ c$ Y8 q' z" J2 G+ _/ Oclassifier.fit(X_train, y_train), N) Q$ o2 c4 w; C
utilities.plot_classifier(classifier, X_train, y_train, 'Training dataset')0 L0 @& u* x# S0 M0 T1 i
7 s0 n& P0 q' w7 U. u/ h' cy_test_pred = classifier.predict(X_test). H) l; S7 P' l+ a, _
utilities.plot_classifier(classifier, X_test, y_test, 'Test dataset')
, M, j! _; i0 c
4 W Y# @" c* r6 l' H/ K( r###############################################
9 K3 }$ L6 P# k n: L# Evaluate classifier performance
6 r. i4 @2 U2 A3 k: L! W6 t( Q8 A: f$ M( y( c' l1 S0 m( O
from sklearn.metrics import classification_report
$ _* u9 b$ }, B1 T$ G$ M
$ w& W7 c" X( c! T/ j0 Y6 b# l0 _target_names = ['Class-' + str(int(i)) for i in set(y)]
8 a9 f! K/ O* ]$ Qprint "\n" + "#"*30
; ]: T/ [+ e% _% Y4 [' ~7 a: Q' sprint "\nClassifier performance on training dataset\n"+ u* O/ m7 [. n# t3 n
print classification_report(y_train, classifier.predict(X_train), target_names=target_names)
+ `3 G1 R' u, S1 C$ \. eprint "#"*30 + "\n"
l" n/ y# _* Y7 v3 ~+ c1 R1 a2 ^" I5 h1 V' n6 x1 f9 D u' p4 }5 P
print "#"*30
; O z" F3 \4 x) F, E# }print "\nClassification report on test dataset\n"
3 V) e `( I+ s! bprint classification_report(y_test, y_test_pred, target_names=target_names)" Y( N+ S$ o! r. b6 Z7 c7 M
print "#"*30 + "\n"" n- `; |( V& h
6 |5 p5 m8 R. x8 [
|
|