- 金錢
- 45
- 威望
- 3183
- 貢獻值
- 0
- 推廣值
- 0
- 性別
- 保密
- 在線時間
- 38 小時
- 最後登錄
- 2024-2-25
- 主題
- 0
- 精華
- 0
- 閱讀權限
- 70
- 註冊時間
- 2012-3-17
- 帖子
- 553
 
該用戶從未簽到 - 推廣值
- 0
- 貢獻值
- 0
- 金錢
- 45
- 威望
- 3183
- 主題
- 0
|
import numpy as np) ~; I0 c) q7 y7 [" n
import matplotlib.pyplot as plt2 P6 N) ]+ X( c/ a
; y! M( F( E6 a9 V0 z" v6 J* ^- Uimport utilities
; m* N0 r& F- R- y/ i
- j; d+ q3 I) [4 M# Load input data
7 c% @* T$ P6 Linput_file = 'D:\\1.Modeling material\\Py_Study\\2.code_model\\Python-Machine-Learning-Cookbook\\Python-Machine-Learning-Cookbook-master\\Chapter03\\data_multivar.txt'5 p. h+ z6 S& u
X, y = utilities.load_data(input_file)
/ M$ W( G/ a5 r/ q5 p) i$ Y1 S& l( ?( e! ^6 O
###############################################
% j+ S( A! P6 C3 R! X6 [! j# D+ Z# Separate the data into classes based on 'y'
$ j! u% p' w- r. L0 |% K; u4 Aclass_0 = np.array([X[i] for i in range(len(X)) if y[i]==0])* B: Q; T' }+ K3 B& B1 N3 _1 y( ]
class_1 = np.array([X[i] for i in range(len(X)) if y[i]==1])8 E6 v9 R/ p2 p% J6 r
! R! o: N( O, j. X I# ?8 i
# Plot the input data% D% E8 L) D0 X" Y( y! {: J
plt.figure()6 i$ y$ V* u: P
plt.scatter(class_0[:,0], class_0[:,1], facecolors='black', edgecolors='black', marker='s')- N" C* }& ?. A' ? q" g
plt.scatter(class_1[:,0], class_1[:,1], facecolors='None', edgecolors='black', marker='s')
. ^: j" J5 t/ L7 B# u; Fplt.title('Input data')' @9 t. [4 z" P; r, }0 z
0 T+ A- i- \& A" O, N0 R0 Y###############################################
# X, h5 b/ L% \* H+ S# Train test split and SVM training( W. y4 ]- W7 A; f m; M" \
from sklearn import cross_validation) v, v. H) A U% r
from sklearn.svm import SVC* C! _% Q9 J8 Y& x; r3 c1 Q
- p& H0 L( I0 r) WX_train, X_test, y_train, y_test = cross_validation.train_test_split(X, y, test_size=0.25, random_state=5)2 B7 O6 ?; t( y1 A$ ^% v9 Y
- x2 E( `' _( [: X: q5 X" j#params = {'kernel': 'linear'}
/ N# o3 n, {& y5 ]" j& W#params = {'kernel': 'poly', 'degree': 3}% H ^. G4 f4 U1 e- G. b9 j
params = {'kernel': 'rbf'} F7 Q7 \% s5 x# t/ q
classifier = SVC(**params) A& s/ x K: @1 A
classifier.fit(X_train, y_train)! m O8 M* B4 L4 w
utilities.plot_classifier(classifier, X_train, y_train, 'Training dataset')
; Z! r* s' D5 d8 H
; Q1 E4 {# D2 f/ {( f2 K% uy_test_pred = classifier.predict(X_test) ]* j0 {# g% p9 N
utilities.plot_classifier(classifier, X_test, y_test, 'Test dataset') T* B9 h4 l2 i! U" {
' O" ~( t0 [6 r" h! m
###############################################
Y! }$ C, T9 n+ |# Evaluate classifier performance+ F$ a0 R( A: s+ J' e8 U
9 x$ \8 [ ?! o! ?3 P9 w
from sklearn.metrics import classification_report
. ?( n/ e# r0 ~, u5 p& U' d; K( \. }. ]/ ~5 b: e; z
target_names = ['Class-' + str(int(i)) for i in set(y)]3 H& @4 V. L J% _
print "\n" + "#"*30
2 S8 x/ g* z! E0 d6 |; b) bprint "\nClassifier performance on training dataset\n"/ V" k, t0 o' D3 M
print classification_report(y_train, classifier.predict(X_train), target_names=target_names)! i7 l# `$ _! h4 S: U
print "#"*30 + "\n"
: y: d* s% X- \/ `' @, m# G' Q- l* X" v& Z& ?9 S) A
print "#"*30
8 {& ^ k( P# `9 B8 fprint "\nClassification report on test dataset\n"* F2 a9 g1 L8 K6 `0 B+ K
print classification_report(y_test, y_test_pred, target_names=target_names)
% O8 n0 p, Q2 [. J4 Bprint "#"*30 + "\n"- t: z, G4 E9 m; O- j- G
/ j4 z, k/ \- E V$ H# n8 h# @9 P% a
|
|