- 金錢
- 46
- 威望
- 3183
- 貢獻值
- 0
- 推廣值
- 0
- 性別
- 保密
- 在線時間
- 38 小時
- 最後登錄
- 2024-2-25
- 主題
- 0
- 精華
- 0
- 閱讀權限
- 70
- 註冊時間
- 2012-3-17
- 帖子
- 556
該用戶從未簽到 - 推廣值
- 0
- 貢獻值
- 0
- 金錢
- 46
- 威望
- 3183
- 主題
- 0
|
import numpy as np
& k, e4 N$ ?% o. X3 W; oimport matplotlib.pyplot as plt
( E) D! t' M# H! @, t4 N( c% u) e4 n* Q' E2 R" E3 I
import utilities
$ g% d1 }7 S) h* S* K K: f; B7 j' d& G4 z: q1 p2 u$ s
# Load input data
G6 D; O. A6 F- P' Y' I; iinput_file = 'D:\\1.Modeling material\\Py_Study\\2.code_model\\Python-Machine-Learning-Cookbook\\Python-Machine-Learning-Cookbook-master\\Chapter03\\data_multivar.txt'8 w$ W1 j+ ~5 V: ?1 S; A
X, y = utilities.load_data(input_file)( O/ w! Y- u8 \9 E, o4 Y# s! g" c
3 W R9 V V' v' D; T' j###############################################
2 G/ m% I7 i! K6 u% |0 x- _# Separate the data into classes based on 'y'
H) C8 [: V8 \8 Q0 j& {! T0 @class_0 = np.array([X[i] for i in range(len(X)) if y[i]==0])
8 t$ F, D7 Q& D; nclass_1 = np.array([X[i] for i in range(len(X)) if y[i]==1])
# t7 e4 }) w& y" K/ J- f \& R/ W
- E E1 H( b1 D# Plot the input data
6 g5 Y8 @. x! X9 B/ W, Splt.figure(). Z4 I1 b2 D2 J* }
plt.scatter(class_0[:,0], class_0[:,1], facecolors='black', edgecolors='black', marker='s')
9 y$ C# I- [9 U) g+ I# h+ d' Splt.scatter(class_1[:,0], class_1[:,1], facecolors='None', edgecolors='black', marker='s')9 Q" @# V' G& f9 g
plt.title('Input data')# a/ y( e- U4 F8 l* @7 x- ~
* f! l5 E2 A" J: ~: F! Y5 ~" ]###############################################
, c# F" w" n1 r& K2 u( C* `# Train test split and SVM training
7 B0 d+ f' ^! M( p) {from sklearn import cross_validation
/ \# A& L c& h- O! G2 I( Wfrom sklearn.svm import SVC% i6 t# I0 I S6 z1 m& g
# h4 \8 [: R4 C- u, ]
X_train, X_test, y_train, y_test = cross_validation.train_test_split(X, y, test_size=0.25, random_state=5)
+ v* ^4 v) L$ Y/ Y& K
9 ^8 o9 {6 k0 A r#params = {'kernel': 'linear'}. Y3 d3 H2 t; l: p B; U& L
#params = {'kernel': 'poly', 'degree': 3}
* h# F! T: ]& z* J3 Fparams = {'kernel': 'rbf'}5 h3 M+ H, Q: o
classifier = SVC(**params)6 C9 \* K( C) J% g& T7 ^8 J2 }
classifier.fit(X_train, y_train)+ |+ F3 H: h$ m
utilities.plot_classifier(classifier, X_train, y_train, 'Training dataset')) A8 a3 q0 W0 n
+ O/ `+ ?6 P# T7 P) A; d- K7 D' uy_test_pred = classifier.predict(X_test)
5 g/ e" s4 l+ C a( e: v( Sutilities.plot_classifier(classifier, X_test, y_test, 'Test dataset')
2 }* i/ J) Q# Y; o/ }
2 g0 p' `0 @5 {1 _###############################################$ l' d) C9 ]% d3 u# Q. t
# Evaluate classifier performance
( T/ W6 b/ g' `, V4 D y" Q) F2 M3 I2 X p4 T6 b. b
from sklearn.metrics import classification_report
6 w7 w2 D& P2 f5 ]/ N7 z2 y" B1 }, u2 s0 e( Z
target_names = ['Class-' + str(int(i)) for i in set(y)]2 B& D" y/ Q* W8 s& I
print "\n" + "#"*30" W9 {1 |0 @ M
print "\nClassifier performance on training dataset\n"
, Z, Q1 }" t3 D$ ?' F' j7 Pprint classification_report(y_train, classifier.predict(X_train), target_names=target_names)
" M8 x1 k+ j3 i% n. i: o; X% cprint "#"*30 + "\n"2 p% h( m: |0 _. g5 N# I
3 D/ q1 v! U+ yprint "#"*30
7 k# q) ^; Y* F1 A, T4 s5 |print "\nClassification report on test dataset\n"
+ E; m" {8 H3 N: ^9 m, bprint classification_report(y_test, y_test_pred, target_names=target_names)
+ ?3 m2 D+ U# d+ U1 Q" m8 F; Fprint "#"*30 + "\n"$ g: j; o7 ?! e! g- _- B: T, U9 z
/ G k# u c9 C3 Y" W
|
|