到目前为止,这是我的代码。我看到一些类似的答案,但这有点不同,因为我没有从互联网上调用我的数据并创建了一个csv文件。它与How can I draw a linear regression line in this graph?相同吗?
提前谢谢。
import datetime as dt # library for dates and time
import matplotlib.pyplot as plt # plots library
import pandas_datareader.data as web
import pandas as pd
import statsmodels.graphics.tsaplots as stplt
from statsmodels.tsa.arima_model import ARIMA
from pandas import DataFrame
from sklearn.metrics import mean_squared_error
import numpy as np
import statistics as ics
plt.close('all')
def parser(x):
return pd.datetime.strptime(x, '%m/%d/%Y')
#Read the *.csv file
amazon = pd.read_csv('AMZN.csv', parse_dates=[1], index_col=1,
squeeze=True, date_parser=parser)
amazon.Close.plot()
print(amazon.describe())
print(amazon['Volume'].var())
答案 0 :(得分:1)
我建议你使用scikit learn的线性回归。代码看起来像这样(它只是伪代码):
from sklearn import linear_model
regr = linear_model.LinearRegression() #create a linear regression object
regr.fit(your_x_data, your_y_data) #fit your data (create the linear regression parameters)
your_y_pred = regr.predict(your_x_data)
#create your linear function
plt.plot(your_x_data, your_y_pred , color='blue', linewidth=3) #plot the linear function`