I am attempting to build a function which will pull data for any stock and then plot a regression. However, I am running into issues with the source data. My question is - how do I take a time series in a pandas data frame and plot the linear trend over time? My code below:
This code will produce the regression:
import matplotlib.pyplot as plt
import seaborn as sns; sns.set()
import numpy as np
rng = np.random.RandomState(1)
x = 10 * rng.rand(50)
y = 2 * x - 5 + rng.randn(50)
plt.scatter(x, y);
plt.show()
from sklearn.linear_model import LinearRegression
model = LinearRegression(fit_intercept=True)
model.fit(x[:, np.newaxis], y)
xfit = np.linspace(0, 10, 1000)
yfit = model.predict(xfit[:, np.newaxis])
plt.scatter(x, y)
plt.plot(xfit, yfit);
plt.show()
This is my attempt to pass the data via a dataframe
from datetime import datetime
import pandas_datareader.data as web
start = datetime(2017, 8, 1)
end = datetime(2018, 7, 30)
data_SP = web.DataReader('JPM', 'iex', start, end)
y = dates # not sure how to get here?
plt.scatter(data['close'], y);
plt.show()
from sklearn.linear_model import LinearRegression
model = LinearRegression(fit_intercept=True)
model.fit(data['close'][:, np.newaxis], y)
xfit = np.linspace(0, 10, 1000)
yfit = model.predict(xfit[:, np.newaxis])
plt.scatter(data['close'], y)
plt.plot(xfit, yfit);
plt.show()