【问题标题】:Confused? If using all scalar values, you must pass an index使困惑?如果使用所有标量值,则必须传递一个索引
【发布时间】:2018-05-29 00:31:14
【问题描述】:
feature_names = X.columns
feature_names = np.array(feature_names)
feature_names = feature_names.reshape(60,1)

X = np.nan_to_num(X)
Y = np.nan_to_num(Y)

lr = LinearRegression()
model = lr.fit(X,Y)

model_coefs = model.coef_

coefs1 = pd.DataFrame(
    data={'feature': feature_names,
          "orig_coefs" : model_coefs})

print coefs1

在尝试打印表 coefs1 时,我不断收到“如果使用所有标量值,则必须传递索引”错误。这非常令人沮丧,因为我过去曾使用此代码列出 X 中的所有变量名称,并在模型构建后将它们与它们的系数匹配。在这个例子中,我的 X 中有 60 个变量,我将它重新整形为一维的,以匹配我的系数 (60 ,1)。

我无法理解这个实例是如何给我这个错误的,当我使用这种语法的变体查看我过去工作的不同示例时,一切运行正常。

【问题讨论】:

  • pd.Series(data={'feature': feature_names,"orig_coefs" : model_coefs}).to_frame()
  • 嗯,这导致我得到一个形状为 (2, 1) 的框架,如果这有意义的话
  • pd.DataFrame(data={'feature': feature_names.flatten(),"orig_coefs" : model_coefs.flatten()})

标签: python pandas coefficients


【解决方案1】:

转换字典

df = pd.Series(dict).to_frame('new_col').reset_index()

重命名列名

df.columns = ['col1', 'col2']

【讨论】:

    猜你喜欢
    • 2016-11-17
    • 2017-02-19
    • 2018-12-04
    • 2023-01-24
    • 2020-03-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多