Amal Nasir
Amal Nasir

Reputation: 164

Create a data frame table for regression results

I am trying to make a function that will calculate the R2 and RMSE for each regressor.

def apply_regressor_test(regressor, X_train, y_train, X_test, y_test, name):

r2= []
RMSE = []
reg_name = []

regressor.fit(X_train, y_train)
y_pred = regressor.predict(X_test)



cor = r2_score(y_test, y_pred)

r = np.sqrt(mean_squared_error(y_test, y_pred)

r2.append(cor)
RMSE.append(r)
reg_name.append(name)

df = pd.DataFrame({'R2': r2, 'RMSE': RMSE}, index = reg_name)

return df

I want to be able to get a table that will combine the results for each one of the regressors. I tried to create another data frame to append the results from the function. What I ended up getting is this.

regressor = LinearRegression() 
df = apply_regressor_test(regressor, X_train_full_ss, y_train_full, X_test_ss, y_test, name = str(regressor))
df_results.append(df)
df_results

# This is what I get: 
 Out[306]:
 [                          R2      RMSE
  LinearRegression()  0.643291  1.344363,
                            R2      RMSE
  RandomForestRegressor()  0.761544  1.099167,
             R2      RMSE
  SVR()  0.706637  1.219165]

How to make it in a table with only one column header?

Thanks

Upvotes: 1

Views: 562

Answers (1)

Amal Nasir
Amal Nasir

Reputation: 164

As @Raphael answer in the comment. I was able to achieve it by concat the returned dfs as,

pd.concat([df1, df2, df3], axis=0)

Upvotes: 1

Related Questions