- 金錢
- 45
- 威望
- 3183
- 貢獻值
- 0
- 推廣值
- 0
- 性別
- 保密
- 在線時間
- 38 小時
- 最後登錄
- 2024-2-25
- 主題
- 0
- 精華
- 0
- 閱讀權限
- 70
- 註冊時間
- 2012-3-17
- 帖子
- 553
 
該用戶從未簽到 - 推廣值
- 0
- 貢獻值
- 0
- 金錢
- 45
- 威望
- 3183
- 主題
- 0
|
import numpy as np
" H, v- r* D; Simport matplotlib.pyplot as plt0 \* u5 V+ T( M6 s" B' N1 F
- l2 @$ g+ R! j: N$ A; P% z& Q/ d
import utilities
' j* ~2 g& N c9 ?- {$ n; L0 a0 U1 M
# Load input data
' m& q! X9 T( f( S( o4 a- xinput_file = 'D:\\1.Modeling material\\Py_Study\\2.code_model\\Python-Machine-Learning-Cookbook\\Python-Machine-Learning-Cookbook-master\\Chapter03\\data_multivar.txt'
/ Z9 A% M5 [2 ~, XX, y = utilities.load_data(input_file)' V5 j6 ~! G5 r, s0 }
2 a6 T& a- A. z###############################################$ z# @9 k+ q6 U! q# t
# Separate the data into classes based on 'y'
8 Z' F, c- H5 f8 [" a. F2 [$ mclass_0 = np.array([X[i] for i in range(len(X)) if y[i]==0])6 q6 j! q# M; L
class_1 = np.array([X[i] for i in range(len(X)) if y[i]==1])
5 ?( G' I _) Y. |# @# Y, k/ L* W! v8 F' {- Q
# Plot the input data
# U) k9 l" f8 p- S& ~plt.figure(). }+ k5 B r7 Q
plt.scatter(class_0[:,0], class_0[:,1], facecolors='black', edgecolors='black', marker='s')
) t# H* ^9 T) E- s5 f5 ~plt.scatter(class_1[:,0], class_1[:,1], facecolors='None', edgecolors='black', marker='s')
& R, u! O( W9 o+ ~3 Fplt.title('Input data')
: V6 Q/ P1 r1 ?* p4 ~
) s0 \, R8 O8 l6 F###############################################3 {9 ?8 [8 S1 t- g4 z
# Train test split and SVM training
4 b# C) J, j' Tfrom sklearn import cross_validation* E9 c& s: Y, [( n
from sklearn.svm import SVC
- m5 n b3 P8 m: I
3 A, `: ?4 K7 L1 AX_train, X_test, y_train, y_test = cross_validation.train_test_split(X, y, test_size=0.25, random_state=5). f+ |7 V/ g6 y& T
5 q. x7 D4 q8 f5 L
#params = {'kernel': 'linear'}
# ]# J8 _5 M. A0 R% y#params = {'kernel': 'poly', 'degree': 3}
; K: `9 H. r, qparams = {'kernel': 'rbf'}
) N: P+ ]- s# h7 k: rclassifier = SVC(**params): i b8 j; @# |0 q- T
classifier.fit(X_train, y_train)1 {+ @ C' {/ T' F# `9 N
utilities.plot_classifier(classifier, X_train, y_train, 'Training dataset')
( b& C( t$ _, \* o
6 ?+ C: c8 I4 Py_test_pred = classifier.predict(X_test)
" ?2 S: }; I' A1 ?utilities.plot_classifier(classifier, X_test, y_test, 'Test dataset')6 M# Y/ e; I& Q+ ~! h2 h
# U9 n4 b: x, f7 x###############################################* j9 S, l7 D% k9 Y* B
# Evaluate classifier performance
) |5 D3 B9 F5 T" I, \7 Y: a8 C0 Q! u/ b4 j
from sklearn.metrics import classification_report
7 g6 q- p r5 g2 A, w' I% B6 G( |8 I. }
target_names = ['Class-' + str(int(i)) for i in set(y)]% d# D& O$ J2 y3 h. ?# I- g
print "\n" + "#"*30* O2 T0 N* u9 X
print "\nClassifier performance on training dataset\n"! ~! c3 [: v! J. h' m3 I5 [
print classification_report(y_train, classifier.predict(X_train), target_names=target_names)
/ V& N5 i$ E0 M) b; \0 E/ n6 Zprint "#"*30 + "\n"
3 [) {5 m6 K& t ]4 z z" ?- W9 S" ] }+ B1 d: y t! F$ N
print "#"*30$ S% f5 v1 E$ A
print "\nClassification report on test dataset\n"' V: h" E2 U" J% x, I# X
print classification_report(y_test, y_test_pred, target_names=target_names)4 n. w2 _- W* k- ^9 P
print "#"*30 + "\n"
4 d; G/ F& \! P) x+ m
! {. ]/ d" E- m5 {+ v$ M |
|