自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

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

開發(fā) 后端 算法
在本教程中,您將發(fā)現(xiàn)Perceptron分類機(jī)器學(xué)習(xí)算法。來看一下吧。

 Perceptron是用于二進(jìn)制分類任務(wù)的線性機(jī)器學(xué)習(xí)算法。它可以被認(rèn)為是人工神經(jīng)網(wǎng)絡(luò)的第一種和最簡單的類型之一。絕對不是“深度”學(xué)習(xí),而是重要的組成部分。與邏輯回歸相似,它可以快速學(xué)習(xí)兩類分類任務(wù)在特征空間中的線性分離,盡管與邏輯回歸不同,它使用隨機(jī)梯度下降優(yōu)化算法學(xué)習(xí)并且不預(yù)測校準(zhǔn)概率。

在本教程中,您將發(fā)現(xiàn)Perceptron分類機(jī)器學(xué)習(xí)算法。完成本教程后,您將知道:

  •  Perceptron分類器是一種線性算法,可以應(yīng)用于二進(jìn)制分類任務(wù)。
  •  如何使用帶有Scikit-Learn的Perceptron模型進(jìn)行擬合,評估和做出預(yù)測。
  •  如何在給定的數(shù)據(jù)集上調(diào)整Perceptron算法的超參數(shù)。

教程概述

本教程分為3個部分,共三個部分。他們是:

  •  感知器算法
  •  Perceptron與Scikit-學(xué)習(xí)
  •  音調(diào)感知器超參數(shù)

感知器算法

Perceptron算法是兩類(二進(jìn)制)分類機(jī)器學(xué)習(xí)算法。它是一種神經(jīng)網(wǎng)絡(luò)模型,可能是最簡單的神經(jīng)網(wǎng)絡(luò)模型類型。它由將一行數(shù)據(jù)作為輸入并預(yù)測類標(biāo)簽的單個節(jié)點(diǎn)或神經(jīng)元組成。這可以通過計(jì)算輸入的加權(quán)和和偏差(設(shè)置為1)來實(shí)現(xiàn)。模型輸入的加權(quán)總和稱為激活。

激活=權(quán)重*輸入+偏差

如果激活高于0.0,則模型將輸出1.0;否則,模型將輸出1.0。否則,將輸出0.0。

預(yù)測1:如果激活> 0.0

預(yù)測0:如果激活<= 0.0

假設(shè)輸入已乘以模型系數(shù),如線性回歸和邏輯回歸,則優(yōu)良作法是在使用模型之前對數(shù)據(jù)進(jìn)行標(biāo)準(zhǔn)化或標(biāo)準(zhǔn)化。感知器是線性分類算法。這意味著它將學(xué)習(xí)在特征空間中使用一條線(稱為超平面)將兩個類別分開的決策邊界。因此,適用于那些類別可以通過線性或線性模型(稱為線性可分離)很好地分離的問題。該模型的系數(shù)稱為輸入權(quán)重,并使用隨機(jī)梯度下降優(yōu)化算法進(jìn)行訓(xùn)練。一次將來自訓(xùn)練數(shù)據(jù)集的示例顯示給模型,模型進(jìn)行預(yù)測并計(jì)算誤差。然后,更新模型的權(quán)重以減少示例的誤差。這稱為Perceptron更新規(guī)則。對于訓(xùn)練數(shù)據(jù)集中的所有示例(稱為時期)都重復(fù)此過程。然后,使用示例更新模型的過程會重復(fù)很多次。在每批中,使用較小比例的誤差來更新模型權(quán)重,并且該比例由稱為學(xué)習(xí)率的超參數(shù)控制,通常將其設(shè)置為較小的值。這是為了確保學(xué)習(xí)不會太快發(fā)生,從而導(dǎo)致技能水平可能較低,這被稱為模型權(quán)重的優(yōu)化(搜索)過程的過早收斂。

權(quán)重(t + 1)=權(quán)重(t)+學(xué)習(xí)率*(expected_i –預(yù)測值)* input_i

當(dāng)模型所產(chǎn)生的誤差降至較低水平或不再改善時,或者執(zhí)行了最大時期數(shù)時,訓(xùn)練將停止。

