- 金錢
- 45
- 威望
- 3183
- 貢獻值
- 0
- 推廣值
- 0
- 性別
- 保密
- 在線時間
- 38 小時
- 最後登錄
- 2024-2-25
- 主題
- 0
- 精華
- 0
- 閱讀權限
- 70
- 註冊時間
- 2012-3-17
- 帖子
- 553
 
該用戶從未簽到 - 推廣值
- 0
- 貢獻值
- 0
- 金錢
- 45
- 威望
- 3183
- 主題
- 0
|
import numpy as np+ Q* z+ v/ @8 N* k: ]
import matplotlib.pyplot as plt) u4 [; x, Q4 u% W. t
) I" T$ O" j0 P3 ^. b1 |7 n0 y" Nimport utilities
$ P1 y) g: i( U/ h1 H5 ~' ~% s* i: ^" w; t! A$ k* G
# Load input data0 q y1 l9 Q+ D, h9 e
input_file = 'D:\\1.Modeling material\\Py_Study\\2.code_model\\Python-Machine-Learning-Cookbook\\Python-Machine-Learning-Cookbook-master\\Chapter03\\data_multivar.txt'
+ l+ M* O- _6 Y3 aX, y = utilities.load_data(input_file)5 a! N5 R6 n D0 j0 I3 G
9 r; {% G% t" R/ l( {" l& `% f( _###############################################5 C3 ^9 N) E4 S9 b! t8 Z' y
# Separate the data into classes based on 'y'' z( ^* F/ T, B
class_0 = np.array([X[i] for i in range(len(X)) if y[i]==0])
: O$ v0 A) _+ T. b) Iclass_1 = np.array([X[i] for i in range(len(X)) if y[i]==1])
9 w! z O6 a% p
, U# Y" F" G. Z1 S) F2 p5 E+ v o# Plot the input data
+ M7 M, R; r" zplt.figure()' i- r9 X: H- R0 f7 Y! P
plt.scatter(class_0[:,0], class_0[:,1], facecolors='black', edgecolors='black', marker='s')
! T/ K4 H) u! G' K! J% m' \6 ?plt.scatter(class_1[:,0], class_1[:,1], facecolors='None', edgecolors='black', marker='s')
) p6 C2 Y" s7 R( i4 |plt.title('Input data')' Y, R7 q4 V k
7 y. }/ q O- x
###############################################7 M5 x$ ^" R' c6 u6 C8 G: w
# Train test split and SVM training
|' {5 ^& k6 I7 F, Sfrom sklearn import cross_validation
* a# v) W7 T: ^3 bfrom sklearn.svm import SVC" K1 I c# W9 e( g
/ B4 D8 }8 p( |" A4 |/ |
X_train, X_test, y_train, y_test = cross_validation.train_test_split(X, y, test_size=0.25, random_state=5)
! O- G; y% M; F7 q: {& S; k3 U) U- Z9 [0 x' p, w) \- Q8 h2 e
#params = {'kernel': 'linear'}' V. j& c5 n7 |5 @/ p
#params = {'kernel': 'poly', 'degree': 3}/ s( J7 |* p. J6 ?- `
params = {'kernel': 'rbf'}6 l1 @7 C' k) n9 k6 L% V/ K3 w& ^ C
classifier = SVC(**params)
& {8 B" h# v9 Aclassifier.fit(X_train, y_train)
1 G4 s& s! Z& X/ r- ~; futilities.plot_classifier(classifier, X_train, y_train, 'Training dataset')
4 ~$ e) h: Z0 ?' u9 ]3 u$ l7 X8 ^* m& _
y_test_pred = classifier.predict(X_test)
2 |3 b+ z! r+ \) |% futilities.plot_classifier(classifier, X_test, y_test, 'Test dataset'): p5 r1 \2 U5 `6 D2 ~. \
9 \$ {9 i- x0 |( Z/ y3 M
###############################################! I# v V3 v/ T! w! _8 P
# Evaluate classifier performance7 y2 i O6 T5 {1 ]
" m! C6 q3 q/ p X. M1 V; ~: Vfrom sklearn.metrics import classification_report6 _. l" _8 n6 i3 J) s/ p
: X; I, }2 k7 k; A. r3 S2 P
target_names = ['Class-' + str(int(i)) for i in set(y)]6 M- Z6 x' o Z& b: l
print "\n" + "#"*30
. ]0 o9 q8 W" ^9 |8 Pprint "\nClassifier performance on training dataset\n"6 R$ d, G9 b$ D% L
print classification_report(y_train, classifier.predict(X_train), target_names=target_names)% c$ t& D/ n+ v( z2 \
print "#"*30 + "\n"
1 g0 l# A1 T, q9 H2 i1 V3 i0 h5 H, M0 I: m( ~
print "#"*30
7 c5 d a* w* ^" }" G( `print "\nClassification report on test dataset\n"
% l( T Y3 v6 k9 s/ oprint classification_report(y_test, y_test_pred, target_names=target_names)
: o7 X8 m( d: \print "#"*30 + "\n"- z6 X+ O0 g3 g$ E7 w9 u0 c
# @* e, Z( e+ T; o |
|