自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

如何爬升用于機(jī)器學(xué)習(xí)的測(cè)試集

人工智能 機(jī)器學(xué)習(xí)
在本教程中,您將發(fā)現(xiàn)如何爬升用于機(jī)器學(xué)習(xí)的測(cè)試集。

 [[387235]]

爬坡測(cè)試集是一種在不影響訓(xùn)練集甚至開(kāi)發(fā)預(yù)測(cè)模型的情況下,在機(jī)器學(xué)習(xí)競(jìng)賽中實(shí)現(xiàn)良好或完美預(yù)測(cè)的方法。作為機(jī)器學(xué)習(xí)競(jìng)賽的一種方法,這是理所當(dāng)然的,大多數(shù)競(jìng)賽平臺(tái)都對(duì)其施加了限制,以防止出現(xiàn)這種情況,這一點(diǎn)很重要。但是,爬坡測(cè)試集是機(jī)器學(xué)習(xí)從業(yè)人員在參加比賽時(shí)不小心做的事情。通過(guò)開(kāi)發(fā)一個(gè)明確的實(shí)現(xiàn)來(lái)爬升測(cè)試集,它有助于更好地了解通過(guò)過(guò)度使用測(cè)試數(shù)據(jù)集來(lái)評(píng)估建模管道而過(guò)度擬合測(cè)試數(shù)據(jù)集的難易程度。

在本教程中,您將發(fā)現(xiàn)如何爬升用于機(jī)器學(xué)習(xí)的測(cè)試集。完成本教程后,您將知道:

  •  無(wú)需查看訓(xùn)練數(shù)據(jù)集,就可以通過(guò)爬上測(cè)試集來(lái)做出完美的預(yù)測(cè)。
  •  如何為分類和回歸任務(wù)爬坡測(cè)試集。
  •  當(dāng)我們過(guò)度使用測(cè)試集來(lái)評(píng)估建模管道時(shí),我們暗中爬升了測(cè)試集。

教程概述

本教程分為五個(gè)部分。他們是:

  •  爬坡測(cè)試儀
  •  爬山算法
  •  如何進(jìn)行爬山
  •  爬坡糖尿病分類數(shù)據(jù)集
  •  爬坡房屋回歸數(shù)據(jù)集

爬坡測(cè)試儀

像Kaggle上的機(jī)器學(xué)習(xí)比賽一樣,機(jī)器學(xué)習(xí)比賽提供了完整的訓(xùn)練數(shù)據(jù)集以及測(cè)試集的輸入。給定比賽的目的是預(yù)測(cè)目標(biāo)值,例如測(cè)試集的標(biāo)簽或數(shù)值。針對(duì)隱藏的測(cè)試設(shè)置目標(biāo)值評(píng)估解決方案,并進(jìn)行適當(dāng)評(píng)分。與測(cè)試集得分最高的參賽作品贏得了比賽。機(jī)器學(xué)習(xí)競(jìng)賽的挑戰(zhàn)可以被定義為一個(gè)優(yōu)化問(wèn)題。傳統(tǒng)上,競(jìng)賽參與者充當(dāng)優(yōu)化算法,探索導(dǎo)致不同組預(yù)測(cè)的不同建模管道,對(duì)預(yù)測(cè)進(jìn)行評(píng)分,然后對(duì)管道進(jìn)行更改以期望獲得更高的分?jǐn)?shù)。此過(guò)程也可以直接用優(yōu)化算法建模,無(wú)需查看訓(xùn)練集就可以生成和評(píng)估候選預(yù)測(cè)。通常,這稱為爬山測(cè)試集,作為解決此問(wèn)題的最簡(jiǎn)單的優(yōu)化算法之一就是爬山算法。盡管在實(shí)際的機(jī)器學(xué)習(xí)競(jìng)賽中應(yīng)該正確地爬升測(cè)試集,但是實(shí)施該方法以了解該方法的局限性和過(guò)度安裝測(cè)試集的危險(xiǎn)可能是一個(gè)有趣的練習(xí)。此外,無(wú)需接觸訓(xùn)練數(shù)據(jù)集就可以完美預(yù)測(cè)測(cè)試集的事實(shí)常常使很多初學(xué)者機(jī)器學(xué)習(xí)從業(yè)人員感到震驚。最重要的是,當(dāng)我們反復(fù)評(píng)估不同的建模管道時(shí),我們暗中爬升了測(cè)試集。風(fēng)險(xiǎn)是測(cè)試集的分?jǐn)?shù)得到了提高,但代價(jià)是泛化誤差增加,即在更廣泛的問(wèn)題上表現(xiàn)較差。進(jìn)行機(jī)器學(xué)習(xí)競(jìng)賽的人們都非常清楚這個(gè)問(wèn)題,并且對(duì)預(yù)測(cè)評(píng)估施加了限制以應(yīng)對(duì)該問(wèn)題,例如將評(píng)估限制為每天一次或幾次,并在測(cè)試集的隱藏子集而不是整個(gè)測(cè)試集上報(bào)告分?jǐn)?shù)。。有關(guān)更多信息,請(qǐng)參閱進(jìn)一步閱讀部分中列出的論文。接下來(lái),讓我們看看如何實(shí)施爬坡算法來(lái)優(yōu)化測(cè)試集的預(yù)測(cè)。

