- 金錢
- 45
- 威望
- 3183
- 貢獻值
- 0
- 推廣值
- 0
- 性別
- 保密
- 在線時間
- 38 小時
- 最後登錄
- 2024-2-25
- 主題
- 0
- 精華
- 0
- 閱讀權限
- 70
- 註冊時間
- 2012-3-17
- 帖子
- 553
 
該用戶從未簽到 - 推廣值
- 0
- 貢獻值
- 0
- 金錢
- 45
- 威望
- 3183
- 主題
- 0
|
import numpy as np+ o- D5 u1 H% p
import matplotlib.pyplot as plt
2 B3 j3 Y+ R1 v9 j% d
2 {+ Q- m9 k- y( q, J) }import utilities H3 X0 l( J, A5 f! p6 ~
7 ]. D- T- R7 Q9 l0 V6 E# Load input data
9 j& H, s7 S1 ~, I/ ninput_file = 'D:\\1.Modeling material\\Py_Study\\2.code_model\\Python-Machine-Learning-Cookbook\\Python-Machine-Learning-Cookbook-master\\Chapter03\\data_multivar.txt'
6 l& G) w' Y8 mX, y = utilities.load_data(input_file)3 K9 V7 b3 c4 k# c
6 ]3 @' u$ Y, Z0 H" p8 D% C###############################################
# K& F- A% o3 H# Separate the data into classes based on 'y'
0 Y& `' {; g9 P$ B4 g9 Kclass_0 = np.array([X[i] for i in range(len(X)) if y[i]==0])4 h5 I. A- E* ^$ g% R1 z
class_1 = np.array([X[i] for i in range(len(X)) if y[i]==1])
& {: Q ^" @; e- U* ^ T/ k0 u& w: B) M, K! U* b, Q( P, e% z
# Plot the input data4 S8 {' [* }7 H3 o" G! U
plt.figure()
3 }; o' h. Z1 o1 m- m- Vplt.scatter(class_0[:,0], class_0[:,1], facecolors='black', edgecolors='black', marker='s')
* Z2 G0 e2 x2 _2 x6 i* R6 Rplt.scatter(class_1[:,0], class_1[:,1], facecolors='None', edgecolors='black', marker='s')
* A a3 Y) W+ b9 B1 o: V5 d3 gplt.title('Input data')) j9 n5 h7 G' Q W6 m: A4 C5 C5 M
/ f8 U E1 S4 j5 x; n; T###############################################8 s% D. O' b+ l+ R5 S, k
# Train test split and SVM training
7 Y! l% p7 m1 Ifrom sklearn import cross_validation4 t, R0 C3 _- x+ g
from sklearn.svm import SVC
! k- Q5 A; l2 v3 I# g. y/ p; O) y" h
X_train, X_test, y_train, y_test = cross_validation.train_test_split(X, y, test_size=0.25, random_state=5). }. a0 K* c/ b- c) n1 u
( d* H; H5 c! Z/ L% q' I# p#params = {'kernel': 'linear'}9 m1 N$ t4 {4 o6 p, c( I
#params = {'kernel': 'poly', 'degree': 3}" j! }; ~" z( ~8 V! V/ r
params = {'kernel': 'rbf'}
. |/ d ^- U5 h( s: H4 vclassifier = SVC(**params)
% J# Q- L5 A/ Fclassifier.fit(X_train, y_train)8 ]) C# r2 P& n& Z3 E2 \
utilities.plot_classifier(classifier, X_train, y_train, 'Training dataset')3 l0 m2 O* ~2 H# c& ?2 Q
. Q8 ]- q6 q8 `& C( }
y_test_pred = classifier.predict(X_test)
% @8 q+ Q/ b* R6 Q6 Sutilities.plot_classifier(classifier, X_test, y_test, 'Test dataset')' R1 s; ` Q; N) G- ~3 |; C
3 U2 Z4 d- i$ U U) q
###############################################, {8 A* z) E& x( C% y; c' [
# Evaluate classifier performance
, {( S6 D8 _8 e' @/ X3 S7 _" X) \0 x! h. @9 x9 u. c6 _! \
from sklearn.metrics import classification_report
# L8 r' v. `2 J) e0 y$ P: N: ^- S: L& c* Y0 q
target_names = ['Class-' + str(int(i)) for i in set(y)]
! B' G5 [" b) E+ Z* Dprint "\n" + "#"*30! o" K) H' _8 `
print "\nClassifier performance on training dataset\n"# Q9 D$ F( o' r8 J- J) Q& e
print classification_report(y_train, classifier.predict(X_train), target_names=target_names)6 J k" e8 y$ D% q2 h- {5 u$ k+ I
print "#"*30 + "\n"& P' g; l+ y* {) z0 h' }/ B: N
, Z- Y" q' |( Q5 N' m- }: u7 f. oprint "#"*30
/ ?! X: V& B/ \- ]2 |, q9 X& Mprint "\nClassification report on test dataset\n"# x% ^$ Q* t* e6 ` b* M& w, s
print classification_report(y_test, y_test_pred, target_names=target_names)8 c, f5 E* w' H
print "#"*30 + "\n"- l9 s* X! ]! `) W
2 L! K% H2 C+ h) W# J) s |
|