0

How to create dataframe that shows permutation importance features values? I tried to code PFI for each classes = ['Car', 'Train', 'Tram', 'Walk', 'Bicycle', 'Motorcycle', 'Bus', 'Taxi'] but it didn't went well.

Here are the code:

classes = ['Car', 'Train', 'Tram', 'Walk', 'Bicycle', 'Motorcycle', 'Bus', 'Taxi']

def pfi_data():
    
    # Create a list to store the dataframes for each class
    dfs_pfi = []
    
    # Loop through the classes
    for i in range(len(classes)):
        y_class = (y_valid == classes[i]).astype(int)
        
        result = permutation_importance(model, X_valid, y_class, n_repeats=10, random_state=0)
        importance_scores = result.importances_mean
        
        # Create a DataFrame to store the results
        importance_df = pd.DataFrame({"Feature": X.columns, "Importance": importance_scores})
        importance_df['Rank'] = importance_df['Importance'].rank(ascending=False)
        
        # Sort the DataFrame by importance
        importance_df.sort_values("Importance", ascending=False, inplace=True)
        
        # Add the class name as a column in the DataFrame ##with class name
        importance_df["Class"] = classes[i]

        # Append the dataframe to the list
        dfs_pfi.append(importance_df)
        
    # Concatenate the dataframes for each class into a single dataframe
    result_pfi_yval = pd.concat(dfs_pfi, axis=1)
                
    return result_pfi_yval

But it gives me the result of 0, like this. Whats happening?

Timus
  • 10,974
  • 5
  • 14
  • 28
  • Please [do not post images of code, data, error messages, etc.](https://stackoverflow.com/help/how-to-ask) Add a [MRE](https://stackoverflow.com/help/minimal-reproducible-example) (also look [here](https://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples)) instead. – Timus Feb 10 '23 at 08:48

0 Answers0