- 金錢
- 46
- 威望
- 3183
- 貢獻值
- 0
- 推廣值
- 0
- 性別
- 保密
- 在線時間
- 38 小時
- 最後登錄
- 2024-2-25
- 主題
- 0
- 精華
- 0
- 閱讀權限
- 70
- 註冊時間
- 2012-3-17
- 帖子
- 556
 
該用戶從未簽到 - 推廣值
- 0
- 貢獻值
- 0
- 金錢
- 46
- 威望
- 3183
- 主題
- 0
|
import numpy as np. c: V/ u. j1 ]# k( E9 X% V5 V
import matplotlib.pyplot as plt. d6 n/ r ?' j0 i0 e% _
+ T/ X% G4 O! l% X- O* L
import utilities
2 }. }# ~, B; B( L& h$ X
& G7 R5 o. p+ z; X: G$ R" g# O* ?- E. u# Load input data
0 t3 B+ A" S5 E9 P3 e: W2 Hinput_file = 'D:\\1.Modeling material\\Py_Study\\2.code_model\\Python-Machine-Learning-Cookbook\\Python-Machine-Learning-Cookbook-master\\Chapter03\\data_multivar.txt'
9 X: Z, v! ]4 q5 y- g! A, rX, y = utilities.load_data(input_file)$ Z! P0 C" [2 Y' i; `
5 h% d4 S* Y8 S5 Q1 h9 h! A1 h
###############################################7 [% ]9 Z4 F" S
# Separate the data into classes based on 'y'
! [) S6 U8 ^% U+ n( qclass_0 = np.array([X[i] for i in range(len(X)) if y[i]==0])
+ q, X& V4 F6 L7 kclass_1 = np.array([X[i] for i in range(len(X)) if y[i]==1])
1 p% y# y% m3 f
* C3 q& R, x1 h& `0 }0 ?+ _# Plot the input data% [* Q# q% T6 K- [* H
plt.figure()1 Z1 P$ K: e, v: }) E$ m/ ^
plt.scatter(class_0[:,0], class_0[:,1], facecolors='black', edgecolors='black', marker='s')
9 ?, C) ^6 h8 f6 ^4 xplt.scatter(class_1[:,0], class_1[:,1], facecolors='None', edgecolors='black', marker='s')
$ R3 z$ n! u8 D* x1 ]9 ?plt.title('Input data')/ K6 m* V* J; g( s4 w
; n7 G+ I+ z+ i: X7 U2 J###############################################
; B) L8 d& K+ L; G. u- w# Train test split and SVM training
& ]! ]& N3 t$ p3 W7 Ifrom sklearn import cross_validation
4 d+ N9 }1 g$ ^) y3 b( F8 hfrom sklearn.svm import SVC
8 e! c$ ]( q$ ]; y9 V- `( ]: n2 I& f3 D) K( A
X_train, X_test, y_train, y_test = cross_validation.train_test_split(X, y, test_size=0.25, random_state=5)2 W% L1 Q. Q( {' j& ]! ]
. w& w) Z/ i4 A8 W* b" h#params = {'kernel': 'linear'}3 a1 B2 Q$ o$ o% Q/ b& v- ~9 L
#params = {'kernel': 'poly', 'degree': 3}
- k+ }# q" I& i2 I6 A% N- _; H" vparams = {'kernel': 'rbf'}" F, Q. l4 b+ K
classifier = SVC(**params)
6 e: u7 N) o: s( m3 Y z0 Pclassifier.fit(X_train, y_train)6 W- \$ d9 ]+ Y" ]
utilities.plot_classifier(classifier, X_train, y_train, 'Training dataset')
) a1 ]2 w) E, N. M9 N
; _1 J$ P1 M" ]y_test_pred = classifier.predict(X_test) E4 b* s2 E8 ^( Z- S
utilities.plot_classifier(classifier, X_test, y_test, 'Test dataset')
" \$ ^8 i7 q# [1 X' ]; Y" b. \) V- t R6 ] d; Y" X
###############################################5 I( f$ U& n, }# H3 P8 f
# Evaluate classifier performance
1 E. U1 s. V% H3 p
8 k# [( D. [/ ~' L+ E7 }from sklearn.metrics import classification_report: E& W; k& t J T) j
0 K- S6 x3 J3 ]% f4 l* E
target_names = ['Class-' + str(int(i)) for i in set(y)]
- {: l1 J" a% D% y& P0 rprint "\n" + "#"*30
b% w$ A& v& W' ~print "\nClassifier performance on training dataset\n"
6 r0 d0 B% `; P& }+ sprint classification_report(y_train, classifier.predict(X_train), target_names=target_names). ^. `7 H/ ]5 W
print "#"*30 + "\n"% w( g) F/ t* U0 \) m1 j1 S! z
* \: X% G4 u* z' }0 \6 y* _
print "#"*30' T4 B. y' L: p3 m5 e( h
print "\nClassification report on test dataset\n": T: \1 T1 o4 b% F3 t. O: W& b5 N+ R- i
print classification_report(y_test, y_test_pred, target_names=target_names)0 X! A: _7 m+ N, V2 |2 T
print "#"*30 + "\n"/ L6 C. v( g$ Y: y9 j8 Z
! k5 l6 Y0 c2 _0 ^. } |% _ |
|