Python 在简单回归模型中使用预测函数时出错(形状未对齐)

Python 在简单回归模型中使用预测函数时出错(形状未对齐),python,pandas,numpy,scikit-learn,Python,Pandas,Numpy,Scikit Learn,有谁能帮我解决上面的错误吗。实际上,我在机器学习中使用的简单回归模型中使用了预测函数,结果发现了一个错误。我已经使用了重塑功能来转换我的测试和训练数据,并相应地使用了它们。 执行的代码是:- import pandas as pd from numpy import * import matplotlib.pyplot as plt dataset=pd.read_csv("Salary_Data.csv") X=dataset.iloc[:,0].values

有谁能帮我解决上面的错误吗。实际上,我在机器学习中使用的简单回归模型中使用了预测函数,结果发现了一个错误。我已经使用了重塑功能来转换我的测试和训练数据,并相应地使用了它们。 执行的代码是:-

   import pandas  as pd
   from numpy import *
   import matplotlib.pyplot as plt
   dataset=pd.read_csv("Salary_Data.csv")
   X=dataset.iloc[:,0].values
   X
   Y=dataset.iloc[:,1].values
   dataset.isnull()
   from sklearn.impute import SimpleImputer
   imputer=SimpleImputer(missing_values="NaN",strategy="mean",verbose=0)
   from sklearn.model_selection import train_test_split
   X_train,X_test,Y_train,Y_test=train_test_split(X,Y,test_size=1/3,random_state=0)
   x_train=X_train.reshape(1,-1)
   X_train
   x_train
   y_train=Y_train.reshape(1,-1)
   Y_test
   from sklearn.linear_model import LinearRegression
   regressor=LinearRegression()
   X_train
   LinearRegression(copy_X=True, fit_intercept=True, n_jobs=None, normalize=False)
   x_test=X_test.reshape(-1,1)
   x_test
   y_test=Y_test.reshape(-1,1)
   y_test
   x_train
   regressor.fit(x_train,y_train)
   x_test
   y_pred=regressor.predict(x_test)
错误是:

ValueError                                Traceback (most recent call last)
<ipython-input-42-350ddd5e0bb0> in <module>()
----> 1 y_pred=regressor.predict(x_test)

2 frames
/usr/local/lib/python3.6/dist-packages/sklearn/utils/extmath.py in safe_sparse_dot(a, b, dense_output)
    140         return ret
    141     else:
--> 142         return np.dot(a, b)
    143 
    144 

<__array_function__ internals> in dot(*args, **kwargs)

ValueError: shapes (10,1) and (20,20) not aligned: 1 (dim 1) != 20 (dim 0) 

模型的尺寸标注错误(即您已用1,-1而不是-1,1重塑)

更改以下行

x_train=X_train.reshape(1,-1) // your code Change to bellow code
x_train=X_train.reshape(-1,1) 
...
y_train=Y_train.reshape(1,-1)// your code Change to bellow code
y_train=Y_train.reshape(-1,1)

我希望这对你有用

你能把这篇文章的格式设置好吗。使用mardown格式添加代码和错误。此外,您可以显示X和Y的大小。包括培训和测试