无法在 python 中实现cross_validation



我是Python机器学习的初学者,我正在用python练习股票预测程序,但根据教程"从sklearn导入cross_validation"被使用。但是在我的编译器Intellij IDEA中,我无法使用它,它显示错误

import quandl, math
import numpy as np
from sklearn import preprocessing, svm
from sklearn import cross_validation
from sklearn.linear_model import LinearRegression
df = quandl.get('NSE/RCOM', api_key='-pQJsBYvTAsU-cSopBvA')
df = df[['Open', 'High', 'Close', 'Low', 'Total Trade Quantity']]
df['PCT_change'] = (df['Close'] - df['Open']) / df['Open'] * 10
df = df[['Close', 'HL_pct', 'PCT_change', 'Total Trade Quantity']]
forecast_col = 'Close'
df.fillna(-99999, inplace=True)
forecast_out = int(math.ceil(0.01 * len(df)))
print (forecast_out)
df['lable'] = df[forecast_col].shift(-forecast_out)
df.dropna(inplace=True)
X = np.array(df.drop(['lable'], 1))
y = np.array(df['lable'])
X = preprocessing.scale(X)
y = np.array(df['lable'])
print(len(X),len(y))
X_train, X_test, y_train, y_test = cross_valalidation.train_test_split(X, y, test_size=0.2)
clf = LinearRegression()
clf.fit(X_train, y_train)
accuracy = clf.score(X_test, y_test)
print(accuracy)

}

OUTPUT:
C:UsersaravindAppDataLocalProgramsPythonPython37-32python.exe
C:/Users/aravind/Desktop/programtopractics/untitled3/stock.py
C:UsersaravindAppDataLocalProgramsPythonPython37-32libsite-packages
sklearnexternalsjoblibexternalscloudpicklecloudpickle.py:47: 
DeprecationWarning: the imp module is deprecated in favour of importlib; 
C:/Users/aravind/Desktop/programtopractics/untitled3/stock.py
see the module's documentation for alternative uses
import imp
31
Traceback (most recent call last):
File "C:/Users/aravind/Desktop/programtopractics/untitled3/stock.py", 
line 29, in <module>
X_train, X_test, y_train, y_test = cross_val_score.train_test_split(X, y, test_size=0.2)
AttributeError: 'function' object has no attribute 'train_test_split'
3019 3019

这是因为train_test_split在模块model_selection中,而不是在模块cross_validation中。

因此,使用

from sklearn.model_selection import train_test_split

另外,替换这个

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

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.20, random_state=42)

相关内容

  • 没有找到相关文章

最新更新