- 金錢
- 46
- 威望
- 3183
- 貢獻值
- 0
- 推廣值
- 0
- 性別
- 保密
- 在線時間
- 38 小時
- 最後登錄
- 2024-2-25
- 主題
- 0
- 精華
- 0
- 閱讀權限
- 70
- 註冊時間
- 2012-3-17
- 帖子
- 556
 
該用戶從未簽到 - 推廣值
- 0
- 貢獻值
- 0
- 金錢
- 46
- 威望
- 3183
- 主題
- 0
|
import numpy as np' w- ]# B$ O4 F% l
import matplotlib.pyplot as plt
; L5 ~2 N2 ~& D3 g& V4 w, e6 ]6 s6 p+ C6 [0 N9 @
import utilities
1 V$ \7 m7 a* b2 _5 @" F3 Q1 X, [, B
# Load input data% H9 s: c3 P) a+ T
input_file = 'D:\\1.Modeling material\\Py_Study\\2.code_model\\Python-Machine-Learning-Cookbook\\Python-Machine-Learning-Cookbook-master\\Chapter03\\data_multivar.txt': z- W J2 W8 x* U! S/ m1 [
X, y = utilities.load_data(input_file)6 G- e; N. ^% \+ a
& l/ ~3 R8 E' O* O5 u+ Q- m###############################################0 {; ^/ B/ O( d
# Separate the data into classes based on 'y'
& ^% p* @$ y8 ?8 K) Cclass_0 = np.array([X[i] for i in range(len(X)) if y[i]==0])
4 a; U& \! X9 ]3 G' b0 e$ Yclass_1 = np.array([X[i] for i in range(len(X)) if y[i]==1]). p8 l: R* k$ y# h/ Y' ^
5 O% @2 Q/ o! ?9 @! S |& \# Plot the input data
7 j) k: ?( e3 z( @! `7 i; p' Rplt.figure()
( [/ t) r9 d5 t* D6 r2 Iplt.scatter(class_0[:,0], class_0[:,1], facecolors='black', edgecolors='black', marker='s')* b& C, @# ^- x) y
plt.scatter(class_1[:,0], class_1[:,1], facecolors='None', edgecolors='black', marker='s')
7 u+ D& d4 \1 O2 \' G; wplt.title('Input data')+ y: I5 x. o' ]) s! z( r m
0 H. r9 a, i3 Z/ r+ G5 f+ H+ R% P3 R
###############################################; x" C1 F% A+ {; N& @
# Train test split and SVM training2 b7 a1 ]( E% M) o% X' G
from sklearn import cross_validation
" X% x$ z8 V' V+ m: afrom sklearn.svm import SVC2 h7 ^: Q4 p5 ?+ L2 ^; Q) s/ H6 P
2 A, B/ r: s: S& F2 O8 c" IX_train, X_test, y_train, y_test = cross_validation.train_test_split(X, y, test_size=0.25, random_state=5)6 a- r# C0 f7 E9 ~5 L$ x# C- ^
1 Q: \) i% P( L, ^ L% P
#params = {'kernel': 'linear'}& {0 m, n' \+ C P- T# T6 B5 \
#params = {'kernel': 'poly', 'degree': 3}
+ E5 k4 a) [/ Rparams = {'kernel': 'rbf'}4 K. P E3 U* x) d' R5 j/ k a
classifier = SVC(**params)
3 ]4 O" b5 O9 c, |; D9 |classifier.fit(X_train, y_train)
/ V8 a4 l4 h' h% [utilities.plot_classifier(classifier, X_train, y_train, 'Training dataset')/ q, ~8 d2 w5 p8 e' \3 O
! Q- ?2 a. X7 {& Ly_test_pred = classifier.predict(X_test)
! d; ` S. O2 r" @utilities.plot_classifier(classifier, X_test, y_test, 'Test dataset')
8 @# n# s" A/ L( ?( q+ f$ u
9 ^ K/ t# S) `: F###############################################
5 ^1 h4 u) l8 S3 n ?# Evaluate classifier performance& W/ y" q( I/ b3 W% E9 A
0 V4 R( y- Y& l' g, mfrom sklearn.metrics import classification_report* C& }# e* a- w. O1 H& J, ]+ w
1 E: G( G8 B7 l! O1 q% u3 t- Ttarget_names = ['Class-' + str(int(i)) for i in set(y)]5 g. m, Y4 K* r4 Q- p# x0 X, _. k) X
print "\n" + "#"*30
' Z! B w/ d. M- M/ w/ ^* ?print "\nClassifier performance on training dataset\n"
: \, T- M2 D2 ~/ n) l. ^8 Gprint classification_report(y_train, classifier.predict(X_train), target_names=target_names)+ U# n" @6 _; y; g
print "#"*30 + "\n"
8 I: Z# K, _& Z3 I& q7 O9 l
* L0 a; c- d3 D" C r! qprint "#"*30
- }- b/ n, q) O: [print "\nClassification report on test dataset\n" O$ f& J( H2 w/ W; C% ~
print classification_report(y_test, y_test_pred, target_names=target_names)1 f# P. Z) y4 H/ [( K
print "#"*30 + "\n"1 t I2 H4 ^5 {' F; a
$ J8 G. ?/ |- O$ U. x2 v |
|