ValueError:MatMul:输入操作数1在其核心维度0中具有不匹配,带有Gufunc签名(n?,k),(k,m?) - >(n?,m?)(尺寸1不同于2)

发布于 2025-02-08 07:28:13 字数 1976 浏览 1 评论 0原文

第一次试图适应Python。我有一组测量值,需要对提供的数据进行非线性拟合。拟合应为二次多项式。这是代码和错误:

from numpy import *
import matplotlib.pyplot as plt
from sklearn.linear_model import LinearRegression
from sklearn.preprocessing import PolynomialFeatures

x = array([[0.121644, 0.243288, 0.486576, 0.60822, 0.729864, 0.851508]]).reshape(-1,1)
y = array([[0.95, 1.02, 1.21, 1.35, 1.47, 1.61]]).reshape(-1,1)

poly = PolynomialFeatures(degree= 2, include_bias=False)
x_poly = poly.fit_transform(x)

reg = LinearRegression()
reg.fit(x,y)

x_val= linspace(0.121644, 0.851508, 100).reshape(-1,1)
x_vp= poly.transform(x_val)


y_val = reg.predict(x_vp)

plt.scatter(x,y)
plt.plot(x_val, y_val)
ValueError                                Traceback (most recent call last)
<ipython-input-49-38d844be643a> in <module>
      9 
     10 
---> 11 y_val = reg.predict(x_vp)
     12 
     13 plt.scatter(x,y)

~\anaconda3\lib\site-packages\sklearn\linear_model\_base.py in predict(self, X)
    223             Returns predicted values.
    224         """
--> 225         return self._decision_function(X)
    226 
    227     _preprocess_data = staticmethod(_preprocess_data)

~\anaconda3\lib\site-packages\sklearn\linear_model\_base.py in _decision_function(self, X)
    207         X = check_array(X, accept_sparse=['csr', 'csc', 'coo'])
    208         return safe_sparse_dot(X, self.coef_.T,
--> 209                                dense_output=True) + self.intercept_
    210 
    211     def predict(self, X):

~\anaconda3\lib\site-packages\sklearn\utils\extmath.py in safe_sparse_dot(a, b, dense_output)
    149             ret = np.dot(a, b)
    150     else:
--> 151         ret = a @ b
    152 
    153     if (sparse.issparse(a) and sparse.issparse(b)

ValueError: matmul: Input operand 1 has a mismatch in its core dimension 0, with gufunc signature (n?,k),(k,m?)->(n?,m?) (size 1 is different from 2)

我知道问题在于x和x_val的维度,但我不知道如何修复它。

First time trying to make a fit on Python. I have sets of measurements and need to make a non-linear fit on provided data. Fit should be quadratic polynomial. Here are the code and the error:

from numpy import *
import matplotlib.pyplot as plt
from sklearn.linear_model import LinearRegression
from sklearn.preprocessing import PolynomialFeatures

x = array([[0.121644, 0.243288, 0.486576, 0.60822, 0.729864, 0.851508]]).reshape(-1,1)
y = array([[0.95, 1.02, 1.21, 1.35, 1.47, 1.61]]).reshape(-1,1)

poly = PolynomialFeatures(degree= 2, include_bias=False)
x_poly = poly.fit_transform(x)

reg = LinearRegression()
reg.fit(x,y)

x_val= linspace(0.121644, 0.851508, 100).reshape(-1,1)
x_vp= poly.transform(x_val)


y_val = reg.predict(x_vp)

plt.scatter(x,y)
plt.plot(x_val, y_val)
ValueError                                Traceback (most recent call last)
<ipython-input-49-38d844be643a> in <module>
      9 
     10 
---> 11 y_val = reg.predict(x_vp)
     12 
     13 plt.scatter(x,y)

~\anaconda3\lib\site-packages\sklearn\linear_model\_base.py in predict(self, X)
    223             Returns predicted values.
    224         """
--> 225         return self._decision_function(X)
    226 
    227     _preprocess_data = staticmethod(_preprocess_data)

~\anaconda3\lib\site-packages\sklearn\linear_model\_base.py in _decision_function(self, X)
    207         X = check_array(X, accept_sparse=['csr', 'csc', 'coo'])
    208         return safe_sparse_dot(X, self.coef_.T,
--> 209                                dense_output=True) + self.intercept_
    210 
    211     def predict(self, X):

~\anaconda3\lib\site-packages\sklearn\utils\extmath.py in safe_sparse_dot(a, b, dense_output)
    149             ret = np.dot(a, b)
    150     else:
--> 151         ret = a @ b
    152 
    153     if (sparse.issparse(a) and sparse.issparse(b)

ValueError: matmul: Input operand 1 has a mismatch in its core dimension 0, with gufunc signature (n?,k),(k,m?)->(n?,m?) (size 1 is different from 2)

I know there the problem is in dimensions of x and x_val, but I don't know how to fix it.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

总以为 2025-02-15 07:28:13

我会发表评论,但可以。

poly = PolynomialFeatures(degree= 2, include_bias=False)
x_poly = poly.fit_transform(x)

reg = LinearRegression()
reg.fit(x,y)

您可以使设计矩阵x_poly,但将模态放在x上。然后,您尝试预测转换的设计矩阵的新数据,因此形状将有所不同。

如果我没错,那应该是

...
reg.fit(x_poly,y)
...

I would comment, but can yet.

poly = PolynomialFeatures(degree= 2, include_bias=False)
x_poly = poly.fit_transform(x)

reg = LinearRegression()
reg.fit(x,y)

you make the design matrix x_poly but fit the modal on x. Then you try to predict on transformed design matrix for new data, so the shapes will be different.

if i am not mistaken, it should be

...
reg.fit(x_poly,y)
...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文