ValueError:MatMul:输入操作数1在其核心维度0中具有不匹配,带有Gufunc签名(n?,k),(k,m?) - >(n?,m?)(尺寸1不同于2)
第一次试图适应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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我会发表评论,但可以。
您可以使设计矩阵x_poly,但将模态放在x上。然后,您尝试预测转换的设计矩阵的新数据,因此形状将有所不同。
如果我没错,那应该是
I would comment, but can yet.
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