爬山算法

爬山算法是一種非常簡(jiǎn)單的優(yōu)化算法。它涉及生成候選解決方案并進(jìn)行評(píng)估。然后是逐步改進(jìn)的起點(diǎn),直到無(wú)法實(shí)現(xiàn)進(jìn)一步的改進(jìn),或者我們用光了時(shí)間,資源或興趣。從現(xiàn)有候選解決方案中生成新的候選解決方案。通常,這涉及對(duì)候選解決方案進(jìn)行單個(gè)更改,對(duì)其進(jìn)行評(píng)估,并且如果候選解決方案與先前的當(dāng)前解決方案一樣好或更好,則將該候選解決方案接受為新的“當(dāng)前”解決方案。否則,將其丟棄。我們可能會(huì)認(rèn)為只接受分?jǐn)?shù)更高的候選人是一個(gè)好主意。對(duì)于許多簡(jiǎn)單問(wèn)題,這是一種合理的方法,盡管在更復(fù)雜的問(wèn)題上,希望接受具有相同分?jǐn)?shù)的不同候選者,以幫助搜索過(guò)程縮放要素空間中的平坦區(qū)域(高原)。當(dāng)爬上測(cè)試集時(shí),候選解決方案是預(yù)測(cè)列表。對(duì)于二進(jìn)制分類任務(wù),這是兩個(gè)類的0和1值的列表。對(duì)于回歸任務(wù),這是目標(biāo)變量范圍內(nèi)的數(shù)字列表。對(duì)候選分類解決方案的修改將是選擇一個(gè)預(yù)測(cè)并將其從0翻轉(zhuǎn)為1或從1翻轉(zhuǎn)為0。對(duì)回歸進(jìn)行候選解決方案的修改將是將高斯噪聲添加到列表中的一個(gè)值或替換一個(gè)值在列表中使用新值。解決方案的評(píng)分涉及計(jì)算評(píng)分指標(biāo),例如分類任務(wù)的分類準(zhǔn)確性或回歸任務(wù)的平均絕對(duì)誤差。現(xiàn)在我們已經(jīng)熟悉了算法,現(xiàn)在就來(lái)實(shí)現(xiàn)它。

如何進(jìn)行爬山

我們將在綜合分類任務(wù)上開(kāi)發(fā)爬坡算法。首先,我們創(chuàng)建一個(gè)包含許多輸入變量和5,000行示例的二進(jìn)制分類任務(wù)。然后,我們可以將數(shù)據(jù)集分為訓(xùn)練集和測(cè)試集。下面列出了完整的示例。 

  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) 

運(yùn)行示例首先報(bào)告創(chuàng)建的數(shù)據(jù)集的形狀,顯示5,000行和20個(gè)輸入變量。然后將數(shù)據(jù)集分為訓(xùn)練集和測(cè)試集,其中約3,300個(gè)用于訓(xùn)練,約1,600個(gè)用于測(cè)試。 

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

