基于 Python 實(shí)踐感知器分類算法

Perceptron分類器是一種線性算法,可以應(yīng)用于二進(jìn)制分類任務(wù)。 如何使用帶有Scikit-Learn的Perceptron模型進(jìn)行擬合,評估和做出預(yù)測。 如何在給定的數(shù)據(jù)集上調(diào)整Perceptron算法的超參數(shù)。
感知器算法 Perceptron與Scikit-學(xué)習(xí) 音調(diào)感知器超參數(shù)
激活=權(quán)重*輸入+偏差如果激活> 0.0如果激活<= 0.0權(quán)重(t + 1)=權(quán)重(t)+學(xué)習(xí)率*(expected_i –預(yù)測值)* input_i#?define?model
model?=?Perceptron(eta0=1.0)
#?define?model
model?=?Perceptron(max_iter=1000)
#?test?classification?dataset
from?sklearn.datasets?import?make_classification
#?define?dataset
X,?y?=?make_classification(n_samples=1000,?n_features=10,?n_informative=10,?n_redundant=0,?random_state=1)
#?summarize?the?dataset
print(X.shape,?y.shape)
(1000,?10)?(1000,)
RepeatedStratifiedKFold類使用重復(fù)的分層k折交叉驗(yàn)證來擬合和評估Perceptron模型。我們將在測試裝置中使用10折和3次重復(fù)。#?create?the?model
model?=?Perceptron()
#?evaluate?a?perceptron?model?on?the?dataset
from?numpy?import?mean
from?numpy?import?std
from?sklearn.datasets?import?make_classification
from?sklearn.model_selection?import?cross_val_score
from?sklearn.model_selection?import?RepeatedStratifiedKFold
from?sklearn.linear_model?import?Perceptron
#?define?dataset
X,?y?=?make_classification(n_samples=1000,?n_features=10,?n_informative=10,?n_redundant=0,?random_state=1)
#?define?model
model?=?Perceptron()
#?define?model?evaluation?method
cv?=?RepeatedStratifiedKFold(n_splits=10,?n_repeats=3,?random_state=1)
#?evaluate?model
scores?=?cross_val_score(model,?X,?y,?scoring='accuracy',?cv=cv,?n_jobs=-1)
#?summarize?result
print('Mean?Accuracy:?%.3f?(%.3f)'?%?(mean(scores),?std(scores)))
Mean?Accuracy:?0.847?(0.052)
predict()函數(shù)來實(shí)現(xiàn)。我們可以通過下面列出的完整示例進(jìn)行演示。#?make?a?prediction?with?a?perceptron?model?on?the?dataset
from?sklearn.datasets?import?make_classification
from?sklearn.linear_model?import?Perceptron
#?define?dataset
X,?y?=?make_classification(n_samples=1000,?n_features=10,?n_informative=10,?n_redundant=0,?random_state=1)
#?define?model
model?=?Perceptron()
#?fit?model
model.fit(X,?y)
#?define?new?data
row?=?[0.12777556,-3.64400522,-2.23268854,-1.82114386,1.75466361,0.1243966,1.03397657,2.35822076,1.01001752,0.56768485]
#?make?a?prediction
yhat?=?model.predict([row])
#?summarize?prediction
print('Predicted?Class:?%d'?%?yhat)
Predicted?Class:?1
#?define?grid
grid?=?dict()
grid['eta0']?=?[0.0001,?0.001,?0.01,?0.1,?1.0]
#?grid?search?learning?rate?for?the?perceptron
from?sklearn.datasets?import?make_classification
from?sklearn.model_selection?import?GridSearchCV
from?sklearn.model_selection?import?RepeatedStratifiedKFold
from?sklearn.linear_model?import?Perceptron
#?define?dataset
X,?y?=?make_classification(n_samples=1000,?n_features=10,?n_informative=10,?n_redundant=0,?random_state=1)
#?define?model
model?=?Perceptron()
#?define?model?evaluation?method
cv?=?RepeatedStratifiedKFold(n_splits=10,?n_repeats=3,?random_state=1)
#?define?grid
grid?=?dict()
grid['eta0']?=?[0.0001,?0.001,?0.01,?0.1,?1.0]
#?define?search
search?=?GridSearchCV(model,?grid,?scoring='accuracy',?cv=cv,?n_jobs=-1)
#?perform?the?search
results?=?search.fit(X,?y)
#?summarize
print('Mean?Accuracy:?%.3f'?%?results.best_score_)
print('Config:?%s'?%?results.best_params_)
#?summarize?all
means?=?results.cv_results_['mean_test_score']
params?=?results.cv_results_['params']
for?mean,?param?in?zip(means,?params):
????print(">%.3f?with:?%r"?%?(mean,?param))
Mean?Accuracy:?0.857
Config:?{'eta0':?0.0001}
>0.857?with:?{'eta0':?0.0001}
>0.857?with:?{'eta0':?0.001}
>0.853?with:?{'eta0':?0.01}
>0.847?with:?{'eta0':?0.1}
>0.847?with:?{'eta0':?1.0}
#?define?grid
grid?=?dict()
grid['max_iter']?=?[1,?10,?100,?1000,?10000]
#?define?model
model?=?Perceptron(eta0=0.0001)
#?grid?search?total?epochs?for?the?perceptron
from?sklearn.datasets?import?make_classification
from?sklearn.model_selection?import?GridSearchCV
from?sklearn.model_selection?import?RepeatedStratifiedKFold
from?sklearn.linear_model?import?Perceptron
#?define?dataset
X,?y?=?make_classification(n_samples=1000,?n_features=10,?n_informative=10,?n_redundant=0,?random_state=1)
#?define?model
model?=?Perceptron(eta0=0.0001)
#?define?model?evaluation?method
cv?=?RepeatedStratifiedKFold(n_splits=10,?n_repeats=3,?random_state=1)
#?define?grid
grid?=?dict()
grid['max_iter']?=?[1,?10,?100,?1000,?10000]
#?define?search
search?=?GridSearchCV(model,?grid,?scoring='accuracy',?cv=cv,?n_jobs=-1)
#?perform?the?search
results?=?search.fit(X,?y)
#?summarize
print('Mean?Accuracy:?%.3f'?%?results.best_score_)
print('Config:?%s'?%?results.best_params_)
#?summarize?all
means?=?results.cv_results_['mean_test_score']
params?=?results.cv_results_['params']
for?mean,?param?in?zip(means,?params):
????print(">%.3f?with:?%r"?%?(mean,?param))
Mean?Accuracy:?0.857
Config:?{'max_iter':?10}
>0.850?with:?{'max_iter':?1}
>0.857?with:?{'max_iter':?10}
>0.857?with:?{'max_iter':?100}
>0.857?with:?{'max_iter':?1000}
>0.857?with:?{'max_iter':?10000}
作者:沂水寒城,CSDN博客專家,個人研究方向:機(jī)器學(xué)習(xí)、深度學(xué)習(xí)、NLP、CV
Blog:?http://yishuihancheng.blog.csdn.net
贊 賞 作 者

更多閱讀
特別推薦

點(diǎn)擊下方閱讀原文加入社區(qū)會員
評論
圖片
表情
