显示值错误:形状(1,2)和(3,1)未对齐:2(尺寸1)!=3(尺寸0)

2024-10-04 07:25:41 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图运行这段代码,预测模型(predicts=model.predict(y1))显示了标题中建议的错误。我是新来的,有什么需要帮忙的吗

from sklearn import linear_model
regr = linear_model.LinearRegression()

import seaborn as seabornInstance 
from sklearn.model_selection import train_test_split 

from sklearn import metrics


x1 = np.asanyarray(train[['MachineAvailability','StatNumIn','MachineMTTR']])
y1 = np.asanyarray(train[['StatNumOut']])
regr.fit (x1, y1)
# The coefficients
print ('Coefficients: ', regr.coef_)
print('Intercept: \n', regr.intercept_)

y_hat= regr.predict(test[['MachineAvailability','StatNumIn','MachineMTTR']])
x = np.asanyarray(test[['MachineAvailability','StatNumIn','MachineMTTR']])
y = np.asanyarray(test[['StatNumOut']])
print("Residual sum of squares: %.2f"
      % np.mean((y_hat - y) ** 2))

# Explained variance score: 1 is perfect prediction
print('Variance score: %.2f' % regr.score(x, y))

import tkinter as tk 
import statsmodels.api as sm
from sklearn.decomposition import PCA

X = sm.add_constant(x1) # adding a constant

model = sm.OLS(x1, y1).fit()

predictions = model.predict(y1) 

# prediction with sklearn
New_MachineAvailability = 90
#New_StatNumIn = 90
New_MachineMTTR = 20
print ('Predicted Production: \n', regr.predict([[New_MachineMTTR,New_MachineAvailability ]]))

Tags: fromtestimportnewmodelnpsklearnpredict
1条回答
网友
1楼 · 发布于 2024-10-04 07:25:41

该错误告诉您需要知道的一切:predict函数输入的形状与预期不匹配

您已经对具有x1形状的输入数据(例如(n, k))进行了模型训练,其中n是行数(轴0)和k列数(轴1)。这意味着predict函数的输入必须在列数方面与预期的形状匹配,即(m, k)。可以有任意数量的行(这无关紧要,只是样本的数量),但有相同数量的列k

在本例中,您使用y1运行predict,这是模型的标签(目标)。它有不同的形状,很可能这不是你的意思。如果你通过了x1,它会起作用,但没有什么意义-x1是你的训练数据。您应该为测试留出一些数据,比如x2,您可以在这些数据上运行预测并评估模型的执行情况

相关问题 更多 >