Home > database >  Getting the MAE values of columns in pandas dataframe with last column
Getting the MAE values of columns in pandas dataframe with last column

Time:01-10

How to compute MAE for the columns in a pandas Dataframe with the last column:

,CPFNN,EN,Blupred,Horvath2,EPM,vMLP,Age
202,4.266596,3.5684403102704,5.2752761330328,5.17705043941232,3.30077613485548,3.412883,4.0
203,5.039452,5.1258136685894,4.40019825995985,5.03563327742846,3.97465334472661,4.140719,4.0
204,5.0227585,5.37207428128756,1.56392554883583,4.41805439337257,4.43779809822224,4.347523,4.0
205,4.796998,5.61052306552109,4.20912233479662,3.57075401779518,3.24902718889411,3.887743,4.0

I have a pandas dataframe and I want to create a list with the mae values of each column with "Age".

Is there a "pandas" way of doing this instead of just doing a for loop for each column?

from sklearn.metrics import mean_absolute_error as mae
mae(blood_bestpred_df["CPFNN"], blood_bestpred_df['Age'])

I'd like to do this:

mae(blood_bestpred_df[["CPFNN,EN,Blupred,Horvath2,EPM,vMLP"]], blood_bestpred_df['Age'])

But I have a dimension issue.

CodePudding user response:

Looks like sklearn's MAE requires both inputs to be the same shape and doesn't do any broadcasting (I'm not an sklearn expert, there might be another way around this). You can use raw pandas instead:

import pandas as pd

df = pd.read_clipboard(sep=",", index_col=0) # Your df here

out = df.drop(columns="Age").sub(df["Age"], axis=0).abs().mean()

out:

CPFNN       0.781451
EN          1.134993
Blupred     1.080168
Horvath2    0.764996
EPM         0.478335
vMLP        0.296904
dtype: float64
  • Related