Search code examples
pythonpython-3.xscikit-learncross-validation

'function' object has no attribute 'train_test_split'


I am new to implementing Machine Learning in Python and am currently trying out KNN classification following YouTube tutorials. Here is the code.

import numpy as np
#from sklearn.model_selection import train_test_split
from sklearn.model_selection import cross_validate
import pandas as pd

df=pd.read_csv('breast-cancer-wisconsin.data.txt')
df.replace('?', -99999, inplace=True)
df.drop(['id'],1,inplace=True)

X=np.array(df.drop(['class'],1))
y=np.array(df['class'])

X_train, X_test, y_train, y_test=cross_validate.train_test_split(X,y,test_size=0.2)

I get the following error: -

X_train, X_test, y_train, y_test=cross_validate.train_test_split(X,y,test_size=0.2)
AttributeError: 'function' object has no attribute 'train_test_split'

I tried importing train_test_split as

 from sklearn.model_selection import train_test_split

but then I get the same error. Any help is appreciated. Thanks!


Solution

  • train_test_split is a separate module (docs), and it is not to be used in combination with cross_validate; the correct usage here is (assuming scikit-learn v0.20):

    from sklearn.model_selection import train_test_split
    # [...]
    X_train, X_test, y_train, y_test=train_test_split(X,y,test_size=0.2)