- 金錢
- 46
- 威望
- 3183
- 貢獻值
- 0
- 推廣值
- 0
- 性別
- 保密
- 在線時間
- 38 小時
- 最後登錄
- 2024-2-25
- 主題
- 0
- 精華
- 0
- 閱讀權限
- 70
- 註冊時間
- 2012-3-17
- 帖子
- 556
 
該用戶從未簽到 - 推廣值
- 0
- 貢獻值
- 0
- 金錢
- 46
- 威望
- 3183
- 主題
- 0
|
import numpy as np! @% A6 H; E2 B) y9 \ ]
import matplotlib.pyplot as plt
2 A* p* U, H3 l7 D* W% u5 d
( W: m& k& |' Y% T' N3 y! J1 eimport utilities 1 k5 d* @( J9 q; _* y# d
' a9 y7 K" t- \7 G2 [+ [7 J# Load input data# ?+ D6 }' d* W4 ^8 j7 J2 j8 d$ J
input_file = 'D:\\1.Modeling material\\Py_Study\\2.code_model\\Python-Machine-Learning-Cookbook\\Python-Machine-Learning-Cookbook-master\\Chapter03\\data_multivar.txt'# K+ b2 J; d% ]7 A% K
X, y = utilities.load_data(input_file) u4 z. |' j' p' h9 X' E6 w
; _0 }5 G B4 J+ _/ a###############################################
# Q, s1 `' D& ~/ { w# Separate the data into classes based on 'y'0 y) E8 K, j- W! f
class_0 = np.array([X[i] for i in range(len(X)) if y[i]==0])
" Y+ W& U" l; h ^& ~8 \class_1 = np.array([X[i] for i in range(len(X)) if y[i]==1])
6 {4 p! A# B3 o# z, Y# E) c. Y( b1 L) g4 `
# Plot the input data( [9 b4 h/ n' S5 n8 G7 ?
plt.figure()$ i. N) w: W2 \$ @6 b; ?2 g
plt.scatter(class_0[:,0], class_0[:,1], facecolors='black', edgecolors='black', marker='s')
( s3 n. t: V! zplt.scatter(class_1[:,0], class_1[:,1], facecolors='None', edgecolors='black', marker='s')8 b( e. p% u9 M" ?! c) b
plt.title('Input data')
3 E" f6 B3 x7 V* Z% Y9 e/ L
# N1 T: W0 w9 @- n8 I$ U###############################################% S6 R, ?% b( ]- Z% W# ]1 I
# Train test split and SVM training
5 y% c" \* x) o3 l% ]from sklearn import cross_validation
( P2 ?) g; q, u; \from sklearn.svm import SVC
, E. E2 d6 ]: x! f: p# z9 C0 r* ^) e8 e; @5 Y+ V' Z$ O
X_train, X_test, y_train, y_test = cross_validation.train_test_split(X, y, test_size=0.25, random_state=5)
$ W) t, ?0 g9 F6 s2 o# f
. V$ y2 w! u( e0 m#params = {'kernel': 'linear'}
# G# j( h/ W% P3 Z; h" A/ F; o#params = {'kernel': 'poly', 'degree': 3}+ t% R/ l: v& M; {
params = {'kernel': 'rbf'}+ l5 |7 ]' U- }
classifier = SVC(**params)$ r( g# t2 N6 D* A# [4 K) T5 @; \
classifier.fit(X_train, y_train)' I$ j6 k2 C7 o8 G8 i5 n& H' u
utilities.plot_classifier(classifier, X_train, y_train, 'Training dataset')
0 S4 x& ~ `6 O$ t, a* Z- ?: Q; y& N- e7 ~0 j2 }& |5 k
y_test_pred = classifier.predict(X_test)
0 r3 U* S( L( v: [) O$ L' t" Xutilities.plot_classifier(classifier, X_test, y_test, 'Test dataset')% T; X& E. c* Q5 {' E
4 k5 u& r3 U( t' i2 Q: Z+ r; P
###############################################
' X6 J% T% h5 w# Evaluate classifier performance
" P1 R8 y! S( C- v- E1 z/ D! X8 X# U6 y" k
from sklearn.metrics import classification_report
4 E. J7 m3 v ~ R4 m# C4 S4 C4 h, X6 `" M
target_names = ['Class-' + str(int(i)) for i in set(y)] B% t, a0 _8 X' M+ B
print "\n" + "#"*30* Q M1 B$ X4 K8 X, x* h$ B
print "\nClassifier performance on training dataset\n"- h8 F! b) k y; |# I; l
print classification_report(y_train, classifier.predict(X_train), target_names=target_names)
( [1 W2 ?2 D% \/ h# hprint "#"*30 + "\n"
" x3 e' s$ x$ |. Q0 ~! D3 |, ~. Y9 r& R2 I) R/ d: }1 q
print "#"*30
3 d# k5 J. P+ ]/ t( O [5 sprint "\nClassification report on test dataset\n"
- I, ^! F& z' l" ~8 }9 W( Kprint classification_report(y_test, y_test_pred, target_names=target_names): H# S; [9 i% j* I2 ?
print "#"*30 + "\n"
# F/ h2 s( K9 @: ?, D+ O6 F) ~1 c, e \! p/ m4 I* C9 d) Q# B
|
|