- 金錢
- 46
- 威望
- 3183
- 貢獻值
- 0
- 推廣值
- 0
- 性別
- 保密
- 在線時間
- 38 小時
- 最後登錄
- 2024-2-25
- 主題
- 0
- 精華
- 0
- 閱讀權限
- 70
- 註冊時間
- 2012-3-17
- 帖子
- 556
 
該用戶從未簽到 - 推廣值
- 0
- 貢獻值
- 0
- 金錢
- 46
- 威望
- 3183
- 主題
- 0
|
import numpy as np* j) ]; U7 V' _' Z. x1 f9 S
import matplotlib.pyplot as plt2 X1 I7 k5 Z/ J9 b6 x# K, g1 h
0 d7 n+ t" W8 F* K5 f# ^import utilities 5 F& `) a7 |$ d! k2 W
3 [5 t; i) R' {1 N, _1 P# Load input data
: ~6 T7 ?2 ^7 n2 T/ einput_file = 'D:\\1.Modeling material\\Py_Study\\2.code_model\\Python-Machine-Learning-Cookbook\\Python-Machine-Learning-Cookbook-master\\Chapter03\\data_multivar.txt'
. m; u+ o3 l' yX, y = utilities.load_data(input_file)$ \" l/ O( R8 A9 ~
: P6 `6 ?0 ~! A. X J7 n; Y###############################################- F! r8 N6 g$ u; G9 z9 T
# Separate the data into classes based on 'y'
. t/ Q: a) N3 X$ Tclass_0 = np.array([X[i] for i in range(len(X)) if y[i]==0])
6 [6 X! `9 y* D4 g) Vclass_1 = np.array([X[i] for i in range(len(X)) if y[i]==1])# z* c: B0 Y- E' N
6 J$ w4 c- z; S4 n: ]/ T( ~5 t# Plot the input data% X3 J! ]! Q2 c H0 M9 I9 g0 G+ U
plt.figure()9 o" V, \$ N& Y* O" y, f: b4 A6 \
plt.scatter(class_0[:,0], class_0[:,1], facecolors='black', edgecolors='black', marker='s')
1 ?% g' i: ]: e, @/ pplt.scatter(class_1[:,0], class_1[:,1], facecolors='None', edgecolors='black', marker='s')
7 A7 t9 N3 F5 w* D8 v& i5 D, splt.title('Input data')7 c" i2 _5 I) g. [# [: G6 v
& ]/ i5 G5 i# \% `; e, E###############################################8 G% g9 b; e$ n3 X' ^- {. Q3 B
# Train test split and SVM training: S h6 r$ [# X( L5 U
from sklearn import cross_validation1 ]7 N( v7 t7 N" }. F% e
from sklearn.svm import SVC1 v6 K0 h) S: P6 U D( ~! K
7 N( ^' ~* P) ~' X' KX_train, X_test, y_train, y_test = cross_validation.train_test_split(X, y, test_size=0.25, random_state=5)
3 H% j) t- i# n5 R' T% s
' R# h5 v; \ B$ y; ~# o4 \#params = {'kernel': 'linear'}
$ |* A3 \5 o7 e; ~: x6 {6 W' O#params = {'kernel': 'poly', 'degree': 3}
7 |7 c' O* h9 ?& R* t& I; U, Eparams = {'kernel': 'rbf'}
8 u/ |0 M7 L2 jclassifier = SVC(**params)
- v6 d, L) r9 e- c4 @5 m3 dclassifier.fit(X_train, y_train)1 n' N% B8 j) G- \/ a* }
utilities.plot_classifier(classifier, X_train, y_train, 'Training dataset')4 | h6 Z& f) q) {8 V3 [. S0 S
; G$ K7 h( H6 ~: \9 i( _. c: H
y_test_pred = classifier.predict(X_test)
Q& x5 T* |- `" d9 Zutilities.plot_classifier(classifier, X_test, y_test, 'Test dataset'): q; j; o- V+ ~/ J1 p8 a$ i
+ S7 {1 a) L6 c, f3 M K3 A1 P
###############################################- A0 P9 x: C9 J, \/ \3 n
# Evaluate classifier performance
t/ \; L+ i$ J0 X' Q0 ?+ z- z* b# C% E) q. J! B
from sklearn.metrics import classification_report) q% Z( X( o' r& p2 T" L; N8 p4 l
1 z. q! L( i }target_names = ['Class-' + str(int(i)) for i in set(y)]5 [+ m; F; q l4 X, `( f% D
print "\n" + "#"*30. e4 p# b; W# I! ?, K \* O1 O6 a: _
print "\nClassifier performance on training dataset\n", K6 c8 g. S8 `, \7 S3 }0 I; E
print classification_report(y_train, classifier.predict(X_train), target_names=target_names)
( ?; d, W. j! {" P3 pprint "#"*30 + "\n"
$ [" B( k- v& |1 G
+ F5 ~: D& m. G' Dprint "#"*30
. F: f) p2 `- O$ l9 Bprint "\nClassification report on test dataset\n"0 G; Z9 J. p5 o3 S5 l& l
print classification_report(y_test, y_test_pred, target_names=target_names)& G5 G! `* x* K4 Y5 b
print "#"*30 + "\n"
8 d2 @+ r- _6 q* c2 F
; P" L+ H$ r1 U9 v |
|