- 金錢
- 45
- 威望
- 3183
- 貢獻值
- 0
- 推廣值
- 0
- 性別
- 保密
- 在線時間
- 38 小時
- 最後登錄
- 2024-2-25
- 主題
- 0
- 精華
- 0
- 閱讀權限
- 70
- 註冊時間
- 2012-3-17
- 帖子
- 553
 
該用戶從未簽到 - 推廣值
- 0
- 貢獻值
- 0
- 金錢
- 45
- 威望
- 3183
- 主題
- 0
|
import numpy as np* Q4 n4 z0 }9 V/ G5 D" @4 g4 f6 Z
import matplotlib.pyplot as plt
; f( ?' \) E3 H$ ^/ M# ~% K; Y: m$ e
import utilities
2 S, u P% g+ @9 J$ \; Y
1 H+ Q; e4 m8 `$ I# Load input data/ N9 p9 k J% J0 [5 _2 F
input_file = 'D:\\1.Modeling material\\Py_Study\\2.code_model\\Python-Machine-Learning-Cookbook\\Python-Machine-Learning-Cookbook-master\\Chapter03\\data_multivar.txt'
' Z* r H- k H8 g F y2 I5 d) oX, y = utilities.load_data(input_file)
/ o* O0 G7 [4 q$ C; ]2 r- q | p
###############################################5 ~% R; g0 j+ a$ Q
# Separate the data into classes based on 'y'( W8 D2 M9 C1 v# ^! i: `" A$ @: o
class_0 = np.array([X[i] for i in range(len(X)) if y[i]==0])
, V1 d' s* i% K' C' |class_1 = np.array([X[i] for i in range(len(X)) if y[i]==1])
- C7 O9 s! r& I, B4 i- B8 }5 p3 W, y6 d+ r3 ^! B
# Plot the input data
! C; m) c: Z* y7 Qplt.figure()
! l! o$ w, J/ l) qplt.scatter(class_0[:,0], class_0[:,1], facecolors='black', edgecolors='black', marker='s')+ I6 c4 ^+ l* W# L8 _8 B1 V1 M
plt.scatter(class_1[:,0], class_1[:,1], facecolors='None', edgecolors='black', marker='s')
3 y g+ h" Z& h0 M# j3 C8 Pplt.title('Input data')
" f6 q3 y( x8 q5 ~; X' l# P
( K* i# O d/ |1 h r9 U$ H: w###############################################8 }7 x* {0 I# C, ~3 z
# Train test split and SVM training
% ~; _, a4 s9 H+ m; y3 J& Jfrom sklearn import cross_validation
- I" a* ~# f0 I/ e/ B& b ], Dfrom sklearn.svm import SVC6 m, }5 Q0 c9 @7 a
6 J" j0 n; R/ eX_train, X_test, y_train, y_test = cross_validation.train_test_split(X, y, test_size=0.25, random_state=5)1 p0 E/ \: m/ A: s
9 u' Q: H# P5 {4 `: b
#params = {'kernel': 'linear'}6 x2 X- Y8 b% `) k; p* w; x$ z1 ~$ w
#params = {'kernel': 'poly', 'degree': 3}" a/ ~& b2 i c1 \/ x( i
params = {'kernel': 'rbf'}- L. L' x- O( X
classifier = SVC(**params)
4 ?9 O- `! }! S y7 ?( M1 q5 n; h( g3 Hclassifier.fit(X_train, y_train)' Z0 o) w* o# c/ _# o8 Y
utilities.plot_classifier(classifier, X_train, y_train, 'Training dataset')' ?0 q ^3 h/ [
3 K9 E' |( v) b5 r! r. H4 P
y_test_pred = classifier.predict(X_test)8 g3 Z$ q! Q0 h$ h7 x$ y
utilities.plot_classifier(classifier, X_test, y_test, 'Test dataset')* N# ]: G% {" X, f2 V
9 g* z3 T9 W: o$ x: \- C: }###############################################
3 b9 s$ e" s# d1 H; Q3 O# Evaluate classifier performance5 `4 E& S$ F b
4 R% j' \3 O% e, o& J3 d+ G, e
from sklearn.metrics import classification_report
( y" ? S- i. n* \8 K6 g2 w. G0 E5 k( j* C4 \0 R7 {; G
target_names = ['Class-' + str(int(i)) for i in set(y)]
$ v6 X. q" w% k0 S7 Zprint "\n" + "#"*30& H: w, w/ t: s& C0 s% B! ?% n |
print "\nClassifier performance on training dataset\n"8 |* H7 B6 J' ?5 q0 s/ K( N1 R
print classification_report(y_train, classifier.predict(X_train), target_names=target_names)
' I6 U0 T- E v4 Iprint "#"*30 + "\n"
1 K; S4 o2 }* I0 I1 {- k+ T- L: H k0 J, l+ A% G4 T$ i. f
print "#"*30# o$ w( }; c: x* ~* F7 x5 l
print "\nClassification report on test dataset\n"+ r: ^8 S' C) p+ z, M' i, Z
print classification_report(y_test, y_test_pred, target_names=target_names)0 y; _* B6 I6 Y9 M/ z, J4 C
print "#"*30 + "\n"
) f2 {5 U. T+ M; _; d& p
( d, v \- j/ N- y |
|