无法在 python 中实现 cross_validation

Posted

技术标签:

【中文标题】无法在 python 中实现 cross_validation【英文标题】:Can't implement the cross_validation in python 【发布时间】:2019-03-24 20:29:15 【问题描述】:

我是 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:\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

【问题讨论】:

cross_val_score 是一个函数,您将它与 cross_validation 模块混淆了。 我应该在哪里改变!! 我只是从这个链接学习-->pythonprogramming.net/… 供您参考,请参阅该链接中的程序,我知道 cross_validation 是一个函数,但如何实现它“无法从输出中的“sklearn” 你可能想用这个:scikit-learn.org/stable/modules/generated/… 【参考方案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)

【讨论】:

以上是关于无法在 python 中实现 cross_validation的主要内容,如果未能解决你的问题,请参考以下文章

使用Apache Spark实现python功能

如何在交叉验证和 GridSearchCV 中实现 SMOTE

C++ 无法在 Visual Studio 中实现抽象类

无法在 GraphQL 中实现联合

在 Python 中实现装饰器模式

在python中实现类接口的正确方法是啥