minmaxscaler.inverse_transform和minmaxscaler.fit_transform区别
时间: 2024-05-22 17:13:16 浏览: 267
minmaxscaler.fit_transform是将数据进行归一化处理,使得数据的范围在[0,1]之间,适用于训练模型前的数据预处理。minmaxscaler.inverse_transform则是将经过归一化处理的数据还原成原始数据,适用于模型训练后的结果反归一化处理。
总结一下:
- fit_transform:对训练集进行归一化处理,得到训练集的归一化数据,并返回归一化后的训练集数据。
- inverse_transform:将归一化后的数据还原成原始数据。
所以,两者的使用场景不同,需要根据具体的需求选择相应的方法。
相关问题
将冒号后面的代码改写成一个nn.module类:data1 = pd.read_csv("终极1.csv", usecols=[17], encoding='gb18030') df = data1.fillna(method='ffill') data = df.values.reshape(-1, 1) scaler = MinMaxScaler(feature_range=(0, 1)) data = scaler.fit_transform(data) train_size = int(len(data) * 0.8) test_size = len(data) - train_size train, test = data[0:train_size, :], data[train_size:len(data), :] def create_dataset(dataset, look_back=1): dataX, dataY = [], [] for i in range(len(dataset)-look_back-1): a = dataset[i:(i+look_back), 0] dataX.append(a) dataY.append(dataset[i + look_back, 0]) return np.array(dataX), np.array(dataY) look_back = 30 trainX, trainY = create_dataset(train, look_back) testX, testY = create_dataset(test, look_back) trainX = np.reshape(trainX, (trainX.shape[0], 1, trainX.shape[1])) testX = np.reshape(testX, (testX.shape[0], 1, testX.shape[1])) model = Sequential() model.add(LSTM(50, input_shape=(1, look_back), return_sequences=True)) model.add(LSTM(50)) model.add(Dense(1)) model.compile(loss='mean_squared_error', optimizer='adam') model.fit(trainX, trainY, epochs=6, batch_size=1, verbose=2) trainPredict = model.predict(trainX) testPredict = model.predict(testX) trainPredict = scaler.inverse_transform(trainPredict) trainY = scaler.inverse_transform([trainY]) testPredict = scaler.inverse_transform(testPredict) testY = scaler.inverse_transform([testY])
以下是将代码改写为nn.module类的示例:
```python
import torch
import torch.nn as nn
import numpy as np
import pandas as pd
from sklearn.preprocessing import MinMaxScaler
class LSTMModel(nn.Module):
def __init__(self, input_dim, hidden_dim, output_dim, num_layers):
super(LSTMModel, self).__init__()
self.hidden_dim = hidden_dim
self.num_layers = num_layers
self.lstm = nn.LSTM(input_dim, hidden_dim, num_layers, batch_first=True)
self.fc = nn.Linear(hidden_dim, output_dim)
def forward(self, x):
h0 = torch.zeros(self.num_layers, x.size(0), self.hidden_dim).requires_grad_()
c0 = torch.zeros(self.num_layers, x.size(0), self.hidden_dim).requires_grad_()
out, (hn, cn) = self.lstm(x, (h0.detach(), c0.detach()))
out = self.fc(out[:, -1, :])
return out
# 读取数据
data1 = pd.read_csv("终极1.csv", usecols=[17], encoding='gb18030')
df = data1.fillna(method='ffill')
data = df.values.reshape(-1, 1)
# 数据归一化
scaler = MinMaxScaler(feature_range=(0, 1))
data = scaler.fit_transform(data)
# 划分数据集
train_size = int(len(data) * 0.8)
test_size = len(data) - train_size
train, test = data[0:train_size, :], data[train_size:len(data), :]
# 创建数据集
def create_dataset(dataset, look_back=1):
dataX, dataY = [], []
for i in range(len(dataset)-look_back-1):
a = dataset[i:(i+look_back), 0]
dataX.append(a)
dataY.append(dataset[i + look_back, 0])
return np.array(dataX), np.array(dataY)
look_back = 30
trainX, trainY = create_dataset(train, look_back)
testX, testY = create_dataset(test, look_back)
trainX = np.reshape(trainX, (trainX.shape[0], trainX.shape[1], 1))
testX = np.reshape(testX, (testX.shape[0], testX.shape[1], 1))
# 模型训练
input_dim = 1
hidden_dim = 50
output_dim = 1
num_layers = 2
model = LSTMModel(input_dim=input_dim, hidden_dim=hidden_dim, output_dim=output_dim, num_layers=num_layers)
criterion = nn.MSELoss()
optimizer = torch.optim.Adam(model.parameters(), lr=0.001)
num_epochs = 6
for epoch in range(num_epochs):
outputs = model(trainX)
optimizer.zero_grad()
loss = criterion(outputs, trainY)
loss.backward()
optimizer.step()
if epoch % 1 == 0:
print("Epoch: %d, loss: %1.5f" % (epoch, loss.item()))
# 预测结果
trainPredict = model(trainX)
testPredict = model(testX)
trainPredict = scaler.inverse_transform(trainPredict.detach().numpy())
trainY = scaler.inverse_transform([trainY])
testPredict = scaler.inverse_transform(testPredict.detach().numpy())
testY = scaler.inverse_transform([testY])
```
import pandas as pd import numpy as np import matplotlib.pyplot as plt import tensorflow as tf from tensorflow.keras.models import Sequential from tensorflow.keras.layers import LSTM, Dense data = pd.read_csv('车辆:274序:4结果数据.csv') x = data[['车头间距', '原车道前车速度']].values y = data['本车速度'].values train_size = int(len(x) * 0.7) test_size = len(x) - train_size x_train, x_test = x[0:train_size,:], x[train_size:len(x),:] y_train, y_test = y[0:train_size], y[train_size:len(y)] from sklearn.preprocessing import MinMaxScaler scaler = MinMaxScaler(feature_range=(0, 1)) x_train = scaler.fit_transform(x_train) x_test = scaler.transform(x_test) model = Sequential() model.add(LSTM(50, input_shape=(2, 1))) model.add(Dense(1)) model.compile(loss='mean_squared_error', optimizer='adam') history = model.fit(x_train.reshape(-1, 2, 1), y_train, epochs=100, batch_size=32, validation_data=(x_test.reshape(-1, 2, 1), y_test)) plt.plot(history.history['loss']) plt.plot(history.history['val_loss']) plt.title('Model loss') plt.ylabel('Loss') plt.xlabel('Epoch') plt.legend(['Train', 'Test'], loc='upper right') plt.show() train_predict = model.predict(x_train.reshape(-1, 2, 1)) test_predict = model.predict(x_test.reshape(-1, 2, 1)) train_predict = scaler.inverse_transform(train_predict) train_predict = train_predict.reshape(-1, 1) y_train = scaler.inverse_transform([y_train]) test_predict = scaler.inverse_transform(test_predict) y_test = scaler.inverse_transform([y_test]) plt.plot(y_train[0], label='train') plt.plot(train_predict[:,0], label='train predict') plt.plot(y_test[0], label='test') plt.plot(test_predict[:,0], label='test predict') plt.legend() plt.show()报错Traceback (most recent call last): File "C:\Users\马斌\Desktop\NGSIM_data_processing\80s\lstmtest.py", line 42, in <module> train_predict = scaler.inverse_transform(train_predict) File "D:\python\python3.9.5\pythonProject\venv\lib\site-packages\sklearn\preprocessing\_data.py", line 541, in inverse_transform X -= self.min_ ValueError: non-broadcastable output operand with shape (611,1) doesn't match the broadcast shape (611,2)
这个错误提示是因为在使用 `scaler.inverse_transform()` 函数时,输入的 `train_predict` 和 `y_train` 的维度不匹配,导致无法进行运算。具体来说,`train_predict` 是一个形状为 `(611, 1)` 的二维数组,而 `y_train` 是一个形状为 `(1, 611)` 的二维数组,因此无法直接进行运算。
你可以尝试使用以下代码将 `y_train` 转换为一维数组,再进行运算:
```
train_predict = scaler.inverse_transform(train_predict)
train_predict = train_predict.reshape(-1) # 将结果变为一维数组
y_train = scaler.inverse_transform(y_train.reshape(-1, 1)).reshape(-1) # 将结果变为一维数组
```
同样的,你也需要将 `y_test` 转换为一维数组,再进行相应的操作。
阅读全文