image_output_1.png
from sklearn.ensemble import RandomForestRegressor

from sklearn.preprocessing import OrdinalEncoder

import pandas as pd

train = pd.read_csv('../input/30-days-of-ml/train.csv', index_col=0)
test = pd.read_csv('../input/30-days-of-ml/test.csv', index_col=0)

y = train['target']
features = train.drop(['target'], axis=1)

object_cols = [col for col in features.columns if 'cat' in col]
X = features.copy()
X_test = test.copy()
ordinal_encoder = OrdinalEncoder()
X[object_cols] = ordinal_encoder.fit_transform(X[object_cols])
X_test[object_cols] = ordinal_encoder.transform(X_test[object_cols])

X.isna().sum()

model = RandomForestRegressor(random_state=1)

model.fit(X_train, y_train)
preds_valid = model.predict(X_valid)

trees = model.get_booster().get_dump()

importance_df = pd.DataFrame({'feature': X.columns, 'importance': model.feature_importances_}).sort_values('importance', ascending=False)
importance_df.head()