-
Notifications
You must be signed in to change notification settings - Fork 2
/
random_forest_regression_basic.py
80 lines (48 loc) · 2.6 KB
/
random_forest_regression_basic.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
##############################################################################
# IMPORT REQUIRED PACKAGES
##############################################################################
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.ensemble import RandomForestRegressor
from sklearn.model_selection import train_test_split
from sklearn.metrics import r2_score
##############################################################################
# IMPORT SAMPLE DATA
##############################################################################
my_df = pd.read_csv("sample_data_regression.csv")
##############################################################################
# SPLIT INPUT VARIABLES & OUTPUT VARIABLES
##############################################################################
X = my_df.drop(["output"], axis = 1)
y = my_df["output"]
##############################################################################
# SPLIT OUT TRAINING & TEST SETS
##############################################################################
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.2, random_state = 42)
##############################################################################
# INSTANTIATE OUR MODEL
##############################################################################
regressor = RandomForestRegressor(random_state = 42, n_estimators = 1000)
##############################################################################
# MODEL TRAINING
##############################################################################
regressor.fit(X_train, y_train)
##############################################################################
# MODEL ASSESSMENT
##############################################################################
y_pred = regressor.predict(X_test)
r2_score(y_test, y_pred)
##############################################################################
# FEATURE IMPORTANCE
##############################################################################
regressor.feature_importances_
feature_importance = pd.DataFrame(regressor.feature_importances_)
feature_names = pd.DataFrame(X.columns)
feature_importance_summary = pd.concat([feature_names, feature_importance], axis = 1)
feature_importance_summary.columns = ["input_variable", "feature_importance"]
feature_importance_summary.sort_values(by = "feature_importance", inplace = True)
plt.barh(feature_importance_summary["input_variable"], feature_importance_summary["feature_importance"])
plt.title("Feature Importance of Random Forest")
plt.xlabel("Feature Importance")
plt.tight_layout()
plt.show()