- 金錢
- 45
- 威望
- 3183
- 貢獻值
- 0
- 推廣值
- 0
- 性別
- 保密
- 在線時間
- 38 小時
- 最後登錄
- 2024-2-25
- 主題
- 0
- 精華
- 0
- 閱讀權限
- 70
- 註冊時間
- 2012-3-17
- 帖子
- 553
 
該用戶從未簽到 - 推廣值
- 0
- 貢獻值
- 0
- 金錢
- 45
- 威望
- 3183
- 主題
- 0
|
import numpy as np. R5 y# r" R1 @6 k1 Z
import matplotlib.pyplot as plt
" ]7 ]$ }* Z% f- i
+ n% U; A2 @! V# G$ c2 q6 ~import utilities
6 q+ Z5 a6 W' R: }. z% K* @ ~0 z) ~/ h) ^
# Load input data
% h. T3 m1 e" }input_file = 'D:\\1.Modeling material\\Py_Study\\2.code_model\\Python-Machine-Learning-Cookbook\\Python-Machine-Learning-Cookbook-master\\Chapter03\\data_multivar.txt'4 D/ t) O* e, L0 s+ T P
X, y = utilities.load_data(input_file)0 e( L P( @/ }
7 Q( g2 x/ E* E) w( j4 X D###############################################4 @' Y( q6 V0 v; s/ x6 e
# Separate the data into classes based on 'y'* K9 S9 c" \5 @. u8 v4 ~
class_0 = np.array([X[i] for i in range(len(X)) if y[i]==0])
5 B2 S5 U# M$ x+ |class_1 = np.array([X[i] for i in range(len(X)) if y[i]==1])" @% S( e5 x3 n" x- ?
# j: L6 [5 r" Z* {6 ?
# Plot the input data
+ B& ^$ Z @- R" nplt.figure()7 S+ O5 L2 ~# ]: v
plt.scatter(class_0[:,0], class_0[:,1], facecolors='black', edgecolors='black', marker='s')6 h8 K2 Y$ a5 t7 r& \" J
plt.scatter(class_1[:,0], class_1[:,1], facecolors='None', edgecolors='black', marker='s')# x. H3 ~7 A7 h9 h; Y' e7 j$ ~
plt.title('Input data')0 A7 q# ?5 s. p% u, g1 H
# N5 i% X# d6 G: l4 L' a
###############################################+ D3 r3 q; N4 S8 B$ c& y
# Train test split and SVM training; n' e8 E+ F2 q4 y8 m0 d
from sklearn import cross_validation
. `5 R/ X3 K) j1 g# \* n+ Q: {from sklearn.svm import SVC9 J. G- Y! Q. |9 ~1 x( C+ G
, F6 v: \* o; |8 u0 vX_train, X_test, y_train, y_test = cross_validation.train_test_split(X, y, test_size=0.25, random_state=5)' {, Z# D( n* W, Y* J0 i, z
6 \5 H" \; l7 R1 u3 U#params = {'kernel': 'linear'}
- E6 E: ^7 _3 a% u9 F#params = {'kernel': 'poly', 'degree': 3}8 ^1 {! ^) n% l: C* W) [- b
params = {'kernel': 'rbf'}$ r6 V' {# i; Z1 G/ S" l/ G7 X% D3 V- ?
classifier = SVC(**params)8 q9 Q6 l! ?: C
classifier.fit(X_train, y_train)
* h# a/ g/ S# X zutilities.plot_classifier(classifier, X_train, y_train, 'Training dataset')
- ? x! }7 s9 u8 \# S2 S% B4 X" z3 l0 g, z+ \, k) l
y_test_pred = classifier.predict(X_test). Q$ U% [ U) a! F3 I' \5 ^ b
utilities.plot_classifier(classifier, X_test, y_test, 'Test dataset')
) `, a* v3 T% X" L0 @& f0 j1 H* [
###############################################" H+ G3 k' T( A' u
# Evaluate classifier performance
: U3 Y) v. D/ J4 m5 T) L0 V, o4 Y- [' A- q) Q0 M
from sklearn.metrics import classification_report
! ^* N. o& ^' O8 q! m* q/ K X( a; N8 y1 [# |: e- j1 i
target_names = ['Class-' + str(int(i)) for i in set(y)]
- i/ f7 l, O; P7 E+ Eprint "\n" + "#"*30' A7 p3 G% k- w0 Y& J
print "\nClassifier performance on training dataset\n"
: ?# K. m3 F4 K' O. z8 O. `+ wprint classification_report(y_train, classifier.predict(X_train), target_names=target_names)9 {5 k y% a$ ^1 l
print "#"*30 + "\n"3 R- B4 w/ y6 O& w; a2 f# F! F) `( Z
, J+ s0 S |- Y2 S% V2 nprint "#"*30) I1 r, r t1 F
print "\nClassification report on test dataset\n"
+ S# H# Q" Z% k2 P' B* M1 ]9 lprint classification_report(y_test, y_test_pred, target_names=target_names)7 r6 u; O/ F" |: L
print "#"*30 + "\n"
3 ]/ {6 I' T+ _& n% g! O) v5 L
% e2 b) {* P9 p' B' g4 J4 Y |
|