无法在python中实现cross_validation

时间:2018-10-19 23:11:23

标签: python python-3.x scikit-learn cross-validation sklearn-pandas

我是Python机器学习的初学者,当时我正在python中练习股票预测程序,但是根据教程“ from sklearn import 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:\Users\aravind\AppData\Local\Programs\Python\Python37-32\python.exe
C:/Users/aravind/Desktop/programtopractics/untitled3/stock.py
C:\Users\aravind\AppData\Local\Programs\Python\Python37-32\lib\site-packages
\sklearn\externals\joblib\externals\cloudpickle\cloudpickle.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

1 个答案:

答案 0 :(得分:1)

这是因为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)