from sklearn.datasets import load_boston
from sklearn.ensemble import RandomForestRegressor
from sklearn.model_selection import train_test_split
import numpy as np
#Load boston housing dataset as an example
boston = load_boston()
x = boston.data
y = boston.target
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.25, random_state=33)

names = boston["feature_names"]
print(names)   # ['CRIM' 'ZN' 'INDUS' 'CHAS' 'NOX' 'RM' 'AGE' 'DIS' 'RAD' 'TAX' 'PTRATIO','B' 'LSTAT']
rf = RandomForestRegressor(random_state=48, n_estimators=10, min_samples_split=0.02)
rf.fit(x_train, y_train)
importamce = rf.feature_importances_ / np.max(rf.feature_importances_)
print("Features sorted by their score:")
print(sorted(zip(map(lambda x: round(x, 4), importamce), names), reverse=True))


indices = np.argsort(importamce)[::-1]
for f in range(x_train.shape[1]):
    print("%2d) %-*s %f" % (f + 1, 30, names[indices[f]], importamce[indices[f]]))

# Features sorted by their score:
# [(1.0, 'LSTAT'),
# (0.5397, 'RM'),
# (0.0978, 'DIS'),
# (0.0536, 'CRIM'),
# (0.0412, 'PTRATIO'),
# (0.022, 'B'),
# (0.0204, 'TAX'),
# (0.0194, 'NOX'),
# (0.0117, 'INDUS'),
# (0.0113, 'AGE'),
# (0.0014, 'RAD'),
# (0.0011, 'ZN'),
# (0.0011, 'CHAS')]