Random Forest Classification with Feature Importance Display

  • Share this:

Code introduction


This function uses a random forest classifier to classify data and uses the eli5 library to display feature importance.


Technology Stack : numpy, pandas, eli5, sklearn

Code Type : Machine learning classification

Code Difficulty : Intermediate


                
                    
import numpy as np
import pandas as pd
from eli5 importeli5
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import train_test_split

def classify_data(X, y):
    # 分割数据集
    X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
    
    # 创建随机森林分类器
    model = RandomForestClassifier(n_estimators=100, random_state=42)
    model.fit(X_train, y_train)
    
    # 使用eli5的eli5.show_weights方法来显示特征重要性
   eli5.show_weights(model)