Search code examples
pythonregressioncross-validationpolynomials

Cross-Validation in Python with Polynomials


I'm trying to cross-validate some stuff in python but using this:

from sklearn.model_selection import cross_val_score

In context, I'm creating the following model:

coef = poly.polyfit(train["X_training"], train["Y_training"], 4)
model = poly.Polynomial(coef)

And now I'm trying to cross validate it this way

from sklearn.model_selection import cross_val_score
cross_val_score(model,test["X_test"], test["Y_test"],cv=5)

But the errors I have are this:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-186-5bcb7ab926a4> in <module>
      1 from sklearn.model_selection import cross_val_score
----> 2 cross_val_score(model,test["X_test"], test["Y_test"],cv=0)

~\anaconda3\envs\TensonFlow\lib\site-packages\sklearn\utils\validation.py in inner_f(*args, **kwargs)
     70                           FutureWarning)
     71         kwargs.update({k: arg for k, arg in zip(sig.parameters, args)})
---> 72         return f(**kwargs)
     73     return inner_f
     74 

~\anaconda3\envs\TensonFlow\lib\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)
    397     """
    398     # To ensure multimetric format is not supported
--> 399     scorer = check_scoring(estimator, scoring=scoring)
    400 
    401     cv_results = cross_validate(estimator=estimator, X=X, y=y, groups=groups,

~\anaconda3\envs\TensonFlow\lib\site-packages\sklearn\utils\validation.py in inner_f(*args, **kwargs)
     70                           FutureWarning)
     71         kwargs.update({k: arg for k, arg in zip(sig.parameters, args)})
---> 72         return f(**kwargs)
     73     return inner_f
     74 

~\anaconda3\envs\TensonFlow\lib\site-packages\sklearn\metrics\_scorer.py in check_scoring(estimator, scoring, allow_none)
    423             return None
    424         else:
--> 425             raise TypeError(
    426                 "If no scoring is specified, the estimator passed should "
    427                 "have a 'score' method. The estimator %r does not."

TypeError: If no scoring is specified, the estimator passed should have a 'score' method. The estimator Polynomial([ 6.0000592 ,  8.02956741, -5.99141415, -3.00869471,  1.99588109], domain=[-1,  1], window=[-1,  1]) does not.

Am I doing somehing wrong?


Solution

  • Well it looks like the way to correctly Cross-Validate this is with

    from sklearn.model_selection import cross_val_score
    from sklearn.linear_model import LinearRegression
    from sklearn.preprocessing import PolynomialFeatures
    test = test.dropna()
    poly_features = PolynomialFeatures(degree=grade)
    X_poly = poly_features.fit_transform(test)
    poly = LinearRegression()
    cross_val_score(poly, X_poly, test["Y_test"], cv=5)