AttributeError:模块“ sklearn”没有属性“ model_selection”

时间:2018-12-12 11:51:53

标签: python scikit-learn

当我想使用sklearn.model_selection.train_test_split拆分火车组和测试组时,会引发如下错误:

AttributeError: module 'sklearn' has no attribute 'model_selection'

我的代码如下:

import pandas as pd
import sklearn
data = pd.read_csv('SpeedVideoDataforModeling.csv',header=0,)
data_x = data.iloc[:,1:4]
data_y = data.iloc[:,4:]
x_train , x_test, y_train, y_test = 
sklearn.model_selection.train_test_split(data_x,data_y,test_size = 0.2)

在Pycharm中,scikit-learn软件包的版本为0.19.1。 enter image description here 谢谢您的帮助!

2 个答案:

答案 0 :(得分:1)

您需要

import sklearn.model_selection

在调用函数之前

答案 1 :(得分:1)

您可以像 from sklearn.model_selection import train_test_split 一样导入。官方文档中的一个例子:)

>>> import numpy as np
>>> from sklearn.model_selection import train_test_split
>>> X, y = np.arange(10).reshape((5, 2)), range(5)
>>> X
array([[0, 1],
       [2, 3],
       [4, 5],
       [6, 7],
       [8, 9]])
>>> list(y)
[0, 1, 2, 3, 4]
>>> X_train, X_test, y_train, y_test = train_test_split(
...     X, y, test_size=0.33, random_state=42)
...
>>> X_train
array([[4, 5],
       [0, 1],
       [6, 7]])
>>> y_train
[2, 0, 3]
>>> X_test
array([[2, 3],
       [8, 9]])
>>> y_test
[1, 4]