- 金錢
- 45
- 威望
- 3183
- 貢獻值
- 0
- 推廣值
- 0
- 性別
- 保密
- 在線時間
- 38 小時
- 最後登錄
- 2024-2-25
- 主題
- 0
- 精華
- 0
- 閱讀權限
- 70
- 註冊時間
- 2012-3-17
- 帖子
- 553
 
該用戶從未簽到 - 推廣值
- 0
- 貢獻值
- 0
- 金錢
- 45
- 威望
- 3183
- 主題
- 0
|
import numpy as np: M/ y$ p" u( z3 n4 r/ b
import matplotlib.pyplot as plt: p( a% P# N6 h! O
& W% b3 m( }# b, O8 c% E1 simport utilities
. ?/ R6 U7 E X7 ?- x8 o3 ?6 Z+ g( `6 E. J
# Load input data; f; B: `! M, w! b5 G1 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'& r6 J6 Y6 S+ i) ?" s( Z
X, y = utilities.load_data(input_file)* T" i6 I' V: M* r5 N: P
- e1 b2 P) [" j( X1 b
###############################################
6 l$ x3 S1 ~& I" x, X6 X( k4 @# Separate the data into classes based on 'y'
" `9 T& r) w8 g4 Q0 ?, B2 S3 pclass_0 = np.array([X[i] for i in range(len(X)) if y[i]==0])
4 C9 l3 _1 u2 j8 b7 X7 w. `class_1 = np.array([X[i] for i in range(len(X)) if y[i]==1])) W4 a# R) E3 o# `' P
H( z; O4 g0 L P% W6 t5 o: ~# Plot the input data
/ g! Z: ~' h5 C3 e2 wplt.figure()9 w; t; i$ _) m/ h" r, m1 ?
plt.scatter(class_0[:,0], class_0[:,1], facecolors='black', edgecolors='black', marker='s')
* ]& g$ W. i( J3 ^3 `4 ^4 Cplt.scatter(class_1[:,0], class_1[:,1], facecolors='None', edgecolors='black', marker='s')% T5 u" I/ { O+ U; R
plt.title('Input data')
+ s6 f T7 W! t9 g7 l K$ @5 h% z; K# G* K2 O! q) W( s
############################################### i3 E; N0 T9 W: A
# Train test split and SVM training
( N# j5 B7 f$ v% O- ?4 Cfrom sklearn import cross_validation( H0 Z$ B; h' K
from sklearn.svm import SVC
2 V5 q7 y" j' b5 X
8 ^5 g7 r9 U+ }. B* Z0 w6 Q) U3 gX_train, X_test, y_train, y_test = cross_validation.train_test_split(X, y, test_size=0.25, random_state=5)' Y0 L# V' _& d5 N
: |# T5 q+ y$ O; C3 Q2 [
#params = {'kernel': 'linear'}
( J4 b) K4 c& f3 M: @0 n, N: @. j#params = {'kernel': 'poly', 'degree': 3}) ?' b$ M; w p; C. D8 g
params = {'kernel': 'rbf'}( X! N2 k' F `# t& N! P
classifier = SVC(**params), }3 `- B% n3 [2 G' P
classifier.fit(X_train, y_train)
9 m+ c- ^) q- A+ V0 Mutilities.plot_classifier(classifier, X_train, y_train, 'Training dataset')3 ]( D3 v( H& }' c1 w
, T, M$ t$ r i$ w8 Q) n& ^
y_test_pred = classifier.predict(X_test)
# @* U! }" B' a5 |utilities.plot_classifier(classifier, X_test, y_test, 'Test dataset')- R# U! M! X+ [' C* r" E
, h/ k" L7 O" q) z( V! k3 `# y###############################################
% n9 h3 h8 z, ~8 E4 k" T+ u: X# Evaluate classifier performance
' _/ D ~- A+ D' Y: R
( w( D0 B- o1 `from sklearn.metrics import classification_report
0 Y9 a$ A' P T5 X. ^! d/ |* r
4 t. w$ I9 Z: Z F5 t3 ^" P( Qtarget_names = ['Class-' + str(int(i)) for i in set(y)]! @& g8 v0 ] _3 r6 n
print "\n" + "#"*30* I" n8 u( x! V' [+ c: ?* W! i! n
print "\nClassifier performance on training dataset\n"
% C' M- y3 E3 l1 Y. sprint classification_report(y_train, classifier.predict(X_train), target_names=target_names)
1 ?- }1 d- A! t8 }* ^2 ^6 oprint "#"*30 + "\n"
* G, ~ n) h7 ^: c# k, G5 i, f- |8 e
print "#"*30: L& j4 C9 B+ x) J- v( l0 E" g
print "\nClassification report on test dataset\n"
5 Z( N7 N' ^. M4 |8 _1 M8 E f) X0 Sprint classification_report(y_test, y_test_pred, target_names=target_names) L0 i( y, @5 q
print "#"*30 + "\n"
+ s, R/ Q8 l; X
" M9 X% o: g# ], T" e |
|