- 金錢
- 45
- 威望
- 3183
- 貢獻值
- 0
- 推廣值
- 0
- 性別
- 保密
- 在線時間
- 38 小時
- 最後登錄
- 2024-2-25
- 主題
- 0
- 精華
- 0
- 閱讀權限
- 70
- 註冊時間
- 2012-3-17
- 帖子
- 553
 
該用戶從未簽到 - 推廣值
- 0
- 貢獻值
- 0
- 金錢
- 45
- 威望
- 3183
- 主題
- 0
|
import numpy as np
9 o& R* V9 E& D v- H% Wimport matplotlib.pyplot as plt5 |( R' [' n; z5 {- w# W2 O- u
8 W. h( P8 X3 O
import utilities
7 ^" a* ~5 l c# |- K Z' R
" v7 N+ b' n$ w8 m5 g2 ]' t' R# Load input data
0 g/ R$ ]0 u, G: h+ y% H* `8 dinput_file = 'D:\\1.Modeling material\\Py_Study\\2.code_model\\Python-Machine-Learning-Cookbook\\Python-Machine-Learning-Cookbook-master\\Chapter03\\data_multivar.txt'( |' _3 U7 f1 y9 f. q
X, y = utilities.load_data(input_file)
A- s4 A2 o0 x D- X3 ?: m$ b, K! M" _
###############################################
; r& q0 j( `2 r5 z) O# Separate the data into classes based on 'y'& Z4 W6 h0 [0 S8 ?5 U
class_0 = np.array([X[i] for i in range(len(X)) if y[i]==0])! g; b% B9 g( }% O3 L7 w
class_1 = np.array([X[i] for i in range(len(X)) if y[i]==1])
3 T5 I1 v3 Y" [' L A/ U' a) L; M% ~/ o8 x# F
# Plot the input data
' [" r- G# P3 [plt.figure()) m; Y' U$ G' h$ O7 c7 j
plt.scatter(class_0[:,0], class_0[:,1], facecolors='black', edgecolors='black', marker='s')
4 w: Z: @" E) y+ A7 l3 Q: oplt.scatter(class_1[:,0], class_1[:,1], facecolors='None', edgecolors='black', marker='s')1 l% v' e* Q% G
plt.title('Input data')) R" w% R5 A( u) b! Q
# P6 i0 u& x; {8 p; Z###############################################' r: U7 \! K% J' |3 g
# Train test split and SVM training$ A* A9 u% u2 e0 r4 k5 i, Z3 R0 Q
from sklearn import cross_validation
* ], Y, a: l3 Sfrom sklearn.svm import SVC# O, x: \" Q0 z! F( w8 @. o
0 Y* n- q- z- j3 O1 mX_train, X_test, y_train, y_test = cross_validation.train_test_split(X, y, test_size=0.25, random_state=5)( r+ I4 ]+ w9 C: P9 y' L; P
: X( Q% S6 X9 Y+ ]
#params = {'kernel': 'linear'}
0 |4 m6 a# X# S5 F7 S" j) U#params = {'kernel': 'poly', 'degree': 3}- D, [8 |3 ~' t; l
params = {'kernel': 'rbf'}7 D) _# o% ~1 n. J4 l9 @+ l
classifier = SVC(**params)
+ U5 g, l$ A3 Z9 Q7 V% v7 Fclassifier.fit(X_train, y_train)
) v( O" Z: g9 A/ H/ @utilities.plot_classifier(classifier, X_train, y_train, 'Training dataset'); F8 ?4 W5 {9 v- c0 b2 n
" g( Q C G3 g1 N3 Cy_test_pred = classifier.predict(X_test)/ ^, H( O' v1 \4 N
utilities.plot_classifier(classifier, X_test, y_test, 'Test dataset')2 o4 j' G" f2 a/ x3 A
, l! Z! l% j, r' Y9 }8 m, a% J
###############################################. [! F* A! d7 W
# Evaluate classifier performance
. N) ^& C: G/ O# j' l
1 k+ i- `6 L' |0 z Ofrom sklearn.metrics import classification_report( P% j* {0 [: q. C8 t
' j2 q! Z/ n1 E/ F. ctarget_names = ['Class-' + str(int(i)) for i in set(y)]4 i9 [2 N S! b" g6 m, {2 L! k
print "\n" + "#"*30/ G) x( x2 j' n' i$ C& @
print "\nClassifier performance on training dataset\n"* t$ X- O* f6 e0 a
print classification_report(y_train, classifier.predict(X_train), target_names=target_names)
9 S- O! J- D4 m2 A! L7 Xprint "#"*30 + "\n"/ K( ~2 K8 [+ _( O5 ~; D
0 E( u0 @- ~% k) i- V; q. n2 Zprint "#"*309 `5 X# F8 u6 }3 {/ k
print "\nClassification report on test dataset\n". X$ [! a( q& X& ?, y9 U
print classification_report(y_test, y_test_pred, target_names=target_names)8 z' n* b% `; a. ^
print "#"*30 + "\n". ]' d; V+ c0 L2 Z' f5 e" Y
+ Q% U ^4 ?2 ?- A+ P+ T2 j" X |
|