- 金錢
- 46
- 威望
- 3183
- 貢獻值
- 0
- 推廣值
- 0
- 性別
- 保密
- 在線時間
- 38 小時
- 最後登錄
- 2024-2-25
- 主題
- 0
- 精華
- 0
- 閱讀權限
- 70
- 註冊時間
- 2012-3-17
- 帖子
- 556
 
該用戶從未簽到 - 推廣值
- 0
- 貢獻值
- 0
- 金錢
- 46
- 威望
- 3183
- 主題
- 0
|
import numpy as np0 |! N" u* W4 R& O" ?
import matplotlib.pyplot as plt
6 \8 U/ ^, ^4 H8 h% i$ A1 x. E/ J' N% [- g" p. h- s
import utilities
]/ V6 D' q: f% V' ]9 [! J: J8 L$ d! \ }9 T, a8 a
# Load input data! r# e) I, ?0 i( q7 u
input_file = 'D:\\1.Modeling material\\Py_Study\\2.code_model\\Python-Machine-Learning-Cookbook\\Python-Machine-Learning-Cookbook-master\\Chapter03\\data_multivar.txt'
$ k3 i1 \0 M8 N7 w4 G. lX, y = utilities.load_data(input_file)
( g. m/ g. _/ e2 [' X& |
% t! z! e/ _+ Q; _ Z( K###############################################
" \( ]$ q, l( n' p6 [) K# Separate the data into classes based on 'y'
/ S' `1 e2 N* B9 Cclass_0 = np.array([X[i] for i in range(len(X)) if y[i]==0])
* h# [9 F9 `7 T) x. H$ n) s( jclass_1 = np.array([X[i] for i in range(len(X)) if y[i]==1])0 p! f' I- b' P- b# n- P+ T& U
. @9 H x2 c2 t2 ?# Plot the input data4 Q7 s4 Y; |7 V7 `- N
plt.figure()) I' g! W- q! x+ Z: t; [
plt.scatter(class_0[:,0], class_0[:,1], facecolors='black', edgecolors='black', marker='s')1 ]+ z# E# A r1 h( Z+ q5 r
plt.scatter(class_1[:,0], class_1[:,1], facecolors='None', edgecolors='black', marker='s')
$ k7 p7 y4 |5 A& B+ o" A$ |) Splt.title('Input data')! E, g0 ^. a: n- {, T
. h, W1 G+ Q7 x; F1 g. B###############################################8 z/ g8 O3 q2 N5 @( G' k) ~' o: e
# Train test split and SVM training
. N5 Q2 @" ^. Z: m" \from sklearn import cross_validation
+ C# {8 n2 f( ]2 wfrom sklearn.svm import SVC
N% D4 C L, a: D: I6 M1 G+ Q. ^4 S8 ?! n" z) ~+ z
X_train, X_test, y_train, y_test = cross_validation.train_test_split(X, y, test_size=0.25, random_state=5)% U& r- n9 o9 I. K* ?9 a
" f% [: u2 \ V2 k#params = {'kernel': 'linear'}. K0 T) x/ f% R4 I% g
#params = {'kernel': 'poly', 'degree': 3}
9 D2 A: y+ G& m$ u0 o ]0 ^/ hparams = {'kernel': 'rbf'}
+ ^2 u, b+ L* ]) o, qclassifier = SVC(**params)! F J- \2 O7 \) g( W8 u; f
classifier.fit(X_train, y_train)& m2 ^1 n c2 C! t; w9 x3 {
utilities.plot_classifier(classifier, X_train, y_train, 'Training dataset')( T/ _4 M3 F! m$ s- c8 S
( f4 h7 ?$ t( q; S8 N0 N' k, ^y_test_pred = classifier.predict(X_test)
: J3 f2 q5 ^: l, k8 ?utilities.plot_classifier(classifier, X_test, y_test, 'Test dataset')/ Y$ d0 A! s2 z+ N$ Y \
& d L& }4 ~5 _5 }5 z q###############################################- O- \9 [" ?+ U: p. B/ |
# Evaluate classifier performance* g6 T' w( I4 e* \: G
, X4 o5 c+ c9 C3 D! w; o( cfrom sklearn.metrics import classification_report
" d. R! q6 L% W) C
) R$ @) a- o6 B! f/ N* Ztarget_names = ['Class-' + str(int(i)) for i in set(y)]" a; J5 v3 g2 e/ G$ N5 ]; z9 J x
print "\n" + "#"*308 O e6 K0 W% U, K" I
print "\nClassifier performance on training dataset\n"
z' `" T1 ~" z0 O6 rprint classification_report(y_train, classifier.predict(X_train), target_names=target_names)3 ^0 t- j! v) C; ?% G
print "#"*30 + "\n". \. b6 R; s) E9 c T) P4 B- t
$ U; ~8 ?8 {5 w1 p! o: j
print "#"*30
7 g0 U8 F7 Q3 Z; d& oprint "\nClassification report on test dataset\n"
4 V7 O3 h- C: H% A2 P& f c! ?print classification_report(y_test, y_test_pred, target_names=target_names)
$ \' P( ~1 H5 L4 Q2 Cprint "#"*30 + "\n"% ]% ]1 E; V! e/ K- z G
6 F7 j( {7 {9 u( H7 V& ?$ `( U2 U |
|