- 金錢
- 45
- 威望
- 3183
- 貢獻值
- 0
- 推廣值
- 0
- 性別
- 保密
- 在線時間
- 38 小時
- 最後登錄
- 2024-2-25
- 主題
- 0
- 精華
- 0
- 閱讀權限
- 70
- 註冊時間
- 2012-3-17
- 帖子
- 553
 
該用戶從未簽到 - 推廣值
- 0
- 貢獻值
- 0
- 金錢
- 45
- 威望
- 3183
- 主題
- 0
|
import numpy as np8 a5 j( a# f- L/ H$ W$ Z2 d
import matplotlib.pyplot as plt* y, w4 u$ {0 a. k+ e8 P K2 F
( D# Y2 ` E$ V1 L bimport utilities * F; a4 p, T2 Z) Y
: |# y, @' q1 Y* P- \# Load input data
" E! B$ ~1 \% @* a1 q5 Pinput_file = 'D:\\1.Modeling material\\Py_Study\\2.code_model\\Python-Machine-Learning-Cookbook\\Python-Machine-Learning-Cookbook-master\\Chapter03\\data_multivar.txt'. A8 m) u! d0 K) F9 ^& e: x/ w
X, y = utilities.load_data(input_file). Z) z s; ] D8 o+ N
7 G5 C; b( v ?0 ~
############################################### F' ^+ P& a$ j6 K1 Z+ K# c
# Separate the data into classes based on 'y'9 a# }0 P0 n0 A* P
class_0 = np.array([X[i] for i in range(len(X)) if y[i]==0])4 C5 o1 p0 k3 W+ D
class_1 = np.array([X[i] for i in range(len(X)) if y[i]==1]); o) ^# L) k$ v) u, E7 p
# B& ~8 L7 W8 X; ^" E# Plot the input data) \3 _: q8 |: q2 n4 Q3 y9 E+ P
plt.figure()9 l" k8 s" ], Y! K/ H
plt.scatter(class_0[:,0], class_0[:,1], facecolors='black', edgecolors='black', marker='s')
( B# b b9 F7 g9 X( G6 Fplt.scatter(class_1[:,0], class_1[:,1], facecolors='None', edgecolors='black', marker='s'); y. g" E* O5 x& t. M1 a ~
plt.title('Input data')
`3 p2 {; T! D) ?4 P' k7 N- ]& Q5 {0 F: b: ?2 A( B( e
###############################################
/ G0 d# j [2 ^* ^# Train test split and SVM training5 x$ g, j: Y7 y( m! b
from sklearn import cross_validation
: P: V. t! w2 R2 L4 Jfrom sklearn.svm import SVC
# ?7 ?7 q# D2 ^! S5 |& @. G1 R! E/ L9 H7 m2 j$ J
X_train, X_test, y_train, y_test = cross_validation.train_test_split(X, y, test_size=0.25, random_state=5)/ [' f& b4 @" `7 `$ j+ ^ r
' K- z$ i( f/ S! e1 E* p, n
#params = {'kernel': 'linear'}3 d/ N1 ?& k- @9 F: m( L# C* ~
#params = {'kernel': 'poly', 'degree': 3}
5 |: X2 ] d3 ]8 k/ jparams = {'kernel': 'rbf'}7 t* P: P3 g, U5 e2 U
classifier = SVC(**params)
5 [2 R# O6 J3 u* H8 hclassifier.fit(X_train, y_train)) o& J* R) `2 v" G T
utilities.plot_classifier(classifier, X_train, y_train, 'Training dataset')
$ |6 f) ~3 r1 `: h! _
6 T, J; t/ O, ] o* V' ay_test_pred = classifier.predict(X_test)& {6 H9 a% d, `0 A+ W1 Y. {6 j
utilities.plot_classifier(classifier, X_test, y_test, 'Test dataset')
. `0 d5 T+ t6 }2 d8 ?4 e5 H2 _- T1 x
############################################### Q' m( o# \& N B) @' U0 a
# Evaluate classifier performance
d8 Q2 D4 p5 g& F- g& N) @- R" G2 C4 d
from sklearn.metrics import classification_report5 D1 p L$ J, U% v- h3 t
@( t: P7 A* E6 L2 M
target_names = ['Class-' + str(int(i)) for i in set(y)]6 D5 D6 s* Z+ h% ?2 x7 ]) F
print "\n" + "#"*30% @. [: H$ p5 F: K9 K
print "\nClassifier performance on training dataset\n"
7 _9 l, y }9 {$ rprint classification_report(y_train, classifier.predict(X_train), target_names=target_names)
0 V0 w9 b! V1 f" t" K }" `print "#"*30 + "\n"3 I0 Z+ E1 w( ]6 |# g S/ E
3 o) m9 ?9 E& ]# i( pprint "#"*30
' ?! d- F+ t) Aprint "\nClassification report on test dataset\n"# T3 ^' H4 p; r. i
print classification_report(y_test, y_test_pred, target_names=target_names)% R s* w7 ?8 |3 d
print "#"*30 + "\n" V3 @5 T- ~" C- r% S
; Y/ u' |0 y9 c9 d( v/ Y |
|