简体   繁体   English

在python sklearn中安排多个回归结果

[英]Arranging multiple regression results in python sklearn

I have coefficients from running multiple regression. 我有多项回归的系数。

I want to arrange the independent variables as column names, and then place coefficients as the first row, to be able to see which independent variable has which coefficient. 我想将自变量设置为列名,然后将系数放置在第一行,以便能够看到哪个自变量具有哪个系数。

#assigning independent variables to x
x = df.drop("dependent var", axis = 1)
...
#creating a data frame with independent variables as headers
df_coef = pd.DataFrame(columns = x.columns)
#adding a row with a coefficients
df.loc[0] = x
#shifting index by 1
df.index = df.index + 1
#sorting by index 
df = df.sort()

I am getting an error: 我收到一个错误:

ValueError: cannot set a row with mismatched columns

What am I doing wrong? 我究竟做错了什么?

The first command line: 第一个命令行:

#assigning independent variables to x
x = df.drop("dependent var", axis = 1)

What you are doing here is copying your original dataframe, df, without the "dependent var" column. 您在这里所做的是复制原始数据帧df,而没有“ dependent var”列。 (it was, as suggested by the command, dropped from the dataframe) (根据命令的建议,它已从数据框中删除

Whatever columns are left at df are than being associated as headers to your new df: 保留在df的任何列都比作为新df的标题关联:

#creating a data frame with independent variables as headers
df_coef = pd.DataFrame(columns = x.columns)

Afterwise, you try to add a row to your new df, at index position 0, with a single value: 然后,您尝试在索引位置0的新df中添加一个具有单个值的行:

#adding a row with a coefficients
df.loc[0] = x

This is most likely where your error occurs, the df most likely has more than one column, yet, you try to create a row with a singular value and hence get the error message: 这很可能是您发生错误的地方,df最有可能具有多个列,但是,您尝试创建一个具有奇异值的行,从而得到错误消息:

ValueError: cannot set a row with mismatched columns

You might be using an IDE to code, if so, a good advice is to use a more friendly data visualization tool, such as anaconda/jupyter notebook. 您可能正在使用IDE进行编码,如果这样,一个好的建议是使用更友好的数据可视化工具,例如anaconda / jupyter笔记本。 With it you can visualize data step by step, which would help avoid the mistake done at the first step. 有了它,您可以逐步可视化数据,这将有助于避免第一步所犯的错误。

Hope that helps, 希望能有所帮助,

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM