我一直在尝试让我的线性回归模型预测到某个目的地的机票价格,然而,我不断遇到这个错误:
ValueError: matmul: Input operand 1 has a mismatch in its core dimension 0, with gufunc signature (n?,k),(k,m?)->(n?,m?) (size 3 is different from 1)
我的代码如下:
df = pd.read_csv("data.csv")df.dropna(inplace=True)x = df[["Distances","Flight_time","Total_Staff_Pay"]]y = df[["Price"]]x_train, x_test, y_train, y_test = train_test_split(x, y, train_size = 0.5, test_size = 0.5, random_state=1)regression = LinearRegression()regression.fit(x_train,y_train)#预测机场a = [[3417.33],[7.30178432893716],[4804.57408844065]]print(regression.predict(a))
我看过其他问题和答案,它们并没有真正帮到我。
回答:
这应该可以工作
a = [[3417.33,7.30178432893716,4804.57408844065]]
一行代表一个观测值,因此应该以1x3
的形式传递,而不是3x1