如何爬升用于机器学习的测试集

人工智能 机器学习
在本教程中,您将发现如何爬升用于机器学习的测试集。

 [[387235]]

爬坡测试集是一种在不影响训练集甚至开发预测模型的情况下,在机器学习竞赛中实现良好或完美预测的方法。作为机器学习竞赛的一种方法,这是理所当然的,大多数竞赛平台都对其施加了限制,以防止出现这种情况,这一点很重要。但是,爬坡测试集是机器学习从业人员在参加比赛时不小心做的事情。通过开发一个明确的实现来爬升测试集,它有助于更好地了解通过过度使用测试数据集来评估建模管道而过度拟合测试数据集的难易程度。

在本教程中,您将发现如何爬升用于机器学习的测试集。完成本教程后,您将知道:

  •  无需查看训练数据集,就可以通过爬上测试集来做出完美的预测。
  •  如何为分类和回归任务爬坡测试集。
  •  当我们过度使用测试集来评估建模管道时,我们暗中爬升了测试集。

教程概述

本教程分为五个部分。他们是:

  •  爬坡测试仪
  •  爬山算法
  •  如何进行爬山
  •  爬坡糖尿病分类数据集
  •  爬坡房屋回归数据集

爬坡测试仪

像Kaggle上的机器学习比赛一样,机器学习比赛提供了完整的训练数据集以及测试集的输入。给定比赛的目的是预测目标值,例如测试集的标签或数值。针对隐藏的测试设置目标值评估解决方案,并进行适当评分。与测试集得分最高的参赛作品赢得了比赛。机器学习竞赛的挑战可以被定义为一个优化问题。传统上,竞赛参与者充当优化算法,探索导致不同组预测的不同建模管道,对预测进行评分,然后对管道进行更改以期望获得更高的分数。此过程也可以直接用优化算法建模,无需查看训练集就可以生成和评估候选预测。通常,这称为爬山测试集,作为解决此问题的最简单的优化算法之一就是爬山算法。尽管在实际的机器学习竞赛中应该正确地爬升测试集,但是实施该方法以了解该方法的局限性和过度安装测试集的危险可能是一个有趣的练习。此外,无需接触训练数据集就可以完美预测测试集的事实常常使很多初学者机器学习从业人员感到震惊。最重要的是,当我们反复评估不同的建模管道时,我们暗中爬升了测试集。风险是测试集的分数得到了提高,但代价是泛化误差增加,即在更广泛的问题上表现较差。进行机器学习竞赛的人们都非常清楚这个问题,并且对预测评估施加了限制以应对该问题,例如将评估限制为每天一次或几次,并在测试集的隐藏子集而不是整个测试集上报告分数。。有关更多信息,请参阅进一步阅读部分中列出的论文。接下来,让我们看看如何实施爬坡算法来优化测试集的预测。

爬山算法

爬山算法是一种非常简单的优化算法。它涉及生成候选解决方案并进行评估。然后是逐步改进的起点,直到无法实现进一步的改进,或者我们用光了时间,资源或兴趣。从现有候选解决方案中生成新的候选解决方案。通常,这涉及对候选解决方案进行单个更改,对其进行评估,并且如果候选解决方案与先前的当前解决方案一样好或更好,则将该候选解决方案接受为新的“当前”解决方案。否则,将其丢弃。我们可能会认为只接受分数更高的候选人是一个好主意。对于许多简单问题,这是一种合理的方法,尽管在更复杂的问题上,希望接受具有相同分数的不同候选者,以帮助搜索过程缩放要素空间中的平坦区域(高原)。当爬上测试集时,候选解决方案是预测列表。对于二进制分类任务,这是两个类的0和1值的列表。对于回归任务,这是目标变量范围内的数字列表。对候选分类解决方案的修改将是选择一个预测并将其从0翻转为1或从1翻转为0。对回归进行候选解决方案的修改将是将高斯噪声添加到列表中的一个值或替换一个值在列表中使用新值。解决方案的评分涉及计算评分指标,例如分类任务的分类准确性或回归任务的平均绝对误差。现在我们已经熟悉了算法,现在就来实现它。

