如何使用训练有素的数据模型来预测未来? Python,SKlearn

时间:2017-09-06 17:18:56

标签: python machine-learning scikit-learn prediction

我正在学习如何使用SKlearn进行机器学习。我有我的CLF,我对从训练中得到的结果感到满意,但我现在无法弄清楚如何使用它来预测未来。我可以预测直到今天,但我希望将我的预测延伸到未来一周。

这是我正在使用的代码::

style.use('ggplot')

df = quandl.get("the_data")
df['PCT_change'] = (df['Close'] - df['Open']) / df['Open'] * 100.0
df['HL_PCT'] = (df['High'] - df['Low']) / df['Close'] * 100.0

df = df[['Close', 'HL_PCT', 'PCT_change', 'Volume']]
forecast_col = 'Close'
df.fillna(value=-99999, inplace=True)
forecast_out = int(math.ceil(0.01 * len(df)))

df['label'] = df[forecast_col].shift(-forecast_out)

X = np.array(df.drop(['label'], 1))
X = preprocessing.scale(X)
X_lately = X[-forecast_out:]
X = X[:-forecast_out]

df.dropna(inplace=True)

y = np.array(df['label'])

X_train, X_test, y_train, y_test = cross_validation.train_test_split(X, y, test_size=0.2)


# clf = LinearRegression(n_jobs=-1)
# clf.fit(X_train, y_train)

pickle_in = open('the_pickle.pickle','rb')
clf = pickle.load(pickle_in)
confidence = clf.score(X_test, y_test)
print('confidence: ', confidence)

print('HERE IS X_LATELY :::::: ', X_lately)

# forecast_set = clf.predict(X_lately)
forecast_set = clf.predict(X_lately)

df['Forecast'] = np.nan
last_date = df.iloc[-1].name
last_unix = last_date.timestamp()
one_day = 86400
next_unix = last_unix + one_day

for i in forecast_set:
    next_date = datetime.datetime.fromtimestamp(next_unix)
    next_unix += 86400
    df.loc[next_date] = [np.nan for _ in range(len(df.columns) - 1)] + [i]

with open('the_pickle.pickle', 'wb') as f:
    pickle.dump(clf, f)


df['Close'].plot()
df['Forecast'].plot()
plt.legend(loc=4)
plt.xlabel('Date')
plt.ylabel('Price')
plt.show()

任何提示??

0 个答案:

没有答案