我正在尝试交叉验证 python 中的一些东西,但使用这个:
from sklearn.model_selection import cross_val_score
在上下文中,我正在创建以下模型:
coef = poly.polyfit(train["X_training"], train["Y_training"], 4)
model = poly.Polynomial(coef)
现在我正在尝试以这种方式交叉验证
from sklearn.model_selection import cross_val_score
cross_val_score(model,test["X_test"], test["Y_test"],cv=5)
但我的错误是这样的:
---------------------------------------------------------------------------
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.
我做错了什么吗?
答案 0 :(得分:0)
看起来正确交叉验证的方法是使用
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)