1
$\begingroup$

I wrote a simple Stock Prediction Algorithm and got the predicted value. Then, I wanted to plot the relation between Adjusted close price and predicted value, but got the ValueError: x and y must be the same size. I tried to reshape it, but no luck. I'm having problem with the last 5 lines of the following code. How can I resize X and y_predict in order to have a same size of them? What is the exact problem with my resize code?

import quandl
import pandas as pd
import numpy as np
import datetime
import matplotlib.pyplot as plt
%matplotlib inline
from sklearn.linear_model import LinearRegression
from sklearn import preprocessing, svm, datasets
from sklearn.model_selection import cross_validate, train_test_split

stock_data = quandl.get("WIKI/AAPL")
stock_data = stock_data[["Adj. Close"]]
forecast_out = int(30)
stock_data["Prediction"] = stock_data[["Adj. Close"]].shift(-forecast_out)
X = np.array(stock_data.drop(['Prediction'],1))
X = preprocessing.scale(X)
X_forecast = X[-forecast_out]
X = X[:-forecast_out]
y = np.array(stock_data["Prediction"])
y = y[:-forecast_out]
LinearRegression().fit(X.reshape(-1,1),y)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state = 0)
lin_reg = LinearRegression().fit(X_train,y_train)
y_predict = lin_reg.predict(X_test)

LinearRegression().fit(X.reshape(-1,1),y_predict)

plt.scatter(X_train, y_predict,  color='blue')
plt.plot(X_train, regr.coef_[0][0]*X_train + lin_reg.intercept_[0], '-r')
plt.xlabel("Adjusted closing price")
plt.ylabel("Predicted price")
```
$\endgroup$
0
$\begingroup$

You are using the full X dataset and want to plot it with the y_predict values, this is not possible since the size of both arrays is not the same. y_predict are the predicted values on X_test while X contains all inputs (i.e. X_train and X_test). You should therefore use X_test instead of X.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.