- 金錢
- 45
- 威望
- 3183
- 貢獻值
- 0
- 推廣值
- 0
- 性別
- 保密
- 在線時間
- 38 小時
- 最後登錄
- 2024-2-25
- 主題
- 0
- 精華
- 0
- 閱讀權限
- 70
- 註冊時間
- 2012-3-17
- 帖子
- 553
 
該用戶從未簽到 - 推廣值
- 0
- 貢獻值
- 0
- 金錢
- 45
- 威望
- 3183
- 主題
- 0
|
import numpy as np- i" I- Y0 N$ I' T
import matplotlib.pyplot as plt6 _& @2 H3 D( }* g- O
, G, v% E6 G: T6 j2 Yimport utilities 2 Z& |$ M* o. p0 N" ^' G; E
( M, L3 R r- G( `. _1 q# d# Load input data
3 M. O$ m7 C6 Binput_file = 'D:\\1.Modeling material\\Py_Study\\2.code_model\\Python-Machine-Learning-Cookbook\\Python-Machine-Learning-Cookbook-master\\Chapter03\\data_multivar.txt'& T% n- R# J+ Z
X, y = utilities.load_data(input_file)
8 f' c# p" k: {9 L8 E/ z% n
) d& g8 v, [% r( V6 d6 ?################################################ ~& j; O" j2 z+ _5 W" P6 W5 O
# Separate the data into classes based on 'y'* i. h e& p2 _* l
class_0 = np.array([X[i] for i in range(len(X)) if y[i]==0])
% D, M2 P$ }( g: wclass_1 = np.array([X[i] for i in range(len(X)) if y[i]==1])
: e/ U) W j- ]! J* ]6 q* h6 U y0 b7 U4 V0 g
# Plot the input data
. B8 D& C2 h; M0 splt.figure()6 [+ Z% k& X6 z/ b0 @0 A4 ]" t- n
plt.scatter(class_0[:,0], class_0[:,1], facecolors='black', edgecolors='black', marker='s')
0 K2 \* r& V# x; J5 h) |' S- A' Zplt.scatter(class_1[:,0], class_1[:,1], facecolors='None', edgecolors='black', marker='s')3 }5 Y8 O8 K4 l4 e- G! t; h, X
plt.title('Input data'); o g* a6 d. w5 U) s, e
+ j/ P6 P/ n5 H* L6 P( i; D
###############################################
/ S4 U' ?! E4 f# Train test split and SVM training4 F# c3 H" V H) f1 i V* l5 s
from sklearn import cross_validation' R1 P& w! N8 D1 C g2 f
from sklearn.svm import SVC& w" K5 x% P0 J: ^8 y1 {6 F! I
" z' b! f' a7 o$ C8 h4 u+ f
X_train, X_test, y_train, y_test = cross_validation.train_test_split(X, y, test_size=0.25, random_state=5)
( Q; s6 R+ {0 C' W
& R$ g4 o$ q* W1 B! X0 O#params = {'kernel': 'linear'}
: }* ~& Q( a1 I) r! ^/ k" Y: @; K#params = {'kernel': 'poly', 'degree': 3}
( {- N: Q9 |& p% V p! h, Mparams = {'kernel': 'rbf'}
1 {6 ~' p3 E6 K5 C* oclassifier = SVC(**params)
8 H: ?5 \4 N# N" X1 Q- q! Yclassifier.fit(X_train, y_train)$ M8 p& e1 o" L% m( \" s
utilities.plot_classifier(classifier, X_train, y_train, 'Training dataset')
& n9 h$ z3 z1 z$ o
+ D1 Q9 L$ @" q. c( h: Oy_test_pred = classifier.predict(X_test)$ [! p ~( I" q2 n6 S
utilities.plot_classifier(classifier, X_test, y_test, 'Test dataset')7 x& n$ d0 ^* \2 T
M- |* y0 H7 `$ f$ H9 |* M
###############################################
0 a* t- l' f2 i. U W# Evaluate classifier performance. X6 d1 N/ L9 E; r) k
9 r% W" y( a5 S9 o1 W+ D
from sklearn.metrics import classification_report' P: i$ O/ g! W( w8 K
" ~' H+ M/ x! e: Q% b: s0 F
target_names = ['Class-' + str(int(i)) for i in set(y)]7 u( V( h( y7 X9 h& E. c( v
print "\n" + "#"*30
/ P4 a3 N% O Y+ Z: v0 W* [print "\nClassifier performance on training dataset\n"1 w7 @2 A( e1 v6 e
print classification_report(y_train, classifier.predict(X_train), target_names=target_names)
& V9 S9 G; u4 pprint "#"*30 + "\n"
; K* Q/ C. \! `7 q- o; W
{) A" P. G& h) F7 Mprint "#"*30! v* N, @! I, B( e# u3 D' v v
print "\nClassification report on test dataset\n"
; A+ d; O, M5 a" M. d9 Yprint classification_report(y_test, y_test_pred, target_names=target_names)# x$ V/ u7 e8 X6 N
print "#"*30 + "\n"( E- e% i* k) d: r8 i" p
6 J' I4 `' N6 o4 k! d& L( s! F
|
|