Error with a code for Cross Validation K-Folds

Hi, I am quite new to python and I just completed Ridge regression on a dataset and I have to check it with K folds cross validation. I’ve been having so many issues with it and yesterday it finally worked, but today I have an error that I can’t go through with. Maybe someone knows what should I change about it for it to finally work properly. :smiley:

The code for the folds works, but when I try to get the scores I have this type of error.
kf = KFold(n_splits = 5, random_state = 677, shuffle = True)
kf.get_n_splits(X)
for i, (train_index, test_index) in enumerate(kf.split(X)):
print(f’Fold {i}‘)
print(f’ Train: index={train_index}‘)
print(f’ Test: index={test_index}')

scores = cross_val_score(B, X, y, scoring = “neg_mean_squared_error”, cv = kf, n_jobs = -1)


TypeError Traceback (most recent call last)
/var/folders/5q/_l03dv393n5_x3nnp1014h7w0000gn/T/ipykernel_9821/3519554670.py in
----> 1 scores = cross_val_score(B, X, y, scoring = “neg_mean_squared_error”, cv = kf, n_jobs = -1)

~/opt/anaconda3/lib/python3.9/site-packages/sklearn/model_selection/_validation.py in cross_val_score(estimator, X, y, groups, scoring, cv, n_jobs, verbose, fit_params, pre_dispatch, error_score)
505 “”"
506 # To ensure multimetric format is not supported
→ 507 scorer = check_scoring(estimator, scoring=scoring)
508
509 cv_results = cross_validate(

~/opt/anaconda3/lib/python3.9/site-packages/sklearn/metrics/_scorer.py in check_scoring(estimator, scoring, allow_none)
446 “”"
447 if not hasattr(estimator, “fit”):
→ 448 raise TypeError(
449 “estimator should be an estimator implementing ‘fit’ method, %r was passed”
450 % estimator

TypeError: estimator should be an estimator implementing ‘fit’ method, popularity
intersept 33.235570
duration_ms -0.230293
danceability 1.503512
energy -0.758205
key -0.089013
loudness 0.631607
mode -0.388885
speechiness -1.284461
acousticness -0.258165
instrumentalness -2.485137
liveness 0.314252
valence -2.431459
tempo 0.411236
time_signature 0.462741 was passed

Hello

The first argument you pass to cross_val_score is an estimator. The estimator must have a “fit method” as the errror says.
Make sure the estimator “B” you are passing on to cross_val_score() has “fit()” method.

I think this is forum is for pytorch specific issues.