I got this error
Found array with 0 sample(s) (shape=(0, 5880)) while a minimum of 1 is required by MinMaxScaler.
Here I am Stuck that I did not know what to do: I am using Django Development and deploy my ML Model: here is the code of my model:
import numpy as np
import pandas as pd
def series_to_supervised(df, cols_in, cols_out, days_in=1, days_out=1):
# input sequence (t-n, ... t-1)
cols, names = list(), list()
for i in range(days_in, 0, -1):
cols.append(df.shift(i))
names += [('{}(t-{})'.format(j, i)) for j in cols_in]
# concatenate input sequence
partial_in = pd.concat(cols, axis=1)
partial_in.columns = names
# forecast sequence (t, t+1, ... t+n)
df = df.filter(items=cols_out)
cols, names = list(), list()
for i in range(0, days_out):
cols.append(df.shift(-i))
if i == 0:
names += [('{}(t)'.format(j)) for j in cols_out]
else:
names += [('{}(t+{})'.format(j, i)) for j in cols_out]
# concatenate forecast sequence
partial_out = pd.concat(cols, axis=1)
partial_out.columns = names
return partial_in.join(partial_out).dropna()
def transform_data(df, cols, scaler, n_days_past=120):
n_cols = len(cols)
# df to np array
np_arr = np.array(df.values.flatten().tolist())
np_arr = scaler.transform([np_arr])
np_arr = np_arr.reshape((np_arr.shape[0], n_days_past, n_cols))
return np_arr
def transform_predictions(pred_df, cols, scaler, n_days_future=30):
n_cols = len(cols)
# np array to df
denormalized = scaler.inverse_transform(pred_df)
pred_df = denormalized.reshape((n_days_future, n_cols))
pred_df = pd.DataFrame(pred_df, columns=cols)
return pred_df
def train_model(df, cols_in, cols_out, days_in=120, days_out=30, lr=0.001, decay=0.2, epochs=100, batch_size=64,
lstm_shape=(64, 32, 32), dense_shape=(1024,), dropout=0.25, optimizer='adam'):
# start up keras
from keras.models import Sequential
from keras.layers import LSTM, Dense, Dropout
#from keras.optimizers import Adam, Nadam
from tensorflow.keras.optimizers import Adam, Nadam
from sklearn.preprocessing import MinMaxScaler
# assert optimizer
if optimizer not in ['adam', 'nadam']:
print("Invalid optimizer specified, can either be adam or nadam")
return
# prepare data
supervised_df = series_to_supervised(df, cols_in, cols_out, days_in, days_out)
values_in = supervised_df.filter(regex='t-').columns
values_out = supervised_df.drop(columns=values_in).values
values_in = supervised_df[values_in].values
# scale data
scaler_in = MinMaxScaler(feature_range=(-1, 1))
scaler_out = MinMaxScaler(feature_range=(-1, 1))
values_in = scaler_in.fit_transform(values_in)
values_out = scaler_out.fit_transform(values_out)
# divide data into train, valid, test
train_x, train_y = values_in[:int(len(values_in) * 0.80)], values_out[:int(len(values_out) * 0.80)]
valid_x, valid_y = values_in[int(len(values_in) * 0.80):], values_out[int(len(values_out) * 0.80):]
# reshape data
n_cols = len(cols_in)
train_x = train_x.reshape((train_x.shape[0], int(train_x.shape[1] / n_cols), n_cols))
valid_x = valid_x.reshape((valid_x.shape[0], int(valid_x.shape[1] / n_cols), n_cols))
# weights to add
# weight_dict = {k: v for (k, v) in weight_generator(1, 4, train_y.shape[1])}
sample_weights = [[1.5 if i > days_in - 7 else 1.25 if i > days_in - 30 else 1 for j in range(len(cols_in))]for i in range(days_in)]
sample_weights = np.array(sample_weights)
# design network
model = Sequential()
# add LSTM layers
model.add(LSTM(lstm_shape[0], input_shape=(train_x.shape[1], train_x.shape[2]), return_sequences=True))
for lstm_l in lstm_shape[1:-1]:
model.add(LSTM(lstm_l, return_sequences=True))
model.add(LSTM(lstm_l, return_sequences=False))
# add Dense layers
for dense_l in dense_shape:
model.add(Dense(dense_l))
# final layers
model.add(Dropout(dropout))
model.add(Dense(train_y.shape[1]))
# optimizer
if optimizer == 'adam':
opt = Adam(decay=decay, lr=lr)
else:
opt = Nadam(schedule_decay=decay, lr=lr)
# compile and train
model.compile(loss='mean_squared_error', optimizer=opt)#, sample_weight_mode='temporal')
history = model.fit(train_x, train_y, epochs=epochs, batch_size=batch_size, validation_data=(valid_x, valid_y),
verbose=1, shuffle=False)#, sample_weight=sample_weights)
return model, scaler_in, scaler_out, history.history
def weight_generator(start, end, count):
j, k = start, end
for i in range(count):
if i > count / k and not k == start:
j, k = j + 1, k - 1
yield (i, j)
请检查您的 ** Python 版本 ** 由于 Python 版本而发生此类错误..
"
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.