1. 程式人生 > 其它 >基於Python實踐感知器分類演算法

基於Python實踐感知器分類演算法

技術標籤:python實踐

【翻譯自: Perceptron Algorithm for Classification in Python

【說明:Jason BrownleePhD大神的文章個人很喜歡,所以閒暇時間裡會做一點翻譯和學習實踐的工作,這裡是相應工作的實踐記錄,希望能幫到有需要的人!】

Perceptron是用於二進位制分類任務的線性機器學習演算法。它可以被認為是人工神經網路的第一種和最簡單的型別之一。 絕對不是“深度”學習,而是重要的組成部分。與邏輯迴歸相似,它可以快速學習兩類分類任務在特徵空間中的線性分離,儘管與邏輯迴歸不同,它使用隨機梯度下降優化演算法學習並且不預測校準概率。

在本教程中,您將發現Perceptron分類機器學習演算法。完成本教程後,您將知道:

Perceptron分類器是一種線性演算法,可以應用於二進位制分類任務。
如何使用帶有Scikit-Learn的Perceptron模型進行擬合,評估和做出預測。
如何在給定的資料集上調整Perceptron演算法的超引數。

教程概述

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

感知器演算法
Perceptron與Scikit-學習
音調感知器超引數

感知器演算法

Perceptron演算法是兩類(二進位制)分類機器學習演算法。它是一種神經網路模型,可能是最簡單的神經網路模型型別。它由將一行資料作為輸入並預測類標籤的單個節點或神經元組成。這可以通過計算輸入的加權和和偏差(設​​置為1)來實現。模型輸入的加權總和稱為啟用。

啟用=權重*輸入+偏差
如果啟用高於0.0,則模型將輸出1.0;否則,模型將輸出1.0。否則,將輸出0.0。

預測1:如果啟用> 0.0
預測0:如果啟用<= 0.0
假設輸入已乘以模型係數,如線性迴歸和邏輯迴歸,則優良作法是在使用模型之前對資料進行標準化或標準化。感知器是線性分類演算法。這意味著它將學習在特徵空間中使用一條線(稱為超平面)將兩個類別分開的決策邊界。因此,適用於那些類別可以通過線性或線性模型(稱為線性可分離)很好地分離的問題。該模型的係數稱為輸入權重,並使用隨機梯度下降優化演算法進行訓練。一次將來自訓練資料集的示例顯示給模型,模型進行預測並計算誤差。然後,更新模型的權重以減少示例的誤差。這稱為Perceptron更新規則。對於訓練資料集中的所有示例(稱為時期)都重複此過程。然後,使用示例更新模型的過程會重複很多次。在每批中,使用較小比例的誤差來更新模型權重,並且該比例由稱為學習率的超引數控制,通常將其設定為較小的值。這是為了確保學習不會太快發生,從而導致技能水平可能較低,這被稱為模型權重的優化(搜尋)過程的過早收斂。

權重(t + 1)=權重(t)+學習率*(expected_i –預測值)* input_i
當模型所產生的誤差降至較低水平或不再改善時,或者執行了最大時期數時,訓練將停止。

模型權重的初始值設定為較小的隨機值。另外,在每個訓練紀元之前對訓練資料集進行混洗。這是設計使然,以加速和改善模型訓練過程。因此,學習演算法是隨機的,並且每次執行都會獲得不同的結果。因此,優良作法是使用重複評估來總結演算法在資料集上的效能,並報告平均分類精度。學習率和訓練時期數是演算法的超引數,可以使用啟發式或超引數調整來設定。

有關Perceptron演算法的更多資訊,請參見教程:

如何從頭開始在Python中實現感知器演算法
現在我們已經熟悉了Perceptron演算法,現在讓我們探索如何在Python中使用該演算法。

Perceptron與Scikit-Learn

可通過Perceptron類在scikit-learn Python機器學習庫中使用Perceptron演算法。該類允許您配置學習率(eta0),預設為1.0。

# define model
model = Perceptron(eta0=1.0)

該實現還允許您配置訓練時期的總數(max_iter),預設為1,000。

# define model
model = Perceptron(max_iter=1000)

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

# 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類使用重複的分層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', cv=cv, n_jobs=-1)
# summarize result
print('Mean Accuracy: %.3f (%.3f)' % (mean(scores), std(scores)))

執行示例將在綜合資料集上評估Perceptron演算法,並報告10倍交叉驗證的三個重複中的平均準確性。鑑於學習演算法的隨機性,您的具體結果可能會有所不同。 考慮執行該示例幾次。在這種情況下,我們可以看到該模型實現了約84.7%的平均準確度。

Mean Accuracy: 0.847 (0.052)

我們可能決定使用Perceptron分類器作為最終模型,並對新資料進行預測。這可以通過在所有可用資料上擬合模型管道並呼叫傳遞新資料行的predict()函式來實現。我們可以通過下面列出的完整示例進行演示。

# 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

接下來,我們可以看一下配置模型的超引數。

調整感知器超引數

必須為您的特定資料集配置Perceptron演算法的超引數。也許最重要的超引數是學習率。較高的學習速度可能會使模型學習速度加快,但可能是以降低技能為代價的。 較小的學習率可以得到效能更好的模型,但是訓練模型可能需要很長時間。您可以在本教程中瞭解有關探索學習率的更多資訊:訓練深度學習神經網路時如何配置學習率通常以較小的對數刻度(例如1e-4(或更小)和1.0)測試學習率。 在這種情況下,我們將測試以下值:

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

下面的示例使用GridSearchCV類以及我們定義的值網格演示了這一點。

# 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))

執行示例將使用重複的交叉驗證來評估配置的每種組合。鑑於學習演算法的隨機性,您的具體結果可能會有所不同。 嘗試執行該示例幾次。在這種情況下,我們可以看到,學習率比預設值小會導致更好的效能,學習率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}

另一個重要的超引數是使用多少個時期來訓練模型。這可能取決於訓練資料集,並且可能相差很大。 同樣,我們將以1到1e + 4的對數刻度探索配置值。

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

我們將使用上次搜尋中的良好學習率0.0001。

# 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))

執行示例將使用重複的交叉驗證來評估配置的每種組合。鑑於學習演算法的隨機性,您的具體結果可能會有所不同。 嘗試執行該示例幾次。在這種情況下,我們可以看到從10到10,000的時間段,分類精度幾乎相同。 一個有趣的例外是探索同時配置學習率和訓練時期的數量,以檢視是否可以獲得更好的結果。

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}