- 金錢
- 45
- 威望
- 3183
- 貢獻值
- 0
- 推廣值
- 0
- 性別
- 保密
- 在線時間
- 38 小時
- 最後登錄
- 2024-2-25
- 主題
- 0
- 精華
- 0
- 閱讀權限
- 70
- 註冊時間
- 2012-3-17
- 帖子
- 553
 
該用戶從未簽到 - 推廣值
- 0
- 貢獻值
- 0
- 金錢
- 45
- 威望
- 3183
- 主題
- 0
|
import numpy as np) n( }( i, l7 l8 W* ~
import matplotlib.pyplot as plt
, T& `. o V4 _7 {* a3 V7 D
1 ]8 k* g! t o- cimport utilities
) r7 m) O" x/ V# y/ e/ F" i1 V8 P" J0 k4 G' D
# Load input data
+ O7 U6 F6 ^$ o- f0 y2 Qinput_file = 'D:\\1.Modeling material\\Py_Study\\2.code_model\\Python-Machine-Learning-Cookbook\\Python-Machine-Learning-Cookbook-master\\Chapter03\\data_multivar.txt'0 s6 ^, c4 Z8 Z t
X, y = utilities.load_data(input_file)
; R' v1 |4 h# ?; U6 j# |0 Z0 Q
' g+ C m1 a7 |4 o Y4 P###############################################
9 u+ Z5 D+ H3 f; |+ s& q1 K# Separate the data into classes based on 'y'* ], V. Q9 {+ _7 g
class_0 = np.array([X[i] for i in range(len(X)) if y[i]==0])
$ r" g; E, c5 S+ h) f( W8 C' @5 sclass_1 = np.array([X[i] for i in range(len(X)) if y[i]==1])+ N! A# w/ X. c# _9 W% J6 N
" x* P8 b* y/ G$ {% ?- G! h
# Plot the input data( h( n2 I/ F+ u3 b$ g j2 V8 X9 I1 [
plt.figure(): H7 [+ W2 `% _* }
plt.scatter(class_0[:,0], class_0[:,1], facecolors='black', edgecolors='black', marker='s')
/ D+ K- `% V: g b6 k: Vplt.scatter(class_1[:,0], class_1[:,1], facecolors='None', edgecolors='black', marker='s')
& g! R! J% @" y% \6 p: B. `plt.title('Input data')
4 S9 R3 t x# J3 o1 ~, ~) A5 |
9 ?3 o% t6 i1 h }6 V. E X; J! y###############################################- u5 c( U' U/ _) d/ r
# Train test split and SVM training
$ d% _5 D. d. j6 `6 mfrom sklearn import cross_validation
/ ]/ c% g0 y; Z, hfrom sklearn.svm import SVC/ d) t* L" ]% }8 z; v
6 l1 e& y1 ?! [3 [1 l& B/ h6 F
X_train, X_test, y_train, y_test = cross_validation.train_test_split(X, y, test_size=0.25, random_state=5)
1 C0 F' f4 Z% i$ e/ m; T: ~; H5 h( l
#params = {'kernel': 'linear'} Z" `3 I0 C3 V# [& P8 _0 P' O
#params = {'kernel': 'poly', 'degree': 3}( a( \. e9 J. ?
params = {'kernel': 'rbf'}
% m6 i) t3 W5 T+ U5 |! N$ ~0 e, |classifier = SVC(**params)
* N% t1 g3 z( ^1 f3 Nclassifier.fit(X_train, y_train)
. o2 M+ G$ o: _: Y! g" @utilities.plot_classifier(classifier, X_train, y_train, 'Training dataset')
; }! J) F& `6 h: a1 T2 L4 ^% X Y) e! [
y_test_pred = classifier.predict(X_test)
$ N- G* y' {- P: L( rutilities.plot_classifier(classifier, X_test, y_test, 'Test dataset')& i; r) e1 n! L/ Z' D% e
* P6 P0 e9 I- y, K' L% o/ T5 z###############################################
% o/ s% T/ Q2 Y0 m6 n% u: ]# Evaluate classifier performance( i' E$ s. n2 w$ @
: y4 k, o1 v0 N( m
from sklearn.metrics import classification_report
( I* X; o( h, y6 P! Y; ~8 A' g ]/ v$ V. p' Z/ O6 U: d$ I
target_names = ['Class-' + str(int(i)) for i in set(y)]0 Z6 V3 Q2 S% R
print "\n" + "#"*30
7 H9 m* G& f* A7 y0 Kprint "\nClassifier performance on training dataset\n"
5 R$ V- ~8 k. p) \8 V+ }5 e9 I9 Vprint classification_report(y_train, classifier.predict(X_train), target_names=target_names)4 H M5 t* B7 V) Q& Y- w2 j/ Z$ p& r
print "#"*30 + "\n"
. I" j3 M/ v" w2 |1 \0 Q# F8 [1 |! h7 y$ k3 \$ X
print "#"*30
7 {, N4 s4 k+ W& ~) n8 eprint "\nClassification report on test dataset\n"
% m6 V( g1 V+ w( tprint classification_report(y_test, y_test_pred, target_names=target_names)1 O1 e' V& d1 i
print "#"*30 + "\n"2 F) v' ^* }: ?
6 O* ?$ I# N9 E1 R! r- c! |
|
|