python - ValueError : Found array with 0 sample (s) (shape= (0, 1) 而 MinMaxScaler 要求最小值为 1

标签 python tensorflow scikit-learn

我是机器学习的初学者。我正在帮助我主修数学的 friend 使用 TensorFlow 基于他提供的 .csv 文件创建一个股票预测器。

我有一些问题。第一个是他的 .csv 文件。该文件只有日期和收盘值,它们没有分开,因此我不得不手动将日期和值分开。我已经设法做到了,现在我在使用 MinMaxScaler() 时遇到了麻烦。有人告诉我,我几乎可以忽略日期,只测试收盘值,将它们归一化,并根据它们做出预测。

我一直收到这个错误:

ValueError: Found array with 0 sample(s) (shape=(0, 1)) while a
minimum of 1 is required by MinMaxScaler()

老实说,我以前从未使用过 SKLearn 或 TensorFlow,这是我第一次从事这样的项目。我看到的关于该主题的所有指南都使用了 pandas,但就我而言,.csv 文件一团糟,我不相信我可以使用 pandas。

我正在关注 this DataCamp tutorial :

但不幸的是,由于我缺乏经验,有些事情并不适合我,如果我能更清楚地了解我应该如何处理我的案例,我将不胜感激。

下面附上我的(乱七八糟的)代码:

import pandas as pd
import numpy as np
import tensorflow as tf
import sklearn
from sklearn.model_selection import KFold
from sklearn.preprocessing import scale
from sklearn.preprocessing import MinMaxScaler
import matplotlib
import matplotlib.pyplot as plt
from dateutil.parser import parse
from datetime import datetime, timedelta
from collections import deque

stock_data = []
stock_date = []
stock_value = []
f = open("s&p500closing.csv","r")
data = f.read()
rows = data.split("\n")
rows_noheader = rows[1:len(rows)]

#Separating values from messy `.csv`, putting each value to it's list and also a combined list of both
for row in rows_noheader:
    [date, value] = row[1:len(row)-1].split('\t')
    stock_date.append(date)
    stock_value.append((value))
    stock_data.append((date, value))

#Numpy array of all closing values converted to floats and normalized against the maximum
stock_value = np.array(stock_value, dtype=np.float32)
normvalue = [i/max(stock_value) for i in stock_value]

#Number of closing values and days. Since there is one closing value for each, they both match and there are 4528 of them (each)
nclose_and_days = 0
for i in range(len(stock_data)):
    nclose_and_days+=1

train_data = stock_value[:2264]
test_data = stock_value[2264:]

scaler = MinMaxScaler()

train_data = train_data.reshape(-1,1)
test_data = test_data.reshape(-1,1)

# Train the Scaler with training data and smooth data
smoothing_window_size = 1100
for di in range(0,4400,smoothing_window_size):
    #error occurs here
    scaler.fit(train_data[di:di+smoothing_window_size,:])
    train_data[di:di+smoothing_window_size,:] = scaler.transform(train_data[di:di+smoothing_window_size,:])

# You normalize the last bit of remaining data
scaler.fit(train_data[di+smoothing_window_size:,:])
train_data[di+smoothing_window_size:,:] = scaler.transform(train_data[di+smoothing_window_size:,:])

# Reshape both train and test data
train_data = train_data.reshape(-1)

# Normalize test data
test_data = scaler.transform(test_data).reshape(-1)

# Now perform exponential moving average smoothing
# So the data will have a smoother curve than the original ragged data
EMA = 0.0
gamma = 0.1
for ti in range(1100):
    EMA = gamma*train_data[ti] + (1-gamma)*EMA
    train_data[ti] = EMA

# Used for visualization and test purposes
all_mid_data = np.concatenate([train_data,test_data],axis=0)

window_size = 100
N = train_data.size
std_avg_predictions = []
std_avg_x = []
mse_errors = []

for pred_idx in range(window_size,N):
    std_avg_predictions.append(np.mean(train_data[pred_idx-window_size:pred_idx]))
    mse_errors.append((std_avg_predictions[-1]-train_data[pred_idx])**2)
    std_avg_x.append(date)

print('MSE error for standard averaging: %.5f'%(0.5*np.mean(mse_errors)))

最佳答案

我知道这篇文章很旧,但是当我在这里偶然发现时,其他人会.. 在遇到同样的问题并在谷歌上搜索了很多之后,我发现了一个帖子 https://github.com/llSourcell/Make_Money_with_Tensorflow_2.0/issues/7

所以看起来如果你下载一个太小的数据集就会抛出这个错误。 下载 1962 年的 .csv 文件,它就足够大了 ;)。

现在,我只需要为我的数据集找到正确的参数..因为我正在将其适应另一种类型的预测.. 希望对你有帮助

关于python - ValueError : Found array with 0 sample (s) (shape= (0, 1) 而 MinMaxScaler 要求最小值为 1,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53421626/

相关文章:

python - 正则表达式用括号前的空格替换点

python - 通过 USB/串口连接到 Python 中的 Newport CONEX-PP 运动 Controller

python - 使用 Python 在带有空白图 block 的拼字游戏中搜索单词列表

python - 如何使用 Tensorboard 在同一图上绘制不同的汇总指标?

python - 在Tensorflow中,如何重命名某个操作名称?

machine-learning - 回归中的 scikit-learn 交叉验证分数

python - 在 KMeans 聚类(scikit 学习)之后查找聚类的长度(与聚类相关联的点数)

python - 简化这个 Django 查询

numpy - 将 numpy 稀疏矩阵保存到文件中

python - 使用 TensorFlow 进行图像识别