[英]MLP with Scikitlearn: Artificial Neural Network application for forecast
我有交通數據,我想通過顯示 model 這些輸入來預測下一小時的車輛數量:這一小時的車輛數量和這一小時的平均速度值。 這是我的代碼:
dataset=pd.read_csv('/content/final - Sayfa5.csv',delimiter=',')
dataset=dataset[[ 'MINIMUM_SPEED', 'MAXIMUM_SPEED', 'AVERAGE_SPEED','NUMBER_OF_VEHICLES','1_LAG_NO_VEHICLES']]
X = np.array(dataset.iloc[:,1:4])
L = len(dataset)
Y = np.array([dataset.iloc[:,4]])
Y= Y[:,0:L]
Y = np.transpose(Y)
#scaling with MinMaxScaler
scaler = MinMaxScaler()
scaler.fit(X)
X = scaler.transform(X)
scaler.fit(Y)
Y = scaler.transform(Y)
print(X,Y)
X_train , X_test, Y_train, Y_test = train_test_split(X,Y,test_size=0.3)
from sklearn.neural_network import MLPRegressor
from sklearn.metrics import mean_squared_error
mlp = MLPRegressor(activation='logistic')
mlp.fit(X_train,Y_train)
predictions = mlp.predict(X_test)
predictions1=mlp.predict(X_train)
print("mse_test :" ,mean_squared_error(Y_test,predictions), "mse_train :",mean_squared_error(Y_train,predictions1))
我得到了很好的 mse 值,例如 mse_test: 0.005467816018933008 mse_train: 0.005072774796622158
但我對兩點感到困惑:
我是否應該縮放 y 值,我讀了很多博客寫的不應該縮放 Y,只縮放 X_train 和 X_test。 但是我的 mse 分數太差了,比如 49,50,100 甚至更高。
我怎樣才能得到對未來的預測,但不能得到縮放值。 例如我寫道:
Xnew=[[ 80 , 40 , 47],
[ 80 , 30, 81],
[ 80 , 33, 115]]
Xnew = scaler.transform(Xnew)
print("prediction for that input is" , mlp.predict(Xnew))
但是我得到了縮放值,例如:該輸入的預測是[0.08533431 0.1402755 0.19497315]
應該是這樣的[81,115,102]
。
恭喜您使用[sklearn's MLPRegressor][1]
,介紹神經網絡總是一件好事。
縮放輸入數據對於神經網絡至關重要。 考慮查看Ethan Alpaydin 的機器學習簡介的第 11 章。 這在Efficient BackProp 論文中也有詳細介紹。 簡而言之,縮放輸入數據至關重要,這樣您的 model 才能了解如何定位 output。
用英語來說,在這種情況下, scaling
意味着將您的數據轉換為0 and 1
(含)之間的值。 一篇很好的Stats Exchange 帖子描述了縮放的差異。 對於 MinMax 縮放,您要保持數據的相同分布,包括對異常值敏感。 sklearn
中確實存在更強大的方法(在那篇文章中描述),例如RobustScaler 。
因此,以一個非常基本的數據集為例:
| Feature 1 | Feature 2 | Feature 3 | Feature 4 | Feature 5 | Target |
|:---------:|:---------:|:---------:|:---------:|:---------:|:------:|
| 1 | 17 | 22 | 3 | 3 | 53 |
| 2 | 18 | 24 | 5 | 4 | 54 |
| 1 | 11 | 22 | 2 | 5 | 96 |
| 5 | 20 | 22 | 7 | 5 | 59 |
| 3 | 10 | 26 | 4 | 5 | 66 |
| 5 | 14 | 30 | 1 | 4 | 63 |
| 2 | 17 | 30 | 9 | 5 | 93 |
| 4 | 5 | 27 | 1 | 5 | 91 |
| 3 | 20 | 25 | 7 | 4 | 70 |
| 4 | 19 | 23 | 10 | 4 | 81 |
| 3 | 13 | 8 | 19 | 5 | 14 |
| 9 | 18 | 3 | 67 | 5 | 35 |
| 8 | 12 | 3 | 34 | 7 | 25 |
| 5 | 15 | 6 | 12 | 6 | 33 |
| 2 | 13 | 2 | 4 | 8 | 21 |
| 4 | 13 | 6 | 28 | 5 | 46 |
| 7 | 17 | 7 | 89 | 6 | 21 |
| 4 | 18 | 4 | 11 | 8 | 5 |
| 9 | 19 | 7 | 21 | 5 | 30 |
| 6 | 14 | 6 | 17 | 7 | 73 |
我可以稍微修改你的代碼來玩這個:
import pandas as pd, numpy as np
from sklearn.neural_network import MLPRegressor
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import RobustScaler
from sklearn.metrics import mean_squared_error
df = pd.read_clipboard()
# Build data
y = df['Target'].to_numpy()
scaled_y = df['Target'].values.reshape(-1, 1) #returns a numpy array
df.drop('Target', inplace=True, axis=1)
X = df.to_numpy()
#scaling with RobustScaler
scaler = RobustScaler()
X = scaler.fit_transform(X)
# Scaling y just to show you the difference
scaled_y = scaler.fit_transform(scaled_y)
# Set random_state so we can replicate results
X_train , X_test, y_train, y_test = train_test_split(X,y,test_size=0.2, random_state=8)
scaled_X_train , scaled_X_test, scaled_y_train, scaled_y_test = train_test_split(X,scaled_y,test_size=0.2, random_state=8)
mlp = MLPRegressor(activation='logistic')
scaled_mlp = MLPRegressor(activation='logistic')
mlp.fit(X_train, y_train)
scaled_mlp.fit(scaled_X_train, scaled_y_train)
preds = mlp.predict(X_test)
scaled_preds = mlp.predict(scaled_X_test)
for pred, scaled_pred, tar, scaled_tar in zip(preds, scaled_preds, y_test, scaled_y_test):
print("Regular MLP:")
print("Prediction: {} | Actual: {} | Error: {}".format(pred, tar, tar-pred))
print()
print("MLP that was shown scaled labels: ")
print("Prediction: {} | Actual: {} | Error: {}".format(scaled_pred, scaled_tar, scaled_tar-scaled_pred))
簡而言之,縮小你的目標自然會縮小你的錯誤,因為你的 model 不是在學習實際值,而是在 0 和 1 之間的值。
這就是我們不縮放目標變量的原因,因為我們將值強制為0...1
空間,因此誤差自然會更小。
主要內容:不要僅在 model 內比較型號之間的 MSE。 MSE 的絕對值很難解釋。
MinMaxScaler 所做的是計算以下內容:
X_std = (X - X.min(axis=0)) / (X.max(axis=0) - X.min(axis=0))
X_scaled = X_std * (max - min) + min
另請參閱MinMaxScaler 文檔。 之后您可以嘗試反向計算預測,但我認為這沒有意義
TL;DR 僅在特征量級不同或給定特征中存在較大異常值時才嘗試縮放。 在此處輸入代碼
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.