- 金錢
- 45
- 威望
- 3183
- 貢獻值
- 0
- 推廣值
- 0
- 性別
- 保密
- 在線時間
- 38 小時
- 最後登錄
- 2024-2-25
- 主題
- 0
- 精華
- 0
- 閱讀權限
- 70
- 註冊時間
- 2012-3-17
- 帖子
- 553
 
該用戶從未簽到 - 推廣值
- 0
- 貢獻值
- 0
- 金錢
- 45
- 威望
- 3183
- 主題
- 0
|
import numpy as np
' `' G. k: f5 `; N* Limport matplotlib.pyplot as plt$ M) w, n; m Q, k6 l! F( w# a, c
! l8 {( K. x9 c) p% j3 J
import utilities 8 d9 Q S! ~+ A
) v1 h y, b `3 c0 l# Load input data
: i, Q4 C- z; K% ]% v# oinput_file = 'D:\\1.Modeling material\\Py_Study\\2.code_model\\Python-Machine-Learning-Cookbook\\Python-Machine-Learning-Cookbook-master\\Chapter03\\data_multivar.txt'7 ^. l9 [4 D3 b% n# F- V
X, y = utilities.load_data(input_file)
8 O) J5 {" g( h- f
' {4 _+ z: d: [+ b, T U###############################################1 ?, R5 W; g1 M. v* I0 }+ f
# Separate the data into classes based on 'y'
1 y# u+ X& ]3 D. Jclass_0 = np.array([X[i] for i in range(len(X)) if y[i]==0])1 G, j: m Y) Z' B N
class_1 = np.array([X[i] for i in range(len(X)) if y[i]==1])9 J' j) w# r6 P, O: ~) s7 }( n
, _* S- i- f# ~0 N
# Plot the input data
: ^# u3 a0 y' ~plt.figure(). S1 V: _" R4 \" t, M
plt.scatter(class_0[:,0], class_0[:,1], facecolors='black', edgecolors='black', marker='s')# z, O2 @* i4 {% o j0 z' @
plt.scatter(class_1[:,0], class_1[:,1], facecolors='None', edgecolors='black', marker='s')' G( O/ k8 F: E4 O- w0 {( w; @; r
plt.title('Input data')7 z) M1 R3 \: l" n- ?+ _2 G# W3 O
) r0 i4 ^ c* o0 e+ t# x' V" V
###############################################- l1 ~3 s3 P$ N. M# O3 Q/ @$ h
# Train test split and SVM training& V# v* M3 N5 X8 q8 c5 ?: d
from sklearn import cross_validation, d! Q7 T, |6 K: h2 H( \) u' \+ c
from sklearn.svm import SVC9 h1 S2 o- w% g, ^) c
' O. D g5 W& `8 W- V; BX_train, X_test, y_train, y_test = cross_validation.train_test_split(X, y, test_size=0.25, random_state=5)
8 K6 g; Z$ [' d$ w
- @) p8 @2 h. ~: o% C#params = {'kernel': 'linear'}
% v) d1 _: ?% D# V#params = {'kernel': 'poly', 'degree': 3}" i% W7 u- { J+ h
params = {'kernel': 'rbf'}
' E& r6 u' }5 T$ P- rclassifier = SVC(**params)) U/ e2 g1 [$ r2 @+ M
classifier.fit(X_train, y_train)
5 l8 Z1 Q4 B- a: i" wutilities.plot_classifier(classifier, X_train, y_train, 'Training dataset')8 K& u) v0 r5 a
& g! L4 j* Z4 u: Xy_test_pred = classifier.predict(X_test)
: V) g& A8 |+ G0 |, P1 Putilities.plot_classifier(classifier, X_test, y_test, 'Test dataset')
* N" K* A, ?# N' ~4 h6 V6 B4 B- \7 ^. m! U3 M1 x
###############################################
- K# z/ g0 M2 c# ^+ e6 Q# Evaluate classifier performance0 J" s1 q0 C9 O0 f
1 b: C! A: O: U$ v$ ?0 }& ~( h
from sklearn.metrics import classification_report7 X" a. S9 R: M
' q& O; h( O) b1 ]- ?- I# l6 e0 w. @target_names = ['Class-' + str(int(i)) for i in set(y)]
% c7 P* k$ S+ }print "\n" + "#"*30
% ?6 r) n/ f7 c* H- E. |; m# \' Oprint "\nClassifier performance on training dataset\n"
6 @0 D+ x/ L' C, j1 [/ R& oprint classification_report(y_train, classifier.predict(X_train), target_names=target_names)7 d) k" a5 O+ T
print "#"*30 + "\n"
7 G, r. |$ }* f) |5 b m# B0 r$ T3 o. q7 Y9 ]
print "#"*305 Q( l4 `+ F; T+ ]# ?* l
print "\nClassification report on test dataset\n"6 u; [% W2 f5 V6 w, M2 a5 K. M1 Q
print classification_report(y_test, y_test_pred, target_names=target_names)" D: q/ p6 l% U
print "#"*30 + "\n"' M; i g3 }; s" E
7 L* J: J! \; I4 F7 p& y |
|