[Python Sklearn] AttributeError: ‘function’ object has no attribute ‘train_test_split’

Here we are only focusing on the train_test_split attribute of sklearn used in machine learning. In previous version of SciKit Learn the module cross_validation of package sklearn used to have this train_test_split attribute. But there has been some change in the pacakge binding and now the module is associated with the model_selection module.

Error is Occurred in the following scenario:

from sklearn import cross_validation
''' Your additional modules and
      intermediate code'''

'''you can use your X and y values as per your problem'''

X_train, X_test, y_train, y_test = \
     cross_validation.train_test_split(X, y, test_size=0.2, random_state=40)

Now in order to perform the cross validaiton with the split of test and train data you need to call cross_validate from module_selection like shown below:

from sklearn.model_selection import cross_validate

If you want to perform the test train splitting than you should implicitly import train_test_split like shown below:

from sklearn.model_selection import train_test_split
'''your other imports
    and your code'''

train_test_split(X, y, test_size=0.2, random_state=40)