机器学习 - 与sklearn

Jam*_*non 1 python dataframe python-3.x scikit-learn

我正在写一个基本的股票预测代码,但是我不断收到以下错误.

AttributeError:'function'对象没有属性'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)
Run Code Online (Sandbox Code Playgroud)

Lak*_*tai 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)
Run Code Online (Sandbox Code Playgroud)

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