模型權(quán)重的初始值設(shè)置為較小的隨機(jī)值。另外,在每個訓(xùn)練紀(jì)元之前對訓(xùn)練數(shù)據(jù)集進(jìn)行混洗。這是設(shè)計(jì)使然,以加速和改善模型訓(xùn)練過程。因此,學(xué)習(xí)算法是隨機(jī)的,并且每次運(yùn)行都會獲得不同的結(jié)果。因此,優(yōu)良作法是使用重復(fù)評估來總結(jié)算法在數(shù)據(jù)集上的性能,并報(bào)告平均分類精度。學(xué)習(xí)率和訓(xùn)練時期數(shù)是算法的超參數(shù),可以使用啟發(fā)式或超參數(shù)調(diào)整來設(shè)置。

現(xiàn)在我們已經(jīng)熟悉了Perceptron算法,現(xiàn)在讓我們探索如何在Python中使用該算法。

Perceptron 與 Scikit-Learn

可通過Perceptron類在scikit-learn Python機(jī)器學(xué)習(xí)庫中使用Perceptron算法。該類允許您配置學(xué)習(xí)率(eta0),默認(rèn)為1.0。 

  1. # define model  
  2. model = Perceptron(eta0=1.0) 

該實(shí)現(xiàn)還允許您配置訓(xùn)練時期的總數(shù)(max_iter),默認(rèn)為1,000。 

  1. # define model  
  2. model = Perceptron(max_iter=1000

Perceptron算法的scikit-learn實(shí)現(xiàn)還提供了您可能想探索的其他配置選項(xiàng),例如提前停止和使用懲罰損失。我們可以通過一個有效的示例來演示Perceptron分類器。首先,讓我們定義一個綜合分類數(shù)據(jù)集。我們將使用make_classification()函數(shù)創(chuàng)建一個包含1,000個示例的數(shù)據(jù)集,每個示例包含20個輸入變量。該示例創(chuàng)建并匯總了數(shù)據(jù)集。 

  1. # test classification dataset  
  2. from sklearn.datasets import make_classification  
  3. # define dataset  
  4. X, y = make_classification(n_samples=1000n_features=10n_informative=10n_redundant=0random_state=1 
  5. # summarize the dataset  
  6. print(X.shape, y.shape) 

運(yùn)行示例將創(chuàng)建數(shù)據(jù)集并確認(rèn)數(shù)據(jù)集的行數(shù)和列數(shù)。 

  1. (1000, 10) (1000,) 

我們可以通過 RepeatedStratifiedKFold類使用重復(fù)的分層k折交叉驗(yàn)證來擬合和評估Perceptron模型。我們將在測試裝置中使用10折和3次重復(fù)。 

  1. # create the model  
  2. model = Perceptron() 

下面列出了為綜合二進(jìn)制分類任務(wù)評估Perceptron模型的完整示例。 

  1. # evaluate a perceptron model on the dataset  
  2. from numpy import mean  
  3. from numpy import std  
  4. from sklearn.datasets import make_classification  
  5. from sklearn.model_selection import cross_val_score  
  6. from sklearn.model_selection import RepeatedStratifiedKFold  
  7. from sklearn.linear_model import Perceptron  
  8. # define dataset  
  9. X, y = make_classification(n_samples=1000n_features=10n_informative=10n_redundant=0random_state=1 
  10. # define model  
  11. model = Perceptron()  
  12. # define model evaluation method  
  13. cv = RepeatedStratifiedKFold(n_splits=10n_repeats=3random_state=1 
  14. # evaluate model  
  15. scores = cross_val_score(model, X, y, scoring='accuracy'cvcv=cv, n_jobs=-1)  
  16. # summarize result  
  17. print('Mean Accuracy: %.3f (%.3f)' % (mean(scores), std(scores))) 

運(yùn)行示例將在綜合數(shù)據(jù)集上評估Perceptron算法,并報(bào)告10倍交叉驗(yàn)證的三個重復(fù)中的平均準(zhǔn)確性。鑒于學(xué)習(xí)算法的隨機(jī)性,您的具體結(jié)果可能會有所不同??紤]運(yùn)行該示例幾次。在這種情況下,我們可以看到該模型實(shí)現(xiàn)了約84.7%的平均準(zhǔn)確度。 

  1. Mean Accuracy: 0.847 (0.052) 

我們可能決定使用Perceptron分類器作為最終模型,并對新數(shù)據(jù)進(jìn)行預(yù)測。這可以通過在所有可用數(shù)據(jù)上擬合模型管道并調(diào)用傳遞新數(shù)據(jù)行的predict()函數(shù)來實(shí)現(xiàn)。我們可以通過下面列出的完整示例進(jìn)行演示。 

  1. # make a prediction with a perceptron model on the dataset  
  2. from sklearn.datasets import make_classification  
  3. from sklearn.linear_model import Perceptron  
  4. # define dataset  
  5. X, y = make_classification(n_samples=1000n_features=10n_informative=10n_redundant=0random_state=1 
  6. # define model  
  7. model = Perceptron()  
  8. # fit model  
  9. model.fit(X, y)  
  10. # define new data  
  11. row = [0.12777556,-3.64400522,-2.23268854,-1.82114386,1.75466361,0.1243966,1.03397657,2.35822076,1.01001752,0.56768485]  
  12. # make a prediction  
  13. yhat = model.predict([row])  
  14. # summarize prediction  
  15. print('Predicted Class: %d' % yhat) 

運(yùn)行示例將使模型適合模型并為新的數(shù)據(jù)行進(jìn)行類標(biāo)簽預(yù)測。

Predicted Class: 1

接下來,我們可以看一下配置模型的超參數(shù)。

調(diào)整感知器超參數(shù)

必須為您的特定數(shù)據(jù)集配置Perceptron算法的超參數(shù)。也許最重要的超參數(shù)是學(xué)習(xí)率。較高的學(xué)習(xí)速度可能會使模型學(xué)習(xí)速度加快,但可能是以降低技能為代價的。較小的學(xué)習(xí)率可以得到性能更好的模型,但是訓(xùn)練模型可能需要很長時間。您可以在本教程中了解有關(guān)探索學(xué)習(xí)率的更多信息:訓(xùn)練深度學(xué)習(xí)神經(jīng)網(wǎng)絡(luò)時如何配置學(xué)習(xí)率通常以較小的對數(shù)刻度(例如1e-4(或更?。┖?.0)測試學(xué)習(xí)率。在這種情況下,我們將測試以下值: 

  1. # define grid  
  2. grid = dict()  
  3. grid['eta0'] = [0.0001, 0.001, 0.01, 0.1, 1.0] 

下面的示例使用GridSearchCV類以及我們定義的值網(wǎng)格演示了這一點(diǎn)。 

  1. # grid search learning rate for the perceptron  
  2. from sklearn.datasets import make_classification  
  3. from sklearn.model_selection import GridSearchCV  
  4. from sklearn.model_selection import RepeatedStratifiedKFold  
  5. from sklearn.linear_model import Perceptron  
  6. # define dataset 
  7. X, y = make_classification(n_samples=1000n_features=10n_informative=10n_redundant=0random_state=1 
  8. # define model  
  9. model = Perceptron()  
  10. # define model evaluation method  
  11. cv = RepeatedStratifiedKFold(n_splits=10n_repeats=3random_state=1 
  12. # define grid  
  13. grid = dict()  
  14. grid['eta0'] = [0.0001, 0.001, 0.01, 0.1, 1.0]  
  15. # define search  
  16. search = GridSearchCV(model, grid, scoring='accuracy'cvcv=cv, n_jobs=-1)  
  17. # perform the search  
  18. results = search.fit(X, y)  
  19. # summarize  
  20. print('Mean Accuracy: %.3f' % results.best_score_)  
  21. print('Config: %s' % results.best_params_)  
  22. # summarize all  
  23. means = results.cv_results_['mean_test_score']  
  24. params = results.cv_results_['params']  
  25. for mean, param in zip(means, params):  
  26.     print(">%.3f with: %r" % (mean, param)) 

運(yùn)行示例將使用重復(fù)的交叉驗(yàn)證來評估配置的每種組合。鑒于學(xué)習(xí)算法的隨機(jī)性,您的具體結(jié)果可能會有所不同。嘗試運(yùn)行該示例幾次。在這種情況下,我們可以看到,學(xué)習(xí)率比默認(rèn)值小會導(dǎo)致更好的性能,學(xué)習(xí)率0.0001和0.001均達(dá)到約85.7%的分類精度,而默認(rèn)值1.0則達(dá)到約84.7%的精度。 

  1. Mean Accuracy: 0.857  
  2. Config: {'eta0': 0.0001}  
  3. >0.857 with: {'eta0': 0.0001}  
  4. >0.857 with: {'eta0': 0.001}  
  5. >0.853 with: {'eta0': 0.01}  
  6. >0.847 with: {'eta0': 0.1}  
  7. >0.847 with: {'eta0': 1.0} 

另一個重要的超參數(shù)是使用多少個時期來訓(xùn)練模型。這可能取決于訓(xùn)練數(shù)據(jù)集,并且可能相差很大。同樣,我們將以1到1e + 4的對數(shù)刻度探索配置值。 

  1. # define grid  
  2. grid = dict()  
  3. grid['max_iter'] = [1, 10, 100, 1000, 10000] 

我們將使用上次搜索中的良好學(xué)習(xí)率0.0001。 

  1. # define model  
  2. model = Perceptron(eta0=0.0001) 

下面列出了搜索訓(xùn)練時期數(shù)的網(wǎng)格的完整示例。 

  1. # grid search total epochs for the perceptron  
  2. from sklearn.datasets import make_classification  
  3. from sklearn.model_selection import GridSearchCV  
  4. from sklearn.model_selection import RepeatedStratifiedKFold  
  5. from sklearn.linear_model import Perceptron  
  6. # define dataset  
  7. X, y = make_classification(n_samples=1000n_features=10n_informative=10n_redundant=0random_state=1 
  8. # define model  
  9. model = Perceptron(eta0=0.0001)  
  10. # define model evaluation method  
  11. cv = RepeatedStratifiedKFold(n_splits=10n_repeats=3random_state=1 
  12. # define grid  
  13. grid = dict() 
  14. grid['max_iter'] = [1, 10, 100, 1000, 10000]  
  15. # define search  
  16. search = GridSearchCV(model, grid, scoring='accuracy'cvcv=cv, n_jobs=-1)  
  17. # perform the search  
  18. results = search.fit(X, y)  
  19. # summarize  
  20. print('Mean Accuracy: %.3f' % results.best_score_)  
  21. print('Config: %s' % results.best_params_)  
  22. # summarize all  
  23. means = results.cv_results_['mean_test_score']  
  24. params = results.cv_results_['params']  
  25. for mean, param in zip(means, params):  
  26.     print(">%.3f with: %r" % (mean, param)) 

運(yùn)行示例將使用重復(fù)的交叉驗(yàn)證來評估配置的每種組合。鑒于學(xué)習(xí)算法的隨機(jī)性,您的具體結(jié)果可能會有所不同。嘗試運(yùn)行該示例幾次。在這種情況下,我們可以看到從10到10,000的時間段,分類精度幾乎相同。一個有趣的例外是探索同時配置學(xué)習(xí)率和訓(xùn)練時期的數(shù)量,以查看是否可以獲得更好的結(jié)果。 

  1. Mean Accuracy: 0.857  
  2. Config: {'max_iter': 10}  
  3. >0.850 with: {'max_iter': 1}  
  4. >0.857 with: {'max_iter': 10}  
  5. >0.857 with: {'max_iter': 100}  
  6. >0.857 with: {'max_iter': 1000}  
  7. >0.857 with: {'max_iter': 10000}  

 

責(zé)任編輯:龐桂玉 來源: 馬哥Linux運(yùn)維
相關(guān)推薦

2021-01-07 10:33:26

Python感知器機(jī)器學(xué)習(xí)

2022-09-11 15:02:22

機(jī)器學(xué)習(xí)算法感知器

2020-11-19 10:17:54

Python開發(fā)感知器

2020-09-08 13:02:00

Python神經(jīng)網(wǎng)絡(luò)感知器

2022-06-16 10:29:33

神經(jīng)網(wǎng)絡(luò)圖像分類算法

2020-11-13 12:52:35

機(jī)器學(xué)習(xí)生產(chǎn)產(chǎn)品

2022-06-01 17:16:42

端到端KQI業(yè)務(wù)

2015-10-09 10:59:26

算法分類

2015-10-30 16:09:23

分類算法總結(jié)

2023-11-30 09:55:27

鴻蒙鄰分類器

2015-11-19 10:08:54

大數(shù)據(jù)空間社會

2020-07-09 15:26:18

Python聚類算法語言

2018-11-14 07:41:58

機(jī)器學(xué)習(xí)算法感知器

2020-09-16 10:09:58

深度學(xué)習(xí)DNN計(jì)算

2021-01-26 09:46:59

PythonStacking機(jī)器學(xué)習(xí)

2016-09-23 20:30:54

Javascriptuiwebview富文本編輯器

2023-05-06 10:02:37

深度學(xué)習(xí)算法

2023-12-26 08:40:06

分類算法數(shù)據(jù)分析Python

2023-07-19 08:55:00

神經(jīng)網(wǎng)絡(luò)推薦系統(tǒng)

2017-04-17 15:03:16

Python自然語言處理
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號