基于Python實踐感知器分類算法
Perceptron是用于二進制分類任務的線性機器學習算法。它可以被認為是人工神經(jīng)網(wǎng)絡的第一種和最簡單的類型之一。絕對不是“深度”學習,而是重要的組成部分。與邏輯回歸相似,它可以快速學習兩類分類任務在特征空間中的線性分離,盡管與邏輯回歸不同,它使用隨機梯度下降優(yōu)化算法學習并且不預測校準概率。
在本教程中,您將發(fā)現(xiàn)Perceptron分類機器學習算法。完成本教程后,您將知道:
- Perceptron分類器是一種線性算法,可以應用于二進制分類任務。
- 如何使用帶有Scikit-Learn的Perceptron模型進行擬合,評估和做出預測。
- 如何在給定的數(shù)據(jù)集上調(diào)整Perceptron算法的超參數(shù)。
教程概述
本教程分為3個部分,共三個部分。他們是:
- 感知器算法
- Perceptron與Scikit-學習
- 音調(diào)感知器超參數(shù)
感知器算法
Perceptron算法是兩類(二進制)分類機器學習算法。它是一種神經(jīng)網(wǎng)絡模型,可能是最簡單的神經(jīng)網(wǎng)絡模型類型。它由將一行數(shù)據(jù)作為輸入并預測類標簽的單個節(jié)點或神經(jīng)元組成。這可以通過計算輸入的加權和和偏差(設置為1)來實現(xiàn)。模型輸入的加權總和稱為激活。
激活=權重*輸入+偏差
如果激活高于0.0,則模型將輸出1.0;否則,模型將輸出1.0。否則,將輸出0.0。
預測1:如果激活> 0.0
預測0:如果激活<= 0.0
假設輸入已乘以模型系數(shù),如線性回歸和邏輯回歸,則優(yōu)良作法是在使用模型之前對數(shù)據(jù)進行標準化或標準化。感知器是線性分類算法。這意味著它將學習在特征空間中使用一條線(稱為超平面)將兩個類別分開的決策邊界。因此,適用于那些類別可以通過線性或線性模型(稱為線性可分離)很好地分離的問題。該模型的系數(shù)稱為輸入權重,并使用隨機梯度下降優(yōu)化算法進行訓練。一次將來自訓練數(shù)據(jù)集的示例顯示給模型,模型進行預測并計算誤差。然后,更新模型的權重以減少示例的誤差。這稱為Perceptron更新規(guī)則。對于訓練數(shù)據(jù)集中的所有示例(稱為時期)都重復此過程。然后,使用示例更新模型的過程會重復很多次。在每批中,使用較小比例的誤差來更新模型權重,并且該比例由稱為學習率的超參數(shù)控制,通常將其設置為較小的值。這是為了確保學習不會太快發(fā)生,從而導致技能水平可能較低,這被稱為模型權重的優(yōu)化(搜索)過程的過早收斂。
權重(t + 1)=權重(t)+學習率*(expected_i –預測值)* input_i
當模型所產(chǎn)生的誤差降至較低水平或不再改善時,或者執(zhí)行了最大時期數(shù)時,訓練將停止。
模型權重的初始值設置為較小的隨機值。另外,在每個訓練紀元之前對訓練數(shù)據(jù)集進行混洗。這是設計使然,以加速和改善模型訓練過程。因此,學習算法是隨機的,并且每次運行都會獲得不同的結果。因此,優(yōu)良作法是使用重復評估來總結算法在數(shù)據(jù)集上的性能,并報告平均分類精度。學習率和訓練時期數(shù)是算法的超參數(shù),可以使用啟發(fā)式或超參數(shù)調(diào)整來設置。
現(xiàn)在我們已經(jīng)熟悉了Perceptron算法,現(xiàn)在讓我們探索如何在Python中使用該算法。
Perceptron 與 Scikit-Learn
可通過Perceptron類在scikit-learn Python機器學習庫中使用Perceptron算法。該類允許您配置學習率(eta0),默認為1.0。
- # define model
- model = Perceptron(eta0=1.0)
該實現(xiàn)還允許您配置訓練時期的總數(shù)(max_iter),默認為1,000。
- # define model
- model = Perceptron(max_iter=1000)
Perceptron算法的scikit-learn實現(xiàn)還提供了您可能想探索的其他配置選項,例如提前停止和使用懲罰損失。我們可以通過一個有效的示例來演示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)
運行示例將創(chuàng)建數(shù)據(jù)集并確認數(shù)據(jù)集的行數(shù)和列數(shù)。
- (1000, 10) (1000,)
我們可以通過 RepeatedStratifiedKFold類使用重復的分層k折交叉驗證來擬合和評估Perceptron模型。我們將在測試裝置中使用10折和3次重復。
- # create the model
- model = Perceptron()
下面列出了為綜合二進制分類任務評估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)))
運行示例將在綜合數(shù)據(jù)集上評估Perceptron算法,并報告10倍交叉驗證的三個重復中的平均準確性。鑒于學習算法的隨機性,您的具體結果可能會有所不同??紤]運行該示例幾次。在這種情況下,我們可以看到該模型實現(xiàn)了約84.7%的平均準確度。
- Mean Accuracy: 0.847 (0.052)
我們可能決定使用Perceptron分類器作為最終模型,并對新數(shù)據(jù)進行預測。這可以通過在所有可用數(shù)據(jù)上擬合模型管道并調(diào)用傳遞新數(shù)據(jù)行的predict()函數(shù)來實現(xià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)
運行示例將使模型適合模型并為新的數(shù)據(jù)行進行類標簽預測。
Predicted Class: 1
接下來,我們可以看一下配置模型的超參數(shù)。
調(diào)整感知器超參數(shù)
必須為您的特定數(shù)據(jù)集配置Perceptron算法的超參數(shù)。也許最重要的超參數(shù)是學習率。較高的學習速度可能會使模型學習速度加快,但可能是以降低技能為代價的。較小的學習率可以得到性能更好的模型,但是訓練模型可能需要很長時間。您可以在本教程中了解有關探索學習率的更多信息:訓練深度學習神經(jīng)網(wǎng)絡時如何配置學習率通常以較小的對數(shù)刻度(例如1e-4(或更?。┖?.0)測試學習率。在這種情況下,我們將測試以下值:
- # define grid
- grid = dict()
- grid['eta0'] = [0.0001, 0.001, 0.01, 0.1, 1.0]
下面的示例使用GridSearchCV類以及我們定義的值網(wǎng)格演示了這一點。
- # 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))
運行示例將使用重復的交叉驗證來評估配置的每種組合。鑒于學習算法的隨機性,您的具體結果可能會有所不同。嘗試運行該示例幾次。在這種情況下,我們可以看到,學習率比默認值小會導致更好的性能,學習率0.0001和0.001均達到約85.7%的分類精度,而默認值1.0則達到約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ù)是使用多少個時期來訓練模型。這可能取決于訓練數(shù)據(jù)集,并且可能相差很大。同樣,我們將以1到1e + 4的對數(shù)刻度探索配置值。
- # define grid
- grid = dict()
- grid['max_iter'] = [1, 10, 100, 1000, 10000]
我們將使用上次搜索中的良好學習率0.0001。
- # define model
- model = Perceptron(eta0=0.0001)
下面列出了搜索訓練時期數(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))
運行示例將使用重復的交叉驗證來評估配置的每種組合。鑒于學習算法的隨機性,您的具體結果可能會有所不同。嘗試運行該示例幾次。在這種情況下,我們可以看到從10到10,000的時間段,分類精度幾乎相同。一個有趣的例外是探索同時配置學習率和訓練時期的數(shù)量,以查看是否可以獲得更好的結果。
- 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}