- 金錢
- 45
- 威望
- 3183
- 貢獻值
- 0
- 推廣值
- 0
- 性別
- 保密
- 在線時間
- 38 小時
- 最後登錄
- 2024-2-25
- 主題
- 0
- 精華
- 0
- 閱讀權限
- 70
- 註冊時間
- 2012-3-17
- 帖子
- 553
 
該用戶從未簽到 - 推廣值
- 0
- 貢獻值
- 0
- 金錢
- 45
- 威望
- 3183
- 主題
- 0
|
import numpy as np
( W6 A Z2 A( U2 Yimport matplotlib.pyplot as plt+ ~4 N; ?5 q" q' W0 u
- M9 m6 ?1 e( I& R h$ p. {* Wimport utilities & w' \8 [( Q" `! u) ~$ h
t. \5 M P6 n" ?$ \
# Load input data2 w8 C0 D# O" R& F
input_file = 'D:\\1.Modeling material\\Py_Study\\2.code_model\\Python-Machine-Learning-Cookbook\\Python-Machine-Learning-Cookbook-master\\Chapter03\\data_multivar.txt'
4 u# L* n+ l6 \ f; T% IX, y = utilities.load_data(input_file)
: L: ?3 n$ h6 Q
/ f6 ~0 ], V: ?% }###############################################$ E; l" @- c5 [9 H- u" I b* H
# Separate the data into classes based on 'y'
7 B9 h' P1 Z. q. l& `3 ~class_0 = np.array([X[i] for i in range(len(X)) if y[i]==0])+ V' U$ m0 `) }$ u7 z4 D2 F' z
class_1 = np.array([X[i] for i in range(len(X)) if y[i]==1])* r5 `* t- s2 R R3 b3 P! i! D
2 ^ Y+ \' l/ }1 E B# Plot the input data
7 @# C8 K: L4 w( g/ r4 @+ Yplt.figure(); p! Y' h* j" R
plt.scatter(class_0[:,0], class_0[:,1], facecolors='black', edgecolors='black', marker='s'); ^$ l7 J% o* v, I: O8 P7 L# C
plt.scatter(class_1[:,0], class_1[:,1], facecolors='None', edgecolors='black', marker='s')- n% a1 z1 S9 U0 T- \4 B
plt.title('Input data')
% l8 _9 t2 o9 g6 m. `* `7 b) I
) B. z6 P, U8 {3 C' I###############################################. n. g, d: f( o
# Train test split and SVM training: `( w* m( Z: x( d/ v, ]! z1 s
from sklearn import cross_validation
3 j- J) S/ ]6 D9 _1 j* ifrom sklearn.svm import SVC9 d* H( h7 R4 A; |, N* x$ i
) N, g$ \% X$ M5 Q, oX_train, X_test, y_train, y_test = cross_validation.train_test_split(X, y, test_size=0.25, random_state=5)
P' L0 I/ z3 _& z6 i$ ~. h/ ]5 x8 @
9 `( R1 k U \# s1 o#params = {'kernel': 'linear'}+ Z' R; I& ^2 L
#params = {'kernel': 'poly', 'degree': 3}
; G% R4 `! a. M9 ~ b/ J" T Jparams = {'kernel': 'rbf'}8 [) u2 A# R% D( f) W( N
classifier = SVC(**params)' I0 m& J- {5 G' R& u2 C8 g
classifier.fit(X_train, y_train)
# g2 m0 V, V+ h+ f8 ?8 S7 _* O% v7 ]" vutilities.plot_classifier(classifier, X_train, y_train, 'Training dataset')
: j \ k/ O2 a9 i5 n0 R( V- V- e' f2 k g$ y3 v
y_test_pred = classifier.predict(X_test)6 _7 W$ P4 Q" N/ t5 @2 J9 y
utilities.plot_classifier(classifier, X_test, y_test, 'Test dataset')$ x. J2 X9 b+ u) ]& ~
3 O5 R* j' U* B( E
###############################################0 E& o0 O) D# C# @+ V0 {+ U% o* H
# Evaluate classifier performance/ W6 o* K9 P9 k* A3 ], K
j/ n4 u6 f% Z7 ^4 ^& J' q7 @from sklearn.metrics import classification_report& ~7 G. k# k* f
4 J5 @- E5 b1 ~% J1 V" f
target_names = ['Class-' + str(int(i)) for i in set(y)]
0 p) P2 e4 d6 @1 ^: I9 h+ {$ h; X$ k* [print "\n" + "#"*30
$ `% u) e$ Y' o" m5 Wprint "\nClassifier performance on training dataset\n"
6 `) {: c: ]7 A% c% L" yprint classification_report(y_train, classifier.predict(X_train), target_names=target_names)
2 L _& E p) D$ xprint "#"*30 + "\n"
# u8 b9 {6 ^3 V2 d/ t) E* p
8 e6 [) J; g' l6 S& c4 a) Kprint "#"*30
: v' ^( a! E2 K3 O% N: h+ l' _ [. e2 ?print "\nClassification report on test dataset\n"
+ P. B7 h! W$ }print classification_report(y_test, y_test_pred, target_names=target_names)
% L7 b; ]9 u6 |& W* M" s8 r: E t( Rprint "#"*30 + "\n"
" A0 w* ]3 ~9 a% l/ {5 w! e; G o! i: B3 h' a+ S' }
|
|