- 金錢
- 45
- 威望
- 3183
- 貢獻值
- 0
- 推廣值
- 0
- 性別
- 保密
- 在線時間
- 38 小時
- 最後登錄
- 2024-2-25
- 主題
- 0
- 精華
- 0
- 閱讀權限
- 70
- 註冊時間
- 2012-3-17
- 帖子
- 553
 
該用戶從未簽到 - 推廣值
- 0
- 貢獻值
- 0
- 金錢
- 45
- 威望
- 3183
- 主題
- 0
|
import numpy as np
; }. W. E: s: ?3 h! d- D7 ~; Bimport matplotlib.pyplot as plt( f6 V! z1 l; Y; v$ X) v
# e5 G5 {, I; i' Rimport utilities
1 R/ `5 s7 ~- N8 k3 U5 `; F) {% Y) p/ l, y
# Load input data+ i5 S t, {) e* V; J# \) M- G, A
input_file = 'D:\\1.Modeling material\\Py_Study\\2.code_model\\Python-Machine-Learning-Cookbook\\Python-Machine-Learning-Cookbook-master\\Chapter03\\data_multivar.txt', e0 C' U% N" u& f( E, _
X, y = utilities.load_data(input_file)8 n& h8 C3 ]# g" J0 Y9 H5 o# K1 x- D6 W
4 @# F7 [6 A4 f) M: v6 t0 _###############################################1 A- l K& `( Z9 l1 v
# Separate the data into classes based on 'y'
/ f3 _/ U. j% Y* L7 A* k1 A5 ]class_0 = np.array([X[i] for i in range(len(X)) if y[i]==0])# d" r* N: n" Q; Z3 p" m( j, ?
class_1 = np.array([X[i] for i in range(len(X)) if y[i]==1])
: f) B3 i; L. w/ ]# }8 k$ _: F: q# g9 l1 g
# Plot the input data
/ c$ M+ d! L4 S: a/ ~% C* \plt.figure()8 v& ]& t& K) _/ h& }6 A2 K
plt.scatter(class_0[:,0], class_0[:,1], facecolors='black', edgecolors='black', marker='s')
# O& e5 `. i4 k' |+ q J7 E0 r ]% bplt.scatter(class_1[:,0], class_1[:,1], facecolors='None', edgecolors='black', marker='s')' Q6 N- \+ o" P% I) s
plt.title('Input data')
+ e1 K0 a3 I' L6 q8 }$ l4 f$ e4 ~/ x- @% ?9 g+ b* ]$ t
###############################################
?8 w+ I$ Z0 U( p# Train test split and SVM training) g+ a0 {) I3 M+ {3 \1 J1 v8 D
from sklearn import cross_validation
2 _# j' |- t& F1 f) | Pfrom sklearn.svm import SVC/ v/ c2 M6 @) Q) _, D1 N
$ o4 @- u3 o5 ^4 J" k) g
X_train, X_test, y_train, y_test = cross_validation.train_test_split(X, y, test_size=0.25, random_state=5)5 w5 b# U$ z: @3 u$ f
+ }+ p) f0 @0 G' b* t
#params = {'kernel': 'linear'}7 t0 p9 N+ D& h0 Z
#params = {'kernel': 'poly', 'degree': 3}
4 [8 Y, K/ e7 _) H. M% }params = {'kernel': 'rbf'}8 X0 Q2 R N0 b. x$ o8 a) w7 M; l- G
classifier = SVC(**params)( L' ^1 h: U: A8 f# D0 I
classifier.fit(X_train, y_train)* w: V: K" a/ f9 M) A
utilities.plot_classifier(classifier, X_train, y_train, 'Training dataset')
2 C. T9 E; L+ h( A6 b* k
6 P. ~6 i8 n! B0 H! j% `5 K, Ny_test_pred = classifier.predict(X_test)
7 i5 h6 d# x3 y% c# z& L3 }utilities.plot_classifier(classifier, X_test, y_test, 'Test dataset')
+ j- [0 q: J/ Z. _2 J
! V8 Y& U, m9 g& n" S. n: w###############################################
6 L* ?6 k( d$ I# Evaluate classifier performance: W8 p( b' j, [) t6 K
5 Z" r( T3 O0 ?. |5 K; g& }+ pfrom sklearn.metrics import classification_report
4 [0 x p4 x4 L# o! F- M4 p" X- }& w+ v& q
target_names = ['Class-' + str(int(i)) for i in set(y)]$ ^4 v) `& c, ^
print "\n" + "#"*306 A* E4 d% P3 N7 `) H0 l- m
print "\nClassifier performance on training dataset\n"
; r6 W( k! w- X1 lprint classification_report(y_train, classifier.predict(X_train), target_names=target_names)
# N, }9 a4 U: ?- i+ Z0 r/ qprint "#"*30 + "\n"! k! N" W/ m: D4 @* R
- N/ p) l( E- k/ \' lprint "#"*30
0 @* `1 J' K, P3 D1 [1 Aprint "\nClassification report on test dataset\n"
- Y8 B0 J" B0 k6 \. bprint classification_report(y_test, y_test_pred, target_names=target_names)
7 a# G5 B' W" X+ Jprint "#"*30 + "\n"" o1 i; {' ^) i$ X
0 k8 T% x& ^$ i# [/ }5 w |
|