- 金錢
- 45
- 威望
- 3183
- 貢獻值
- 0
- 推廣值
- 0
- 性別
- 保密
- 在線時間
- 38 小時
- 最後登錄
- 2024-2-25
- 主題
- 0
- 精華
- 0
- 閱讀權限
- 70
- 註冊時間
- 2012-3-17
- 帖子
- 553
 
該用戶從未簽到 - 推廣值
- 0
- 貢獻值
- 0
- 金錢
- 45
- 威望
- 3183
- 主題
- 0
|
import numpy as np* y1 B) E: K s$ y( L
import matplotlib.pyplot as plt& s, S$ s+ I+ h6 d1 j5 f
8 f0 v* Q; S% l) X1 w* ^import utilities
8 { l6 }5 G# N" L t$ h) w: u' F" ?6 G5 P& v0 n7 t% l
# Load input data
. { K8 q9 T% Y/ m3 [$ H0 o/ E9 Vinput_file = 'D:\\1.Modeling material\\Py_Study\\2.code_model\\Python-Machine-Learning-Cookbook\\Python-Machine-Learning-Cookbook-master\\Chapter03\\data_multivar.txt'
$ s, I$ w- N V; DX, y = utilities.load_data(input_file)
* F% S* @- n+ v; S* U# s
: t+ [) w' k% ^2 V# S###############################################% z' }% U* u+ n, ?& L
# Separate the data into classes based on 'y'
) q9 C" C: q- e6 Q% X o3 l% Yclass_0 = np.array([X[i] for i in range(len(X)) if y[i]==0])
) W9 V9 I6 y: uclass_1 = np.array([X[i] for i in range(len(X)) if y[i]==1])
2 Z6 N: n' N" C0 d' n. g
6 Z, s8 a7 p4 f# e# V# p# Plot the input data
& q/ @1 D5 T7 m5 I6 lplt.figure()5 r0 l% V$ O% F: G
plt.scatter(class_0[:,0], class_0[:,1], facecolors='black', edgecolors='black', marker='s')
; `4 u+ h% i7 D S; [plt.scatter(class_1[:,0], class_1[:,1], facecolors='None', edgecolors='black', marker='s')
u! S! v7 G+ T, I( u) Aplt.title('Input data')6 q4 p9 E. z, f1 S. K3 t" F
4 b. I% m$ l$ m; F k& F; K" C
###############################################
! S: k5 o" L" _" u2 u# Train test split and SVM training
9 g. Q5 M V Q) ffrom sklearn import cross_validation
2 ^8 v* @; y; h# hfrom sklearn.svm import SVC
% u1 h5 p4 H' ~* e( B
, m7 S8 S2 {) U- XX_train, X_test, y_train, y_test = cross_validation.train_test_split(X, y, test_size=0.25, random_state=5)
$ M$ A: Z8 t* V# d" `9 S
7 z4 X' g8 H, T [9 _! ~#params = {'kernel': 'linear'}- c2 Q! v# |( O# Q* ^% m9 S
#params = {'kernel': 'poly', 'degree': 3}
/ |- S8 o: i+ m n& T! {: Nparams = {'kernel': 'rbf'}" J l2 H& d; b/ W
classifier = SVC(**params)
1 `& |& ?, a& `( R5 C+ Tclassifier.fit(X_train, y_train)) R% @8 Z- ]- f& ?% T+ i$ ^, P
utilities.plot_classifier(classifier, X_train, y_train, 'Training dataset')
6 F. H9 t, c, q8 E r6 A$ C
: ]! o& Z+ i4 F. w" o2 sy_test_pred = classifier.predict(X_test)5 P; x8 L9 \ s! m
utilities.plot_classifier(classifier, X_test, y_test, 'Test dataset')
0 x% K, q+ d+ e4 G; ~4 f
0 F+ k4 y# Z. s& y0 _###############################################
6 R3 p; `% _6 E: Q: M: L1 K, @# Evaluate classifier performance
* q. ?+ {' P+ H6 i$ w/ j8 r$ d7 l0 x
from sklearn.metrics import classification_report5 \) ]8 ]: q% g
0 J6 [; M/ ~& x+ Q
target_names = ['Class-' + str(int(i)) for i in set(y)]
# D9 ^) ~2 j& y: }* Sprint "\n" + "#"*30, H& n/ N- Z! H" z" q2 z; v( H
print "\nClassifier performance on training dataset\n". w! H9 K. G% i- T3 A
print classification_report(y_train, classifier.predict(X_train), target_names=target_names)
+ @! B! g, O$ Y/ K0 [- Y* ]print "#"*30 + "\n": w' }" S9 A; a9 W1 F
% [8 M+ @9 F, G5 M+ }1 c2 Oprint "#"*30
. Z5 x" W( F9 i; ^8 Mprint "\nClassification report on test dataset\n"
+ H5 p A/ A$ P' L3 Z: ]% eprint classification_report(y_test, y_test_pred, target_names=target_names)
o; [, P" V6 aprint "#"*30 + "\n"2 d$ c8 l6 q5 H
3 X$ G/ i3 @0 C' j& w2 h- P, H! c
|
|