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 与 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', cvcv=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', 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} 

另一个重要的超参数是使用多少个时期来训练模型。这可能取决于训练数据集,并且可能相差很大。同样,我们将以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', 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的时间段,分类精度几乎相同。一个有趣的例外是探索同时配置学习率和训练时期的数量,以查看是否可以获得更好的结果。

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}  

以上就是基于 python 实践感知器分类算法的详细内容,更多关于python 实践感知器分类算法的资料请关注www.887551.com其它相关文章!