首页 > 解决方案 > Function to run ANOVA and give F stat values as the output

问题描述

The function im trying to write would take the dataframe provided and calculate the F statistic values and provide those as the output.

Data Format Final

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?

标签: pythonscikit-learnregression

解决方案


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)

You can simply call regression() like this:

m_p_values = regression()

And now the variable m_p_values contains the return value of regression().


推荐阅读