- 金錢
- 46
- 威望
- 3183
- 貢獻值
- 0
- 推廣值
- 0
- 性別
- 保密
- 在線時間
- 38 小時
- 最後登錄
- 2024-2-25
- 主題
- 0
- 精華
- 0
- 閱讀權限
- 70
- 註冊時間
- 2012-3-17
- 帖子
- 557
該用戶從未簽到 - 推廣值
- 0
- 貢獻值
- 0
- 金錢
- 46
- 威望
- 3183
- 主題
- 0
|
import numpy as np
3 _2 A. ]2 Z3 z. i% pimport matplotlib.pyplot as plt
' K+ z0 L; N& n: ~" R0 H1 j5 d9 b! F; b J- A1 N0 y; Y! s3 c" j
import utilities
: T) z# h0 z( q
4 }7 @& N, i- f4 F ?% [1 [# Load input data
* Q8 p& t7 ^6 E$ G* Cinput_file = 'D:\\1.Modeling material\\Py_Study\\2.code_model\\Python-Machine-Learning-Cookbook\\Python-Machine-Learning-Cookbook-master\\Chapter03\\data_multivar.txt'
4 J' ]- e+ E3 TX, y = utilities.load_data(input_file)
8 E4 l; C+ _: ~
$ M$ g4 k( d6 m% z2 z###############################################( U! |2 B' O5 Y% Z1 X6 b* R
# Separate the data into classes based on 'y'6 z4 `( K8 K& c) t! s
class_0 = np.array([X[i] for i in range(len(X)) if y[i]==0])1 j0 F8 [+ j9 S: @
class_1 = np.array([X[i] for i in range(len(X)) if y[i]==1])2 g, ]* u# d3 {) n( ?. M: g E
# @: b" v6 ~* w* |* r- w# Plot the input data: f' t5 e: ]1 n( n' X
plt.figure()
9 U8 A o |" t& s% a/ Bplt.scatter(class_0[:,0], class_0[:,1], facecolors='black', edgecolors='black', marker='s')" B+ ^4 Y6 n# k! @ u* q0 B
plt.scatter(class_1[:,0], class_1[:,1], facecolors='None', edgecolors='black', marker='s')9 {. D5 X/ J& Q. g4 [: K6 v5 c; q
plt.title('Input data')+ }# N# g/ G/ g. U& I5 ?1 \7 J
& G6 o, Z, V0 u7 m$ v. a( ^###############################################
, i- K) T9 {& u% _. d8 r2 e3 s" [; {# Train test split and SVM training5 b* p5 J0 U% l% ^; ]- G0 B F2 g/ S
from sklearn import cross_validation
1 y! q7 ^: r9 r) w* F3 w- X- Y" ^, Kfrom sklearn.svm import SVC
! h$ @& {3 U4 U# f7 ?8 v' h7 k2 p4 M+ d( y+ v" |" ~ F
X_train, X_test, y_train, y_test = cross_validation.train_test_split(X, y, test_size=0.25, random_state=5)
$ j* E- g, G# w4 v1 {3 Q/ O8 o7 p% P7 ^# h
#params = {'kernel': 'linear'}4 K+ P* w+ Y7 E9 ]1 @8 P
#params = {'kernel': 'poly', 'degree': 3}- i; \3 b- G. e! ?. |4 b. M
params = {'kernel': 'rbf'}! h: V2 F8 ?) V6 r( S
classifier = SVC(**params)
7 k, g$ ]) `9 K( o4 K7 Z' R; Sclassifier.fit(X_train, y_train)
8 r/ o4 s, f/ q/ A2 Butilities.plot_classifier(classifier, X_train, y_train, 'Training dataset')
, b; [! a& P4 q
5 B0 J$ V, Y t8 N, ]/ k! Y+ Jy_test_pred = classifier.predict(X_test)3 L2 N5 S6 r% G: q/ ?
utilities.plot_classifier(classifier, X_test, y_test, 'Test dataset')6 E9 y# O- ?% L
7 |5 R$ f4 G: g) o& ]' D###############################################7 M2 C/ B2 D" [ s* h9 d. E# Q5 \; A
# Evaluate classifier performance& j3 n8 _7 _# V5 C' c+ t8 ^) q
7 ~4 T2 s! z8 X5 C" j" x+ u: z% Bfrom sklearn.metrics import classification_report0 i0 H! h' x6 d6 J6 b; T1 i [
+ b9 K' _2 t4 `" {target_names = ['Class-' + str(int(i)) for i in set(y)]6 x0 F& ~% }9 A/ ^
print "\n" + "#"*30+ f. C0 I- o3 l2 z
print "\nClassifier performance on training dataset\n". u1 A3 ~3 u1 [/ |& X
print classification_report(y_train, classifier.predict(X_train), target_names=target_names)- W6 X$ ~: U' Z$ |
print "#"*30 + "\n"
1 }$ m' A# Y. g% ?: Y- p
0 p5 i. \0 n$ u/ K% Y, Rprint "#"*30) | r. O3 M! j. y# y: z
print "\nClassification report on test dataset\n"0 e9 B7 x4 `- {
print classification_report(y_test, y_test_pred, target_names=target_names)4 s% I- q( f6 f# ~5 f6 G& ?* G+ X
print "#"*30 + "\n"
' g: M7 L2 ^. v' n
. |8 R4 p& W/ R% _ H6 g; S& e |
|