- 金錢
- 45
- 威望
- 3183
- 貢獻值
- 0
- 推廣值
- 0
- 性別
- 保密
- 在線時間
- 38 小時
- 最後登錄
- 2024-2-25
- 主題
- 0
- 精華
- 0
- 閱讀權限
- 70
- 註冊時間
- 2012-3-17
- 帖子
- 553
 
該用戶從未簽到 - 推廣值
- 0
- 貢獻值
- 0
- 金錢
- 45
- 威望
- 3183
- 主題
- 0
|
import numpy as np$ d' d" p3 W% p( Q0 T/ \
import matplotlib.pyplot as plt: `2 ^, Y) j7 L, E Q
0 O' `* b: j o/ G1 M
import utilities 2 k0 O; ^+ V$ i6 _) ?
/ p* f% h0 w B# b# Load input data& {1 f# u5 R$ Y
input_file = 'D:\\1.Modeling material\\Py_Study\\2.code_model\\Python-Machine-Learning-Cookbook\\Python-Machine-Learning-Cookbook-master\\Chapter03\\data_multivar.txt'
$ i6 {8 f6 ?$ W' G; J ^" h* qX, y = utilities.load_data(input_file)
- C; c0 H+ I, ]5 W& j" j
$ |/ G# i4 {' Y$ Q6 I# @###############################################5 u0 ~: \2 F3 f3 ?( G" O2 q
# Separate the data into classes based on 'y'/ O! ]/ p; N" T* y0 j5 W! }
class_0 = np.array([X[i] for i in range(len(X)) if y[i]==0]); X6 c; x& e; n- V# [
class_1 = np.array([X[i] for i in range(len(X)) if y[i]==1])
: x/ s5 T' I" D* k! n; t" u8 J; x/ p8 V4 |* f
# Plot the input data
4 B% C2 g8 @0 Fplt.figure()) A3 k) w- A) `' S1 ^2 [, L0 \7 X
plt.scatter(class_0[:,0], class_0[:,1], facecolors='black', edgecolors='black', marker='s') v7 v* |8 L' g2 l! Y ^4 ^2 X
plt.scatter(class_1[:,0], class_1[:,1], facecolors='None', edgecolors='black', marker='s')
( E7 r+ z. i& G' N6 C' c% iplt.title('Input data')
/ _# j- k- ~. P* U' W8 L$ [8 v/ y1 h E
###############################################" z6 V* E7 e& j" C4 B
# Train test split and SVM training0 Q4 n( a( V6 s. t% F0 I1 D! g
from sklearn import cross_validation4 M1 ~& n5 I& o
from sklearn.svm import SVC
8 H; `6 b M% G
* Y4 V5 ~, C) E9 G' ^2 wX_train, X_test, y_train, y_test = cross_validation.train_test_split(X, y, test_size=0.25, random_state=5)6 q+ c' N! q$ U# o3 K
( {7 G5 ]4 Y5 y- v
#params = {'kernel': 'linear'}) \, n# g7 Z7 o# E( ?6 L
#params = {'kernel': 'poly', 'degree': 3}/ Q9 f1 v% E( s( c
params = {'kernel': 'rbf'}
! a* B& V/ G& A$ c$ z: Aclassifier = SVC(**params)
. w4 b5 S- [" Y- Y4 O9 F5 Aclassifier.fit(X_train, y_train)8 m; P5 h: X0 S7 y
utilities.plot_classifier(classifier, X_train, y_train, 'Training dataset')
$ y9 L2 S/ c; D( f0 O" Q! {* @' o2 M% L* z+ c2 q' y. \
y_test_pred = classifier.predict(X_test)% _: F0 n. d( {
utilities.plot_classifier(classifier, X_test, y_test, 'Test dataset')
6 b1 o% T9 M" Q' T# Z L6 y& c- o4 _
* h* F. Q$ A# @& p' K$ u/ t###############################################" N$ d9 `9 W/ t0 t9 h m- n) G0 P
# Evaluate classifier performance! ], O! }% r) J* C
+ X3 e& p0 y* v- O% s2 Pfrom sklearn.metrics import classification_report6 t2 p: S6 L1 X: {5 ]* n* H6 B
) ?0 @7 ^& ]5 c) k, \
target_names = ['Class-' + str(int(i)) for i in set(y)]
2 j% e- p5 y1 _( yprint "\n" + "#"*30
# m- Y1 E5 l1 m9 ?6 Lprint "\nClassifier performance on training dataset\n"
: \" I, _* F2 {0 f7 n3 yprint classification_report(y_train, classifier.predict(X_train), target_names=target_names)7 ?' _ [7 J# R! ~; _. [
print "#"*30 + "\n"- a3 d' g( y+ p8 z) X
: u3 b; O5 A Z, U8 d; ] Cprint "#"*305 X4 z3 A" p0 F7 ]
print "\nClassification report on test dataset\n"
, R3 e% U. a2 F: E Wprint classification_report(y_test, y_test_pred, target_names=target_names)
: y( v) z. V( y( B) {( Iprint "#"*30 + "\n"
2 p3 _! P% Q) }1 i8 J/ Y# C! g. I Q% D- i
|
|