- 金錢
- 45
- 威望
- 3183
- 貢獻值
- 0
- 推廣值
- 0
- 性別
- 保密
- 在線時間
- 38 小時
- 最後登錄
- 2024-2-25
- 主題
- 0
- 精華
- 0
- 閱讀權限
- 70
- 註冊時間
- 2012-3-17
- 帖子
- 553
 
該用戶從未簽到 - 推廣值
- 0
- 貢獻值
- 0
- 金錢
- 45
- 威望
- 3183
- 主題
- 0
|
import numpy as np
( {) @5 _# i! ?# a: _import matplotlib.pyplot as plt& n6 M4 R( f3 _. g
- D! A6 [/ I& t. t( U# }7 [% z
import utilities
0 v/ l0 j& r* @. M S+ y3 y( T# F0 D3 L; J: I, y
# Load input data
) z1 l8 f; i8 q9 d7 ninput_file = 'D:\\1.Modeling material\\Py_Study\\2.code_model\\Python-Machine-Learning-Cookbook\\Python-Machine-Learning-Cookbook-master\\Chapter03\\data_multivar.txt'
. ]3 Z. V- A# e6 B+ u5 m2 mX, y = utilities.load_data(input_file)" V f$ }4 z5 Z2 b1 z
2 `# Y5 t# U2 r# d. j9 V3 n& F###############################################
* k" p, {0 q2 S: b# Separate the data into classes based on 'y'. l, M- B9 y/ y6 }
class_0 = np.array([X[i] for i in range(len(X)) if y[i]==0])
2 q$ A7 }$ P+ n% o) Q: g, u# Lclass_1 = np.array([X[i] for i in range(len(X)) if y[i]==1])7 F: ~% t% h/ A" M6 V& H( b9 Z. H
, ~' l6 b' T- A% o" V# E. l# Plot the input data; z* p& _( q2 @: d9 U
plt.figure()8 W; r' Q' u0 V& h. I
plt.scatter(class_0[:,0], class_0[:,1], facecolors='black', edgecolors='black', marker='s')
9 q9 X R, I8 E* w6 V0 Y: |plt.scatter(class_1[:,0], class_1[:,1], facecolors='None', edgecolors='black', marker='s')
% v% }9 e6 h1 `5 C4 s% v# bplt.title('Input data')- B' n6 M* k' K1 B& v" n4 H
c8 n: W a, N: R* N1 S, A @
###############################################
. E' I1 p, V6 @, T# Train test split and SVM training- R) ]+ Y. }; f) Q7 ]3 q" M
from sklearn import cross_validation# {' @; v {: ~* P! U0 d9 M
from sklearn.svm import SVC6 @! c& t, i3 O: h8 f2 y
3 }9 Z& ?) G5 p; N! P* S6 J# O, ~* eX_train, X_test, y_train, y_test = cross_validation.train_test_split(X, y, test_size=0.25, random_state=5). {% t( I0 w8 _6 m+ \ U
& G/ X" b: W2 ~5 t
#params = {'kernel': 'linear'}
3 C: e Y; ?$ u; s0 d4 j#params = {'kernel': 'poly', 'degree': 3}. }8 w" Z ^! K/ y; O
params = {'kernel': 'rbf'}% Y: A2 P& V' Z/ g: J4 x: v/ m
classifier = SVC(**params)
8 Z9 P# ?, l* K+ t" [ uclassifier.fit(X_train, y_train)
. v* i$ ?- N% R8 butilities.plot_classifier(classifier, X_train, y_train, 'Training dataset')& @- M1 L' N/ D% ~5 T3 j4 P8 }
) u) u2 n! X. q6 q0 e
y_test_pred = classifier.predict(X_test)
3 W2 R' ^, ?& B& O( ?9 _utilities.plot_classifier(classifier, X_test, y_test, 'Test dataset')3 K0 x8 H; M+ G% D2 l- h+ ~
% }8 X3 |) W: t4 `* v
###############################################
4 }% d6 b- H- d/ E4 W! Z% _# Evaluate classifier performance' _3 c8 I. l+ ^& U
, f; M# K) G9 n6 [/ @
from sklearn.metrics import classification_report
4 f% h0 b7 C; N
" v9 C l' n) t" O3 Ktarget_names = ['Class-' + str(int(i)) for i in set(y)]
4 ?( b& M- e# [print "\n" + "#"*309 F% ?+ G+ l& l2 g6 X
print "\nClassifier performance on training dataset\n"( E2 Z# L @5 Z6 b& D, N1 `3 D: b
print classification_report(y_train, classifier.predict(X_train), target_names=target_names)( ` Z: H: B* d; h4 u
print "#"*30 + "\n"
7 Y# j- H/ h4 V0 N3 h, G3 f& H3 k t/ u- f" P9 g3 W0 S
print "#"*30
7 L* z! |4 A) L: S% _ F2 s" o' dprint "\nClassification report on test dataset\n"
8 E0 u% A P' r3 wprint classification_report(y_test, y_test_pred, target_names=target_names)& G2 Z3 C% |% S5 D2 X
print "#"*30 + "\n"5 T, x7 i3 D% `2 S) G
, ^( }' u; e' h' `( r. [2 G |
|