現(xiàn)在我們可以開(kāi)發(fā)一個(gè)登山者。首先,我們可以創(chuàng)建一個(gè)將加載的函數(shù),或者在這種情況下,定義數(shù)據(jù)集。當(dāng)我們要更改數(shù)據(jù)集時(shí),可以稍后更新此功能。 

  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

接下來(lái),我們需要一個(gè)函數(shù)來(lái)評(píng)估候選解決方案,即預(yù)測(cè)列表。我們將使用分類精度,其中分?jǐn)?shù)范圍在0(最壞的解決方案)到1(完美的預(yù)測(cè)集)之間。 

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

接下來(lái),我們需要一個(gè)函數(shù)來(lái)創(chuàng)建初始候選解決方案。這是0和1類標(biāo)簽的預(yù)測(cè)列表,長(zhǎng)度足以匹配測(cè)試集中的示例數(shù),在這種情況下為1650。我們可以使用randint()函數(shù)生成0和1的隨機(jī)值。 

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

接下來(lái),我們需要一個(gè)函數(shù)來(lái)創(chuàng)建候選解決方案的修改版本。在這種情況下,這涉及在解決方案中選擇一個(gè)值并將其從0翻轉(zhuǎn)為1或從1翻轉(zhuǎn)為0。通常,我們會(huì)在爬坡期間對(duì)每個(gè)新的候選解決方案進(jìn)行一次更改,但是我已經(jīng)對(duì)該函數(shù)進(jìn)行了參數(shù)化,因此您可以根據(jù)需要探索多個(gè)更改。 

  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 

到現(xiàn)在為止還挺好。接下來(lái),我們可以開(kāi)發(fā)執(zhí)行搜索的功能。首先,通過(guò)調(diào)用random_predictions()函數(shù)和隨后的validate_predictions()函數(shù)來(lái)創(chuàng)建和評(píng)估初始解決方案。然后,我們循環(huán)進(jìn)行固定次數(shù)的迭代,并通過(guò)調(diào)用Modify_predictions()生成一個(gè)新的候選值,對(duì)其進(jìn)行求值,如果分?jǐn)?shù)與當(dāng)前解決方案相同或更好,則將其替換。當(dāng)我們完成預(yù)設(shè)的迭代次數(shù)(任意選擇)或達(dá)到理想分?jǐn)?shù)時(shí),該循環(huán)結(jié)束,在這種情況下,我們知道其精度為1.0(100%)。下面的函數(shù)hill_climb_testset()實(shí)現(xiàn)了此功能,將測(cè)試集作為輸入并返回在爬坡過(guò)程中發(fā)現(xiàn)的最佳預(yù)測(cè)集。 

  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 

這里的所有都是它的。下面列出了爬坡測(cè)試裝置的完整示例。 

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

運(yùn)行示例將使搜索進(jìn)行20,000次迭代,或者如果達(dá)到理想的準(zhǔn)確性,則停止搜索。注意:由于算法或評(píng)估程序的隨機(jī)性,或者數(shù)值精度的差異,您的結(jié)果可能會(huì)有所不同??紤]運(yùn)行該示例幾次并比較平均結(jié)果。在這種情況下,我們?cè)诩s12,900次迭代中找到了一組理想的測(cè)試集預(yù)測(cè)?;叵胍幌拢@是在不接觸訓(xùn)練數(shù)據(jù)集且不通過(guò)查看測(cè)試集目標(biāo)值進(jìn)行欺騙的情況下實(shí)現(xiàn)的。相反,我們只是簡(jiǎn)單地優(yōu)化了一組數(shù)字。這里的教訓(xùn)是,將測(cè)試管道用作爬山優(yōu)化算法,對(duì)測(cè)試集重復(fù)建模管道的評(píng)估會(huì)做同樣的事情。解決方案將過(guò)度適合測(cè)試集。 

  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 

還創(chuàng)建了優(yōu)化進(jìn)度圖。這有助于了解優(yōu)化算法的更改(例如,在坡道上更改內(nèi)容的選擇以及更改方式)如何影響搜索的收斂性。

