Reputation: 151
when I want to use sklearn.model_selection.train_test_split
to split train set and test set,it raises error this like:
AttributeError: module 'sklearn' has no attribute 'model_selection'
My code is as follow:
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)
In Pycharm,the scikit-learn package version is 0.19.1.
Thank you for your help!
Upvotes: 14
Views: 20091
Reputation: 408
In Pycharm you have to reload the Python Console for sklearn to show all its components. If you do not all you see is sklearn.base and a few other general things. I just recreated this myself and found this answer.
Upvotes: 1
Reputation: 14011
You can import like from sklearn.model_selection import train_test_split
. An example from the official docs :)
>>> 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]
Upvotes: 1
Reputation: 29099
You need
import sklearn.model_selection
before calling your function
Upvotes: 18