- 金錢
- 45
- 威望
- 3183
- 貢獻值
- 0
- 推廣值
- 0
- 性別
- 保密
- 在線時間
- 38 小時
- 最後登錄
- 2024-2-25
- 主題
- 0
- 精華
- 0
- 閱讀權限
- 70
- 註冊時間
- 2012-3-17
- 帖子
- 553
 
該用戶從未簽到 - 推廣值
- 0
- 貢獻值
- 0
- 金錢
- 45
- 威望
- 3183
- 主題
- 0
|
import numpy as np
1 K$ @$ S6 x# ^9 f+ ?, q. Wimport matplotlib.pyplot as plt
- x8 \3 _4 k6 c- w) f) l3 O
% S9 p& S' ~+ Y u# aimport utilities
7 a$ ^( {8 D% d$ a
& G" y# g4 G" s9 G; I) V `, q# Load input data
/ ~% a, {6 O! vinput_file = 'D:\\1.Modeling material\\Py_Study\\2.code_model\\Python-Machine-Learning-Cookbook\\Python-Machine-Learning-Cookbook-master\\Chapter03\\data_multivar.txt': Q6 {2 k1 m3 g/ ~
X, y = utilities.load_data(input_file)6 t: ?' t. L- W: G3 G
9 {2 P3 y0 Y& ?7 V7 R# v" K$ f" u+ S" I
###############################################
: n3 `) J) d2 I# s; }+ t- d$ m- I" \# Separate the data into classes based on 'y'% ^" x, w; E2 U; t' {
class_0 = np.array([X[i] for i in range(len(X)) if y[i]==0]) f6 C" ~# s0 I
class_1 = np.array([X[i] for i in range(len(X)) if y[i]==1])
& \) o, Y; c& C5 R3 a. `6 c
; _+ R/ ]( V9 `; c6 t* c# Plot the input data3 |* w" G# l# w& h% X% i+ E, `
plt.figure()
6 f7 j% d% h+ ]4 J% F3 I; Wplt.scatter(class_0[:,0], class_0[:,1], facecolors='black', edgecolors='black', marker='s')
$ q% I* U. L4 r$ b# C% ]plt.scatter(class_1[:,0], class_1[:,1], facecolors='None', edgecolors='black', marker='s')
' l5 r8 L6 F# I3 A9 f+ Y# e# Dplt.title('Input data')
) w) @( h! E6 b2 `, k) y# z
5 N% ~; S% k. w% [' {###############################################
1 U m' Y3 ^ H2 `2 Z# N% k; P# Train test split and SVM training7 W1 \: X" G! T
from sklearn import cross_validation/ q! u% N6 v, A
from sklearn.svm import SVC- a1 S# J- X% Q: Y4 }8 ?+ D' Q
% G# g1 J$ T8 t5 ?! w; g: hX_train, X_test, y_train, y_test = cross_validation.train_test_split(X, y, test_size=0.25, random_state=5)0 m3 ?- U( g( [/ T4 q+ u8 x
+ w3 ~+ T( X- N: l' _8 Q: `#params = {'kernel': 'linear'}, N! e/ M' K' Q1 z% b
#params = {'kernel': 'poly', 'degree': 3}& ?4 i3 T+ G7 i9 l4 W8 M/ X& `
params = {'kernel': 'rbf'}
7 t8 F. B2 w. E0 Jclassifier = SVC(**params)
; u1 D4 J7 W% M) i, aclassifier.fit(X_train, y_train)0 T/ H8 B/ M9 B3 s
utilities.plot_classifier(classifier, X_train, y_train, 'Training dataset')
! w, F5 H: F0 N# r6 |
' v7 Y' e9 R: H) @& ry_test_pred = classifier.predict(X_test)# ~, H P/ a- L/ o
utilities.plot_classifier(classifier, X_test, y_test, 'Test dataset')
& [* ~: K- u) l7 A1 P- U) s# h( ^$ @4 J
###############################################3 Q, l9 u0 a) w# i+ j; ^
# Evaluate classifier performance
9 n, Y* Q* Z- ?& N: c) Y4 D% `" k
1 Y1 q4 J0 G% W5 m3 ~5 Z2 lfrom sklearn.metrics import classification_report) j0 ~! v* a& j5 n' a( k5 G8 e
+ E4 {, j3 y+ h- _target_names = ['Class-' + str(int(i)) for i in set(y)]
) Q' L) \$ F' b4 W6 I# qprint "\n" + "#"*30( |* e* a% H l0 A7 A7 D
print "\nClassifier performance on training dataset\n"# R0 W" f' x$ o! C: e
print classification_report(y_train, classifier.predict(X_train), target_names=target_names)
/ H' f4 T% u0 h! Vprint "#"*30 + "\n"9 u" z3 L. F; j# f0 \9 X% Q8 L
0 {# L7 Z4 \; P3 Yprint "#"*30
, |( j U0 o" M+ Q ?print "\nClassification report on test dataset\n"
3 E, Z9 o5 t+ }print classification_report(y_test, y_test_pred, target_names=target_names)
% N9 n7 {7 L5 k: L- gprint "#"*30 + "\n"" r2 [0 `' g$ j
3 @" Q0 K- j- T- S6 _/ Z2 N
|
|