- 金錢
- 46
- 威望
- 3183
- 貢獻值
- 0
- 推廣值
- 0
- 性別
- 保密
- 在線時間
- 38 小時
- 最後登錄
- 2024-2-25
- 主題
- 0
- 精華
- 0
- 閱讀權限
- 70
- 註冊時間
- 2012-3-17
- 帖子
- 556
 
該用戶從未簽到 - 推廣值
- 0
- 貢獻值
- 0
- 金錢
- 46
- 威望
- 3183
- 主題
- 0
|
import numpy as np/ I }: q; u% W! y
import matplotlib.pyplot as plt8 |1 m, n2 f5 r* e# f o
b- @; \+ ]6 Z9 s
import utilities
- f+ f# v# n. _7 J v, s: ^7 @5 B- U. j# K2 n' f
# Load input data" q3 ~2 X7 B6 m; D1 ?1 s6 _
input_file = 'D:\\1.Modeling material\\Py_Study\\2.code_model\\Python-Machine-Learning-Cookbook\\Python-Machine-Learning-Cookbook-master\\Chapter03\\data_multivar.txt'6 g! p5 D% ]- }
X, y = utilities.load_data(input_file)
% y+ |. L/ g: ~0 D' l" X' P
" C6 e; j6 H# H: W0 q$ O###############################################
k( P6 }! m, X/ {# K# Separate the data into classes based on 'y'0 i) Q# k& P$ U7 n, F4 s
class_0 = np.array([X[i] for i in range(len(X)) if y[i]==0])" I; A- Z/ o$ z& w% ~ S
class_1 = np.array([X[i] for i in range(len(X)) if y[i]==1])" h. S$ o9 U) [( W0 H: [9 _
0 ?" _' t1 m( g$ Z* c0 c# Plot the input data) i3 x" g/ u8 C
plt.figure()6 v5 |3 S1 o9 i
plt.scatter(class_0[:,0], class_0[:,1], facecolors='black', edgecolors='black', marker='s')8 J* h; X) C# l. b% R% M9 l" m7 w& g
plt.scatter(class_1[:,0], class_1[:,1], facecolors='None', edgecolors='black', marker='s')* i0 f. ^7 U9 A! Y% p! g( u5 q" x
plt.title('Input data')
7 B( N k k; ?3 n5 F# s+ D" K- p+ Z, A# }
###############################################. I: [ x9 s2 I( m
# Train test split and SVM training
4 L: O" R' K+ n! G# c( mfrom sklearn import cross_validation. h4 j) @3 o8 M2 V3 N% { a
from sklearn.svm import SVC4 E2 |* Q5 F7 k2 H
5 k; ^6 j6 J1 I: w8 R* d0 g# E
X_train, X_test, y_train, y_test = cross_validation.train_test_split(X, y, test_size=0.25, random_state=5)9 c1 X0 T" L- X% A/ @1 Q
1 ]3 B2 z0 t' ^/ K3 H* G+ ]#params = {'kernel': 'linear'}$ `$ c6 {0 {4 P! e. A1 j( C+ y" p
#params = {'kernel': 'poly', 'degree': 3}
5 |" ?( k3 [2 H" Y! `params = {'kernel': 'rbf'}
* c$ Y' k; ~& Xclassifier = SVC(**params)
0 A" M8 E2 f* k% rclassifier.fit(X_train, y_train)' @# J+ E/ o" H; s( X9 Y
utilities.plot_classifier(classifier, X_train, y_train, 'Training dataset')/ }- M0 D% N8 e: z
8 {5 w. Z* r4 Q3 |8 Oy_test_pred = classifier.predict(X_test)
! X; u6 v5 h5 d: P; Lutilities.plot_classifier(classifier, X_test, y_test, 'Test dataset')
! j4 Z7 {, f3 m5 l0 a( T% H+ y
% R0 Y' f3 q2 v& I: P###############################################$ [1 U# F8 R# Z. z) `+ }
# Evaluate classifier performance
5 F, S! H% z' j o5 B q: M
+ [8 G/ p; d8 d+ Z& Pfrom sklearn.metrics import classification_report1 T1 F" p: k9 d$ i! l* c
1 v3 X% a3 f- Z+ ^* |
target_names = ['Class-' + str(int(i)) for i in set(y)]
# I' \& v7 i H- L1 j3 }print "\n" + "#"*30
' @9 q, k1 Q, j' ^" \( c% p+ Hprint "\nClassifier performance on training dataset\n"3 Q0 j3 h) `6 {5 E
print classification_report(y_train, classifier.predict(X_train), target_names=target_names)
2 [2 A& C; c' uprint "#"*30 + "\n"
: V" d, f0 M" |& X, G2 l8 d7 [0 F3 J, S( O5 q/ B( o
print "#"*30
$ a g2 V7 \ k0 A+ h8 ]print "\nClassification report on test dataset\n"2 p4 M! X; i- u4 z) X
print classification_report(y_test, y_test_pred, target_names=target_names)
% K7 v' f' D2 v( y5 p0 |3 Fprint "#"*30 + "\n"/ T) Z" Y) ^. ~" B" R# b3 X: o3 U
0 G- I) @2 C z4 Y& O c2 V! @
|
|