- 金錢
- 45
- 威望
- 3183
- 貢獻值
- 0
- 推廣值
- 0
- 性別
- 保密
- 在線時間
- 38 小時
- 最後登錄
- 2024-2-25
- 主題
- 0
- 精華
- 0
- 閱讀權限
- 70
- 註冊時間
- 2012-3-17
- 帖子
- 553
 
該用戶從未簽到 - 推廣值
- 0
- 貢獻值
- 0
- 金錢
- 45
- 威望
- 3183
- 主題
- 0
|
import numpy as np: S7 C9 @! l1 y3 ^% I" x7 m: W
import matplotlib.pyplot as plt, Z' ^6 u: f" D
: E3 t: I* T6 @- f6 ~$ F, X
import utilities 1 V+ ]( h) w7 _0 H q! K- K: N
0 g: f! Z3 M; _2 O& g6 c. ^% I/ @) D
# Load input data9 b; O6 K) u) S+ N
input_file = 'D:\\1.Modeling material\\Py_Study\\2.code_model\\Python-Machine-Learning-Cookbook\\Python-Machine-Learning-Cookbook-master\\Chapter03\\data_multivar.txt'
3 n* g! W" Q5 y5 T/ u6 kX, y = utilities.load_data(input_file)2 H6 X% y+ v+ {, i4 y
8 ~; V! l4 ~: a( Z; Q) j###############################################
+ a3 m" ]- X, V# Separate the data into classes based on 'y'2 f1 t' z7 }+ y1 l
class_0 = np.array([X[i] for i in range(len(X)) if y[i]==0])
/ ?6 G0 W/ S, m( [6 Iclass_1 = np.array([X[i] for i in range(len(X)) if y[i]==1])
1 @; o1 d0 N0 D0 e8 x6 p! ?$ H2 N) R" g' [2 `
# Plot the input data
/ j( A& c/ X6 ?) ]8 Xplt.figure()
+ U) c$ v1 s/ E, _: ?& r* Tplt.scatter(class_0[:,0], class_0[:,1], facecolors='black', edgecolors='black', marker='s')
" j6 m, p0 r: e7 J' m- ^4 [plt.scatter(class_1[:,0], class_1[:,1], facecolors='None', edgecolors='black', marker='s')3 ?( K2 p O* K; a5 z! |
plt.title('Input data')) j+ V" B- Y! O3 _2 h% B) i/ [; X
: p) b; A' D3 l
###############################################
2 d$ _/ L2 d: p# Train test split and SVM training% A( R: R+ [. i( d$ {# f! [
from sklearn import cross_validation
3 I3 i2 e" E7 ~5 }6 ]3 n, c$ Mfrom sklearn.svm import SVC
6 B4 b4 x. h# n: F3 Q" n& T" n" P6 T4 @" P( x9 g/ S4 L/ x
X_train, X_test, y_train, y_test = cross_validation.train_test_split(X, y, test_size=0.25, random_state=5)
# U, R7 s$ i4 E4 i) s$ A
_9 t. s1 L7 a- C) I#params = {'kernel': 'linear'}9 ^! f n0 T( D6 G6 R) H: J* ~% T
#params = {'kernel': 'poly', 'degree': 3}
6 n" O7 K/ K& M1 T& V; r( bparams = {'kernel': 'rbf'}1 k* n: @/ _5 g$ \! Z
classifier = SVC(**params)
, m1 ~2 a$ T5 C3 Nclassifier.fit(X_train, y_train)
5 M: ^$ _- j2 m0 Q9 v ^& C" uutilities.plot_classifier(classifier, X_train, y_train, 'Training dataset')
) f) G5 f! b8 Y
! O% ` h3 t8 D4 Y; i3 c% Fy_test_pred = classifier.predict(X_test)1 f5 h7 S% s4 a# c$ l
utilities.plot_classifier(classifier, X_test, y_test, 'Test dataset')7 _' n# {% ]5 P& {- ]- f- g- a
1 {5 d1 c `" f* h) k) `" }###############################################, X; @ L& S4 c |( A4 V1 f9 g
# Evaluate classifier performance$ c) A( U7 z; K: i! w
@0 m( j @0 L8 P) d! x
from sklearn.metrics import classification_report
, ^7 @4 y9 K" ~) _! \( t1 W5 S; P
# r9 `7 p* A, m6 U5 otarget_names = ['Class-' + str(int(i)) for i in set(y)]; ]' G) L3 ^& L8 n/ m# f
print "\n" + "#"*30
( H2 m) Q+ B" ]! G0 N% v! R- ^print "\nClassifier performance on training dataset\n" S4 y/ O1 s( j. |6 u
print classification_report(y_train, classifier.predict(X_train), target_names=target_names)
7 W0 i8 x/ P e, ]) j6 O* g2 eprint "#"*30 + "\n"# O5 N" u4 {0 D2 j
( m5 } n! N( t/ `& S4 b# aprint "#"*30: v8 s J5 S2 c7 ]/ N d4 `
print "\nClassification report on test dataset\n"
& U3 k; m% r3 b5 Z; Z! A, Eprint classification_report(y_test, y_test_pred, target_names=target_names)
! U# e# N: U1 B3 _print "#"*30 + "\n"
0 d6 L* k( T8 b3 W6 h. v
$ Y7 q3 @9 x' t& |; z0 O5 [ |
|