我得到一个矩阵:输入操作数 1 不匹配...错误。。。正文中出现完全错误



错误: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数据帧进行线性回归。在对x和y值进行整形和拟合后,我遇到了这个错误,突出显示了model.prpredict部分。

我的代码:

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)中拟合模型的x形状是(361, 1)。您在model.predict(x_new)predict的x形状是(5, 2)。他们的第二维度不匹配。您可能需要像一样更改x_new形状

x_new = np.arange(10).reshape((-1, 1))

最新更新