![](/img/trans.png)
[英]Error in colmn vector multiplication ValueError: matmul: Input operand 1 has a mismatch in its core dimension 0
[英]I'm getting a matmul: input operand 1 has a mismatch...error...full error in body
错误: 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)
我正在使用 csv dataframe 进行线性回归。 在重塑和拟合 x 和 y 值后,我遇到了这个错误,突出显示了 model.predict 部分。
我的代码:
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
from sklearn.linear_model import LinearRegression
import requests
import io
import csv
url = "https://raw.githubusercontent.com/DairyProducts/misc/main/cadeath.csv"
download = requests.get(url).content
df = pd.read_csv(io.StringIO(download.decode('utf-8')))
x = df['x'].to_numpy()
y = df['y'].to_numpy()
model = LinearRegression()
x = x.reshape(-1, 1)
y = y.reshape(-1, 1)
model.fit(x, y)
r_sq = model.score(x, y)
y_pred = model.intercept_ + np.sum(model.coef_ * x, axis=1)
x_new = np.arange(10).reshape((-1, 2))
y_new = model.predict(x_new)
plt.plot(x_new, y_new)
plt.show()
您在model.fit(x, y)
中适合 model 的 x 形状是(361, 1)
。 您在model.predict(x_new)
中predict
的 x 形状是(5, 2)
。 他们的第二个维度不匹配。 您可能需要更改x_new
形状,例如
x_new = np.arange(10).reshape((-1, 1))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.