- 金錢
- 45
- 威望
- 3183
- 貢獻值
- 0
- 推廣值
- 0
- 性別
- 保密
- 在線時間
- 38 小時
- 最後登錄
- 2024-2-25
- 主題
- 0
- 精華
- 0
- 閱讀權限
- 70
- 註冊時間
- 2012-3-17
- 帖子
- 553
 
該用戶從未簽到 - 推廣值
- 0
- 貢獻值
- 0
- 金錢
- 45
- 威望
- 3183
- 主題
- 0
|
import numpy as np' y; w3 w& B& ~8 Y/ p" s0 Y
import matplotlib.pyplot as plt
0 u* K/ w- d! k* Z0 l6 n
0 X$ W( U/ @# _import utilities # a7 o$ p8 H. l
. A$ b, V! o3 O$ \& n' z# Load input data
; y( y5 g- j' j2 I, H8 ginput_file = 'D:\\1.Modeling material\\Py_Study\\2.code_model\\Python-Machine-Learning-Cookbook\\Python-Machine-Learning-Cookbook-master\\Chapter03\\data_multivar.txt', o$ S( w9 Y* P, ?4 V2 v- A; S
X, y = utilities.load_data(input_file)9 l7 i D2 {2 E( z) `; q2 C8 n
8 l: @5 `2 Y2 K2 d###############################################( g6 X2 n0 U. x# h) [
# Separate the data into classes based on 'y'$ u; \! _+ v# h$ C) T+ \
class_0 = np.array([X[i] for i in range(len(X)) if y[i]==0])
- `: \8 q+ p" I% Yclass_1 = np.array([X[i] for i in range(len(X)) if y[i]==1]); ]; m Y0 @6 ^% m0 W
! N, K2 N/ i' s& l
# Plot the input data$ a$ y2 \0 X, o* n; U7 H
plt.figure(). n7 U3 ?9 Y l8 B# ^& T) j
plt.scatter(class_0[:,0], class_0[:,1], facecolors='black', edgecolors='black', marker='s')% Y- N5 ?/ p& q( A
plt.scatter(class_1[:,0], class_1[:,1], facecolors='None', edgecolors='black', marker='s')
: c1 R. L3 L' j( x3 U0 vplt.title('Input data')( o X3 s) W! w8 V- D
) ]# V9 u+ i3 U; S! ~###############################################5 Q. \' c1 E8 ?; z n# \$ {
# Train test split and SVM training
7 k9 ]& j! {) r# F' }from sklearn import cross_validation
% y2 ~* U8 U2 ~from sklearn.svm import SVC* X9 i' Y$ V ^- v
2 I* N/ Y7 \+ [X_train, X_test, y_train, y_test = cross_validation.train_test_split(X, y, test_size=0.25, random_state=5)
1 ] {+ u8 ^$ c4 r& Q! N0 q( i' Z0 ^' l
#params = {'kernel': 'linear'}" I$ {6 @' e, q" x/ s" _9 P* j
#params = {'kernel': 'poly', 'degree': 3}
* c, \# `, [# G) I1 Gparams = {'kernel': 'rbf'}$ d; G2 y8 r5 ^, k3 s
classifier = SVC(**params)/ ^- ]' u3 d* Y2 O8 w5 F6 Z
classifier.fit(X_train, y_train)
2 g; s8 w6 W& ^7 L8 N3 a4 qutilities.plot_classifier(classifier, X_train, y_train, 'Training dataset')
# W+ R8 r2 u2 Z
- l' z# C2 }: s7 s# d" y ?7 py_test_pred = classifier.predict(X_test)
) u9 h* j2 `" @& S0 H) e. U1 Z$ Autilities.plot_classifier(classifier, X_test, y_test, 'Test dataset')! [& H% L g# u8 d
: y" B' g1 `& Y1 w3 R, q5 g/ A
###############################################
* h% W0 }4 p. ]" B- i# Evaluate classifier performance
# O: Q1 s2 n4 a6 s! g; V
; A/ q9 x) `& h( v) jfrom sklearn.metrics import classification_report
9 V" I3 H4 k6 s( k4 h
- j \* @" C9 ?: w! g: p1 J! ltarget_names = ['Class-' + str(int(i)) for i in set(y)]
- W# U3 {' z) B! ~+ ]* u& Hprint "\n" + "#"*30( w A" }# z. k: u
print "\nClassifier performance on training dataset\n"
4 ?* w3 n' F z) s7 P8 d& vprint classification_report(y_train, classifier.predict(X_train), target_names=target_names)
7 d+ F0 Y8 f" ^' I( Fprint "#"*30 + "\n"
5 o7 s: O+ d, Q/ s, {. U) K
* f2 ]2 t( B7 z, H% A! V' Jprint "#"*30
& O% F5 I, r) L/ e7 W3 r8 x5 @- Hprint "\nClassification report on test dataset\n") e, T0 L' V* h. \9 V
print classification_report(y_test, y_test_pred, target_names=target_names)4 ^1 `, `$ e. u
print "#"*30 + "\n"
2 ~4 Z# V2 C0 Q6 h( Y* _' S$ \/ Y! L5 D; y7 C' F4 U
|
|