爬坡糖尿病分類數(shù)據(jù)集

我們將使用糖尿病數(shù)據(jù)集作為探索爬坡測(cè)試集以解決分類問(wèn)題的基礎(chǔ)。每條記錄都描述了女性的醫(yī)療細(xì)節(jié),并且預(yù)測(cè)是未來(lái)五年內(nèi)糖尿病的發(fā)作。

數(shù)據(jù)集詳細(xì)信息:pima-indians-diabetes.names數(shù)據(jù)集:pima-indians-diabetes.csv

數(shù)據(jù)集有八個(gè)輸入變量和768行數(shù)據(jù);輸入變量均為數(shù)字,目標(biāo)具有兩個(gè)類別標(biāo)簽,例如 這是一個(gè)二進(jìn)制分類任務(wù)。下面提供了數(shù)據(jù)集前五行的示例。 

  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直接加載數(shù)據(jù)集,如下所示。 

  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] 

其余代碼保持不變。創(chuàng)建該文件是為了使您可以放入自己的二進(jìn)制分類任務(wù)并進(jìn)行嘗試。下面列出了完整的示例。 

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

運(yùn)行示例將報(bào)告每次搜索過(guò)程中看到改進(jìn)時(shí)的迭代次數(shù)和準(zhǔn)確性。

在這種情況下,我們使用的迭代次數(shù)較少,因?yàn)橐M(jìn)行的預(yù)測(cè)較少,因此優(yōu)化起來(lái)比較簡(jiǎn)單。

注意:由于算法或評(píng)估程序的隨機(jī)性,或者數(shù)值精度的差異,您的結(jié)果可能會(huì)有所不同??紤]運(yùn)行該示例幾次并比較平均結(jié)果。

在這種情況下,我們可以看到在大約1,500次迭代中達(dá)到了完美的精度。 

  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 

還創(chuàng)建了搜索進(jìn)度的折線圖,表明收斂迅速。

爬坡房屋回歸數(shù)據(jù)集

我們將使用住房數(shù)據(jù)集作為探索爬坡測(cè)試集回歸問(wèn)題的基礎(chǔ)。住房數(shù)據(jù)集包含給定房屋及其附近地區(qū)詳細(xì)信息的數(shù)千美元房屋價(jià)格預(yù)測(cè)。

數(shù)據(jù)集詳細(xì)信息:housing.names數(shù)據(jù)集:housing.csv

這是一個(gè)回歸問(wèn)題,這意味著我們正在預(yù)測(cè)一個(gè)數(shù)值。共有506個(gè)觀測(cè)值,其中包含13個(gè)輸入變量和一個(gè)輸出變量。下面列出了前五行的示例。 

  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()函數(shù)以加載住房數(shù)據(jù)集。作為加載數(shù)據(jù)集的一部分,我們將標(biāo)準(zhǔn)化目標(biāo)值。由于我們可以將浮點(diǎn)值限制在0到1的范圍內(nèi),這將使爬坡的預(yù)測(cè)更加簡(jiǎn)單。通常不需要這樣做,只是此處采用的簡(jiǎn)化搜索算法的方法。 

  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 

接下來(lái),我們可以更新評(píng)分函數(shù),以使用預(yù)期值和預(yù)測(cè)值之間的平均絕對(duì)誤差。 

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

我們還必須將解決方案的表示形式從0和1標(biāo)簽更新為介于0和1之間的浮點(diǎn)值。必須更改初始候選解的生成以創(chuàng)建隨機(jī)浮點(diǎn)列表。 

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

在這種情況下,對(duì)解決方案所做的單個(gè)更改以創(chuàng)建新的候選解決方案,包括簡(jiǎn)單地用新的隨機(jī)浮點(diǎn)數(shù)替換列表中的隨機(jī)選擇的預(yù)測(cè)。我選擇它是因?yàn)樗芎?jiǎn)單。 

  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 

更好的方法是將高斯噪聲添加到現(xiàn)有值,我將其作為擴(kuò)展留給您。如果您嘗試過(guò),請(qǐng)?jiān)谙旅娴脑u(píng)論中告訴我。例如: 

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

