- 金錢
- 45
- 威望
- 3183
- 貢獻值
- 0
- 推廣值
- 0
- 性別
- 保密
- 在線時間
- 38 小時
- 最後登錄
- 2024-2-25
- 主題
- 0
- 精華
- 0
- 閱讀權限
- 70
- 註冊時間
- 2012-3-17
- 帖子
- 553
 
該用戶從未簽到 - 推廣值
- 0
- 貢獻值
- 0
- 金錢
- 45
- 威望
- 3183
- 主題
- 0
|
import numpy as np
/ v* g( q- _' D: G. x' S6 x. Pimport matplotlib.pyplot as plt4 ^- }* V. d }) I. y& l; j* s
. H( V7 ~ F& q' simport utilities
}% ], x, A% \ V- d9 K/ S+ S6 [- `& O! c. o
# Load input data2 z5 a0 p. s2 z1 q2 N- L5 e; i
input_file = 'D:\\1.Modeling material\\Py_Study\\2.code_model\\Python-Machine-Learning-Cookbook\\Python-Machine-Learning-Cookbook-master\\Chapter03\\data_multivar.txt'
1 i8 l0 r+ I2 @" gX, y = utilities.load_data(input_file)
7 g) S4 I9 z" @$ ?9 k
/ H. q+ {1 o6 ~###############################################
( H5 I a" U l0 S4 z" Q4 [, g# Separate the data into classes based on 'y'
% v9 f5 c. m. v# M6 _4 y2 Wclass_0 = np.array([X[i] for i in range(len(X)) if y[i]==0])
, l: M$ ~" c, d! J* e, r8 L/ oclass_1 = np.array([X[i] for i in range(len(X)) if y[i]==1])9 m9 [, |7 g2 d
1 I( f) Z0 A+ G _
# Plot the input data
|, }' x8 r; u8 z- K Hplt.figure()
3 Y' o: c9 J- B( Hplt.scatter(class_0[:,0], class_0[:,1], facecolors='black', edgecolors='black', marker='s')
z/ e5 J) l: b2 Eplt.scatter(class_1[:,0], class_1[:,1], facecolors='None', edgecolors='black', marker='s')- V; `. s4 @0 T$ Y; [2 {
plt.title('Input data'); m2 |* W; \1 m) P( Q# j% `
; b% g0 m' C2 m. ?- x
###############################################- \% `5 j# U: c. z/ J
# Train test split and SVM training( A9 Y+ k8 {9 y! ?3 b' @
from sklearn import cross_validation: w8 z8 ^6 ^% r/ o( n7 D5 Q
from sklearn.svm import SVC6 i8 g: O% B, \
; i2 _2 i. ~0 P0 r5 G! U$ W- i/ J5 @# KX_train, X_test, y_train, y_test = cross_validation.train_test_split(X, y, test_size=0.25, random_state=5)& v6 H1 @, t! \& U* u
' ~( I& X) _9 H' e
#params = {'kernel': 'linear'}1 m/ ]' Z+ z" y& w& a
#params = {'kernel': 'poly', 'degree': 3}4 X7 D, C4 U" T' o5 J( e+ e
params = {'kernel': 'rbf'}
1 Q( g5 Q0 m5 Q" s( u* rclassifier = SVC(**params)
4 B; n+ z8 D$ R1 iclassifier.fit(X_train, y_train) H% E6 ~. V9 k4 }4 ]+ P3 A
utilities.plot_classifier(classifier, X_train, y_train, 'Training dataset') P [; Y K, D, [
) u) j# O" I, qy_test_pred = classifier.predict(X_test)3 E9 J6 Z; u2 u" f" O5 V
utilities.plot_classifier(classifier, X_test, y_test, 'Test dataset')
- {+ W3 R- Q* \; f
# e) W# ~6 M# S$ u###############################################2 Z/ l$ {* f- E3 I0 j8 P/ V* i
# Evaluate classifier performance
- C6 ^/ ]: S+ g. c" ?/ o3 D, {
0 @" H" _/ f5 d2 j! K0 x% Bfrom sklearn.metrics import classification_report
9 n( \) q/ T3 C" G2 L" j4 [
k* Q0 j- V3 `) _: T, c. qtarget_names = ['Class-' + str(int(i)) for i in set(y)]
8 e+ _& Q) Q5 C& \print "\n" + "#"*30 T8 V+ V; d* y+ B! K+ q4 Z
print "\nClassifier performance on training dataset\n"
# E I4 t& U" n: ]print classification_report(y_train, classifier.predict(X_train), target_names=target_names)* k) w8 R) q* w; j: g
print "#"*30 + "\n", P3 A+ Z8 r& g
: }4 V, q: R) ~% V' F( Xprint "#"*30
- \0 d: \3 P0 C( K$ h4 Qprint "\nClassification report on test dataset\n"
( K2 ^. ]- F0 D0 y* g9 }, |print classification_report(y_test, y_test_pred, target_names=target_names)3 p' H& J) S! r* e
print "#"*30 + "\n"
& M, ]2 S4 q6 h1 W
: I3 E: Y8 B2 n" b* V |
|