- 金錢
- 45
- 威望
- 3183
- 貢獻值
- 0
- 推廣值
- 0
- 性別
- 保密
- 在線時間
- 38 小時
- 最後登錄
- 2024-2-25
- 主題
- 0
- 精華
- 0
- 閱讀權限
- 70
- 註冊時間
- 2012-3-17
- 帖子
- 553
 
該用戶從未簽到 - 推廣值
- 0
- 貢獻值
- 0
- 金錢
- 45
- 威望
- 3183
- 主題
- 0
|
import numpy as np
- \6 V8 O6 u4 I Bimport matplotlib.pyplot as plt
4 C* z- r' v0 b; G9 o, K
. R$ f0 ?; l1 L$ P, v% S( @) T3 timport utilities . T( ?' f. x3 p; G6 B5 g% B
8 ?% h/ P( K X9 ]) C' a
# Load input data0 M' Q* P% M" ^ D
input_file = 'D:\\1.Modeling material\\Py_Study\\2.code_model\\Python-Machine-Learning-Cookbook\\Python-Machine-Learning-Cookbook-master\\Chapter03\\data_multivar.txt'4 y; O' p' K& W1 n0 [$ W# J
X, y = utilities.load_data(input_file)
' W( `& B2 @$ H ] w* e! b( N3 o& Q2 g% n/ N5 B, i. X
###############################################
) D d) P$ l! }, b# Separate the data into classes based on 'y'
# | Y4 n3 R1 e( R6 Gclass_0 = np.array([X[i] for i in range(len(X)) if y[i]==0])
0 y( O% J1 h. z4 u# ^class_1 = np.array([X[i] for i in range(len(X)) if y[i]==1])
6 l$ |1 H; E; u. _$ i4 V+ _" `+ M+ h) A6 e5 f1 }1 q6 R1 h* B7 J
# Plot the input data. X# w) P0 w4 b, c3 B
plt.figure()
% |2 ]2 e7 u$ B' }' T& o# O; _% eplt.scatter(class_0[:,0], class_0[:,1], facecolors='black', edgecolors='black', marker='s')8 ~. K5 z2 T# C: q! n
plt.scatter(class_1[:,0], class_1[:,1], facecolors='None', edgecolors='black', marker='s')
; l, b; S J. ]plt.title('Input data')
* R' b: L. P1 c1 a3 {1 X; P2 J
5 H/ g$ j2 a, p* j$ J0 F- a( O- X. d###############################################
1 Y1 x! |/ m! o, o; v- ?# Train test split and SVM training
+ ?# a$ `3 v M2 j5 h. e9 V+ Y- Ofrom sklearn import cross_validation
6 M8 X' {( [, Afrom sklearn.svm import SVC
- p4 j( f5 z: Q; t9 b$ _9 s: x0 S1 U1 e* {
X_train, X_test, y_train, y_test = cross_validation.train_test_split(X, y, test_size=0.25, random_state=5)
* u3 f$ z. e" H5 {. s( |
4 x) G" w: l. c+ |: p8 k, [$ p#params = {'kernel': 'linear'}, U! i2 z0 i; n
#params = {'kernel': 'poly', 'degree': 3}9 Y' [" s* V8 T0 r
params = {'kernel': 'rbf'}& ?: u5 B# d3 |4 I/ K+ d* w
classifier = SVC(**params)
% } u' Z2 J2 V7 Sclassifier.fit(X_train, y_train)
8 j, I0 }0 }7 jutilities.plot_classifier(classifier, X_train, y_train, 'Training dataset')9 A0 m; F. a6 O+ v
/ N, K, i3 k8 V% X4 m4 P+ o; p2 Q& Y# By_test_pred = classifier.predict(X_test)
0 g- g, t2 P- e6 g! s5 P& }utilities.plot_classifier(classifier, X_test, y_test, 'Test dataset')
|3 Q2 X7 [* U9 y. }' O* e( p& R
###############################################8 k t& ]/ M! a3 E
# Evaluate classifier performance
( F, t6 }" m2 V& T. z' N1 F* V6 P4 j% ^
from sklearn.metrics import classification_report
- H3 W* K' Z: R# D& J
( H. H& N8 s1 Y' W1 @target_names = ['Class-' + str(int(i)) for i in set(y)]
! m# ]. O2 Q {- ~. S4 N& q: b8 K- Pprint "\n" + "#"*30
/ q. G; m) i4 }; z, ^7 x# zprint "\nClassifier performance on training dataset\n"
' V5 n+ k e: L* M3 W, c) c( tprint classification_report(y_train, classifier.predict(X_train), target_names=target_names)
! @! E; D( q4 N/ e8 s! \4 d* j' c, Yprint "#"*30 + "\n"
7 P3 X6 U* @- ^6 [* ?8 r
4 X; ^; i1 k6 G1 sprint "#"*30
6 v- I4 g$ D6 ^, p1 q+ F) G& W6 Fprint "\nClassification report on test dataset\n"
' [; g% c7 p) W) Zprint classification_report(y_test, y_test_pred, target_names=target_names): I8 q% |$ |( Q! h2 `
print "#"*30 + "\n"% f( X9 `" r- i, S0 `8 k' S, K9 J
" _' P+ {; E% L( [ |
|