机器学习-使用sklearn

时间:2019-01-19 22:52:40

标签: python python-3.x dataframe scikit-learn

我正在编写基本的股票预测代码段,但是不断出现以下错误。

  

AttributeError:“函数”对象没有属性“ train_test_split”

除此之外,我的代码似乎是正确的,并且已经在编码过程中进行了测试。因此,我很确定除了python的库问题之外,没有其他问题会导致此问题。有谁知道解决此问题的方法,以便项目可以继续进行?这是我的代码,如果有帮助的话。

import quandl, math
import numpy as np
import pandas as pd
from sklearn import preprocessing, svm
from sklearn.model_selection import cross_validate
from sklearn.linear_model import LinearRegression

#Getting the data
df = quandl.get("WIKI/GOOGL")

#Selecting the data we want from the database
df = df[['Adj. Open','Adj. High','Adj. Low','Adj. Close','Adj. Volume']]

#Calculating percentage changes
df['HL_PCT'] = (df['Adj. High'] - df['Adj. Close']) / df['Adj. Close'] * 100
df['PCT_change'] = (df['Adj. Close'] - df['Adj. Open']) / df['Adj. Open'] * 100

#Refining the data even further
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))
y = np.array(df['label'])
x  = preprocessing.scale(x)
y = np.array(df['label'])

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

1 个答案:

答案 0 :(得分:5)

问题是train_test_split不在sklearn.crossvalidate内部,而是在sklearn.model_selection内部。如果要使用train_test_split,则应使用它-

from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)

有关更多详细信息,请查看此网址- https://scikit-learn.org/stable/modules/generated/sklearn.model_selection.train_test_split.html