基于Python實(shí)踐感知器分類算法
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。
- # define model
- model = Perceptron(eta0=1.0)
該實(shí)現(xiàn)還允許您配置訓(xùn)練時期的總數(shù)(max_iter),默認(rèn)為1,000。
- # define model
- 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ù)集。
- # 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)
運(yùn)行示例將創(chuàng)建數(shù)據(jù)集并確認(rèn)數(shù)據(jù)集的行數(shù)和列數(shù)。
- (1000, 10) (1000,)
我們可以通過 RepeatedStratifiedKFold類使用重復(fù)的分層k折交叉驗(yàn)證來擬合和評估Perceptron模型。我們將在測試裝置中使用10折和3次重復(fù)。
- # create the model
- model = Perceptron()
下面列出了為綜合二進(jìn)制分類任務(wù)評估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', cvcv=cv, n_jobs=-1)
- # summarize result
- 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)確度。
- 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)行演示。
- # 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)
運(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í)率。在這種情況下,我們將測試以下值:
- # define grid
- grid = dict()
- grid['eta0'] = [0.0001, 0.001, 0.01, 0.1, 1.0]
下面的示例使用GridSearchCV類以及我們定義的值網(wǎng)格演示了這一點(diǎn)。
- # 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', cvcv=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))
運(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%的精度。
- 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}
另一個重要的超參數(shù)是使用多少個時期來訓(xùn)練模型。這可能取決于訓(xùn)練數(shù)據(jù)集,并且可能相差很大。同樣,我們將以1到1e + 4的對數(shù)刻度探索配置值。
- # define grid
- grid = dict()
- grid['max_iter'] = [1, 10, 100, 1000, 10000]
我們將使用上次搜索中的良好學(xué)習(xí)率0.0001。
- # define model
- model = Perceptron(eta0=0.0001)
下面列出了搜索訓(xùn)練時期數(shù)的網(wǎng)格的完整示例。
- # 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', cvcv=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))
運(yùn)行示例將使用重復(fù)的交叉驗(yàn)證來評估配置的每種組合。鑒于學(xué)習(xí)算法的隨機(jī)性,您的具體結(jié)果可能會有所不同。嘗試運(yùn)行該示例幾次。在這種情況下,我們可以看到從10到10,000的時間段,分類精度幾乎相同。一個有趣的例外是探索同時配置學(xué)習(xí)率和訓(xùn)練時期的數(shù)量,以查看是否可以獲得更好的結(jié)果。
- 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}