- 金錢
- 45
- 威望
- 3183
- 貢獻值
- 0
- 推廣值
- 0
- 性別
- 保密
- 在線時間
- 38 小時
- 最後登錄
- 2024-2-25
- 主題
- 0
- 精華
- 0
- 閱讀權限
- 70
- 註冊時間
- 2012-3-17
- 帖子
- 553
 
該用戶從未簽到 - 推廣值
- 0
- 貢獻值
- 0
- 金錢
- 45
- 威望
- 3183
- 主題
- 0
|
import numpy as np
8 i- s* h9 a* r! [/ g+ q. Aimport matplotlib.pyplot as plt
( `% s4 ^5 A5 O# @# L }" [8 j3 S. ~+ c3 {; j7 F0 w" ]' ?( y, A3 j( b
import utilities
3 Z* F+ h- o7 d9 V) n9 Q7 H% p! Q' M1 Y: o
# Load input data
+ b# y" ?* u8 O7 _/ h/ Rinput_file = 'D:\\1.Modeling material\\Py_Study\\2.code_model\\Python-Machine-Learning-Cookbook\\Python-Machine-Learning-Cookbook-master\\Chapter03\\data_multivar.txt'
8 g u G. Z9 }; A+ ^3 I/ pX, y = utilities.load_data(input_file)
- W+ K( p( F- B4 U' V$ n6 Y) R) Y
$ T0 e8 G( {. ^ e$ j- k###############################################5 S3 P* F- [; I* v
# Separate the data into classes based on 'y'
% z# s' ?3 q; O- ]+ O) Y1 k2 m& q: ]class_0 = np.array([X[i] for i in range(len(X)) if y[i]==0])
# C, W& ~! r" e1 A) ?. Fclass_1 = np.array([X[i] for i in range(len(X)) if y[i]==1])
0 B' }$ O4 X! w' b8 F8 ^# S3 ~9 a9 ?2 I1 | C& t# G; F
# Plot the input data) w8 l& F: K8 v" ` k
plt.figure()
8 p' x9 g; {- s( |1 B3 {( `9 Fplt.scatter(class_0[:,0], class_0[:,1], facecolors='black', edgecolors='black', marker='s')8 S/ R* e/ @: z& ]& ~
plt.scatter(class_1[:,0], class_1[:,1], facecolors='None', edgecolors='black', marker='s')
+ I5 K! D( x: J2 l l9 n' Cplt.title('Input data')
' j/ l7 n8 B: `3 R. Y+ F, l: T, g# {1 u
###############################################
- ^3 a) m7 T1 C0 t# Train test split and SVM training
, _3 y( \' k6 C6 H' e" P ?from sklearn import cross_validation# \0 g- z V: P. _5 K3 H1 F- U3 w" N
from sklearn.svm import SVC
7 H8 l" G. [( D8 e8 l( i* t) v7 U) N4 l
X_train, X_test, y_train, y_test = cross_validation.train_test_split(X, y, test_size=0.25, random_state=5)
. ^3 H( R8 n. U$ X. }1 {7 c2 @# S! |) R! \
#params = {'kernel': 'linear'}
$ V2 T: c5 T! |#params = {'kernel': 'poly', 'degree': 3}
9 ?' a# p; P( t7 o! I9 r# J9 i- Iparams = {'kernel': 'rbf'}
! Y6 K2 U5 ^* W: Vclassifier = SVC(**params)
" |) ^9 O `' B2 q9 m9 {classifier.fit(X_train, y_train)3 w( A) _$ b! m) y4 e
utilities.plot_classifier(classifier, X_train, y_train, 'Training dataset')) l/ M) T) E! V2 n P+ a. c- V
# d0 A; \" J" v
y_test_pred = classifier.predict(X_test)# j0 `# p8 C8 ^ u. d$ o m
utilities.plot_classifier(classifier, X_test, y_test, 'Test dataset')
6 d8 k% Z7 ~. `# W/ J
# t& `$ C2 F7 A; {1 S( b###############################################
v) C8 y% r/ e( p4 b2 b; u1 A$ C# Evaluate classifier performance
3 B8 n8 a7 G1 o7 B1 m! c' n* z& i! y6 S. s2 l6 n2 w: w7 f5 F+ v4 ^9 l% O
from sklearn.metrics import classification_report0 k- P+ _1 Y( m- s: _
% v1 Z% h3 b& c& V
target_names = ['Class-' + str(int(i)) for i in set(y)]
( o- c6 t% v' X: r' ^print "\n" + "#"*30) f) y s) g4 W7 e
print "\nClassifier performance on training dataset\n"$ ~6 i0 X6 ]2 j+ j
print classification_report(y_train, classifier.predict(X_train), target_names=target_names)0 ~2 r; g+ c W8 r: z
print "#"*30 + "\n"
% q3 {8 R3 Y. ?) F. U
- o8 A2 o5 j7 }+ ]5 zprint "#"*307 L$ }. _6 [8 u S, B4 D/ v, s
print "\nClassification report on test dataset\n"
' g$ Z1 y- z& O" c% o% C, pprint classification_report(y_test, y_test_pred, target_names=target_names)
3 v# ]# g- t& M* r, Oprint "#"*30 + "\n"
1 Q/ m' Z. M# b3 w1 W6 L5 r8 S
|
|