运行方差分析并给出 F 统计值作为输出的函数 [英] Function to run ANOVA and give F stat values as the output
问题描述
我尝试编写的函数将获取提供的数据帧并计算 F 统计值并将其作为输出提供.
The function im trying to write would take the dataframe provided and calculate the F statistic values and provide those as the output.
数据格式最终
Color Strength Fabric Sales
0 1 1 10
1 2 2 15
这里颜色、强度和面料是独立的,而销售额是相关的.
Here Color, strength and Fabric are independent while Sales is dependent.
def regression():
X=Final.copy()
y=Final[['Sales']].copy()
X_train, X_test, y_train, y_test = train_test_split(X,y,test_size=.2, random_state=0)
sel=f_classif(X_train, y_train)
p_values=pd.Series(sel[0], index=X_train.columns)
p_values=p_values.reset_index()
pd.options.display.float_format = "{:,.2f}".format
return p_values
Final.apply(regression)
这是我想出的代码,但它抛出了一个错误
This is the code I came up with but its throwing an error
TypeError: regression() takes 0 positional arguments but 1 was given
这段代码可能有什么问题?
What could be going wrong with this code?
推荐答案
当您使用 .apply()
时,数据框或系列将作为参数传递给您调用的函数.文档 对此进行了更多解释.为了解决这个问题,而不是:
When you use .apply()
, the dataframe or series is passed as an argument to the function you call. The documentation explains it more. In order to fix this, instead of:
Final.apply(regression)
你可以简单地调用 regression()
像这样:
You can simply call regression()
like this:
m_p_values = regression()
现在变量 m_p_values
包含了 regression()
的返回值.
And now the variable m_p_values
contains the return value of regression()
.
这篇关于运行方差分析并给出 F 统计值作为输出的函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!