- 金錢
- 45
- 威望
- 3183
- 貢獻值
- 0
- 推廣值
- 0
- 性別
- 保密
- 在線時間
- 38 小時
- 最後登錄
- 2024-2-25
- 主題
- 0
- 精華
- 0
- 閱讀權限
- 70
- 註冊時間
- 2012-3-17
- 帖子
- 553
 
該用戶從未簽到 - 推廣值
- 0
- 貢獻值
- 0
- 金錢
- 45
- 威望
- 3183
- 主題
- 0
|
import numpy as np1 `1 B6 O+ L/ U1 H) q
import matplotlib.pyplot as plt
: R* i9 [' p7 y( P _) Q9 X0 W) Q
0 m3 V0 B1 Y+ |, T9 aimport utilities
, X' Z# J7 B: G" o8 d( ]& O
/ R' ~' h( b) L: I2 o# Load input data
9 n2 I, N/ e3 r2 `input_file = 'D:\\1.Modeling material\\Py_Study\\2.code_model\\Python-Machine-Learning-Cookbook\\Python-Machine-Learning-Cookbook-master\\Chapter03\\data_multivar.txt'
$ M/ f0 T5 Q. p! X% o; KX, y = utilities.load_data(input_file)
9 I) H5 o- @5 a9 R' [$ r/ b2 t2 O+ c+ Q- p5 T5 w6 V" V
###############################################, i' M2 x; e/ R5 d' a7 L8 l
# Separate the data into classes based on 'y'
" C8 O( s$ L8 H' m' l7 B% c. hclass_0 = np.array([X[i] for i in range(len(X)) if y[i]==0])
& S( i+ [* n( cclass_1 = np.array([X[i] for i in range(len(X)) if y[i]==1])
0 H/ @6 s5 T T- C6 Z& }: t7 ~% k; r/ P+ V, W+ x
# Plot the input data
' j5 l3 e1 W. C9 A+ \+ Mplt.figure()
! r y$ w5 y; pplt.scatter(class_0[:,0], class_0[:,1], facecolors='black', edgecolors='black', marker='s')( d; u, U8 g4 m _6 b4 ^$ |! M, K6 z
plt.scatter(class_1[:,0], class_1[:,1], facecolors='None', edgecolors='black', marker='s')' k5 V, D5 \4 M) i3 Q( C& y C2 o
plt.title('Input data')
8 ]& @/ [2 ]9 v1 e9 T( |8 g6 S2 a9 b
###############################################
2 O7 O. `# d* @# Train test split and SVM training3 B2 M e" A+ A/ R$ d( E. v' @" Y
from sklearn import cross_validation
8 T( ~/ y) @( r3 q1 x" j: Ufrom sklearn.svm import SVC0 n% n$ P; V6 O) h. ?! J
6 O1 }1 p8 D, H v, t6 s7 \7 E; y' J8 |
X_train, X_test, y_train, y_test = cross_validation.train_test_split(X, y, test_size=0.25, random_state=5)6 u# A& S# u0 ]' \0 V3 g
/ Z" `; e% C w/ r2 ]$ g#params = {'kernel': 'linear'}
. s$ n4 A$ Z5 T0 Z; n# B& J. E$ V#params = {'kernel': 'poly', 'degree': 3}
, C+ `+ b0 o& w ^& \9 vparams = {'kernel': 'rbf'}8 R8 Z6 }6 \" d8 H0 s
classifier = SVC(**params)% y# ^, s3 K5 M/ c( Z6 Y
classifier.fit(X_train, y_train)
$ {4 s$ q( g) U) f# butilities.plot_classifier(classifier, X_train, y_train, 'Training dataset'); p$ I$ U( x" b4 d) U
" M/ ]3 `2 f6 Z, D' R
y_test_pred = classifier.predict(X_test)! A' D& C) w$ l/ H- F# C K( i
utilities.plot_classifier(classifier, X_test, y_test, 'Test dataset')6 m% p: c s& ?6 `' u; p/ R4 k" x) ]
1 f9 g" A. H6 ?
###############################################
6 z" [# N+ f9 k/ f3 j, W5 p( K. T# Evaluate classifier performance5 u1 A! N( h9 Y2 i4 X- p
2 {+ {5 [* w$ m
from sklearn.metrics import classification_report
- q- U+ m: H( ?+ b7 H0 g' c
8 W* J' A$ U8 Vtarget_names = ['Class-' + str(int(i)) for i in set(y)]
5 H0 K0 K3 t9 P! ]1 rprint "\n" + "#"*30. {# q* T. o) j6 Y* o6 D. P/ I
print "\nClassifier performance on training dataset\n"
3 a* Y- i0 [- K: G: bprint classification_report(y_train, classifier.predict(X_train), target_names=target_names)
: `9 o* D5 i1 i& r1 V- H( a9 Oprint "#"*30 + "\n"% C0 b" @: _* T% F: v
' S2 l. i" V: A6 H
print "#"*304 z5 l/ P( ?2 b) a/ n* d3 a
print "\nClassification report on test dataset\n"
9 K$ G4 O1 `. N, }) Q7 c5 q( cprint classification_report(y_test, y_test_pred, target_names=target_names)
( }' b% |* ~* L$ Yprint "#"*30 + "\n"' O! M! V% G" C I* x8 ?
8 A ?* V4 K9 f6 P8 ]
|
|