Why do I get ValueError: could not broadcast input array from shape (15163,19) into shape (15163,0)?

Why do I get ValueError: could not broadcast input array from shape (15163,19) into shape (15163,0)?

这看起来像是一个关于 SO 的常见问题,但是其他问题的答案对我没有帮助。所以我试着解释发生了什么。我想回归以下 Pandas 数据框

# Data
y = df[['variation']]
x = df.drop(['variation','value_next','id'], axis=1)

现在y

Name: variation, dtype: float64
<class 'pandas.core.frame.DataFrame'>
Int64Index: 17690 entries, 0 to 17693
Data columns (total 1 columns):
 #   Column     Non-Null Count  Dtype  
---  ------     --------------  -----  
 0   variation  17690 non-null  float64
dtypes: float64(1)
memory usage: 276.4 KB

x有19列,每列有17690个条目。

然后我尝试做回归:

# Uses pipeline to create a polynomial regression equation
def PolynomialRegression(degree=2, **kwargs):
    return make_pipeline(PolynomialFeatures(degree), LinearRegression(**kwargs))

# Uses grid search to find the best polynomial
param_grid = {'polynomialfeatures__degree': np.arange(21),
              'linearregression__fit_intercept': [True, False],
              'linearregression__normalize': [True, False]}

grid = GridSearchCV(PolynomialRegression(), param_grid, cv=7)
grid.fit(x,y)

grid.fit 调用中我得到了那个错误。 我也尝试将 xy 转换为 np 数据,但没有成功。

以下代码片段应该可以解决您的错误:

param_grid = {
               'polynomialfeatures__degree': np.arange(1,21)
              ,'linearregression__fit_intercept': [True, False]
              ,'linearregression__normalize': [True, False]
             }

尽管我必须承认 21 言过其实。