如何进行爬山

我们将在综合分类任务上开发爬坡算法。首先,我们创建一个包含许多输入变量和5,000行示例的二进制分类任务。然后,我们可以将数据集分为训练集和测试集。下面列出了完整的示例。 

  1. # example of a synthetic dataset.  
  2. from sklearn.datasets import make_classification  
  3. from sklearn.model_selection import train_test_split  
  4. # define dataset  
  5. X, y = make_classification(n_samples=5000n_features=20n_informative=15n_redundant=5random_state=1 
  6. print(X.shape, y.shape)  
  7. # split dataset  
  8. X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.33, random_state=1) 
  9. print(X_train.shape, X_test.shape, y_train.shape, y_test.shape) 

运行示例首先报告创建的数据集的形状,显示5,000行和20个输入变量。然后将数据集分为训练集和测试集,其中约3,300个用于训练,约1,600个用于测试。 

  1. (5000, 20) (5000,)  
  2. (3350, 20) (1650, 20) (3350,) (1650,) 

现在我们可以开发一个登山者。首先,我们可以创建一个将加载的函数,或者在这种情况下,定义数据集。当我们要更改数据集时,可以稍后更新此功能。 

  1. # load or prepare the classification dataset  
  2. def load_dataset():  
  3.  return make_classification(n_samples=5000n_features=20n_informative=15n_redundant=5random_state=1

接下来,我们需要一个函数来评估候选解决方案,即预测列表。我们将使用分类精度,其中分数范围在0(最坏的解决方案)到1(完美的预测集)之间。 

  1. # evaluate a set of predictions  
  2. def evaluate_predictions(y_test, yhat):  
  3.  return accuracy_score(y_test, yhat) 

接下来,我们需要一个函数来创建初始候选解决方案。这是0和1类标签的预测列表,长度足以匹配测试集中的示例数,在这种情况下为1650。我们可以使用randint()函数生成0和1的随机值。 

  1. # create a random set of predictions  
  2. def random_predictions(n_examples):  
  3.  return [randint(0, 1) for _ in range(n_examples)] 

接下来,我们需要一个函数来创建候选解决方案的修改版本。在这种情况下,这涉及在解决方案中选择一个值并将其从0翻转为1或从1翻转为0。通常,我们会在爬坡期间对每个新的候选解决方案进行一次更改,但是我已经对该函数进行了参数化,因此您可以根据需要探索多个更改。 

  1. # modify the current set of predictions  
  2. def modify_predictions(current, n_changes=1):  
  3.  # copy current solution  
  4.  updated = current.copy() 
  5.  for i in range(n_changes):  
  6.   # select a point to change  
  7.   ix = randint(0, len(updated)-1)  
  8.   # flip the class label  
  9.   updated[ix] = 1 - updated[ix]  
  10.  return updated 

到现在为止还挺好。接下来,我们可以开发执行搜索的功能。首先,通过调用random_predictions()函数和随后的validate_predictions()函数来创建和评估初始解决方案。然后,我们循环进行固定次数的迭代,并通过调用Modify_predictions()生成一个新的候选值,对其进行求值,如果分数与当前解决方案相同或更好,则将其替换。当我们完成预设的迭代次数(任意选择)或达到理想分数时,该循环结束,在这种情况下,我们知道其精度为1.0(100%)。下面的函数hill_climb_testset()实现了此功能,将测试集作为输入并返回在爬坡过程中发现的最佳预测集。 

  1. # run a hill climb for a set of predictions  
  2. def hill_climb_testset(X_test, y_test, max_iterations):  
  3.  scores = list()  
  4.  # generate the initial solution  
  5.  solution = random_predictions(X_test.shape[0])  
  6.  # evaluate the initial solution  
  7.  score = evaluate_predictions(y_test, solution) 
  8.  scores.append(score)  
  9.  # hill climb to a solution  
  10.  for i in range(max_iterations):  
  11.   # record scores  
  12.   scores.append(score)  
  13.   # stop once we achieve the best score  
  14.   if score == 1.0:  
  15.    break  
  16.   # generate new candidate  
  17.   candidate = modify_predictions(solution)  
  18.   # evaluate candidate  
  19.   value = evaluate_predictions(y_test, candidate)  
  20.   # check if it is as good or better  
  21.   if value >= score:  
  22.    solution, score = candidate, value  
  23.    print('>%d, score=%.3f' % (i, score))  
  24.  return solution, scores 

这里的所有都是它的。下面列出了爬坡测试装置的完整示例。 

  1. # example of hill climbing the test set for a classification task  
  2. from random import randint  
  3. from sklearn.datasets import make_classification  
  4. from sklearn.model_selection import train_test_split 
  5. from sklearn.metrics import accuracy_score  
  6. from matplotlib import pyplot   
  7. # load or prepare the classification dataset  
  8. def load_dataset():  
  9.  return make_classification(n_samples=5000n_features=20n_informative=15n_redundant=5random_state=1)   
  10. # evaluate a set of predictions  
  11. def evaluate_predictions(y_test, yhat):  
  12.  return accuracy_score(y_test, yhat)   
  13. # create a random set of predictions  
  14. def random_predictions(n_examples):  
  15.  return [randint(0, 1) for _ in range(n_examples)] 
  16. # modify the current set of predictions  
  17. def modify_predictions(current, n_changes=1):  
  18.  # copy current solution  
  19.  updated = current.copy()  
  20.  for i in range(n_changes):  
  21.   # select a point to change  
  22.   ix = randint(0, len(updated)-1)  
  23.   # flip the class label  
  24.   updated[ix] = 1 - updated[ix]  
  25.  return updated   
  26. # run a hill climb for a set of predictions  
  27. def hill_climb_testset(X_test, y_test, max_iterations):  
  28.  scores = list()  
  29.  # generate the initial solution  
  30.  solution = random_predictions(X_test.shape[0])  
  31.  # evaluate the initial solution  
  32.  score = evaluate_predictions(y_test, solution)  
  33.  scores.append(score)  
  34.  # hill climb to a solution  
  35.  for i in range(max_iterations):  
  36.   # record scores  
  37.   scores.append(score)  
  38.   # stop once we achieve the best score  
  39.   if score == 1.0:  
  40.    break  
  41.   # generate new candidate  
  42.   candidate = modify_predictions(solution)  
  43.   # evaluate candidate  
  44.   value = evaluate_predictions(y_test, candidate)  
  45.   # check if it is as good or better  
  46.   if value >= score:  
  47.    solution, score = candidate, value  
  48.    print('>%d, score=%.3f' % (i, score))  
  49.  return solution, scores  
  50. # load the dataset  
  51. X, y = load_dataset()  
  52. print(X.shape, y.shape)  
  53. # split dataset into train and test sets  
  54. X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.33, random_state=1 
  55. print(X_train.shape, X_test.shape, y_train.shape, y_test.shape)  
  56. # run hill climb  
  57. yhat, scores = hill_climb_testset(X_test, y_test, 20000)  
  58. # plot the scores vs iterations 
  59. pyplot.plot(scores)  
  60. pyplot.show() 

运行示例将使搜索进行20,000次迭代,或者如果达到理想的准确性,则停止搜索。注意:由于算法或评估程序的随机性,或者数值精度的差异,您的结果可能会有所不同。考虑运行该示例几次并比较平均结果。在这种情况下,我们在约12,900次迭代中找到了一组理想的测试集预测。回想一下,这是在不接触训练数据集且不通过查看测试集目标值进行欺骗的情况下实现的。相反,我们只是简单地优化了一组数字。这里的教训是,将测试管道用作爬山优化算法,对测试集重复建模管道的评估会做同样的事情。解决方案将过度适合测试集。 

  1. ...  
  2. >8092, score=0.996  
  3. >8886, score=0.997  
  4. >9202, score=0.998  
  5. >9322, score=0.998  
  6. >9521, score=0.999  
  7. >11046, score=0.999  
  8. >12932, score=1.000 

还创建了优化进度图。这有助于了解优化算法的更改(例如,在坡道上更改内容的选择以及更改方式)如何影响搜索的收敛性。

爬坡糖尿病分类数据集

我们将使用糖尿病数据集作为探索爬坡测试集以解决分类问题的基础。每条记录都描述了女性的医疗细节,并且预测是未来五年内糖尿病的发作。

数据集详细信息:pima-indians-diabetes.names数据集:pima-indians-diabetes.csv

数据集有八个输入变量和768行数据;输入变量均为数字,目标具有两个类别标签,例如 这是一个二进制分类任务。下面提供了数据集前五行的示例。 

  1. 6,148,72,35,0,33.6,0.627,50,1  
  2. 1,85,66,29,0,26.6,0.351,31,0  
  3. 8,183,64,0,0,23.3,0.672,32,1  
  4. 1,89,66,23,94,28.1,0.167,21,0  
  5. 0,137,40,35,168,43.1,2.288,33,1  
  6. ... 

我们可以使用Pandas直接加载数据集,如下所示。 

  1. # load or prepare the classification dataset  
  2. def load_dataset(): 
  3.   url = 'https://raw.githubusercontent.com/jbrownlee/Datasets/master/pima-indians-diabetes.csv'  
  4.  df = read_csv(url, header=None
  5.  data = df.values  
  6.  return data[:, :-1], data[:, -1] 

其余代码保持不变。创建该文件是为了使您可以放入自己的二进制分类任务并进行尝试。下面列出了完整的示例。 

  1. # example of hill climbing the test set for the diabetes dataset  
  2. from random import randint  
  3. from pandas import read_csv  
  4. from sklearn.model_selection import train_test_split  
  5. from sklearn.metrics import accuracy_score  
  6. from matplotlib import pyplot   
  7. # load or prepare the classification dataset  
  8. def load_dataset():  
  9.  url = 'https://raw.githubusercontent.com/jbrownlee/Datasets/master/pima-indians-diabetes.csv'  
  10.  df = read_csv(url, header=None 
  11.  data = df.values  
  12.  return data[:, :-1], data[:, -1]  
  13. # evaluate a set of predictions  
  14. def evaluate_predictions(y_test, yhat):  
  15.  return accuracy_score(y_test, yhat)   
  16. # create a random set of predictions  
  17. def random_predictions(n_examples):  
  18.  return [randint(0, 1) for _ in range(n_examples)]  
  19. # modify the current set of predictions  
  20. def modify_predictions(current, n_changes=1):  
  21.  # copy current solution  
  22.  updated = current.copy()  
  23.  for i in range(n_changes):  
  24.   # select a point to change  
  25.   ix = randint(0, len(updated)-1)  
  26.   # flip the class label  
  27.   updated[ix] = 1 - updated[ix]  
  28.  return updated   
  29. # run a hill climb for a set of predictions  
  30. def hill_climb_testset(X_test, y_test, max_iterations):  
  31.  scores = list()  
  32.  # generate the initial solution  
  33.  solution = random_predictions(X_test.shape[0])  
  34.  # evaluate the initial solution  
  35.  score = evaluate_predictions(y_test, solution)  
  36.  scores.append(score)  
  37.  # hill climb to a solution  
  38.  for i in range(max_iterations):  
  39.   # record scores  
  40.   scores.append(score)  
  41.   # stop once we achieve the best score  
  42.   if score == 1.0: 
  43.    break  
  44.   # generate new candidate  
  45.   candidate = modify_predictions(solution)  
  46.   # evaluate candidate  
  47.   value = evaluate_predictions(y_test, candidate)  
  48.   # check if it is as good or better  
  49.   if value >= score:  
  50.    solution, score = candidate, value  
  51.    print('>%d, score=%.3f' % (i, score))  
  52.  return solution, scores  
  53. # load the dataset  
  54. X, y = load_dataset()  
  55. print(X.shape, y.shape)  
  56. # split dataset into train and test sets  
  57. X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.33, random_state=1 
  58. print(X_train.shape, X_test.shape, y_train.shape, y_test.shape)  
  59. # run hill climb  
  60. yhat, scores = hill_climb_testset(X_test, y_test, 5000)  
  61. # plot the scores vs iterations  
  62. pyplot.plot(scores)  
  63. pyplot.show() 

运行示例将报告每次搜索过程中看到改进时的迭代次数和准确性。

在这种情况下,我们使用的迭代次数较少,因为要进行的预测较少,因此优化起来比较简单。

注意:由于算法或评估程序的随机性,或者数值精度的差异,您的结果可能会有所不同。考虑运行该示例几次并比较平均结果。

在这种情况下,我们可以看到在大约1,500次迭代中达到了完美的精度。 

  1. ...  
  2. >617, score=0.961  
  3. >627, score=0.965  
  4. >650, score=0.969  
  5. >683, score=0.972  
  6. >743, score=0.976  
  7. >803, score=0.980  
  8. >817, score=0.984  
  9. >945, score=0.988  
  10. >1350, score=0.992  
  11. >1387, score=0.996  
  12. >1565, score=1.000 

还创建了搜索进度的折线图,表明收敛迅速。

爬坡房屋回归数据集

我们将使用住房数据集作为探索爬坡测试集回归问题的基础。住房数据集包含给定房屋及其附近地区详细信息的数千美元房屋价格预测。

数据集详细信息:housing.names数据集:housing.csv

这是一个回归问题,这意味着我们正在预测一个数值。共有506个观测值,其中包含13个输入变量和一个输出变量。下面列出了前五行的示例。 

  1. 0.00632,18.00,2.310,0,0.5380,6.5750,65.20,4.0900,1,296.0,15.30,396.90,4.98,24.00  
  2. 0.02731,0.00,7.070,0,0.4690,6.4210,78.90,4.9671,2,242.0,17.80,396.90,9.14,21.60 
  3. 0.02729,0.00,7.070,0,0.4690,7.1850,61.10,4.9671,2,242.0,17.80,392.83,4.03,34.70  
  4. 0.03237,0.00,2.180,0,0.4580,6.9980,45.80,6.0622,3,222.0,18.70,394.63,2.94,33.40  
  5. 0.06905,0.00,2.180,0,0.4580,7.1470,54.20,6.0622,3,222.0,18.70,396.90,5.33,36.20 
  6.  ... 

首先,我们可以更新load_dataset()函数以加载住房数据集。作为加载数据集的一部分,我们将标准化目标值。由于我们可以将浮点值限制在0到1的范围内,这将使爬坡的预测更加简单。通常不需要这样做,只是此处采用的简化搜索算法的方法。 

  1. # load or prepare the classification dataset  
  2. def load_dataset():  
  3.  url = 'https://raw.githubusercontent.com/jbrownlee/Datasets/master/housing.csv'  
  4.  df = read_csv(url, header=None 
  5.  data = df.values  
  6.  X, y = data[:, :-1], data[:, -1]  
  7.  # normalize the target  
  8.  scaler = MinMaxScaler()  
  9.  yy = y.reshape((len(y), 1))  
  10.  y = scaler.fit_transform(y)  
  11.  return X, y 

接下来,我们可以更新评分函数,以使用预期值和预测值之间的平均绝对误差。 

  1. # evaluate a set of predictions  
  2. def evaluate_predictions(y_test, yhat):  
  3.  return mean_absolute_error(y_test, yhat) 

我们还必须将解决方案的表示形式从0和1标签更新为介于0和1之间的浮点值。必须更改初始候选解的生成以创建随机浮点列表。 

  1. # create a random set of predictions  
  2. def random_predictions(n_examples):  
  3.  return [random() for _ in range(n_examples)] 

在这种情况下,对解决方案所做的单个更改以创建新的候选解决方案,包括简单地用新的随机浮点数替换列表中的随机选择的预测。我选择它是因为它很简单。 

  1. # modify the current set of predictions  
  2. def modify_predictions(current, n_changes=1):  
  3.  # copy current solution  
  4.  updated = current.copy()  
  5.  for i in range(n_changes):  
  6.   # select a point to change  
  7.   ix = randint(0, len(updated)-1)  
  8.   # flip the class label  
  9.   updated[ix] = random()  
  10.  return updated 

更好的方法是将高斯噪声添加到现有值,我将其作为扩展留给您。如果您尝试过,请在下面的评论中告诉我。例如: 

  1. # add gaussian noise  
  2. updated[ix] += gauss(0, 0.1) 

最后,必须更新搜索。最佳值现在是错误0.0,如果发现错误,该错误将用于停止搜索。 

  1. # stop once we achieve the best score  
  2. if score == 0.0:  
  3.  break 

我们还需要将搜索从最大分数更改为现在最小分数。 

  1. # check if it is as good or better  
  2. if value <= score:  
  3.  solution, score = candidate, value  
  4.  print('>%d, score=%.3f' % (i, score)) 

下面列出了具有这两个更改的更新的搜索功能。 

  1. # run a hill climb for a set of predictions  
  2. def hill_climb_testset(X_test, y_test, max_iterations):  
  3.  scores = list()  
  4.  # generate the initial solution  
  5.  solution = random_predictions(X_test.shape[0])  
  6.  # evaluate the initial solution  
  7.  score = evaluate_predictions(y_test, solution)  
  8.  print('>%.3f' % score)  
  9.  # hill climb to a solution  
  10.  for i in range(max_iterations):  
  11.   # record scores  
  12.   scores.append(score)  
  13.   # stop once we achieve the best score  
  14.   if score == 0.0:  
  15.    break  
  16.   # generate new candidate  
  17.   candidate = modify_predictions(solution)  
  18.   # evaluate candidate  
  19.   value = evaluate_predictions(y_test, candidate)  
  20.   # check if it is as good or better  
  21.   if value <= score:  
  22.    solution, score = candidate, value  
  23.    print('>%d, score=%.3f' % (i, score))  
  24.  return solution, scores 

结合在一起,下面列出了用于回归任务的测试集爬坡的完整示例。 

  1. # example of hill climbing the test set for the housing dataset  
  2. from random import random  
  3. from random import randint  
  4. from pandas import read_csv  
  5. from sklearn.model_selection import train_test_split  
  6. from sklearn.metrics import mean_absolute_error  
  7. from sklearn.preprocessing import MinMaxScaler  
  8. from matplotlib import pyplot   
  9. # load or prepare the classification dataset 
  10. def load_dataset():  
  11.  url = 'https://raw.githubusercontent.com/jbrownlee/Datasets/master/housing.csv'  
  12.  df = read_csv(url, header=None 
  13.  data = df.values  
  14.  X, y = data[:, :-1], data[:, -1]  
  15.  # normalize the target  
  16.  scaler = MinMaxScaler()  
  17.  yy = y.reshape((len(y), 1))  
  18.  y = scaler.fit_transform(y)  
  19.  return X, y  
  20. # evaluate a set of predictions  
  21. def evaluate_predictions(y_test, yhat):  
  22.  return mean_absolute_error(y_test, yhat)   
  23. # create a random set of predictions  
  24. def random_predictions(n_examples): 
  25.   return [random() for _ in range(n_examples)]  
  26. # modify the current set of predictions  
  27. def modify_predictions(current, n_changes=1):  
  28.  # copy current solution  
  29.  updated = current.copy()  
  30.  for i in range(n_changes):  
  31.   # select a point to change  
  32.   ix = randint(0, len(updated)-1)  
  33.   # flip the class label  
  34.   updated[ix] = random()  
  35.  return updated   
  36. # run a hill climb for a set of predictions  
  37. def hill_climb_testset(X_test, y_test, max_iterations):  
  38.  scores = list()  
  39.  # generate the initial solution  
  40.  solution = random_predictions(X_test.shape[0])  
  41.  # evaluate the initial solution  
  42.  score = evaluate_predictions(y_test, solution)  
  43.  print('>%.3f' % score)  
  44.  # hill climb to a solution  
  45.  for i in range(max_iterations):  
  46.   # record scores  
  47.   scores.append(score)  
  48.   # stop once we achieve the best score  
  49.   if score == 0.0:  
  50.    break  
  51.   # generate new candidate  
  52.   candidate = modify_predictions(solution)  
  53.   # evaluate candidate  
  54.   value = evaluate_predictions(y_test, candidate)  
  55.   # check if it is as good or better  
  56.   if value <= score:  
  57.    solution, score = candidate, value  
  58.    print('>%d, score=%.3f' % (i, score))  
  59.  return solution, scores  
  60. # load the dataset  
  61. X, y = load_dataset()  
  62. print(X.shape, y.shape)  
  63. # split dataset into train and test sets  
  64. X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.33, random_state=1 
  65. print(X_train.shape, X_test.shape, y_train.shape, y_test.shape)  
  66. # run hill climb  
  67. yhat, scores = hill_climb_testset(X_test, y_test, 100000)  
  68. # plot the scores vs iterations  
  69. pyplot.plot(scores)  
  70. pyplot.show() 

运行示例将在搜索过程中每次看到改进时报告迭代次数和MAE。

在这种情况下,我们将使用更多的迭代,因为要优化它是一个更复杂的问题。选择的用于创建候选解决方案的方法也使它变慢了,也不太可能实现完美的误差。实际上,我们不会实现完美的错误;相反,如果错误达到的值低于最小值(例如1e-7)或对目标域有意义的值,则最好停止操作。这也留给读者作为练习。例如: 

  1. # stop once we achieve a good enough  
  2. if score <= 1e-7:  
  3.  break 

注意:由于算法或评估程序的随机性,或者数值精度的差异,您的结果可能会有所不同。考虑运行该示例几次并比较平均结果。

在这种情况下,我们可以看到在运行结束时实现了良好的错误。 

  1. >95991, score=0.001  
  2. >96011, score=0.001  
  3. >96295, score=0.001  
  4. >96366, score=0.001  
  5. >96585, score=0.001  
  6. >97575, score=0.001  
  7. >98828, score=0.001  
  8. >98947, score=0.001  
  9. >99712, score=0.001  
  10. >99913, score=0.001 

还创建了搜索进度的折线图,显示收敛速度很快,并且在大多数迭代中保持不变。

 

 

责任编辑:庞桂玉 来源: Python中文社区 (ID:python-china)
相关推荐

2021-03-04 12:40:25

机器学习人工智能爬坡测试

2022-09-19 15:37:51

人工智能机器学习大数据

2020-06-24 07:53:03

机器学习技术人工智能

2017-04-06 09:20:10

机器学习模型信用卡诈骗

2020-08-19 09:20:00

机器学习人工智能Python

2019-12-18 10:25:12

机器学习单元测试神经网络

2020-06-10 07:46:39

机器学习预测性维护工业物联网

2022-10-24 08:02:00

2020-04-27 09:52:03

预测销售机器学习ML

2017-12-12 13:17:36

机器学习代码单元测试

2019-07-17 09:59:46

JavaScriptJava机器学习

2021-12-13 09:14:06

清单管理数据集

2019-06-19 09:13:29

机器学习中数据集深度学习

2017-11-03 12:57:06

机器学习文本数据Python

2021-09-16 15:41:59

机器学习数据科学算法

2019-04-15 13:25:29

数据科学机器学习Gartner

2018-09-15 23:23:04

Web开发机器学习软件开发

2017-05-05 09:56:08

神经网络模型绘画

2020-07-15 13:51:48

TensorFlow数据机器学习

2020-08-12 09:46:46

TensorFlow数据机器学习
点赞
收藏

51CTO技术栈公众号