首页 > 解决方案 > Having trouble with stock prediction script in Python

问题描述

I recently found this script that you can find here, and I have tried it out, but it says [TICKER] has not been predicted. So, I dug into the code, and found a little code that helped me:

for i in stock_list:
        print("Number: " + str(number))
        try:
        predictData(i, 5)
        except:
            print("Stock: " + i + " was not predicted")
        number += 1

I then commented that out to see what was causing it to not predict it. This is what I got out of it:

Number: 0
AKS
Traceback (most recent call last):
  File "finance.py", line 104, in <module>
    getStocks(200)
  File "finance.py", line 34, in getStocks
    predictData(i, 5)
  File "finance.py", line 86, in predictData
    X, Y, test_size=0.5)
ValueError: too many values to unpack (expected 3)

Aha! So, the little piece of code that was being a b*tch was this:

    X = np.array(df.drop(['prediction'], 1))
    Y = np.array(df['prediction'])
    X = preprocessing.scale(X)
    X_prediction = X[-forecast_time:]
    X_train, Y_train, Y_test = model_selection.train_test_split(
        X, Y, test_size=0.5)

I think the reason that this was not working is because

 X_train, Y_train, Y_test = model_selection.train_test_split(
        X, Y, test_size=0.5)

was giving 4 outputs, but there were only 3 variables. I don't know what I should do here, because I tried to add another variable to that, but got this: TypeError: only size-1 arrays can be converted to Python scalars... sigh

标签: pythonstock

解决方案


model_selection.train_test_split(...)cross_validation.train_test_split(...)返回偶数个数组(实际上是 2 * 原始数组个数)。话虽如此,您正在输入一个 X 和 Y 数组,因此您应该期望输出包含 4 个数组。尝试更改您的代码以读取

X_train, X_test, Y_train, Y_test = model_selection.train_test_split(X, Y, test_size = 0.5)

如果你说输入第 4 个变量时出错,我们可以看到回溯吗?我怀疑它不在同一行但我现在无法运行代码所以我不能自己做


推荐阅读