- 金錢
- 46
- 威望
- 3183
- 貢獻值
- 0
- 推廣值
- 0
- 性別
- 保密
- 在線時間
- 38 小時
- 最後登錄
- 2024-2-25
- 主題
- 0
- 精華
- 0
- 閱讀權限
- 70
- 註冊時間
- 2012-3-17
- 帖子
- 556
該用戶從未簽到 - 推廣值
- 0
- 貢獻值
- 0
- 金錢
- 46
- 威望
- 3183
- 主題
- 0
|
import numpy as np" V1 ? D+ C9 f" s$ n
import matplotlib.pyplot as plt8 u/ q* J4 |+ u) J4 H1 n
5 e/ Y& G; m* Q. i# [* `
import utilities
7 U+ L1 ?; V( [8 K; q( G& i8 {/ V; ~- h. b! v
# Load input data
|, b! |% E, H* c i7 x/ R4 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'
1 g* V4 E/ e+ v! G+ X, eX, y = utilities.load_data(input_file)
& N& o6 `% \# G) }- v+ ]" O, y5 m. ^- l$ Y. I
###############################################3 }7 j9 F: W% `4 v; }2 G
# Separate the data into classes based on 'y'2 O( A1 x- ` ]+ `& S" z
class_0 = np.array([X[i] for i in range(len(X)) if y[i]==0])+ B7 f# M' s6 Q6 _ V1 P& Z* P2 P9 C
class_1 = np.array([X[i] for i in range(len(X)) if y[i]==1])
/ y0 a; i3 X9 E5 o5 u6 g/ b
( c9 _! u5 U0 v' [. |# Plot the input data
6 Z3 d3 X, D h( d8 pplt.figure()+ S" a- G8 r4 h& M! q
plt.scatter(class_0[:,0], class_0[:,1], facecolors='black', edgecolors='black', marker='s')# @* x& m; f. X4 N8 j
plt.scatter(class_1[:,0], class_1[:,1], facecolors='None', edgecolors='black', marker='s')( D5 M9 A! H: U! p- q( N: O6 s
plt.title('Input data')# Q$ Q1 Q5 v% z4 Y, ^7 F V; Q
; ~% a6 X+ W$ e7 o6 a: o' O! V###############################################8 O, B$ s: s @) c
# Train test split and SVM training7 k# }* [# T9 t3 ?
from sklearn import cross_validation0 q. ^1 X' K9 R+ Q
from sklearn.svm import SVC1 G7 w! e, o, t' N+ |; G
1 D2 c; W9 q; |3 z& g% l
X_train, X_test, y_train, y_test = cross_validation.train_test_split(X, y, test_size=0.25, random_state=5)
; e. I X& p4 B) ~8 Y' T2 I# ^
3 @; Z/ {7 B. i5 d) q5 z8 }, _#params = {'kernel': 'linear'}1 m4 c% Q9 ?9 ~% E( s
#params = {'kernel': 'poly', 'degree': 3}
; u j5 F, ~0 ~/ Xparams = {'kernel': 'rbf'}4 D2 p2 M& ?) i4 v6 o% K8 X3 W
classifier = SVC(**params)
, ~+ ^$ `- Q7 s! Y$ U5 p( u& A6 W; `8 Hclassifier.fit(X_train, y_train)
4 Q9 L5 z# t# W: H8 R0 K3 G" U. Dutilities.plot_classifier(classifier, X_train, y_train, 'Training dataset')
, m! W$ O" ^- s* R/ e$ h, d- Q
- [8 f+ |0 |& N& y5 \y_test_pred = classifier.predict(X_test)
0 i$ |% g! ^- a% |( a- Gutilities.plot_classifier(classifier, X_test, y_test, 'Test dataset')% ], H7 {5 L5 ?4 z% \* @
; e/ r& s3 Y5 k) v
###############################################4 r% d5 A& x% W+ d5 t
# Evaluate classifier performance/ ]- e# Y) l# Q" m1 n3 V
6 S- B. d8 K: wfrom sklearn.metrics import classification_report$ `, |4 t5 w! c% `7 N" t2 l' U
8 d. o( M9 R4 _" R- v( F: L8 Z
target_names = ['Class-' + str(int(i)) for i in set(y)]4 J- m+ h) E7 X1 i7 E
print "\n" + "#"*30( v# F! l; [- U; J
print "\nClassifier performance on training dataset\n"* m# {# Q' l& A! h! o8 G
print classification_report(y_train, classifier.predict(X_train), target_names=target_names); _+ T) J% I: \
print "#"*30 + "\n"
4 }2 S5 P1 B! H$ ]/ l- {% b: c
# T" A0 O# Q% g: h' y" a* h) l& pprint "#"*30
! m3 t' \# @* k+ u/ D& Q5 s. iprint "\nClassification report on test dataset\n"
2 ^" r5 b) {3 f0 @# l( `$ W- hprint classification_report(y_test, y_test_pred, target_names=target_names)" \! D! r2 m5 ?8 \8 b5 D0 b
print "#"*30 + "\n"" F) g/ r4 Q1 }5 n& m ~7 D
2 D- o8 P1 J, @- ?1 j
|
|