df = Quandl.get("WIKI/GOOGL")
df = df[['Adj. Open', 'Adj. High', 'Adj. Low', 'Adj. Close', 'Adj. Volume']]
df['HL_PCT'] = (df['Adj. High'] - df['Adj. Low']) / df['Adj. Close'] * 100.0
df['PCT_change'] = (df['Adj. Close'] - df['Adj. Open']) / df['Adj. Open'] * 100.0
df = df[['Adj. Close', 'HL_PCT', 'PCT_change', 'Adj. Volume']]
forecast_col = 'Adj. 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)
confidence = clf.score(X_test, y_test)
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]
df['Adj. Close'].plot()
df['Forecast'].plot()
plt.legend(loc=4)
plt.xlabel('Date')
plt.ylabel('Price')
plt.show()
結(jié)果:
?
保存和擴(kuò)展
上一篇教程中,我們使用回歸完成了對股票價格的預(yù)測,并使用 Matplotlib 可視化。這個教程中,我們會討論一些接下來的步驟。
我記得我第一次嘗試學(xué)習(xí)機(jī)器學(xué)習(xí)的時候,多數(shù)示例僅僅涉及到訓(xùn)練和測試的部分,完全跳過了預(yù)測部分。對于那些包含訓(xùn)練、測試和預(yù)測部分的教程來說,我沒有找到一篇解釋保存算法的文章。在那些例子中,數(shù)據(jù)通常非常小,所以訓(xùn)練、測試和預(yù)測過程都很快。在真實(shí)世界中,數(shù)據(jù)都非常大,并且花費(fèi)更長時間來處理。由于沒有一篇教程真正談?wù)摰竭@一重要的過程,我打算包含一些處理時間和保存算法的信息。
雖然我們的機(jī)器學(xué)習(xí)分類器花費(fèi)幾秒來訓(xùn)練,在一些情況下,訓(xùn)練分類器需要幾個小時甚至是幾天。想象你想要預(yù)測價格的每天都需要這么做。這不是必要的,因為我們呢可以使用 Pickle 模塊來保存分類器。首先確保你導(dǎo)入了它:
import pickle
使用 Pickle,你可以保存 Python 對象,就像我們的分類器那樣。在定義、訓(xùn)練和測試你的分類器之后,添加:
with open('linearregression.pickle','wb') as f:
pickle.dump(clf, f)
現(xiàn)在,再次執(zhí)行腳本,你應(yīng)該得到了linearregression.pickle,它是分類器的序列化數(shù)據(jù)。現(xiàn)在,你需要做的所有事情就是加載pickle文件,將其保存到clf,并照常使用,例如:
pickle_in = open('linearregression.pickle','rb')
clf = pickle.load(pickle_in)
代碼中:
import Quandl, math
import numpy as np
import pandas as pd
from sklearn import preprocessing, cross_validation, svm
from sklearn.linear_model import LinearRegression
import matplotlib.pyplot as plt
from matplotlib import style
import datetime
import pickle
style.use('ggplot')
df = Quandl.get("WIKI/GOOGL")
df = df[['Adj. Open', 'Adj. High', 'Adj. Low', 'Adj. Close', 'Adj. Volume']]
df['HL_PCT'] = (df['Adj. High'] - df['Adj. Low']) / df['Adj. Close'] * 100.0
df['PCT_change'] = (df['Adj. Close'] - df['Adj. Open']) / df['Adj. Open'] * 100.0
df = df[['Adj. Close', 'HL_PCT', 'PCT_change', 'Adj. Volume']]
forecast_col = 'Adj. Close'
df.fillna(value=-99999, inplace=True)
forecast_out = int(math.ceil(0.1 * 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)
#COMMENTED OUT:
##clf = svm.SVR(kernel='linear')
##clf.fit(X_train, y_train)
##confidence = clf.score(X_test, y_test)
##print(confidence)
pickle_in = open('linearregression.pickle','rb')
clf = pickle.load(pickle_in)
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]
df['Adj. Close'].plot()
df['Forecast'].plot()
plt.legend(loc=4)
plt.xlabel('Date')
plt.ylabel('Price')
plt.show()
?
?
要注意我們注釋掉了分類器的原始定義,并替換為加載我們保存的分類器。就是這么簡單。
評論
查看更多