gpt4 book ai didi

pandas - 多项式回归给出错误 "shapes (18,17) and (1140,1) not aligned: 17 (dim 1) != 1140 (dim 0)"

转载 作者:行者123 更新时间:2023-11-30 09:32:50 27 4
gpt4 key购买 nike

我正在我的数据帧上使用多项式回归。这是代码

from sklearn.preprocessing import PolynomialFeatures
from sklearn import metrics
from sklearn.metrics import mean_squared_error
from math import sqrt


from sklearn.linear_model import LinearRegression
poly_reg = PolynomialFeatures(3)
lin_reg_2 = LinearRegression()
iv_poly = poly_reg.fit_transform(X_train)
poly_reg.fit(iv_poly, y_train)


lin_reg_2.fit(iv_poly, y_train)


predictValues = lin_reg_2.predict(X_test)
print("RMSE is:-")
print(np.sqrt(metrics.mean_squared_error(y_test, predictValues)))

错误:

ValueError                                Traceback (most recent call last)
<ipython-input-4-a77f27b571a6> in <module>()
106
107
--> 108 predictValues = lin_reg_2.predict(X_test)
109 print("RMSE is:-")
110 print(np.sqrt(metrics.mean_squared_error(y_test, predictValues)))

~\Anaconda3\lib\site-packages\sklearn\linear_model\base.py in predict(self, X)
254 Returns predicted values.
255 """
--> 256 return self._decision_function(X)
257
258 _preprocess_data = staticmethod(_preprocess_data)

~\Anaconda3\lib\site-packages\sklearn\linear_model\base.py in _decision_function(self, X)
239 X = check_array(X, accept_sparse=['csr', 'csc', 'coo'])
240 return safe_sparse_dot(X, self.coef_.T,
--> 241 dense_output=True) + self.intercept_
242
243 def predict(self, X):

~\Anaconda3\lib\site-packages\sklearn\utils\extmath.py in safe_sparse_dot(a, b, dense_output)
138 return ret
139 else:
--> 140 return np.dot(a, b)
141
142

ValueError: shapes (18,17) and (1140,1) not aligned: 17 (dim 1) != 1140 (dim 0)

我使用了线性回归,但它没有给我任何错误并成功计算了 RMSE,但应用多项式回归,它给出了错误

最佳答案

您没有转换X_test

iv_poly_test = poly_reg.transform(X_test)
lin_reg_2.predict(iv_poly_test)

关于pandas - 多项式回归给出错误 "shapes (18,17) and (1140,1) not aligned: 17 (dim 1) != 1140 (dim 0)",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51436922/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com