目錄
前言
一、實(shí)驗(yàn)?zāi)康?/p>
二、實(shí)驗(yàn)環(huán)境
三、實(shí)驗(yàn)內(nèi)容與結(jié)果
1、SVM(support vector Machine)是什么?
2、SVM能干什么?
3、SVM如何實(shí)現(xiàn)?
4、獨(dú)熱編碼:獨(dú)熱編碼(One-Hot Encoding) - 知乎
5、?隨機(jī)森林算法的基本原理
四、模型構(gòu)建
1、讀入數(shù)據(jù)
2、數(shù)據(jù)初始化
3、訓(xùn)練模型,評(píng)價(jià)分類器性能
4、將數(shù)據(jù)集拆分為訓(xùn)練集和測試集,在測試集上查看分類效果
5、數(shù)據(jù)處理
總結(jié)
前言
隨著人工智能的不斷發(fā)展,機(jī)器學(xué)習(xí)這門技術(shù)也越來越重要,很多人都開啟了學(xué)習(xí)機(jī)器學(xué)習(xí),本文就介紹了機(jī)器學(xué)習(xí)的基礎(chǔ)內(nèi)容。
一、實(shí)驗(yàn)?zāi)康?/h2>
- 掌握機(jī)器學(xué)習(xí)建模分析
- 掌握回歸分析、分類分析、聚類分析、降維等
- 了解各分類器之間的差異
二、實(shí)驗(yàn)環(huán)境
-
??操作系統(tǒng):Windows
- ??應(yīng)用軟件:anaconda jupyter
三、實(shí)驗(yàn)內(nèi)容與結(jié)果
1、SVM(support vector Machine)是什么?
支持向量機(jī)是基于數(shù)學(xué)優(yōu)化方法的分類學(xué)習(xí)算法
? 通過使用最大分類間隔(Margin)來確定最優(yōu)的最優(yōu)的劃分超平面,以獲得良好的泛化能力
? 通過核函數(shù)的方法將低維數(shù)據(jù)映射到高維空間,并使得在高維空間的數(shù)據(jù)是線性可分的,從而能夠處理低維空間中線性不可分的情況
具體理解可參考以下鏈接:[白話解析] 深入淺出支持向量機(jī)(SVM)之核函數(shù) - 騰訊云開發(fā)者社區(qū)-騰訊云
2、SVM能干什么?
? SVM最基本的應(yīng)用:分類
? ? ?求解一個(gè)最優(yōu)的分類面,將數(shù)據(jù)集分割為兩個(gè)的子集
? ? ?數(shù)據(jù)集在低維空間中無法使用超平面劃分
? ? ?映射到高維空間,尋找超平面分割
?3、SVM如何實(shí)現(xiàn)?
SVM采用核函數(shù)(Kernel Function)將低維數(shù)據(jù)映射到高維空間
? ? ?多種核函數(shù),適應(yīng)不同特性的數(shù)據(jù)集,影響SVM分類性能的關(guān)鍵因素
? ? ?常用的核函數(shù):線性核、多項(xiàng)式核、高斯核和sigmoid核等
4、獨(dú)熱編碼:獨(dú)熱編碼(One-Hot Encoding) - 知乎
可以大概這么理解:平等地位的就獨(dú)熱編碼,有大小順序的就標(biāo)簽編碼;
其實(shí)嚴(yán)格來說性別也應(yīng)該獨(dú)熱編碼,因?yàn)樗麄兪瞧降鹊摹?/p>
5、?隨機(jī)森林算法的基本原理
核心思想是“三個(gè)臭皮匠,頂個(gè)諸葛亮”
? 通過隨機(jī)的方式建立一個(gè)森林
? 每棵樹都是由從訓(xùn)練集中抽取的部分樣本,且基于部分隨機(jī)選擇的特征子集訓(xùn)練構(gòu)建
? 預(yù)測未知數(shù)據(jù)時(shí),多個(gè)決策樹投票決定最終結(jié)果:如果是數(shù)值形的輸出,則采取多個(gè)決策樹結(jié)果的平均或者加權(quán)作為最終輸出;如果是分類任務(wù),則采取投票機(jī)制或者是加權(quán)作為最終輸出。?
四、模型構(gòu)建
例題
1.使用scikit-learn建立SVM模型為葡萄酒數(shù)據(jù)集構(gòu)造分類器(分類結(jié)果為’good’或‘not’ ) [“不可使用quantity”列]
2.評(píng)估分類器在此數(shù)據(jù)集上的分類性能* 需要?jiǎng)澐钟?xùn)練集和測試集
1、讀入數(shù)據(jù)
原始數(shù)據(jù)共有3899條。?
代碼如下:
import pandas as pd
filename='data\wine.csv'
data=pd.read_csv(filename,index_col='idx')
data.loc[data['good_or_not']=='good','good_or_not']=1
data.loc[data['good_or_not']=='not','good_or_not']=0
data.drop('quality',axis=1,inplace=True)
print(data[0:5])
?2、數(shù)據(jù)初始化
代碼如下(示例):
from sklearn.model_selection import train_test_split
from sklearn.svm import SVC
from sklearn.preprocessing import MinMaxScaler
x = data.drop('good_or_not', axis=1).values.astype(float)
y = data['good_or_not'].values.astype(float)
print(type(x),type(y))
3、訓(xùn)練模型,評(píng)價(jià)分類器性能
from sklearn import svm
clf = svm.SVC(kernel='rbf', gamma=0.6, C=100)
clf.fit(x,y)
print('Accuracy = ', clf.score(x, y))
y_pred = clf.predict(x)
from sklearn import metrics
print(metrics.classification_report( y, y_pred) )
?
?4、將數(shù)據(jù)集拆分為訓(xùn)練集和測試集,在測試集上查看分類效果
from sklearn import svm
from sklearn import model_selection
x_train, x_test, y_train, y_test = model_selection.train_test_split(x, y, test_size=0.2,random_state=1)
clf = svm.SVC(kernel='rbf',gamma=0.7, C=1)
clf.fit(x_train, y_train)
b = clf.score(x_train, y_train)
print("訓(xùn)練集準(zhǔn)確率:",b)
a = clf.score(x_test, y_test)
print("測試集準(zhǔn)確率:",a)
?
?5、數(shù)據(jù)處理
from sklearn import preprocessing
from sklearn import model_selection
% 對(duì)不同方差的數(shù)據(jù)標(biāo)準(zhǔn)化
x_scale = preprocessing.scale(x)
%將標(biāo)準(zhǔn)化后的數(shù)據(jù)集拆分為訓(xùn)練集和測試集,在測試集上查看分類效果
from sklearn import svm
x_train, x_test, y_train, y_test = model_selection.train_test_split(x_scale, y, test_size=0.2,random_state=1)
clf = svm.SVC(kernel='rbf',gamma=0.7, C=30)
clf.fit(x_train, y_train)
y_pred = clf.predict(x_test)
a = clf.score(x_test, y_test)
print(a)
print(metrics.classification_report(y_test, y_pred))
print(metrics.confusion_matrix(y_test, y_pred))
3.使用scikit-learn建立隨機(jī)森林回歸模型預(yù)測葡萄酒質(zhì)量(1-10之間)[“不可使用good_or_not”列]
step 1. 從文件中讀入數(shù)據(jù),進(jìn)行預(yù)處理,將所有特征轉(zhuǎn)換為數(shù)值型?
import numpy as np
import pandas as pd
filename='data\wine.csv'
data=pd.read_csv(filename,index_col='idx')
data.drop('good_or_not',axis=1,inplace=True)
x = data.drop('quality', axis=1).values.astype(float)
y = data['quality'].values.astype(float)
step 2. 從DataFrame對(duì)象中取出特征矩陣X和分類標(biāo)簽y,無需進(jìn)行歸一化處理
%劃分測試集和訓(xùn)練集
from sklearn import model_selection
x_train, x_test, y_train, y_test = model_selection.train_test_split(x,y, test_size=0.3, random_state=1)
step 3. 使用隨機(jī)森林算法訓(xùn)練集成分類器
參數(shù)n_ estimators和max_depth的設(shè)置直接影響模型的性能
且不同的數(shù)據(jù)集取值差別較大,通常通過搜索的方式找出合適的值
from sklearn.ensemble import RandomForestClassifier
%固定決策樹個(gè)數(shù),搜索最大深度max_depth在給定范圍內(nèi)的最優(yōu)取值
%從1到10中探索最優(yōu)深度
d_score = []
for i in range(1,10):
RF = RandomForestClassifier(n_estimators=15, criterion='entropy', max_depth=i)
RF.fit(x_train, y_train)
d_score.append(RF.score(x_test, y_test))
depth = d_score.index(max(d_score)) #列表求最大值的索引
print(depth,d_score[depth])
% 按最優(yōu)深度,搜索最優(yōu)決策樹個(gè)樹n_estimators
% 從1到21中探索最優(yōu)決策樹的數(shù)目
e_score = []
for i in range(1,21):
RF = RandomForestClassifier(n_estimators=i, criterion='entropy', max_depth=depth)
RF.fit(x_train, y_train)
e_score.append(RF.score(x_test, y_test))
est = e_score.index(max(e_score))
print(est,e_score[est])
?
%雙層搜索
scores = [] % 記錄深度
pos = [] %記錄決策樹數(shù)目
for i in range(1, 10): %深度
temp = []
for j in range(1, 40): % 決策樹數(shù)目
RF = RandomForestClassifier(n_estimators=j, criterion='entropy', max_depth=i)
RF.fit(x_train, y_train)
temp.append(RF.score(x_test, y_test))
scores.append(max(temp))% 存儲(chǔ)這21個(gè)中表現(xiàn)最好的模型的scores
pos.append(temp.index(max(temp))) % 存儲(chǔ)表現(xiàn)最好的模型的決策樹數(shù)目
max_scores = max(scores) % 找出每種深度下的所有模型的最好模型
si = scores.index(max(scores)) % 該最好模型對(duì)應(yīng)的決策樹的數(shù)目
depth = pos[si]
print(max_scores, depth)
?文章來源:http://www.zghlxwxcb.cn/news/detail-424912.html
總結(jié)
以上就是今天要講的內(nèi)容,本文僅僅簡單介紹了使用SVM模型對(duì)葡萄酒的數(shù)據(jù)進(jìn)行回歸分析的使用,SVM的算法添加了限制條件,來保證盡可能減少不可分割的點(diǎn)的影響,使分割達(dá)到相對(duì)最優(yōu)。文章來源地址http://www.zghlxwxcb.cn/news/detail-424912.html
到了這里,關(guān)于為葡萄酒數(shù)據(jù)集構(gòu)造SVM分類器和使用隨機(jī)森林回歸模型預(yù)測葡萄酒質(zhì)量的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!