Search code examples
pythonstock

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


Solution

  • model_selection.train_test_split(...) and cross_validation.train_test_split(...) return an even number of arrays (in fact, it is 2 * the original number of arrays). With that said, you're inputting an X and Y array so you should expect the output to contain 4 arrays. Try changing your code to read

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

    If you said you get an error when you put in the 4th variable, can we see the traceback? I suspect it's not on the same line but I have no way to run the code right now so I can't do it myself