<jupyter_start><jupyter_text>1. LOFO binary classification ref: https://github.com/aerdem4/lofo-importance<jupyter_code>import pandas as pd
from sklearn.ensemble import RandomForestClassifier
from sklearn.datasets import load_breast_cancer, load_iris
from sklearn.model_selection import KFold
from lofo import LOFOImportance, Dataset, plot_importance
#data

data = load_breast_cancer(as_frame=True)# load as dataframe
df = data.data
df['target']=data.target.values
print(df.shape)# (569, 31)
df.head()
df['target']
# model
model = RandomForestClassifier()
# dataset
##dataset = Dataset(df=df, target="target", features=['sepal length (cm)','sepal width (cm)','petal length (cm)','petal length (cm)'])
dataset = Dataset(df=df, target="target", features=[col for col in df.columns if col != 'target'])

# get feature importance

cv = KFold(n_splits=5, shuffle=True, random_state=666)
lofo_imp = LOFOImportance(dataset, cv=cv, scoring="f1",model=model)# f1_macro
importance_df = lofo_imp.get_importance()
importance_df
%matplotlib inline


p = plot_importance(importance_df, figsize=(12, 20))<jupyter_output><empty_output><jupyter_text>2. LOFO multiclass classification<jupyter_code>import pandas as pd
from sklearn.ensemble import RandomForestClassifier
from sklearn.datasets import load_breast_cancer, load_iris
from sklearn.model_selection import KFold
from lofo import LOFOImportance, Dataset, plot_importance

data = load_iris(as_frame=True)# load as dataframe
df = data.data
df['target']=data.target.values

# model
model = RandomForestClassifier()
# dataset
dataset = Dataset(df=df, target="target", features=[col for col in df.columns if col != 'target'])
# get feature importance
cv = KFold(n_splits=5, shuffle=True, random_state=666)
lofo_imp = LOFOImportance(dataset, cv=cv, scoring="f1_macro",model=model)
importance_df = lofo_imp.get_importance()
print(importance_df)<jupyter_output><empty_output><jupyter_text>3. Fast LOFO<jupyter_code>from lofo import FLOFOImportance
import pandas as pd
from sklearn.ensemble import RandomForestClassifier
from sklearn.datasets import load_breast_cancer, load_iris
from sklearn.model_selection import KFold
from lofo import LOFOImportance, Dataset, plot_importance
# step-01: prepare data
data = load_iris(as_frame=True)# load as dataframe
x_data = data.data.to_numpy()
y_data = data.target.values
df = data.data
df['target']=data.target.values
# repeat more data since FLOFO need > 1000 data
df=pd.DataFrame(pd.np.repeat(df.values,10,axis=0),columns=df.columns)
# step-02: train model
model = RandomForestClassifier()
model.fit(x_data,y_data)
# step-03: fast-lofo
lofo_imp = FLOFOImportance(validation_df=df, target="target", features=[col for col in df.columns if col != 'target'],scoring="f1_macro",trained_model=model)
importance_df = lofo_imp.get_importance()
print(importance_df)<jupyter_output>/home/mtrt_service/anaconda3/envs/env_ml6_py37/lib/python3.7/site-packages/ipykernel_launcher.py:14: FutureWarning: The pandas.np module is deprecated and will be removed from pandas in a future version. Import numpy directly instead
{'fraction_non_alphanumeric': 0.10876623376623376, 'fraction_numerical': 0.014935064935064935, 'mean_word_length': 7.7280453257790365, 'pattern_counts': {'":': 0, '<': 12, '<?xml version=': 0, '>': 13, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '636090', 'license': 'MIT', 'n_tokens_mistral': 1094, 'n_tokens_neox': 1065, 'n_words': 246, 'path': 'lofo/lofo_feature_importance.ipynb', 'repo_name': 'ybdesire/machinelearning', 'size': '3080'}