- 金錢
- 46
- 威望
- 3183
- 貢獻值
- 0
- 推廣值
- 0
- 性別
- 保密
- 在線時間
- 38 小時
- 最後登錄
- 2024-2-25
- 主題
- 0
- 精華
- 0
- 閱讀權限
- 70
- 註冊時間
- 2012-3-17
- 帖子
- 556
 
該用戶從未簽到 - 推廣值
- 0
- 貢獻值
- 0
- 金錢
- 46
- 威望
- 3183
- 主題
- 0
|
import numpy as np
6 }) F+ e4 T7 s4 t6 D2 X! Bimport matplotlib.pyplot as plt# p8 s% ]' N5 f: v- ?% `
' ]3 {8 K8 E% V n j; ]* a( E, s: W; F2 o
import utilities 4 E- O) v; `4 y. h! G
- ^5 [0 a) B, [7 v
# Load input data. I9 u- P) H3 B+ a/ _& _, n
input_file = 'D:\\1.Modeling material\\Py_Study\\2.code_model\\Python-Machine-Learning-Cookbook\\Python-Machine-Learning-Cookbook-master\\Chapter03\\data_multivar.txt'
0 ]8 q; N8 S) [, x8 z+ a! \X, y = utilities.load_data(input_file)4 p+ s) _6 g- H+ T
& L6 }5 w! D9 m5 k3 ], g
###############################################
* N% g2 R) i( H$ t# Separate the data into classes based on 'y'
3 O+ Y" X) F6 K9 L6 f( ~ |% [+ Hclass_0 = np.array([X[i] for i in range(len(X)) if y[i]==0])* Q( F( g( I! G: ]; I5 G
class_1 = np.array([X[i] for i in range(len(X)) if y[i]==1])
0 H( j: Q: S5 H5 s4 N
+ g8 v$ p! l4 E! ?+ h# Plot the input data& W- ~( w1 Z! Y: p$ A
plt.figure()
+ a0 T6 d2 H' wplt.scatter(class_0[:,0], class_0[:,1], facecolors='black', edgecolors='black', marker='s')
* W1 s7 l: P2 E* R) iplt.scatter(class_1[:,0], class_1[:,1], facecolors='None', edgecolors='black', marker='s')
* Y. p' ]% r' `$ l/ i* \( ?) K! r1 vplt.title('Input data')( n8 G' Z- O5 o- ~ C
; H$ n' ]) O P& s0 ~- K
###############################################+ ]3 V& c6 Y2 V
# Train test split and SVM training
5 S/ z3 ^$ j( q6 d \, n6 ^4 c( kfrom sklearn import cross_validation/ @6 ^+ I: G5 ]8 E! e! y. v
from sklearn.svm import SVC. X. \2 l: M4 ]5 g$ d
q0 Z9 w& m5 w; F5 W) e/ nX_train, X_test, y_train, y_test = cross_validation.train_test_split(X, y, test_size=0.25, random_state=5)
2 a" q$ w7 {6 b/ K' A
6 k. r; j* ]8 ?! _% s#params = {'kernel': 'linear'}0 y+ _! ^. g1 P2 {9 v* r* I7 }% D$ n. D
#params = {'kernel': 'poly', 'degree': 3}& v2 z+ n4 P" t5 `8 N1 q
params = {'kernel': 'rbf'}/ C' D4 c& M" v) j
classifier = SVC(**params)7 W9 W2 h9 u* _5 I& @
classifier.fit(X_train, y_train): c8 k K1 r% w( p- v: u+ B
utilities.plot_classifier(classifier, X_train, y_train, 'Training dataset'), e' T' x* N5 E# S3 u) F
6 m5 ?& I2 f# g" U; ?' C- Cy_test_pred = classifier.predict(X_test)! f/ ~5 U3 e7 V
utilities.plot_classifier(classifier, X_test, y_test, 'Test dataset')9 i- d- i7 z& j# ~
2 g+ s9 h+ [0 q
###############################################+ x+ e' Y( C/ y
# Evaluate classifier performance& e6 d- t$ q+ U: S+ K! D' u! S, Z
: t' T4 ~# A% `+ O2 {6 Z0 j3 c1 O
from sklearn.metrics import classification_report
: f, {/ r0 S' o* F% K$ ~% H( N, ]$ ~+ l6 i
target_names = ['Class-' + str(int(i)) for i in set(y)]
3 S3 ^1 T: e' N% {0 L: kprint "\n" + "#"*30/ t+ g5 o% G5 {4 X* b7 J
print "\nClassifier performance on training dataset\n"
/ S4 z5 @6 l4 }- e ?' D+ bprint classification_report(y_train, classifier.predict(X_train), target_names=target_names)+ c2 m% [- V+ |4 _8 t5 C
print "#"*30 + "\n"
n+ f) O o7 w& ?0 n6 J7 N$ X: b3 X4 i
print "#"*30
- O' p/ a( T. O; Q) D% iprint "\nClassification report on test dataset\n"% B- @$ c: O3 s0 U; C
print classification_report(y_test, y_test_pred, target_names=target_names)
% ~9 g+ N' Z( K! L( H# h. | Z7 `print "#"*30 + "\n"
1 A0 q$ y3 J2 j' w% Y8 h0 W5 D7 M5 E
|
|