- 金錢
- 46
- 威望
- 3183
- 貢獻值
- 0
- 推廣值
- 0
- 性別
- 保密
- 在線時間
- 38 小時
- 最後登錄
- 2024-2-25
- 主題
- 0
- 精華
- 0
- 閱讀權限
- 70
- 註冊時間
- 2012-3-17
- 帖子
- 556
 
該用戶從未簽到 - 推廣值
- 0
- 貢獻值
- 0
- 金錢
- 46
- 威望
- 3183
- 主題
- 0
|
import numpy as np
5 F. W; W) e I/ Himport matplotlib.pyplot as plt
; T5 g4 p. E) N* m- \: ^3 T* r+ Q
import utilities 1 F3 V$ D2 ]. s- m7 U+ W
: |# L' k2 B& s$ W' D# Load input data, T' w' X) c7 F* z3 [. @8 U8 \: W2 a; {
input_file = 'D:\\1.Modeling material\\Py_Study\\2.code_model\\Python-Machine-Learning-Cookbook\\Python-Machine-Learning-Cookbook-master\\Chapter03\\data_multivar.txt'
4 h" o; [% Q& R! Z- T) sX, y = utilities.load_data(input_file)
* f, i) n. ~) _3 ^& e% K' _( q4 |5 l9 s& A) n3 e% `
###############################################
( E9 o+ R/ s% [# Separate the data into classes based on 'y'
. c, |5 D7 j* z) p* r6 Q: z( Pclass_0 = np.array([X[i] for i in range(len(X)) if y[i]==0])4 S3 R1 J4 p# p' f* h
class_1 = np.array([X[i] for i in range(len(X)) if y[i]==1])# z# ?% c3 z4 _$ r7 o. g
! V J* |1 N* A$ R# Plot the input data/ G( I( M4 M2 C, P
plt.figure()
6 A) s- F7 w9 X. C( k% c7 j* q) l" ^plt.scatter(class_0[:,0], class_0[:,1], facecolors='black', edgecolors='black', marker='s'): M# o6 ]' N/ Y' z) Y- @& I
plt.scatter(class_1[:,0], class_1[:,1], facecolors='None', edgecolors='black', marker='s')
' E6 X- z- P& Q1 [; u4 u1 mplt.title('Input data'). g7 j+ V, L# }: a9 g9 ?! {
, W/ k+ \; i0 f- D; P
###############################################5 \; C- L5 {+ j
# Train test split and SVM training
6 J% x, p1 f. n' j2 n, f6 ?( ofrom sklearn import cross_validation
6 ?. E t( W, }( J6 W. t1 lfrom sklearn.svm import SVC
+ R( Q* _: V7 i" o, }8 ~4 z- b& [7 n0 \. a6 Y) b6 p" p
X_train, X_test, y_train, y_test = cross_validation.train_test_split(X, y, test_size=0.25, random_state=5). V: s6 G. n! h) \5 n6 Z
5 d5 c* b, S# P" e9 z1 |' N5 e x
#params = {'kernel': 'linear'}; J! p: `7 c; [! W% d: C. b7 q/ U
#params = {'kernel': 'poly', 'degree': 3}. W5 d* G# J% F7 }2 ]4 Z( ~! N
params = {'kernel': 'rbf'}
# T0 K0 u3 x; p; Z& U- rclassifier = SVC(**params)
( k' m d5 V3 D* c* P2 ?6 yclassifier.fit(X_train, y_train)% L% S( J6 O/ T8 W5 ]5 C
utilities.plot_classifier(classifier, X_train, y_train, 'Training dataset')
3 M [" k" W& M. u( L( m; M8 W. L9 _* X, s; v: o5 i, i
y_test_pred = classifier.predict(X_test). B2 }5 A E1 k Z# }0 W, x! C
utilities.plot_classifier(classifier, X_test, y_test, 'Test dataset') S: `5 G: l Y8 p% H* Q
. D( l5 i7 G M# R
###############################################) A1 h7 `+ l; k- Z! i* u( j6 Z' s
# Evaluate classifier performance) n8 O! Z! ]- @7 F; Z$ k( n1 m2 r
2 S6 s! J. F% bfrom sklearn.metrics import classification_report
, x1 z/ A2 H6 G3 \' v* s7 _/ Z& T5 g- T" H' Z4 a
target_names = ['Class-' + str(int(i)) for i in set(y)]7 m: _2 c; L8 w0 \1 P
print "\n" + "#"*30/ [/ }. U" D3 z" k2 }* p
print "\nClassifier performance on training dataset\n"
, c* R" R+ |5 c, P3 S, o) ?print classification_report(y_train, classifier.predict(X_train), target_names=target_names)
8 m4 o; B! E) ]7 w- {print "#"*30 + "\n"6 ^4 f5 B( s6 V& m
" r; W& H+ r4 G7 T9 [# P3 R) K7 o0 b
print "#"*30" x. v1 C' E E" \9 {; Y" X$ G! P
print "\nClassification report on test dataset\n"* I+ D' i) X/ r; ?
print classification_report(y_test, y_test_pred, target_names=target_names)
) y" `* x1 w& M) X# u3 S2 w4 Aprint "#"*30 + "\n"
) v1 f# E$ i6 ?7 P8 W! i# e
0 I0 Y# z/ k6 {% H! I e |
|