- 金錢
- 45
- 威望
- 3183
- 貢獻值
- 0
- 推廣值
- 0
- 性別
- 保密
- 在線時間
- 38 小時
- 最後登錄
- 2024-2-25
- 主題
- 0
- 精華
- 0
- 閱讀權限
- 70
- 註冊時間
- 2012-3-17
- 帖子
- 553
 
該用戶從未簽到 - 推廣值
- 0
- 貢獻值
- 0
- 金錢
- 45
- 威望
- 3183
- 主題
- 0
|
import numpy as np
, g2 q& F0 N8 Vimport matplotlib.pyplot as plt# F! }: T1 n; d6 |
9 x i9 ^; i9 o9 R
import utilities 3 ]2 k1 L/ V( K0 ~3 p9 ?3 L
2 y8 J! p. x9 y, s0 @# Load input data
x e: a8 M5 d6 d" ainput_file = 'D:\\1.Modeling material\\Py_Study\\2.code_model\\Python-Machine-Learning-Cookbook\\Python-Machine-Learning-Cookbook-master\\Chapter03\\data_multivar.txt'
5 ~6 Q& m. u) \- mX, y = utilities.load_data(input_file)2 y! o# b% S) A; L5 c' F
) _( ?' @# s' b H* Z& u y: y2 c
###############################################
' B. A4 a2 ?/ n7 m6 s9 Q& w# Separate the data into classes based on 'y'
+ q7 z% V$ \# l9 t) n4 l0 ^% g" N5 i3 \class_0 = np.array([X[i] for i in range(len(X)) if y[i]==0])0 R, t0 f! k, I
class_1 = np.array([X[i] for i in range(len(X)) if y[i]==1])
, l2 T$ i) M3 {4 Z4 j/ j' A. G
- b/ d7 @* P7 J$ F5 s! ~# Plot the input data
& y7 C4 H0 U# }* v, H) _plt.figure(): h% D# g) H& {) l
plt.scatter(class_0[:,0], class_0[:,1], facecolors='black', edgecolors='black', marker='s')
. i! x! T7 a, c$ \9 o3 J% Qplt.scatter(class_1[:,0], class_1[:,1], facecolors='None', edgecolors='black', marker='s')
5 v1 {( M$ f: }; o. Y; pplt.title('Input data')) ]7 r" a1 g, }
+ s; T- h o3 s- N###############################################
) \% c9 V( |# C. r: I& ]4 k2 Q# Train test split and SVM training4 P7 T$ r! e5 i. r& E9 I
from sklearn import cross_validation
4 y# P( d2 q+ P7 D! jfrom sklearn.svm import SVC
. h0 e5 S# T J/ K( O
# n5 C9 m8 J8 q# F6 uX_train, X_test, y_train, y_test = cross_validation.train_test_split(X, y, test_size=0.25, random_state=5)& Z/ t6 q: `' q' O5 s5 U
) h/ V# k9 _- h! z! O
#params = {'kernel': 'linear'}) h; z {/ z9 W) _% Z
#params = {'kernel': 'poly', 'degree': 3}* i/ h9 C0 K7 ^, I9 _" |% V3 T
params = {'kernel': 'rbf'}
. x8 i4 A, q0 |1 I% N6 ^* xclassifier = SVC(**params)0 d6 z/ m7 H& K K5 f* B
classifier.fit(X_train, y_train); G0 Y) u0 ?+ y P" f6 l G
utilities.plot_classifier(classifier, X_train, y_train, 'Training dataset'); e0 E& l# U; b* ~4 f
* d; N9 c( A/ g8 x. a1 x3 U' Ey_test_pred = classifier.predict(X_test)
! `3 a; j9 H3 @) S) w3 t0 k* {0 Nutilities.plot_classifier(classifier, X_test, y_test, 'Test dataset')
8 L$ ?: d! Y# y$ C. F$ E a/ r* _
- P! J: r& w6 }- t###############################################
1 B& p" j" X7 q# Evaluate classifier performance7 O4 e6 P; p0 e4 W& Z
, Q6 k! t6 ^0 Q1 I2 x0 B; g
from sklearn.metrics import classification_report
' ]( U4 z: o8 d2 S6 E' h% v
' S' ^9 `; U! V) \, u+ U) U$ Ytarget_names = ['Class-' + str(int(i)) for i in set(y)]6 p2 v0 O0 m3 ~7 @- I' l2 D
print "\n" + "#"*30
7 ~% s# y4 S2 A, B/ B m" B1 Cprint "\nClassifier performance on training dataset\n"0 q; x( B9 V9 `& M0 g0 u$ c
print classification_report(y_train, classifier.predict(X_train), target_names=target_names)
8 y9 V# |$ b8 d) |# ]; U( j0 Aprint "#"*30 + "\n"
2 X7 _. Z6 l! g* z! h7 y% q) @/ p5 r' I8 }; ~
print "#"*30/ s+ Q) m5 |2 w3 }
print "\nClassification report on test dataset\n"
# A1 A% T* ?6 Tprint classification_report(y_test, y_test_pred, target_names=target_names)
) W# D0 ?0 \, q1 o1 X6 M3 }print "#"*30 + "\n"- p; j' q7 L0 n# x' w
( a. d9 t- [5 F; a2 W |
|