pythonmachine-learningscikit-learncross-validationk-fold

difference between cross_val_score and KFold


I am learning Machine learning and I am having this doubt. Can anyone tell me what is the difference between:-

from sklearn.model_selection import cross_val_score

and

from sklearn.model_selection import KFold

I think both are used for k fold cross validation, but I am not sure why to use two different code for same function. If there is something I am missing please do let me know. ( If possible please explain difference between these two methods)

Thanks,


Solution

  • So, these are completely different. Yo can make K fold of data and use it on cross validation like this:

    # create a splitter object
    kfold = KFold(n_splits = 10)       
    # define your model (any model)
    model = XGBRegressor(**params)     
    # pass your model and KFold object to cross_val_score
    # to fit and get the mse of each fold of data
    cv_score = cross_val_score(model,
                               X, y, 
                               cv=kfold, 
                               scoring='neg_root_mean_squared_error')
    print(cv_score.mean(), cv_score.std())