- 金錢
- 45
- 威望
- 3183
- 貢獻值
- 0
- 推廣值
- 0
- 性別
- 保密
- 在線時間
- 38 小時
- 最後登錄
- 2024-2-25
- 主題
- 0
- 精華
- 0
- 閱讀權限
- 70
- 註冊時間
- 2012-3-17
- 帖子
- 553
 
該用戶從未簽到 - 推廣值
- 0
- 貢獻值
- 0
- 金錢
- 45
- 威望
- 3183
- 主題
- 0
|
import numpy as np' n+ z n) _/ V. F6 z
import matplotlib.pyplot as plt5 O5 w$ T2 f1 O7 }9 e
; c; L5 O$ }6 D4 s
import utilities
6 y. o4 \) e1 w }. {9 i; k: k- c B, Y0 R& E7 [8 y
# Load input data5 X" w/ \) Y( b( m7 C4 ]# H6 X1 x& u
input_file = 'D:\\1.Modeling material\\Py_Study\\2.code_model\\Python-Machine-Learning-Cookbook\\Python-Machine-Learning-Cookbook-master\\Chapter03\\data_multivar.txt': @9 |3 v; }( n, C) s) `% _
X, y = utilities.load_data(input_file) v" g. \5 X2 d% S( H
# S' l+ i) B& h+ S! A1 o+ ~, x
###############################################2 e5 \' q5 {* m. I2 I! R7 J Z
# Separate the data into classes based on 'y'
! G8 l) h9 R4 \& }; ~3 L+ [; R. m$ nclass_0 = np.array([X[i] for i in range(len(X)) if y[i]==0])3 N4 s; N) {$ ^0 ?+ S% W
class_1 = np.array([X[i] for i in range(len(X)) if y[i]==1])! ~; a% B! F: ~$ v6 `0 }
" U5 m2 S5 D* u2 a3 I( Q" t: T( z# Plot the input data
$ k3 G6 P7 e8 ?plt.figure()
9 _; Q7 C4 s6 ?5 O( kplt.scatter(class_0[:,0], class_0[:,1], facecolors='black', edgecolors='black', marker='s')
& C! e' X! x" [# Nplt.scatter(class_1[:,0], class_1[:,1], facecolors='None', edgecolors='black', marker='s')
/ d( A" }: I* n6 z, C$ K# Mplt.title('Input data')
$ V' P$ v- i# z5 v# b4 V& J5 l1 h
2 d, h# Q/ g w, r8 I# o###############################################
$ S6 y6 D+ E" `* [$ E# Train test split and SVM training
e) R; A I, Jfrom sklearn import cross_validation% j+ H" F% I4 ~0 o2 a4 t
from sklearn.svm import SVC z4 t- s% a* H: r
1 O' s: l' Q B: k/ I( kX_train, X_test, y_train, y_test = cross_validation.train_test_split(X, y, test_size=0.25, random_state=5)
" Q1 H' a1 a7 l. L. G5 U9 A# A1 a4 O( V
$ h+ c$ s: p2 N; i#params = {'kernel': 'linear'}: U; F! E7 f0 O1 \6 l' H+ l# `3 }. P+ ]8 d
#params = {'kernel': 'poly', 'degree': 3}
' ^ h J1 i5 n2 r* G1 p/ b0 ^params = {'kernel': 'rbf'}
' M+ U9 }. A5 [& I4 M# U6 Kclassifier = SVC(**params) L6 Z+ T, s* H8 L# c' a J
classifier.fit(X_train, y_train): a. H9 x: A2 C8 e
utilities.plot_classifier(classifier, X_train, y_train, 'Training dataset')0 x' z' @$ P# {, [ w$ x
& U" }' e8 u2 Y7 {
y_test_pred = classifier.predict(X_test)8 g0 x, l$ d7 x; P& ?% d( \
utilities.plot_classifier(classifier, X_test, y_test, 'Test dataset'), r4 j$ D: e' p! e/ J3 M n
" Y4 n, H2 D6 V% l
###############################################
3 S8 A- s- A( i# r# Evaluate classifier performance' X! T; U. l6 w) D: `
c( h | D# P) H* Wfrom sklearn.metrics import classification_report
1 L) \* s2 u! h1 B Q0 q4 Z5 Q% H6 U# A2 A9 }
target_names = ['Class-' + str(int(i)) for i in set(y)]
- N' X* _2 ]7 wprint "\n" + "#"*30% A3 _7 z) v: E2 B5 ^
print "\nClassifier performance on training dataset\n"
* P# n' x# B, f6 r' P, ~print classification_report(y_train, classifier.predict(X_train), target_names=target_names)) V- k8 A ]$ u; M$ v
print "#"*30 + "\n"+ R; |. g! H# U( U
) d. r V. |7 H- u" Gprint "#"*303 V* ?1 j6 K5 q4 {; H) s- h% m
print "\nClassification report on test dataset\n"
" k+ m' R, ~0 G2 ?4 A# Jprint classification_report(y_test, y_test_pred, target_names=target_names), J* \6 R/ [. k& v: {6 g
print "#"*30 + "\n"; ~& A/ o5 o% s, _' Z0 g1 [
/ x0 T0 U, t# m1 C& h |
|