- 金錢
- 45
- 威望
- 3183
- 貢獻值
- 0
- 推廣值
- 0
- 性別
- 保密
- 在線時間
- 38 小時
- 最後登錄
- 2024-2-25
- 主題
- 0
- 精華
- 0
- 閱讀權限
- 70
- 註冊時間
- 2012-3-17
- 帖子
- 553
 
該用戶從未簽到 - 推廣值
- 0
- 貢獻值
- 0
- 金錢
- 45
- 威望
- 3183
- 主題
- 0
|
import numpy as np
& `1 h# {- g# r! S- [import matplotlib.pyplot as plt
; m* b1 |3 U. @1 C0 ]: n9 J
: E$ I1 o6 Q% B+ `! Z$ Uimport utilities
5 y( Q; G' S+ E9 r- `: J" ^( H8 p& C7 y9 Y) [
# Load input data
2 @$ y* J% {7 x3 H: h7 y8 `. Q/ {input_file = 'D:\\1.Modeling material\\Py_Study\\2.code_model\\Python-Machine-Learning-Cookbook\\Python-Machine-Learning-Cookbook-master\\Chapter03\\data_multivar.txt'8 N9 N8 l5 r6 n9 o
X, y = utilities.load_data(input_file)
: U2 T8 M* L5 S( h) Y1 n3 c4 r7 V+ u/ n8 r7 P( _
###############################################! s) G9 R# W' l
# Separate the data into classes based on 'y'" V' ^% |) [- Z" h" C1 |$ J7 H
class_0 = np.array([X[i] for i in range(len(X)) if y[i]==0])
1 @+ I* I' t# f. t4 }5 bclass_1 = np.array([X[i] for i in range(len(X)) if y[i]==1])
& x5 ] c5 F& s( D8 y/ K! q0 X/ u# r
# Plot the input data, Q. B' h( Z6 v: @) S. i
plt.figure()
& Q& \/ K8 L$ c$ }( \# T5 dplt.scatter(class_0[:,0], class_0[:,1], facecolors='black', edgecolors='black', marker='s')* n+ O5 a5 N3 |
plt.scatter(class_1[:,0], class_1[:,1], facecolors='None', edgecolors='black', marker='s')0 [3 f& x' Y4 y
plt.title('Input data')7 ?2 B& P" I5 j
6 c' m) R2 p1 G1 D8 A8 l7 |; ?
###############################################6 ]6 n4 x! b2 P' D2 a2 s6 G8 e
# Train test split and SVM training& Z6 R- O6 R' |; o: ?
from sklearn import cross_validation
" G b. [- |3 n- Xfrom sklearn.svm import SVC
w3 @; @+ J6 I2 J$ p' ?1 N" ^4 r4 p) q r8 S
X_train, X_test, y_train, y_test = cross_validation.train_test_split(X, y, test_size=0.25, random_state=5)
) j8 V: @. l) f& X, c9 N3 P) o+ q1 N! m
#params = {'kernel': 'linear'}
) y$ e' |% i% ^9 j#params = {'kernel': 'poly', 'degree': 3}' U6 M$ w; D$ q. c
params = {'kernel': 'rbf'}
1 h* J" w4 }5 X& vclassifier = SVC(**params)7 M! t# S; s0 t: c3 p8 @
classifier.fit(X_train, y_train)5 w& S) F6 P. }1 I
utilities.plot_classifier(classifier, X_train, y_train, 'Training dataset')# I, w, O% U2 ]% W0 G# f
6 B z9 f3 m/ u
y_test_pred = classifier.predict(X_test)' t% j9 ]9 O9 {
utilities.plot_classifier(classifier, X_test, y_test, 'Test dataset')
/ m: B N# `, f5 n9 G/ f
$ t f$ U7 h& F5 T###############################################
/ J. T" H( R: S+ T# Evaluate classifier performance
: W R1 |9 [; b# k* m9 L
) M/ R6 S9 l" L& vfrom sklearn.metrics import classification_report
3 `: |( \5 \. U4 l$ j# g. T% _; z; K! z% p
target_names = ['Class-' + str(int(i)) for i in set(y)]
' I! \1 w. ]" g$ A5 Iprint "\n" + "#"*30" |( ]! S5 A P% f
print "\nClassifier performance on training dataset\n"8 { E% {. Q; J7 M& v3 a
print classification_report(y_train, classifier.predict(X_train), target_names=target_names)$ q: n! V* w& K
print "#"*30 + "\n"
5 k9 G' w8 d! I1 I# J7 b- k4 ?) Y1 Q8 U6 R9 F
print "#"*30
8 `' M5 N, r9 U$ C$ nprint "\nClassification report on test dataset\n"
0 a, R# e; y3 E/ w' pprint classification_report(y_test, y_test_pred, target_names=target_names)
& b1 Y- ~' S9 eprint "#"*30 + "\n"* R" P1 C/ L" |1 [# f8 W
6 G$ `7 W! t% v
|
|