需要全部源碼和數(shù)據(jù)集請(qǐng)點(diǎn)贊關(guān)注收藏后評(píng)論區(qū)留言私信~~~
SVM簡(jiǎn)介
支持向量機(jī)(support vector machines, SVM)是一種二分類模型,它的基本模型是定義在特征空間上的間隔最大的線性分類器,間隔最大使它有別于感知機(jī);SVM還包括核技巧,這使它成為實(shí)質(zhì)上的非線性分類器。SVM的的學(xué)習(xí)策略就是間隔最大化,可形式化為一個(gè)求解凸二次規(guī)劃的問題,也等價(jià)于正則化的合頁損失函數(shù)的最小化問題。SVM的的學(xué)習(xí)算法就是求解凸二次規(guī)劃的最優(yōu)化算法。
非線性SVM算法原理
對(duì)于輸入空間中的非線性分類問題,可以通過非線性變換將它轉(zhuǎn)化為某個(gè)維特征空間中的線性分類問題,在高維特征空間中學(xué)習(xí)線性支持向量機(jī)。由于在線性支持向量機(jī)學(xué)習(xí)的對(duì)偶問題里,目標(biāo)函數(shù)和分類決策函數(shù)都只涉及實(shí)例和實(shí)例之間的內(nèi)積,所以不需要顯式地指定非線性變換,而是用核函數(shù)替換當(dāng)中的內(nèi)積。核函數(shù)表示,通過一個(gè)非線性轉(zhuǎn)換后的兩個(gè)實(shí)例間的內(nèi)積
SVM學(xué)習(xí)的基本想法是求解能夠正確劃分訓(xùn)練數(shù)據(jù)集并且?guī)缀伍g隔最大的分離超平面。如下圖所示,?w?x+b=0?即為分離超平面,對(duì)于線性可分的數(shù)據(jù)集來說,這樣的超平面有無窮多個(gè)(即感知機(jī)),但是幾何間隔最大的分離超平面卻是唯一的。
?SVM解決非線性問題
實(shí)驗(yàn)中用到半環(huán)形數(shù)據(jù)集
結(jié)果如下 SVM算法較好的分開了兩個(gè)區(qū)域,強(qiáng)于聚類算法
?部分代碼如下
import numpy as np
import matplotlib.pyplot as plt
from sklearn.datasets import make_moons
from sklearn.preprocessing import PolynomialFeatures
from sklearn.preprocessing import StandardScaler
from sklearn.svm import LinearSVC
from sklearn.pipeline import Pipeline
# 生成半環(huán)形數(shù)據(jù)
X, y = make_moons(n_samples=100, noise=0.1, random_state=1)
moonAxe=[-1.5, 2.5, -1, 1.5] #moons數(shù)據(jù)集的區(qū)間
# 顯示數(shù)據(jù)樣本
def dispData(x, y, moonAxe):
pos_x0=[x[i,0]for i in range(len(y)) if y[i]==1]
pos_x1=[x[i,1]for i in range(len(y)) if y[i]==1]
neg_x0=[x[i,0]for i in range(len(y)) if y[i]==0]
neg_x1=[x[i,1]for i in range(len(y)) if y[i]==0]
plt.plot(pos_x0, pos_x1, "bo")
plt.plot(neg_x0, neg_x1, "r^")
plt.axis(moonAxe)
plt.xlabel("x")
plt.ylabel("y")
# 顯示決策線
def dispPredict(clf, moonAxe):
#生成區(qū)間內(nèi)的數(shù)據(jù)
d0 = np.linspace(moonAxe[0], moonAxe[1], 200)
d1 = np.linspace(moonAxe[2], moonAxe[3], 200)
xntourf(x0, x1, y_pred, alpha=0.8)
# 1.顯示樣本
dispData(X, y, moonAxe)
# 2.構(gòu)建模型組合,整合三個(gè)函數(shù)
polynomial_svm_clf=Pipeline(
(("multiFeature",PolynomialFeatures(degree=3)),
("NumScale",StandardScaler()),
("SVC",LinearSVC(C=100)))
)
# 3.使用模型組合進(jìn)行訓(xùn)練
poly類線
dispPredict(polynomial_svm_clf, moonAxe)
# 5.顯示圖表標(biāo)題
plt.title('Linear SVM classifies Moons data')
plt.show()
使用SVM進(jìn)行信用卡欺詐檢測(cè)
讀取數(shù)據(jù)如下
數(shù)據(jù)可視化如下?
?
打印出數(shù)據(jù)中年齡大于70歲的人群信息
?
預(yù)測(cè)結(jié)果如下
大部分人進(jìn)行信用卡欺詐的概率還是比較低 精度可以達(dá)到百分之九十三左右
?
部分代碼如下文章來源:http://www.zghlxwxcb.cn/news/detail-464022.html
import pandas as pd
from sklearn.preprocessing import StandardScaler
from sklearn.model_selection import train_test_split
#(1) 載入數(shù)據(jù)
data = pd.read_csv("data/KaggleCredit2.csv",index_col= 0)
data.dropna(inplace=True)
#(2)對(duì)特征列進(jìn)行標(biāo)準(zhǔn)化
cols = data.columns[1:]
ss = StandardScaler()
data[cols] = ss.fit_transform(data[cols])
#(3)構(gòu)造數(shù)據(jù)和標(biāo)簽
X = data.drop('SeriousDlqin2yrs', axis=1) # 數(shù)據(jù)特征
y = data['SeriousDlqin2yrs'] #標(biāo)簽列
#(4)進(jìn)行數(shù)據(jù)切分,測(cè)試集占比30%,生成隨機(jī)數(shù)的種子是0
X_train,X_tes
#(5)構(gòu)建SVM模型
#只使用特征“NumberOfTime60-89DaysPastDueNotWorse”進(jìn)行SVM分類
from sklearn.svm import SVC
svm = SVC()
svm.fit(X_train[['NumberOfTime60-89DaysPastDueNotWorse']], y_train)
# svm.fit(X_train, y_train) 此句使用的是全部特征,時(shí)間耗費(fèi)長(zhǎng)
93%
svm.score(X_test[['NumberOfTime60-89DaysPastDueNotWorse']], y_test)
?創(chuàng)作不易 覺得有幫助請(qǐng)點(diǎn)贊關(guān)注收藏~~~文章來源地址http://www.zghlxwxcb.cn/news/detail-464022.html
到了這里,關(guān)于【Python機(jī)器學(xué)習(xí)】SVM解決非線性問題和信用卡欺詐檢測(cè)實(shí)戰(zhàn)(附源碼和數(shù)據(jù)集)的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!