最后,必須更新搜索。最佳值現(xiàn)在是錯(cuò)誤0.0,如果發(fā)現(xiàn)錯(cuò)誤,該錯(cuò)誤將用于停止搜索。 

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

我們還需要將搜索從最大分?jǐn)?shù)更改為現(xiàn)在最小分?jǐn)?shù)。 

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

下面列出了具有這兩個(gè)更改的更新的搜索功能。 

  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 

結(jié)合在一起,下面列出了用于回歸任務(wù)的測(cè)試集爬坡的完整示例。 

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

運(yùn)行示例將在搜索過(guò)程中每次看到改進(jìn)時(shí)報(bào)告迭代次數(shù)和MAE。

在這種情況下,我們將使用更多的迭代,因?yàn)橐獌?yōu)化它是一個(gè)更復(fù)雜的問(wèn)題。選擇的用于創(chuàng)建候選解決方案的方法也使它變慢了,也不太可能實(shí)現(xiàn)完美的誤差。實(shí)際上,我們不會(huì)實(shí)現(xiàn)完美的錯(cuò)誤;相反,如果錯(cuò)誤達(dá)到的值低于最小值(例如1e-7)或?qū)δ繕?biāo)域有意義的值,則最好停止操作。這也留給讀者作為練習(xí)。例如: 

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

注意:由于算法或評(píng)估程序的隨機(jī)性,或者數(shù)值精度的差異,您的結(jié)果可能會(huì)有所不同??紤]運(yùn)行該示例幾次并比較平均結(jié)果。

在這種情況下,我們可以看到在運(yùn)行結(jié)束時(shí)實(shí)現(xiàn)了良好的錯(cuò)誤。 

  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 

還創(chuàng)建了搜索進(jìn)度的折線圖,顯示收斂速度很快,并且在大多數(shù)迭代中保持不變。

 

 

責(zé)任編輯:龐桂玉 來(lái)源: Python中文社區(qū) (ID:python-china)
相關(guān)推薦

2021-03-04 12:40:25

機(jī)器學(xué)習(xí)人工智能爬坡測(cè)試

2022-09-19 15:37:51

人工智能機(jī)器學(xué)習(xí)大數(shù)據(jù)

2020-08-19 09:20:00

機(jī)器學(xué)習(xí)人工智能Python

2020-06-24 07:53:03

機(jī)器學(xué)習(xí)技術(shù)人工智能

2017-04-06 09:20:10

機(jī)器學(xué)習(xí)模型信用卡詐騙

2019-12-18 10:25:12

機(jī)器學(xué)習(xí)單元測(cè)試神經(jīng)網(wǎng)絡(luò)

2022-10-24 08:02:00

2020-04-27 09:52:03

預(yù)測(cè)銷售機(jī)器學(xué)習(xí)ML

2020-06-10 07:46:39

機(jī)器學(xué)習(xí)預(yù)測(cè)性維護(hù)工業(yè)物聯(lián)網(wǎng)

2017-12-12 13:17:36

機(jī)器學(xué)習(xí)代碼單元測(cè)試

2019-07-17 09:59:46

JavaScriptJava機(jī)器學(xué)習(xí)

2024-07-10 14:25:20

2021-12-13 09:14:06

清單管理數(shù)據(jù)集

2019-06-19 09:13:29

機(jī)器學(xué)習(xí)中數(shù)據(jù)集深度學(xué)習(xí)

2021-09-16 15:41:59

機(jī)器學(xué)習(xí)數(shù)據(jù)科學(xué)算法

2019-04-15 13:25:29

數(shù)據(jù)科學(xué)機(jī)器學(xué)習(xí)Gartner

2018-09-15 23:23:04

Web開(kāi)發(fā)機(jī)器學(xué)習(xí)軟件開(kāi)發(fā)

2017-11-03 12:57:06

機(jī)器學(xué)習(xí)文本數(shù)據(jù)Python

2020-08-12 09:46:46

TensorFlow數(shù)據(jù)機(jī)器學(xué)習(xí)

2020-07-15 13:51:48

TensorFlow數(shù)據(jù)機(jī)器學(xué)習(xí)
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)