Machine Learning With Python 简明教程

Improving Performance of ML Model (Contd…)

Performance Improvement with Algorithm Tuning

众所周知,ML 模型以这样一种方式进行参数化,即可以调整它们的行为以解决特定问题。算法调优是指找到这些参数的最佳组合,以便提高 ML 模型的性能。此过程有时称为超参数优化,算法本身的参数称为超参数,ML 算法找到的系数称为参数。

在此,我们将讨论 Python Scikit-learn 提供的算法参数调优的一些方法。

Grid Search Parameter Tuning

这是一种参数调优方法。此方法的工作要点是,针对网格中指定的每个可能的算法参数组合有条不紊地构建和评估模型方法。因此,我们可以说此算法具有搜索性质。

Example

在以下 Python 代码示例中,我们将使用 Sklearn 的 GridSearchCV 类对 Pima 印第安人糖尿病数据集执行网格搜索,以评估岭回归算法的各个 alpha 值。

首先,按如下所示导入所需包:

import numpy
from pandas import read_csv
from sklearn.linear_model import Ridge
from sklearn.model_selection import GridSearchCV

现在,我们需要加载 Pima diabetes 数据集,如在之前的示例中所做的那样:

path = r"C:\pima-indians-diabetes.csv"
headernames = ['preg', 'plas', 'pres', 'skin', 'test', 'mass', 'pedi', 'age', 'class']
data = read_csv(path, names=headernames)
array = data.values
X = array[:,0:8]
Y = array[:,8]

接下来,按如下方式评估各个 alpha 值 -

alphas = numpy.array([1,0.1,0.01,0.001,0.0001,0])
param_grid = dict(alpha=alphas)

现在,我们需要对我们的模型应用网格搜索 -

model = Ridge()
grid = GridSearchCV(estimator=model, param_grid=param_grid)
grid.fit(X, Y)

使用以下脚本行打印结果 -

print(grid.best_score_)
print(grid.best_estimator_.alpha)

Output

0.2796175593129722
1.0

以上输出为我们提供了最佳分数以及达到该分数的网格中的参数集。此例中的 alpha 值为 1.0。

Random Search Parameter Tuning

这是一种参数调优方法。此方法的工作要点是,针对固定次数的迭代从随机分布中对算法参数进行采样。

Example

在以下 Python 代码示例中,我们将使用 Sklearn 的 RandomizedSearchCV 类对 Pima 印第安人糖尿病数据集执行随机搜索,以评估岭回归算法的 0 到 1 之间的不同 alpha 值。

首先,按如下所示导入所需包:

import numpy
from pandas import read_csv
from scipy.stats import uniform
from sklearn.linear_model import Ridge
from sklearn.model_selection import RandomizedSearchCV

现在,我们需要加载 Pima diabetes 数据集,如在之前的示例中所做的那样:

path = r"C:\pima-indians-diabetes.csv"
headernames = ['preg', 'plas', 'pres', 'skin', 'test', 'mass', 'pedi', 'age', 'class']
data = read_csv(path, names=headernames)
array = data.values
X = array[:,0:8]
Y = array[:,8]

接下来,按如下方式在岭回归算法上评估各个 alpha 值 -

param_grid = {'alpha': uniform()}
model = Ridge()
random_search = RandomizedSearchCV(estimator=model, param_distributions=param_grid, n_iter=50,
random_state=7)
random_search.fit(X, Y)

使用以下脚本行打印结果 -

print(random_search.best_score_)
print(random_search.best_estimator_.alpha)

Output

0.27961712703051084
0.9779895119966027

以上输出为我们提供了与网格搜索非常相似的最佳分数。