各位老师,PY中出错如何处理呢?谢谢了
Traceback (most recent call last):
File "E:/副本.py", line 64, in <module>
csv_to_dataset("daily.csv")
File "E:/副本.py", line 33, in csv_to_dataset
y_normaliser.fit(next_day_open_values)
File "C:\ProgramData\Anaconda3\envs\TF2.1\lib\site-packages\sklearn\preprocessing\_data.py", line 416, in fit
return self.partial_fit(X, y)
File "C:\ProgramData\Anaconda3\envs\TF2.1\lib\site-packages\sklearn\preprocessing\_data.py", line 458, in partial_fit
force_all_finite="allow-nan",
File "C:\ProgramData\Anaconda3\envs\TF2.1\lib\site-packages\sklearn\base.py", line 566, in _valIDAte_data
X = check_array(X, **check_params)
File "C:\ProgramData\Anaconda3\envs\TF2.1\lib\site-packages\sklearn\utils\validation.py", line 808, in check_array
% (n_samples, array.shape, ensure_min_samples, context)
ValueError: Found array with 0 sample(s) (shape=(0, 1)) while a minimum of 1 is required by MinMaxScaler.
[Python] 纯文本查看复制代码
import pandas as pd
import matplotlib.pyplot as plt
from keras import layers
import tensorflow as tf
from keras.models import Model
from keras.layers import Dense, Dropout, LSTM, Input, Activation, concatenate
import numpy as np
from keras import optimizers
from sklearn import preprocessing
np.random.seed(4)
tf.random.set_seed(4)
history_points = 50
def csv_to_dataset(csv_path):
data = pd.read_csv(csv_path)
data = data.drop('date', axis=1)
data = data.drop(0, axis=0)
data_normaliser = preprocessing.MinMaxScaler()
data_normalised = data_normaliser.fit_transform(data)
# using the last {history_points} open high low close volume data points, predict the next open value
ohlcv_histories_normalised = np.array(
[data_normalised[i: i + history_points].copy() for i in range(len(data_normalised) - history_points)])
next_day_open_values_normalised = np.array(
[data_normalised[:, 0][i + history_points].copy() for i in range(len(data_normalised) - history_points)])
next_day_open_values_normalised = np.expand_dims(next_day_open_values_normalised,
axis=-1) # add 'axis' parameter here
next_day_open_values = np.array([data[:, 0][i + history_points].copy() for i in range(len(data) - history_points)])
next_day_open_values = np.expand_dims(next_day_open_values, axis=-1) # and here
y_normaliser = preprocessing.MinMaxScaler()
y_normaliser.fit(next_day_open_values)
assert ohlcv_histories_normalised.shape[0] == next_day_open_values_normalised.shape[0]
return ohlcv_histories_normalised, next_day_open_values_normalised, next_day_open_values, y_normaliser
ohlcv_histories, next_day_open_values, unscaled_y, y_normaliser = csv_to_dataset('daily.csv')
test_split = 0.9 # the percent of data to be used for testing
n = int(ohlcv_histories.shape[0] * test_split)
# splitting the dataset up into train and test sets
ohlcv_train = ohlcv_histories[:n]
y_train = next_day_open_values[:n]
ohlcv_test = ohlcv_histories[n:]
y_test = next_day_open_values[n:]
unscaled_y_test = unscaled_y[n:]
lstm_input = Input(shape=(history_points, 5), name='lstm_input')
x = LSTM(50, name='lstm_0')(lstm_input)
x = Dropout(0.2, name='lstm_dropout_0')(x)
x = Dense(64, name='dense_0')(x)
x = Activation('sigmoid', name='sigmoid_0')(x)
x = Dense(1, name='dense_1')(x)
output = Activation('linear', name='linear_output')(x)
model = Model(inputs=lstm_input, outputs=output)
adam = optimizers.Adam(lr=0.0005)
model.compile(optimizer=adam, loss='mse')
from keras.utils import plot_model
# plot_model(model, to_file='model.png')
model.fit(x=ohlcv_train, y=y_train, batch_size=32, epochs=50, shuffle=True, validation_split=0.1)
evaluation = model.evaluate(ohlcv_test, y_test)
print(evaluation)
csv_to_dataset("daily.csv")