<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          用 Python 神經(jīng)網(wǎng)絡預測汽車保險支出

          共 30985字,需瀏覽 62分鐘

           ·

          2021-02-27 12:13

          ↑↑↑關注后"星標"簡說Python
          人人都可以簡單入門Python、爬蟲、數(shù)據(jù)分析
           簡說Python推薦 
          來源/Python中文社區(qū)
          作者/沂水寒城



          為新數(shù)據(jù)集開發(fā)神經(jīng)網(wǎng)絡預測模型可能具有挑戰(zhàn)性。
          一種方法是首先檢查數(shù)據(jù)集并為可能使用的模型開發(fā)思路,然后探索數(shù)據(jù)集上簡單模型的學習動態(tài),然后最后使用健壯的測試工具為數(shù)據(jù)集開發(fā)和調(diào)整模型。此過程可用于為分類和回歸預測建模問題開發(fā)有效的神經(jīng)網(wǎng)絡模型。
          在本教程中,您將發(fā)現(xiàn)如何為瑞典汽車保險回歸數(shù)據(jù)集開發(fā)多層Perceptron神經(jīng)網(wǎng)絡模型。完成本教程后,您將知道:
          • 如何加載和匯總?cè)鸬淦嚤kU數(shù)據(jù)集,以及如何使用結(jié)果建議要使用的數(shù)據(jù)準備和模型配置。
          • 如何探索簡單的MLP模型的學習動態(tài)以及數(shù)據(jù)集上的數(shù)據(jù)轉(zhuǎn)換。
          • 如何開發(fā)出對模型性能的可靠估計,調(diào)整模型性能以及對新數(shù)據(jù)進行預測。
          教程概述
          本教程分為四個部分。他們是:
          • 汽車保險回歸數(shù)據(jù)集
          • 首個MLP和學習動力
          • 評估和調(diào)整MLP模型
          • 最終模型和做出預測
          汽車保險回歸數(shù)據(jù)集
          第一步是定義和探索數(shù)據(jù)集。我們將使用“汽車保險”標準回歸數(shù)據(jù)集。該數(shù)據(jù)集描述了瑞典的汽車保險。只有一個輸入變量,即索賠的數(shù)量,目標變量是以數(shù)千瑞典克朗為單位的索賠總額。目的是在給定索賠數(shù)量的情況下預測總付款額。
          您可以在此處了解有關數(shù)據(jù)集的更多信息:
          • 汽車保險數(shù)據(jù)集(auto-insurance.csv
          • 汽車保險數(shù)據(jù)集詳細信息(auto-insurance.names
          您可以在下面看到數(shù)據(jù)集的前幾行。
          108,392.5
          19,46.2
          13,15.7
          124,422.2
          40,119.4
          我們可以看到這些值是數(shù)字的,范圍從幾十到幾百。這表明在使用神經(jīng)網(wǎng)絡進行建模時,某種類型的縮放比例適合于數(shù)據(jù)。
          我們可以直接從URL將數(shù)據(jù)集作為pandas DataFrame加載;例如:
          # load the dataset and summarize the shape
          from pandas import read_csv
          # define the location of the dataset
          url = 'https://raw.githubusercontent.com/jbrownlee/Datasets/master/auto-insurance.csv'
          # load the dataset
          df = read_csv(url, header=None)
          # summarize shape
          print(df.shape)
          運行示例將直接從URL加載數(shù)據(jù)集并報告數(shù)據(jù)集的形狀。
          在這種情況下,我們可以確認該數(shù)據(jù)集具有兩個變量(一個輸入和一個輸出),并且該數(shù)據(jù)集具有63行數(shù)據(jù)。
          對于神經(jīng)網(wǎng)絡來說,這不是很多數(shù)據(jù)行,這表明一個小型的網(wǎng)絡(可能帶有正則化)將是合適的。
          這也表明使用k倍交叉驗證是一個好主意,因為與火車/測試拆分相比,它可以提供更可靠的模型性能估算值,并且因為單個模型可以在數(shù)秒而不是數(shù)小時或數(shù)天的時間內(nèi)完成擬合。最大的數(shù)據(jù)集。
          (632)
          接下來,我們可以通過查看摘要統(tǒng)計信息和數(shù)據(jù)圖來了解有關數(shù)據(jù)集的更多信息。
          # show summary statistics and plots of the dataset
          from pandas import read_csv
          from matplotlib import pyplot
          # define the location of the dataset
          url = 'https://raw.githubusercontent.com/jbrownlee/Datasets/master/auto-insurance.csv'
          # load the dataset
          df = read_csv(url, header=None)
          # show summary statistics
          print(df.describe())
          # plot histograms
          df.hist()
          pyplot.show()
          運行示例之前,先加載數(shù)據(jù),然后輸出每個變量的摘要統(tǒng)計信息
          我們可以看到每個變量的平均值在十個以內(nèi),范圍從0到數(shù)百。這證實了縮放數(shù)據(jù)可能是一個好主意。
                          0           1
          count   63.000000   63.000000
          mean    22.904762   98.187302
          std     23.351946   87.327553
          min      0.000000    0.000000
          25%      7.500000   38.850000
          50%     14.000000   73.400000
          75%     29.000000  140.000000
          max    124.000000  422.200000
          然后為每個變量創(chuàng)建一個直方圖。
          我們可以看到每個變量都有相似的分布。它看起來像偏態(tài)的高斯分布或指數(shù)分布。
          我們可以在每個變量上使用冪變換來降低概率分布的偏斜度,這可能會提高模型性能。

          現(xiàn)在我們已經(jīng)熟悉了數(shù)據(jù)集,讓我們探討如何開發(fā)神經(jīng)網(wǎng)絡模型。
          首個MLP和學習動力
          我們將使用TensorFlow為數(shù)據(jù)集開發(fā)一個多層感知器(MLP)模型。我們不知道學習超參數(shù)的哪種模型架構(gòu)對這個數(shù)據(jù)集將是好的還是最好的,所以我們必須進行實驗并發(fā)現(xiàn)什么是行之有效的。假設數(shù)據(jù)集很小,則小批量可能是個好主意,例如8或16行。入門時,使用Adam版本的隨機梯度下降法是一個好主意,因為它會自動適應學習率,并且在大多數(shù)數(shù)據(jù)集上都能很好地工作。在認真評估模型之前,最好回顧一下學習動態(tài)并調(diào)整模型體系結(jié)構(gòu)和學習配置,直到我們擁有穩(wěn)定的學習動態(tài),然后再充分利用模型。
          我們可以通過簡單的訓練/測試數(shù)據(jù)拆分并查看學習曲線圖來實現(xiàn)。這將幫助我們了解我們是學習過度還是學習不足;然后我們可以相應地調(diào)整配置。首先,我們可以將數(shù)據(jù)集分為輸入和輸出變量,然后分為67/33訓練和測試集。
          # split into input and output columns
          X, y = df.values[:, :-1], df.values[:, -1]
          # split into train and test datasets
          X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.33)
          接下來,我們可以定義一個最小的MLP模型。在這種情況下,我們將使用一個包含10個節(jié)點的隱藏層和一個輸出層(任意選擇)。我們將在隱藏層中使用ReLU激活功能和“ he_normal”權(quán)重初始化,因為它們是一種很好的做法。
          模型的輸出是線性激活(不激活),我們將最小化均方誤差(MSE)損失。
          # determine the number of input features
          n_features = X.shape[1]
          # define model
          model = Sequential()
          model.add(Dense(10, activation='relu', kernel_initializer='he_normal', input_shape=(n_features,)))
          model.add(Dense(1))
          # compile the model
          model.compile(optimizer='adam', loss='mse')
          我們將模型擬合為100個訓練時期(任意選擇),批量為8個,因為它是一個很小的數(shù)據(jù)集。我們正在原始數(shù)據(jù)上擬合模型,我們認為這可能不是一個好主意,但這是一個重要的起點。
          # fit the model
          history = model.fit(X_train, y_train, epochs=100, batch_size=8, verbose=0, validation_data=(X_test,y_test))
          在訓練結(jié)束時,我們將評估模型在測試數(shù)據(jù)集上的性能,并將性能報告為平均絕對誤差(MAE),我通常更喜歡MSE或RMSE。
          # predict test set
          yhat = model.predict(X_test)
          # evaluate predictions
          score = mean_absolute_error(y_test, yhat)
          print('MAE: %.3f' % score)
          最后,我們將在訓練期間在訓練和測試集上繪制MSE損失的學習曲線。
          # plot learning curves
          pyplot.title('Learning Curves')
          pyplot.xlabel('Epoch')
          pyplot.ylabel('Mean Squared Error')
          pyplot.plot(history.history['loss'], label='train')
          pyplot.plot(history.history['val_loss'], label='val')
          pyplot.legend()
          pyplot.show()
          綜上所述,下面列出了評估我們在汽車保險數(shù)據(jù)集上的第一個MLP的完整示例。
          # fit a simple mlp model and review learning curves
          from pandas import read_csv
          from sklearn.model_selection import train_test_split
          from sklearn.metrics import mean_absolute_error
          from tensorflow.keras import Sequential
          from tensorflow.keras.layers import Dense
          from matplotlib import pyplot
          # load the dataset
          path = 'https://raw.githubusercontent.com/jbrownlee/Datasets/master/auto-insurance.csv'
          df = read_csv(path, header=None)
          # split into input and output columns
          X, y = df.values[:, :-1], df.values[:, -1]
          # split into train and test datasets
          X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.33)
          # determine the number of input features
          n_features = X.shape[1]
          # define model
          model = Sequential()
          model.add(Dense(10, activation='relu', kernel_initializer='he_normal', input_shape=(n_features,)))
          model.add(Dense(1))
          # compile the model
          model.compile(optimizer='adam', loss='mse')
          # fit the model
          history = model.fit(X_train, y_train, epochs=100, batch_size=8, verbose=0, validation_data=(X_test,y_test))
          # predict test set
          yhat = model.predict(X_test)
          # evaluate predictions
          score = mean_absolute_error(y_test, yhat)
          print('MAE: %.3f' % score)
          # plot learning curves
          pyplot.title('Learning Curves')
          pyplot.xlabel('Epoch')
          pyplot.ylabel('Mean Squared Error')
          pyplot.plot(history.history['loss'], label='train')
          pyplot.plot(history.history['val_loss'], label='val')
          pyplot.legend()
          pyplot.show()
          運行示例首先使模型適合訓練數(shù)據(jù)集,然后報告測試數(shù)據(jù)集的MAE。
          注意:由于算法或評估程序的隨機性,或者數(shù)值精度的差異,您的結(jié)果可能會有所不同。考慮運行該示例幾次并比較平均結(jié)果。
          在這種情況下,我們可以看到該模型實現(xiàn)了大約33.2的MAE,這是性能的良好基準,我們可能可以對其進行改進。
          MAE: 33.233
          然后在火車和測試裝置上創(chuàng)建MSE的線圖。
          我們可以看到該模型具有良好的擬合度,并且收斂良好。模型的配置是一個很好的起點。

          到目前為止,學習動力很好,MAE是一個粗略的估計,不應該被依賴。
          我們可能可以稍微增加模型的容量,并期待類似的學習動態(tài)。例如,我們可以添加具有八個節(jié)點(任意選擇)的第二個隱藏層,并將訓練時期數(shù)增加一倍至200。
          # define model
          model = Sequential()
          model.add(Dense(10, activation='relu', kernel_initializer='he_normal', input_shape=(n_features,)))
          model.add(Dense(8, activation='relu', kernel_initializer='he_normal'))
          model.add(Dense(1))
          # compile the model
          model.compile(optimizer='adam', loss='mse')
          # fit the model
          history = model.fit(X_train, y_train, epochs=200, batch_size=8, verbose=0, validation_data=(X_test,y_test))
          完整實例如下:
          # fit a deeper mlp model and review learning curves
          from pandas import read_csv
          from sklearn.model_selection import train_test_split
          from sklearn.metrics import mean_absolute_error
          from tensorflow.keras import Sequential
          from tensorflow.keras.layers import Dense
          from matplotlib import pyplot
          # load the dataset
          path = 'https://raw.githubusercontent.com/jbrownlee/Datasets/master/auto-insurance.csv'
          df = read_csv(path, header=None)
          # split into input and output columns
          X, y = df.values[:, :-1], df.values[:, -1]
          # split into train and test datasets
          X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.33)
          # determine the number of input features
          n_features = X.shape[1]
          # define model
          model = Sequential()
          model.add(Dense(10, activation='relu', kernel_initializer='he_normal', input_shape=(n_features,)))
          model.add(Dense(8, activation='relu', kernel_initializer='he_normal'))
          model.add(Dense(1))
          # compile the model
          model.compile(optimizer='adam', loss='mse')
          # fit the model
          history = model.fit(X_train, y_train, epochs=200, batch_size=8, verbose=0, validation_data=(X_test,y_test))
          # predict test set
          yhat = model.predict(X_test)
          # evaluate predictions
          score = mean_absolute_error(y_test, yhat)
          print('MAE: %.3f' % score)
          # plot learning curves
          pyplot.title('Learning Curves')
          pyplot.xlabel('Epoch')
          pyplot.ylabel('Mean Squared Error')
          pyplot.plot(history.history['loss'], label='train')
          pyplot.plot(history.history['val_loss'], label='val')
          pyplot.legend()
          pyplot.show()
          運行示例首先使模型適合訓練數(shù)據(jù)集,然后報告測試數(shù)據(jù)集的MAE。
          注意:由于算法或評估程序的隨機性,或者數(shù)值精度的差異,您的結(jié)果可能會有所不同。考慮運行該示例幾次并比較平均結(jié)果。
          在這種情況下,我們可以看到MAE略有改善,約為27.9,盡管訓練/測試拆分的高方差意味著該評估是不可靠的。
          MAE: 27.939
          然后繪制MSE訓練和測試集的學習曲線。我們可以看到,正如預期的那樣,該模型在合理的迭代次數(shù)內(nèi)實現(xiàn)了良好的擬合和收斂。

          最后,我們可以嘗試轉(zhuǎn)換數(shù)據(jù),看看它如何影響學習動力。
          在這種情況下,我們將使用冪變換來減少數(shù)據(jù)分布的偏差。這還將自動標準化變量,以使它們的平均值為零,標準偏差為1,這是使用神經(jīng)網(wǎng)絡進行建模時的一種好習慣。
          首先,我們必須確保目標變量是二維數(shù)組。
          # ensure that the target variable is a 2d array
          y_train, y_test = y_train.reshape((len(y_train),1)), y_test.reshape((len(y_test),1))
          接下來,我們可以將PowerTransformer應用于輸入變量和目標變量。
          這可以通過首先將轉(zhuǎn)換適合訓練數(shù)據(jù),然后轉(zhuǎn)換訓練和測試集來實現(xiàn)。
          此過程將分別應用于輸入和輸出變量,以避免數(shù)據(jù)泄漏。
          # power transform input data
          pt1 = PowerTransformer()
          pt1.fit(X_train)
          X_train = pt1.transform(X_train)
          X_test = pt1.transform(X_test)
          # power transform output data
          pt2 = PowerTransformer()
          pt2.fit(y_train)
          y_train = pt2.transform(y_train)
          y_test = pt2.transform(y_test)
          然后將數(shù)據(jù)用于擬合模型。
          后面可以根據(jù)模型做出的預測以及測試集中的預期目標值對變換進行求逆,我們可以像以前一樣以正確的比例計算MAE。
          # inverse transforms on target variable
          y_test = pt2.inverse_transform(y_test)
          yhat = pt2.inverse_transform(yhat)
          結(jié)合在一起,下面列出了使用轉(zhuǎn)換后的數(shù)據(jù)擬合和評估MLP以及創(chuàng)建模型的學習曲線的完整示例。
          # fit a mlp model with data transforms and review learning curves
          from pandas import read_csv
          from sklearn.model_selection import train_test_split
          from sklearn.metrics import mean_absolute_error
          from sklearn.preprocessing import PowerTransformer
          from tensorflow.keras import Sequential
          from tensorflow.keras.layers import Dense
          from matplotlib import pyplot
          # load the dataset
          path = 'https://raw.githubusercontent.com/jbrownlee/Datasets/master/auto-insurance.csv'
          df = read_csv(path, header=None)
          # split into input and output columns
          X, y = df.values[:, :-1], df.values[:, -1]
          # split into train and test datasets
          X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.33)
          # ensure that the target variable is a 2d array
          y_train, y_test = y_train.reshape((len(y_train),1)), y_test.reshape((len(y_test),1))
          # power transform input data
          pt1 = PowerTransformer()
          pt1.fit(X_train)
          X_train = pt1.transform(X_train)
          X_test = pt1.transform(X_test)
          # power transform output data
          pt2 = PowerTransformer()
          pt2.fit(y_train)
          y_train = pt2.transform(y_train)
          y_test = pt2.transform(y_test)
          # determine the number of input features
          n_features = X.shape[1]
          # define model
          model = Sequential()
          model.add(Dense(10, activation='relu', kernel_initializer='he_normal', input_shape=(n_features,)))
          model.add(Dense(8, activation='relu', kernel_initializer='he_normal'))
          model.add(Dense(1))
          # compile the model
          model.compile(optimizer='adam', loss='mse')
          # fit the model
          history = model.fit(X_train, y_train, epochs=200, batch_size=8, verbose=0, validation_data=(X_test,y_test))
          # predict test set
          yhat = model.predict(X_test)
          # inverse transforms on target variable
          y_test = pt2.inverse_transform(y_test)
          yhat = pt2.inverse_transform(yhat)
          # evaluate predictions
          score = mean_absolute_error(y_test, yhat)
          print('MAE: %.3f' % score)
          # plot learning curves
          pyplot.title('Learning Curves')
          pyplot.xlabel('Epoch')
          pyplot.ylabel('Mean Squared Error')
          pyplot.plot(history.history['loss'], label='train')
          pyplot.plot(history.history['val_loss'], label='val')
          pyplot.legend()
          pyplot.show()
          運行示例首先使模型適合訓練數(shù)據(jù)集,然后報告測試數(shù)據(jù)集的MAE。
          注意:由于算法或評估程序的隨機性,或者數(shù)值精度的差異,您的結(jié)果可能會有所不同。考慮運行該示例幾次并比較平均結(jié)果。
          在這種情況下,該模型可以達到合理的MAE分數(shù),盡管比以前報告的性能差。我們暫時將忽略模型性能。
          MAE: 34.320
          創(chuàng)建了學習曲線的線圖,表明該模型達到了合理的擬合并且有足夠的時間收斂。

          現(xiàn)在,我們對帶有或不帶有數(shù)據(jù)轉(zhuǎn)換的簡單MLP模型的學習動態(tài)有了一些了解,現(xiàn)在我們可以看一下評估模型的性能以及調(diào)整模型的配置。
          評估和調(diào)整MLP模型
          k倍交叉驗證過程可以提供更可靠的MLP性能估計,盡管它可能非常慢。這是因為必須擬合和評估k個模型。當數(shù)據(jù)集大小較小時(例如汽車保險數(shù)據(jù)集),這不是問題。我們可以使用KFold類創(chuàng)建拆分并手動枚舉每個折疊,擬合模型,對其進行評估,然后在過程結(jié)束時報告評估分數(shù)的平均值。
          # prepare cross validation
          kfold = KFold(10)
          # enumerate splits
          scores = list()
          for train_ix, test_ix in kfold.split(X, y):
           # fit and evaluate the model...
           ...
          ...
          # summarize all scores
          print('Mean MAE: %.3f (%.3f)' % (mean(scores), std(scores)))
          我們可以使用此框架通過一系列不同的數(shù)據(jù)準備,模型架構(gòu)和學習配置來開發(fā)MLP模型性能的可靠估計。
          重要的是,在使用k-fold交叉驗證來評估性能之前,我們首先對上一部分中的數(shù)據(jù)集模型的學習動態(tài)有了了解。如果我們開始直接調(diào)整模型,我們可能會獲得良好的結(jié)果,但是如果沒有,我們可能不知道為什么,例如 模型超出或不足。
          如果我們再次對模型進行較大的更改,則最好返回并確認模型正在適當收斂。
          下面列出了評估上一節(jié)中的基本MLP模型的此框架的完整示例。
          # k-fold cross-validation of base model for the auto insurance regression dataset
          from numpy import mean
          from numpy import std
          from pandas import read_csv
          from sklearn.model_selection import KFold
          from sklearn.metrics import mean_absolute_error
          from tensorflow.keras import Sequential
          from tensorflow.keras.layers import Dense
          from matplotlib import pyplot
          # load the dataset
          path = 'https://raw.githubusercontent.com/jbrownlee/Datasets/master/auto-insurance.csv'
          df = read_csv(path, header=None)
          # split into input and output columns
          X, y = df.values[:, :-1], df.values[:, -1]
          # prepare cross validation
          kfold = KFold(10)
          # enumerate splits
          scores = list()
          for train_ix, test_ix in kfold.split(X, y):
           # split data
           X_train, X_test, y_train, y_test = X[train_ix], X[test_ix], y[train_ix], y[test_ix]
           # determine the number of input features
           n_features = X.shape[1]
           # define model
           model = Sequential()
           model.add(Dense(10, activation='relu', kernel_initializer='he_normal', input_shape=(n_features,)))
           model.add(Dense(1))
           # compile the model
           model.compile(optimizer='adam', loss='mse')
           # fit the model
           model.fit(X_train, y_train, epochs=100, batch_size=8, verbose=0)
           # predict test set
           yhat = model.predict(X_test)
           # evaluate predictions
           score = mean_absolute_error(y_test, yhat)
           print('>%.3f' % score)
           scores.append(score)
          # summarize all scores
          print('Mean MAE: %.3f (%.3f)' % (mean(scores), std(scores)))
          運行示例將在評估過程的每次迭代中報告模型性能,并在運行結(jié)束時報告MAE的平均值和標準偏差。
          注意:由于算法或評估程序的隨機性,或者數(shù)值精度的差異,您的結(jié)果可能會有所不同。考慮運行該示例幾次并比較平均結(jié)果。
          在這種情況下,我們可以看到MLP模型的MAE約為38.913。
          我們將使用此結(jié)果作為基準,以查看是否可以實現(xiàn)更好的性能。
          >27.314
          >69.577
          >20.891
          >14.810
          >13.412
          >69.540
          >25.612
          >49.508
          >35.769
          >62.696
          Mean MAE: 38.913 (21.056)
          首先,讓我們嘗試在原始數(shù)據(jù)集上評估更深的模型,以查看其性能是否比基準模型更好。
          # define model
          model = Sequential()
          model.add(Dense(10, activation='relu', kernel_initializer='he_normal', input_shape=(n_features,)))
          model.add(Dense(8, activation='relu', kernel_initializer='he_normal'))
          model.add(Dense(1))
          # compile the model
          model.compile(optimizer='adam', loss='mse')
          # fit the model
          model.fit(X_train, y_train, epochs=200, batch_size=8, verbose=0)
          完整實例如下:
          # k-fold cross-validation of deeper model for the auto insurance regression dataset
          from numpy import mean
          from numpy import std
          from pandas import read_csv
          from sklearn.model_selection import KFold
          from sklearn.metrics import mean_absolute_error
          from tensorflow.keras import Sequential
          from tensorflow.keras.layers import Dense
          from matplotlib import pyplot
          # load the dataset
          path = 'https://raw.githubusercontent.com/jbrownlee/Datasets/master/auto-insurance.csv'
          df = read_csv(path, header=None)
          # split into input and output columns
          X, y = df.values[:, :-1], df.values[:, -1]
          # prepare cross validation
          kfold = KFold(10)
          # enumerate splits
          scores = list()
          for train_ix, test_ix in kfold.split(X, y):
           # split data
           X_train, X_test, y_train, y_test = X[train_ix], X[test_ix], y[train_ix], y[test_ix]
           # determine the number of input features
           n_features = X.shape[1]
           # define model
           model = Sequential()
           model.add(Dense(10, activation='relu', kernel_initializer='he_normal', input_shape=(n_features,)))
           model.add(Dense(8, activation='relu', kernel_initializer='he_normal'))
           model.add(Dense(1))
           # compile the model
           model.compile(optimizer='adam', loss='mse')
           # fit the model
           model.fit(X_train, y_train, epochs=200, batch_size=8, verbose=0)
           # predict test set
           yhat = model.predict(X_test)
           # evaluate predictions
           score = mean_absolute_error(y_test, yhat)
           print('>%.3f' % score)
           scores.append(score)
          # summarize all scores
          print('Mean MAE: %.3f (%.3f)' % (mean(scores), std(scores)))
          運行報告運行結(jié)束時MAE的平均值和標準偏差。
          注意:由于算法或評估程序的隨機性,或者數(shù)值精度的差異,您的結(jié)果可能會有所不同。考慮運行該示例幾次并比較平均結(jié)果。
          在這種情況下,我們可以看到MLP模型獲得的MAE約為35.384,這略好于獲得MAE約為38.913的基線模型。
          Mean MAE: 35.384 (14.951)
          接下來,讓我們嘗試使用與上一節(jié)相同的對輸入和目標變量進行冪變換的模型。
          下面列出了完整的示例。
          # k-fold cross-validation of deeper model with data transforms
          from numpy import mean
          from numpy import std
          from pandas import read_csv
          from sklearn.model_selection import KFold
          from sklearn.metrics import mean_absolute_error
          from sklearn.preprocessing import PowerTransformer
          from tensorflow.keras import Sequential
          from tensorflow.keras.layers import Dense
          from matplotlib import pyplot
          # load the dataset
          path = 'https://raw.githubusercontent.com/jbrownlee/Datasets/master/auto-insurance.csv'
          df = read_csv(path, header=None)
          # split into input and output columns
          X, y = df.values[:, :-1], df.values[:, -1]
          # prepare cross validation
          kfold = KFold(10)
          # enumerate splits
          scores = list()
          for train_ix, test_ix in kfold.split(X, y):
           # split data
           X_train, X_test, y_train, y_test = X[train_ix], X[test_ix], y[train_ix], y[test_ix]
           # ensure target is a 2d array
           y_train, y_test = y_train.reshape((len(y_train),1)), y_test.reshape((len(y_test),1))
           # prepare input data
           pt1 = PowerTransformer()
           pt1.fit(X_train)
           X_train = pt1.transform(X_train)
           X_test = pt1.transform(X_test)
           # prepare target
           pt2 = PowerTransformer()
           pt2.fit(y_train)
           y_train = pt2.transform(y_train)
           y_test = pt2.transform(y_test)
           # determine the number of input features
           n_features = X.shape[1]
           # define model
           model = Sequential()
           model.add(Dense(10, activation='relu', kernel_initializer='he_normal', input_shape=(n_features,)))
           model.add(Dense(8, activation='relu', kernel_initializer='he_normal'))
           model.add(Dense(1))
           # compile the model
           model.compile(optimizer='adam', loss='mse')
           # fit the model
           model.fit(X_train, y_train, epochs=200, batch_size=8, verbose=0)
           # predict test set
           yhat = model.predict(X_test)
           # inverse transforms
           y_test = pt2.inverse_transform(y_test)
           yhat = pt2.inverse_transform(yhat)
           # evaluate predictions
           score = mean_absolute_error(y_test, yhat)
           print('>%.3f' % score)
           scores.append(score)
          # summarize all scores
          print('Mean MAE: %.3f (%.3f)' % (mean(scores), std(scores)))
          運行報告運行結(jié)束時MAE的平均值和標準偏差。
          注意:由于算法或評估程序的隨機性,或者數(shù)值精度的差異,您的結(jié)果可能會有所不同。考慮運行該示例幾次并比較平均結(jié)果。
          在這種情況下,我們可以看到MLP模型獲得的MAE約為37.371,這比基準模型好,但不比更深的基準模型好。
          也許這種轉(zhuǎn)變沒有我們最初認為的那樣有用。
          Mean MAE: 37.371 (29.326)
          另一種變換是對輸入變量和目標變量進行規(guī)范化。
          這意味著將每個變量的值縮放到[0,1]范圍。我們可以使用MinMaxScaler來實現(xiàn)。例如:
          # prepare input data
          pt1 = MinMaxScaler()
          pt1.fit(X_train)
          X_train = pt1.transform(X_train)
          X_test = pt1.transform(X_test)
          # prepare target
          pt2 = MinMaxScaler()
          pt2.fit(y_train)
          y_train = pt2.transform(y_train)
          y_test = pt2.transform(y_test)
          結(jié)合在一起,下面列出了使用數(shù)據(jù)規(guī)范化評估更深層MLP的完整示例。
          # k-fold cross-validation of deeper model with normalization transforms
          from numpy import mean
          from numpy import std
          from pandas import read_csv
          from sklearn.model_selection import KFold
          from sklearn.metrics import mean_absolute_error
          from sklearn.preprocessing import MinMaxScaler
          from tensorflow.keras import Sequential
          from tensorflow.keras.layers import Dense
          from matplotlib import pyplot
          # load the dataset
          path = 'https://raw.githubusercontent.com/jbrownlee/Datasets/master/auto-insurance.csv'
          df = read_csv(path, header=None)
          # split into input and output columns
          X, y = df.values[:, :-1], df.values[:, -1]
          # prepare cross validation
          kfold = KFold(10)
          # enumerate splits
          scores = list()
          for train_ix, test_ix in kfold.split(X, y):
           # split data
           X_train, X_test, y_train, y_test = X[train_ix], X[test_ix], y[train_ix], y[test_ix]
           # ensure target is a 2d array
           y_train, y_test = y_train.reshape((len(y_train),1)), y_test.reshape((len(y_test),1))
           # prepare input data
           pt1 = MinMaxScaler()
           pt1.fit(X_train)
           X_train = pt1.transform(X_train)
           X_test = pt1.transform(X_test)
           # prepare target
           pt2 = MinMaxScaler()
           pt2.fit(y_train)
           y_train = pt2.transform(y_train)
           y_test = pt2.transform(y_test)
           # determine the number of input features
           n_features = X.shape[1]
           # define model
           model = Sequential()
           model.add(Dense(10, activation='relu', kernel_initializer='he_normal', input_shape=(n_features,)))
           model.add(Dense(8, activation='relu', kernel_initializer='he_normal'))
           model.add(Dense(1))
           # compile the model
           model.compile(optimizer='adam', loss='mse')
           # fit the model
           model.fit(X_train, y_train, epochs=200, batch_size=8, verbose=0)
           # predict test set
           yhat = model.predict(X_test)
           # inverse transforms
           y_test = pt2.inverse_transform(y_test)
           yhat = pt2.inverse_transform(yhat)
           # evaluate predictions
           score = mean_absolute_error(y_test, yhat)
           print('>%.3f' % score)
           scores.append(score)
          # summarize all scores
          print('Mean MAE: %.3f (%.3f)' % (mean(scores), std(scores)))
          運行報告運行結(jié)束時MAE的平均值和標準偏差。
          注意:由于算法或評估程序的隨機性,或者數(shù)值精度的差異,您的結(jié)果可能會有所不同。考慮運行該示例幾次并比較平均結(jié)果。
          在這種情況下,我們可以看到MLP模型獲得的MAE約為30.388,這比我們迄今為止嘗試過的任何其他配置都要好。
          Mean MAE: 30.388 (14.258)
          我們可以繼續(xù)測試模型架構(gòu)的替代配置(更多或更少的節(jié)點或?qū)樱瑢W習超參數(shù)(更多或更少的批處理)以及數(shù)據(jù)轉(zhuǎn)換。
          我將其保留為練習;讓我知道你發(fā)現(xiàn)了什么。您可以獲得更好的結(jié)果嗎?
          將您的結(jié)果發(fā)表在下面的評論中,我很樂意看到您所得到的。
          接下來,讓我們看看如何擬合最終模型并使用它進行預測。
          最終模型和做出預測
          選擇模型配置后,我們可以在所有可用數(shù)據(jù)上訓練最終模型,并使用它對新數(shù)據(jù)進行預測。在這種情況下,我們將使用數(shù)據(jù)標準化的更深層模型作為最終模型。這意味著,如果我們想將模型保存到文件中,則必須保存模型本身(用于進行預測),輸入數(shù)據(jù)的轉(zhuǎn)換(用于新的輸入數(shù)據(jù))和目標變量的轉(zhuǎn)換(用于新預測)。盡管可以在整個數(shù)據(jù)集而不是數(shù)據(jù)集的訓練子集上,但我們?nèi)钥梢韵褚郧耙粯訙蕚鋽?shù)據(jù)并擬合模型。
          # split into input and output columns
          X, y = df.values[:, :-1], df.values[:, -1]
          # ensure target is a 2d array
          y = y.reshape((len(y),1))
          # prepare input data
          pt1 = MinMaxScaler()
          pt1.fit(X)
          X = pt1.transform(X)
          # prepare target
          pt2 = MinMaxScaler()
          pt2.fit(y)
          y = pt2.transform(y)
          # determine the number of input features
          n_features = X.shape[1]
          # define model
          model = Sequential()
          model.add(Dense(10, activation='relu', kernel_initializer='he_normal', input_shape=(n_features,)))
          model.add(Dense(8, activation='relu', kernel_initializer='he_normal'))
          model.add(Dense(1))
          # compile the model
          model.compile(optimizer='adam', loss='mse')
          然后,我們可以使用此模型對新數(shù)據(jù)進行預測。首先,我們可以定義一行新數(shù)據(jù),這只是該數(shù)據(jù)集的一個變量。
          # define a row of new data
          row = [13]
          然后,我們可以轉(zhuǎn)換此新數(shù)據(jù),以準備用作模型的輸入。
          # transform the input data
          X_new = pt1.transform([row])
          然后我們可以做出預測。
          # make prediction
          yhat = model.predict(X_new)
          然后將預測的變換反轉(zhuǎn),以便我們可以按正確的比例使用或解釋結(jié)果。
          # invert transform on prediction
          yhat = pt2.inverse_transform(yhat)
          在這種情況下,我們將僅報告預測。
          # report prediction
          print('f(%s) = %.3f' % (row, yhat[0]))
          綜上所述,下面列出了為汽車保險數(shù)據(jù)集擬合最終模型并使用其對新數(shù)據(jù)進行預測的完整示例。
          # fit a final model and make predictions on new data.
          from pandas import read_csv
          from sklearn.model_selection import KFold
          from sklearn.metrics import mean_absolute_error
          from sklearn.preprocessing import MinMaxScaler
          from tensorflow.keras import Sequential
          from tensorflow.keras.layers import Dense
          # load the dataset
          path = 'https://raw.githubusercontent.com/jbrownlee/Datasets/master/auto-insurance.csv'
          df = read_csv(path, header=None)
          # split into input and output columns
          X, y = df.values[:, :-1], df.values[:, -1]
          # ensure target is a 2d array
          y = y.reshape((len(y),1))
          # prepare input data
          pt1 = MinMaxScaler()
          pt1.fit(X)
          X = pt1.transform(X)
          # prepare target
          pt2 = MinMaxScaler()
          pt2.fit(y)
          y = pt2.transform(y)
          # determine the number of input features
          n_features = X.shape[1]
          # define model
          model = Sequential()
          model.add(Dense(10, activation='relu', kernel_initializer='he_normal', input_shape=(n_features,)))
          model.add(Dense(8, activation='relu', kernel_initializer='he_normal'))
          model.add(Dense(1))
          # compile the model
          model.compile(optimizer='adam', loss='mse')
          # fit the model
          model.fit(X, y, epochs=200, batch_size=8, verbose=0)
          # define a row of new data
          row = [13]
          # transform the input data
          X_new = pt1.transform([row])
          # make prediction
          yhat = model.predict(X_new)
          # invert transform on prediction
          yhat = pt2.inverse_transform(yhat)
          # report prediction
          print('f(%s) = %.3f' % (row, yhat[0]))
          運行示例可以使模型適合整個數(shù)據(jù)集,并為單行新數(shù)據(jù)做出預測。
          注意:由于算法或評估程序的隨機性,或者數(shù)值精度的差異,您的結(jié)果可能會有所不同。考慮運行該示例幾次并比較平均結(jié)果。
          在這種情況下,我們可以看到輸入13導致輸出62(千瑞典克朗)。
          f([13]) = 62.595


          作者:沂水寒城,CSDN博客專家,個人研究方向:機器學習、深度學習、NLP、CV

          Blog: http://yishuihancheng.blog.csdn.net


          掃碼回復:2021

          即可獲取最新Python學習資源

          點擊上方卡片,一起學Python

          學習更多:
          整理了我開始分享學習筆記到現(xiàn)在超過250篇優(yōu)質(zhì)文章,涵蓋數(shù)據(jù)分析、爬蟲、機器學習等方面,別再說不知道該從哪開始,實戰(zhàn)哪里找了

          看完“點贊”的美德不能丟 

          瀏覽 72
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          評論
          圖片
          表情
          推薦
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  AAA免费人成看片 | 男人的天堂青青草 | 婷婷加勒比 | 无码免费性爱视频 | 日本精品人妻无码免费大全 |