- 金錢
- 46
- 威望
- 3183
- 貢獻值
- 0
- 推廣值
- 0
- 性別
- 保密
- 在線時間
- 38 小時
- 最後登錄
- 2024-2-25
- 主題
- 0
- 精華
- 0
- 閱讀權限
- 70
- 註冊時間
- 2012-3-17
- 帖子
- 556
 
該用戶從未簽到 - 推廣值
- 0
- 貢獻值
- 0
- 金錢
- 46
- 威望
- 3183
- 主題
- 0
|
import numpy as np" \2 E$ t% V% d. h( |" j" s
import matplotlib.pyplot as plt
7 ]" s& }0 u7 L3 i1 h, d& h* W7 \: F( F1 G+ M: I) k3 E
import utilities , c4 N1 y1 S( \9 t7 h9 w6 c5 u
2 i: e. G( o# V8 b! _
# Load input data
8 L3 X+ _0 E2 Tinput_file = 'D:\\1.Modeling material\\Py_Study\\2.code_model\\Python-Machine-Learning-Cookbook\\Python-Machine-Learning-Cookbook-master\\Chapter03\\data_multivar.txt'
3 B" J2 S, W% p3 ^" n4 G. G" j& aX, y = utilities.load_data(input_file)) }2 G' P; Z" m/ M# h
/ {9 m b% Y, ^5 E' `- M9 n5 d9 J( O###############################################
6 D5 @/ J5 G. l, U% S# Separate the data into classes based on 'y'1 |7 S7 \3 j6 N- S
class_0 = np.array([X[i] for i in range(len(X)) if y[i]==0])
6 s! f: V, N3 x7 wclass_1 = np.array([X[i] for i in range(len(X)) if y[i]==1])8 M1 z7 h! j1 l& d9 U& s- N
8 p; {$ g$ f3 |9 ^; B* I6 d# Plot the input data
: a# l# S3 Q# H; K& ]% }' Mplt.figure()
; \5 ^, D7 \5 H3 E% l9 ]plt.scatter(class_0[:,0], class_0[:,1], facecolors='black', edgecolors='black', marker='s')1 Q C7 q2 Q( G' _* l- C4 ?; L4 K
plt.scatter(class_1[:,0], class_1[:,1], facecolors='None', edgecolors='black', marker='s')' K7 v# f* {( w! m
plt.title('Input data')
( r9 Y0 Y- c& {! v4 c3 @) @2 p) S0 j: ^( l4 g* o
###############################################0 @: j, h" X7 \0 g1 T$ f; Y
# Train test split and SVM training
! X: O! ]) n( d# hfrom sklearn import cross_validation
" e7 H) Q8 M. m) G0 J5 cfrom sklearn.svm import SVC5 w1 N1 y! C" `
1 i0 C- t4 [: l2 w# |) G: dX_train, X_test, y_train, y_test = cross_validation.train_test_split(X, y, test_size=0.25, random_state=5)8 W' o1 b1 w7 M3 u- l# o4 A5 a
5 B; c- T7 X/ y$ z, W. |" E#params = {'kernel': 'linear'}
. k/ P; E, H' y- l; p0 g#params = {'kernel': 'poly', 'degree': 3}3 o8 @0 h4 P/ T* a
params = {'kernel': 'rbf'}
- ]% t X( [' Q" L% Vclassifier = SVC(**params), x7 H( Y( N0 G( j" L I" x
classifier.fit(X_train, y_train)
8 g2 I7 D5 f+ ^! ~$ r% Dutilities.plot_classifier(classifier, X_train, y_train, 'Training dataset')
% |4 ]. Y; f( } ^/ ?0 w
: m1 j+ c: x/ U( Cy_test_pred = classifier.predict(X_test)/ W) z4 w0 C* o$ ?* h$ Q
utilities.plot_classifier(classifier, X_test, y_test, 'Test dataset')# s0 A# L! h4 g/ A0 A$ v. l
! T- W' Q& f9 s
###############################################* w0 y$ f1 T' `
# Evaluate classifier performance0 Q# R& w% B; G, O+ j
7 `6 |. H- `6 ^6 x2 [& q: Wfrom sklearn.metrics import classification_report" Q' s$ o2 ]( K
/ a- z, l; r7 ], c
target_names = ['Class-' + str(int(i)) for i in set(y)]7 {/ J2 i! b0 G$ Y
print "\n" + "#"*30% c1 S( @9 W( m) G$ I k
print "\nClassifier performance on training dataset\n"
5 G4 g5 Q$ ^% u* a: r" y! _! N5 Tprint classification_report(y_train, classifier.predict(X_train), target_names=target_names)# j1 e' `' M$ m
print "#"*30 + "\n"$ V2 o+ G2 N5 `2 h
! R% Y. k3 ~2 k- D; B9 S. `print "#"*30
2 G. d' \9 T7 c) o j6 A5 sprint "\nClassification report on test dataset\n"
7 B. i- Z9 m& lprint classification_report(y_test, y_test_pred, target_names=target_names). _2 K% x4 S" N2 h
print "#"*30 + "\n"
6 w9 g+ D/ d$ t( l. l! j% T$ A8 ~; [: a/ q' c' J0 n
|
|