【发布时间】:2020-09-22 05:33:07
【问题描述】:
我正在尝试使用 LinearRegression 创建一个简单的预测。在我看来,这应该可以预测未来的价值,但我显然弄错了。它似乎是从数据帧的尾部获取数据,而不是最近的数据点。我正在使用谷歌的股票价格和 alpha_vantage 的 获取股票信息的API
ts = TimeSeries(key=api_key, output_format='pandas')
df, df_meta = ts.get_daily(symbol='GOOGL', outputsize='full')
df = df[['1. open', '2. high', '3. low', '4. close', '5. volume']]
df['HL_PCT'] = (df['2. high'] - df['4. close']) / df['4. close'] * 100.0
df['PCT_change'] = (df['4. close'] - df['1. open']) / df['1. open'] * 100.0
df = df[['4. close', 'HL_PCT', 'PCT_change', '5. volume']]
forecast_col = '4. close'
df.fillna(-99999, inplace=True)
forecast_out = int(math.ceil(0.01*len(df)))
print(forecast_out)
# Moving columns negatively
df['label'] = df[forecast_col].shift(-forecast_out)
# Features
X = np.array(df.drop(['label'], 1))
X = preprocessing.scale(X)
X_lately = X[-forecast_out:]
X = X[:-forecast_out]
df.dropna(inplace=True)
# Labels
y = np.array(df['label'])
y = np.array(df['label'])
X_train, X_test, y_train, y_test = model_selection.train_test_split(X, y, test_size=0.2)
clf = LinearRegression(n_jobs=-1)
# Fit is synonymous with train
clf.fit(X_train, y_train)
# Score is synonymous with test
accuracy = clf.score(X_test, y_test)
forecast_set = clf.predict(X_lately)
print(forecast_set, accuracy, forecast_out)
它返回 200 左右的值,这显然不是对 2020 年的预测。
【问题讨论】:
标签: python-3.x pandas numpy scikit-learn