from sklearn.datasets import load_boston
from sklearn.ensemble import RandomForestRegressor
from sklearn.model_selection import train_test_split
import numpy as np
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)
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]]))