- 金錢
- 45
- 威望
- 3183
- 貢獻值
- 0
- 推廣值
- 0
- 性別
- 保密
- 在線時間
- 38 小時
- 最後登錄
- 2024-2-25
- 主題
- 0
- 精華
- 0
- 閱讀權限
- 70
- 註冊時間
- 2012-3-17
- 帖子
- 553
 
該用戶從未簽到 - 推廣值
- 0
- 貢獻值
- 0
- 金錢
- 45
- 威望
- 3183
- 主題
- 0
|
import numpy as np
# b) d( d$ B8 Y* wimport matplotlib.pyplot as plt$ r4 f& T* l& C8 G' [, \- H
) ~. `8 G: H- _! E; @& w% \; U7 `
import utilities
7 W/ o/ { ~+ A, M% ^! C$ U2 x' ~) b% P4 ^3 Z6 R
# Load input data
$ L T- n) W6 Z# R s- Einput_file = 'D:\\1.Modeling material\\Py_Study\\2.code_model\\Python-Machine-Learning-Cookbook\\Python-Machine-Learning-Cookbook-master\\Chapter03\\data_multivar.txt'5 ^* h4 l1 V; I* I
X, y = utilities.load_data(input_file): z8 ~: l+ ^% }8 f/ @5 ?
( I, y0 ^* P' e" C
###############################################
1 F! p2 k7 r' Z& D: F; v9 |9 q( g3 S# Separate the data into classes based on 'y') c6 Y- x5 g9 w
class_0 = np.array([X[i] for i in range(len(X)) if y[i]==0])) Q7 V6 I4 }3 v4 W( }
class_1 = np.array([X[i] for i in range(len(X)) if y[i]==1])4 ?( S1 Z6 x- \2 D z9 `, b3 V" f
- o, ^0 m; A; w0 E
# Plot the input data" W. ?3 z8 R( I+ x
plt.figure()
8 L1 @7 ?/ t8 i- W8 [2 a$ i+ c) ~plt.scatter(class_0[:,0], class_0[:,1], facecolors='black', edgecolors='black', marker='s')) w& L, A" A3 r2 r# J \: W
plt.scatter(class_1[:,0], class_1[:,1], facecolors='None', edgecolors='black', marker='s')& m" E1 l0 D4 v. n
plt.title('Input data')
' M. t1 B! p# [* x V1 k
5 D- |. \" |5 l/ a9 {: w8 t; [) h###############################################
/ l& |: c! G# K O# ?# Train test split and SVM training
% v' N- C, C, Z; cfrom sklearn import cross_validation0 B J, h3 S& t, }8 p
from sklearn.svm import SVC! y% }6 O; W# @ s* R8 f Y1 T
0 {( g! [& D$ s; J
X_train, X_test, y_train, y_test = cross_validation.train_test_split(X, y, test_size=0.25, random_state=5)
3 w3 Z0 u8 @4 R! {
- F( ^* S7 \3 b( s5 |. v1 K#params = {'kernel': 'linear'}, L' [* V5 k/ I' W1 X
#params = {'kernel': 'poly', 'degree': 3}& u" ]) a' o! i0 t b% {
params = {'kernel': 'rbf'}9 `7 n- }/ @7 ?% b2 M2 @* D
classifier = SVC(**params)! ^7 N+ ^( O6 f: V
classifier.fit(X_train, y_train)- a% C) K& }) A. T* L
utilities.plot_classifier(classifier, X_train, y_train, 'Training dataset')
. ~+ L x+ F I/ e) ?8 g* D( ?) {
/ P M" d0 }1 ]& Ey_test_pred = classifier.predict(X_test)
8 b* x c3 m; _. ]; Wutilities.plot_classifier(classifier, X_test, y_test, 'Test dataset'); u# J& x: a+ M. S* y1 j
5 r& U6 S. H( n( o' _$ g! u############################################### e% U7 T5 P+ ~% E* c9 l) ~
# Evaluate classifier performance
1 T: V- E7 l. Q& W1 H* _7 r/ a. D. | M$ S- _4 x$ M
from sklearn.metrics import classification_report
: h3 p" `. c" W8 s: ?$ `+ w7 k2 Z7 t
target_names = ['Class-' + str(int(i)) for i in set(y)]+ b: m. M. l2 a3 r [) w% u
print "\n" + "#"*30* j: Z3 s# K( u$ ?! A: z G
print "\nClassifier performance on training dataset\n"
1 U' { ]2 R$ W9 Yprint classification_report(y_train, classifier.predict(X_train), target_names=target_names)
# F( Q! _0 U, E* M: qprint "#"*30 + "\n"
8 M7 y$ @! Y0 K# j9 X: I
6 z+ O1 k6 T4 E$ t: rprint "#"*30! } ~) u6 W! [9 r; Z) Q
print "\nClassification report on test dataset\n"
" O, C& W2 @9 ^8 ]$ G. sprint classification_report(y_test, y_test_pred, target_names=target_names)
3 U. t' R1 Q; o4 Gprint "#"*30 + "\n"7 a' h, |" o" }& p: r8 ]
( x) f4 s' l/ O8 ? |
|