2

我试图预测我的系列第二天的股价,但我不知道如何“查询”我的模型。这是我在 Python 中的代码:

# Define my period
d1 = datetime.datetime(2016,1,1)
d2 = datetime.datetime(2016,7,1)

# Get the data
df = web.DataReader("GOOG", 'yahoo', d1, d2)
# Calculate some indicators
df['20d_ma'] = pandas.rolling_mean(df['Adj Close'], window=20)
df['50d_ma'] = pandas.rolling_mean(df['Adj Close'], window=50)

# Create the model
from sklearn.linear_model import LinearRegression
from sklearn.cross_validation import train_test_split

X = df[list(df.columns)[6:]] # Adj Close and indicators...
y = df['Adj Close']

X_train, X_test, y_train, y_test = train_test_split(X, y)

model = LinearRegression()
model.fit(X_train,y_train)

好的,我需要查询模型( model.predict(..¿?..) )来预测“下一天”的股价。

我该怎么做?

提前致谢!!!

4

2 回答 2

4
model.predict(X_test) 

将完成这项工作。这直接来自精彩的文档 在提问之前做你的基本阅读。

Edit1:回应评论,那么您的特征工程有问题。您无法使用模型预测值(使用您没有价值的特征。)。您将不得不回过头来重新考虑为什么选择这些功能以及它们如何影响您的结果变量等。

Edit2:您可能需要做的是两个模型,一个时间序列模型在那个 20d-avg 上预测明天的 20d-avg。然后用它来预测股票价格。我个人认为,如果您可以进行时间序列模型并获得不错的结果,则不需要第二个模型。

于 2016-07-09T19:14:04.537 回答
1

您可以使用属于 sklearn 的 Predict()。并计算“下一天”的 X 值(您需要通过自己的算法定义它)。

直接来自sklearn库源码:

def predict(self, X):
        """Predict using the linear model
        Parameters
        ----------
        X : {array-like, sparse matrix}, shape = (n_samples, n_features)
            Samples.
        Returns
        -------
        C : array, shape = (n_samples,)
            Returns predicted values.
        """
        return self._decision_function(X)

    _center_data = staticmethod(center_data)
于 2016-07-09T19:17:20.893 回答