- 金錢
- 45
- 威望
- 3183
- 貢獻值
- 0
- 推廣值
- 0
- 性別
- 保密
- 在線時間
- 38 小時
- 最後登錄
- 2024-2-25
- 主題
- 0
- 精華
- 0
- 閱讀權限
- 70
- 註冊時間
- 2012-3-17
- 帖子
- 553
 
該用戶從未簽到 - 推廣值
- 0
- 貢獻值
- 0
- 金錢
- 45
- 威望
- 3183
- 主題
- 0
|
import numpy as np
! c1 v' O! e5 I5 U# n8 |& }1 ^import matplotlib.pyplot as plt
2 |: V2 \/ i/ Z. a( J
! T$ i- w5 M% u. Cimport utilities
6 C$ q/ a3 q h: x$ }
5 }* _+ P! v) }' H, ]9 @# Load input data }, p9 i- W$ _% J& ?, 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'
# Y0 a1 S3 K/ ~. T6 dX, y = utilities.load_data(input_file)* f- S0 k- j$ D
% g% w+ l2 I4 K5 \
###############################################% _! O, q7 e8 D! Q
# Separate the data into classes based on 'y'
, X/ F+ b+ Z( uclass_0 = np.array([X[i] for i in range(len(X)) if y[i]==0])( `) P r8 a$ W1 V0 E; l+ }
class_1 = np.array([X[i] for i in range(len(X)) if y[i]==1])# i* W3 Q: f6 S- G1 K
% J9 Z6 J" j1 E7 B; O) c! ]# Plot the input data
! c5 D" V3 Q% U: @* Qplt.figure()2 I% w: F% X( }- i9 S% l: X8 W
plt.scatter(class_0[:,0], class_0[:,1], facecolors='black', edgecolors='black', marker='s')- e% e& z+ q3 D
plt.scatter(class_1[:,0], class_1[:,1], facecolors='None', edgecolors='black', marker='s')6 \/ B' C- t! c; ^" i9 ?' T! s
plt.title('Input data')
1 \( q1 N% `/ T7 o; C( n0 F& G2 G( ^* O& |% }+ ^: I8 `/ F
###############################################
7 ?! l. y% H- G, _# o3 y# Train test split and SVM training
* D* d. F2 i$ N: qfrom sklearn import cross_validation9 |: K1 U6 @2 _
from sklearn.svm import SVC3 Y# c1 K; Y* e4 d( u& S4 g5 M" L
) Z1 ~* b% m0 S' p0 |' |: P& q( F* s
X_train, X_test, y_train, y_test = cross_validation.train_test_split(X, y, test_size=0.25, random_state=5)
}; O$ N7 \. \$ h1 k$ r1 r0 q. r- c) l! Y! @# S& H
#params = {'kernel': 'linear'}: G0 @- Z, n& v/ Q; ?
#params = {'kernel': 'poly', 'degree': 3}
( X1 _) P& p u; rparams = {'kernel': 'rbf'}
7 c7 l# y8 f! d7 x! C; ?. eclassifier = SVC(**params)
. b* N, _ Q T$ {% J3 l/ s5 Xclassifier.fit(X_train, y_train)
0 k% E: m# Q) w) v0 _+ Eutilities.plot_classifier(classifier, X_train, y_train, 'Training dataset')9 r3 o1 @+ R, | G
9 k' j% M' ^- p( |- e
y_test_pred = classifier.predict(X_test)
0 x9 p4 K& m6 G" N) k2 p; c' Vutilities.plot_classifier(classifier, X_test, y_test, 'Test dataset')
! `7 u. l7 a+ O# [: }5 N; a) n
) r, p5 c5 L) B3 Z###############################################
' t( u1 T; T1 E4 z+ w, ~8 ^# Evaluate classifier performance
; u# Q5 ~0 n6 A. [$ K# H ?
% u# M g" G# S+ l/ q' g% lfrom sklearn.metrics import classification_report s1 r d4 V2 s2 t
4 @( h1 Y; [+ ]% c7 t. Y0 Ttarget_names = ['Class-' + str(int(i)) for i in set(y)]- K/ L5 U9 D1 h
print "\n" + "#"*30$ D1 F; ]. G( _# L; x
print "\nClassifier performance on training dataset\n". I* c; Z: k. ` f9 R
print classification_report(y_train, classifier.predict(X_train), target_names=target_names)* H! I8 T8 i: M# j; o
print "#"*30 + "\n"
8 z o6 [ ~! j4 G& H& v% b `8 I+ {. t. f
print "#"*30+ Q4 G8 `* A0 s) }/ {
print "\nClassification report on test dataset\n"6 |0 l8 \7 T, R( s1 P ?' ^( {2 c. _
print classification_report(y_test, y_test_pred, target_names=target_names)6 E- g% W- r6 a" I
print "#"*30 + "\n"
) M4 Q0 u3 ?- u1 r% H- k, O5 n
4 P3 h1 P: l2 W" {: e6 R4 T% S3 q t |
|