Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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. enter image description here Thank you for your help!

like image 781
hkwei Avatar asked Dec 12 '18 11:12

hkwei


2 Answers

You need

import sklearn.model_selection

before calling your function

like image 141
blue_note Avatar answered Sep 28 '22 00:09

blue_note


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]
like image 37
DevLoverUmar Avatar answered Sep 27 '22 22:09

DevLoverUmar