- 金錢
- 45
- 威望
- 3183
- 貢獻值
- 0
- 推廣值
- 0
- 性別
- 保密
- 在線時間
- 38 小時
- 最後登錄
- 2024-2-25
- 主題
- 0
- 精華
- 0
- 閱讀權限
- 70
- 註冊時間
- 2012-3-17
- 帖子
- 553
 
該用戶從未簽到 - 推廣值
- 0
- 貢獻值
- 0
- 金錢
- 45
- 威望
- 3183
- 主題
- 0
|
import numpy as np% O7 l9 V) n) d: }4 H1 ?
import matplotlib.pyplot as plt
, R" c$ |$ W- a% L; f" K" W5 o0 F0 B x& B: t+ R6 u. H5 m) }
import utilities
* z6 r! `$ a$ o" {. E8 i7 y+ t6 p
* A; l! o4 R' n# Load input data+ l1 U' l: d/ @6 q/ X4 s- f
input_file = 'D:\\1.Modeling material\\Py_Study\\2.code_model\\Python-Machine-Learning-Cookbook\\Python-Machine-Learning-Cookbook-master\\Chapter03\\data_multivar.txt'
/ r9 J4 h4 I& I, W$ pX, y = utilities.load_data(input_file)
2 Q% A8 W/ T) q1 \( g7 ~8 \9 b% R7 \! M: ]* H
###############################################' Y0 \( A% Y3 p) O
# Separate the data into classes based on 'y'
% A% k; T/ Y) {$ ^% M3 F. G2 Kclass_0 = np.array([X[i] for i in range(len(X)) if y[i]==0])0 T8 s% L+ C# I+ y
class_1 = np.array([X[i] for i in range(len(X)) if y[i]==1])# | ]9 }: u/ o* h$ H6 r
3 v8 M" O# ^# i% A5 m& }
# Plot the input data% ]8 h' T& p: m9 I; y9 A! w
plt.figure()/ k3 f* F% G0 p7 D: g
plt.scatter(class_0[:,0], class_0[:,1], facecolors='black', edgecolors='black', marker='s')
. ~2 z0 H, |+ S2 ^0 I" g6 _$ _plt.scatter(class_1[:,0], class_1[:,1], facecolors='None', edgecolors='black', marker='s')9 E7 ~6 F0 g# f3 F
plt.title('Input data')
+ c& m/ `+ |+ W* ?+ A# a
% N8 e9 M( N$ {' ^7 R5 K6 h###############################################
6 o; S3 c' o7 E; J6 z6 B6 ~6 E* X" }# Train test split and SVM training( X* I" J+ m) S6 \
from sklearn import cross_validation/ C, r6 e. d/ _. m8 h% k/ _
from sklearn.svm import SVC5 c9 r8 @; j% i `
}& ]5 H1 M; N, L5 u7 X( O% t
X_train, X_test, y_train, y_test = cross_validation.train_test_split(X, y, test_size=0.25, random_state=5)
! s+ c A3 x* L+ X/ \5 v0 A* v+ b$ ~7 S
#params = {'kernel': 'linear'}) }; m0 `* V( \' D/ ^6 r+ ^! `
#params = {'kernel': 'poly', 'degree': 3}
5 f+ `+ e( T8 i9 nparams = {'kernel': 'rbf'}
9 O' p2 x0 H4 C2 h6 d% zclassifier = SVC(**params)1 n" s( K; T1 G' g7 w$ {; X9 h# U
classifier.fit(X_train, y_train)& E8 s2 t, R. F- T* \7 [& C0 h
utilities.plot_classifier(classifier, X_train, y_train, 'Training dataset')$ [) U' ]8 u: X0 {3 F. A" I* {
2 x4 R3 S# h2 c( i0 {y_test_pred = classifier.predict(X_test)! n& g1 D/ V% Y! e8 D8 P8 Y6 r
utilities.plot_classifier(classifier, X_test, y_test, 'Test dataset')
; ^+ V5 r! t- S2 M) F3 L6 k3 n3 Q. N1 w* T8 y) |5 H
###############################################
" D8 d1 ~) z7 L2 ]% l D1 f# V# Evaluate classifier performance5 o- `, C- e& B: d8 Y
r6 T1 n$ K J' X" U) Z3 ^from sklearn.metrics import classification_report
! |) D' g# U: d5 q/ y+ N- Q& C3 j' U6 C8 O6 n7 j
target_names = ['Class-' + str(int(i)) for i in set(y)]7 [9 q( @# b g4 |
print "\n" + "#"*30
R) J% N+ m# G( Gprint "\nClassifier performance on training dataset\n"3 A0 Q- Z7 }9 Y4 B- E9 W2 i9 n
print classification_report(y_train, classifier.predict(X_train), target_names=target_names)
* X: T& G* d9 e8 V* s" [print "#"*30 + "\n"
3 a5 u ~( W' x ?
" p8 \% F: z6 U: eprint "#"*30" G9 Q, Y. D* [0 o
print "\nClassification report on test dataset\n"
7 J- p& R1 m9 ^; k& ~- sprint classification_report(y_test, y_test_pred, target_names=target_names)
; s3 X0 r8 @, ~print "#"*30 + "\n"; L7 ^8 `$ P S3 I8 |3 Z
9 v' t0 f3 p6 X% O* @7 v; T. [
|
|