- 金錢
- 45
- 威望
- 3183
- 貢獻值
- 0
- 推廣值
- 0
- 性別
- 保密
- 在線時間
- 38 小時
- 最後登錄
- 2024-2-25
- 主題
- 0
- 精華
- 0
- 閱讀權限
- 70
- 註冊時間
- 2012-3-17
- 帖子
- 553
 
該用戶從未簽到 - 推廣值
- 0
- 貢獻值
- 0
- 金錢
- 45
- 威望
- 3183
- 主題
- 0
|
import numpy as np
/ ]6 @# [# R1 Q! F/ I* h( @import matplotlib.pyplot as plt, ~; O7 j0 T6 A
' r% ` E1 W* f0 D3 s( b6 |- }
import utilities
3 C1 |- h3 P0 L: Q% p4 J
: z. S) n& W# V8 K$ o% `. j3 w# Load input data) k3 x) M) m; r8 f5 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' H+ X( W9 S! F2 ~) D
X, y = utilities.load_data(input_file)
# H" F5 j6 f. v0 w( e4 @9 H, o
0 X, Q1 t, e* N Y# k) c###############################################
3 ~: z& Y2 W% O. G9 }2 R# Separate the data into classes based on 'y'# s0 J: B/ a" N2 e0 p6 s
class_0 = np.array([X[i] for i in range(len(X)) if y[i]==0]). K3 R& z/ h4 S, f% T* ]$ f
class_1 = np.array([X[i] for i in range(len(X)) if y[i]==1])" \% o! V- [& f- o# l r N
1 S; r/ F; H0 `3 b3 q, s" Y# Plot the input data# w1 f# ~5 B% P. |; h* N' C9 S
plt.figure()7 Y j6 A5 ]: R" m( i. O
plt.scatter(class_0[:,0], class_0[:,1], facecolors='black', edgecolors='black', marker='s')
' r% `# w5 k- q5 gplt.scatter(class_1[:,0], class_1[:,1], facecolors='None', edgecolors='black', marker='s')6 t) ?! J) b) T9 w, p$ X3 _ y& e
plt.title('Input data')
3 K9 v. t, H, |; o0 I' @- Y' \. ~/ ~! q( ^& ^% l8 g
###############################################: m. N/ c( ]. z( r4 s. h6 ~
# Train test split and SVM training
6 v6 ? T) @; [5 Nfrom sklearn import cross_validation
8 w+ l6 ]) T& j* |from sklearn.svm import SVC
% n9 K# D! @8 K% d, R* N
7 M1 y4 n) y0 \: W7 F" s, ]/ YX_train, X_test, y_train, y_test = cross_validation.train_test_split(X, y, test_size=0.25, random_state=5)
) U! f1 ~+ L) u, J; } Q' W9 [7 }& K3 T
#params = {'kernel': 'linear'}
3 @# h2 l7 M, g( r r# c#params = {'kernel': 'poly', 'degree': 3}
" y% p! F0 L1 H1 uparams = {'kernel': 'rbf'}
" t8 U. N2 X- w& ?classifier = SVC(**params)
5 P" o8 o J6 qclassifier.fit(X_train, y_train)* ]9 h$ l: w5 x
utilities.plot_classifier(classifier, X_train, y_train, 'Training dataset')
- q' }8 e0 Q" d7 A7 q8 m
! p8 L% z8 o4 F4 o C6 cy_test_pred = classifier.predict(X_test): s# T$ N& J1 O j" s9 x
utilities.plot_classifier(classifier, X_test, y_test, 'Test dataset') V H$ l3 J& a4 Z
) {1 B8 F, u3 j) d& o3 s###############################################
. p; U1 ~" ?0 j: j) D! i$ @# Evaluate classifier performance7 c% t8 S$ v3 ~0 {4 i7 U
* e0 k' a6 h1 ]0 x y: Hfrom sklearn.metrics import classification_report! p5 x, u/ z! `! v
6 D9 E4 F+ _( ~target_names = ['Class-' + str(int(i)) for i in set(y)]
3 R/ T- K7 y- J% k q: dprint "\n" + "#"*30! q+ Y' _7 n, ?
print "\nClassifier performance on training dataset\n"
) p9 C+ {) R4 k6 z" A8 |print classification_report(y_train, classifier.predict(X_train), target_names=target_names)7 l7 N+ b! n' Z* q4 _
print "#"*30 + "\n"
8 H: ?4 E7 c* p7 V4 `6 Q& K+ q5 z0 r$ Z/ b1 f. z7 c8 q, j! J4 U
print "#"*301 X, W9 k* r& P0 h5 z8 L
print "\nClassification report on test dataset\n"
9 L, m; `$ Y2 i B2 b( zprint classification_report(y_test, y_test_pred, target_names=target_names), ~) ~" P0 c, w% b5 n
print "#"*30 + "\n"+ W% {* v( T7 s/ {( M- U
6 d6 X/ c @+ N. ^/ S9 `
|
|