Why am I receiving an error for return_train_score when trying to cross validate

Question:

I am trying to execute a for loop for the following dictionary, but I am receiving an error that states:

"cross_val_score() got an unexpected keyword argument ‘return_train_score’"

I thought this was a valid parameter for cross_val_score so I’m a bit confused any help would be appreciated,

thanks

results_dict = {
    "n_neighbors": [],
    "mean_train_score": [],
    "mean_cv_score": []}

results_dict

for k in range(2,20, 2):
    knn = KNeighborsClassifier(n_neighbors=k)
    scores = cross_val_score(knn, X_train, y_train, cv=5, return_train_score=True)
    results_dict['n_neighbors'].append(k)
    results_dict['mean_train_score'].append(['train_score'])
    results_dict['mean_cv_score'].append(['test_score'])

results_dict
Asked By: user20266015

||

Answers:

The error message suggests that the return_train_score argument is not a valid parameter for the cross_val_score() function. You can confirm this by looking at the documentation for the function, which you can access by running help(cross_val_score) or by looking it up online.

Assuming that return_train_score is not a valid parameter for the cross_val_score() function, you can fix the error by simply removing that argument from the function call.

Note that you may need to adjust the code that appends the scores to the results_dict dictionary depending on how the cross_val_score() function returns its results. You can refer to the documentation for the function to learn how to access the train and test scores.

Answered By: Justin Dean

Check documentation of the cross_val_score function here

The cross_val_score doesn’t have any such parameter named return_train_score. So, I suggest removing this parameter will eliminate this error.

Answered By: Sadman Sakib