-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrandom_forest.py
More file actions
58 lines (46 loc) · 1.6 KB
/
Copy pathrandom_forest.py
File metadata and controls
58 lines (46 loc) · 1.6 KB
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
#random_forest.py
from sklearn.ensemble import RandomForestRegressor
import pandas as pd
from sklearn.metrics import mean_absolute_error, r2_score
def rfr(X_train, y_train, X_test, y_test, features, stuff=''):
#call the rfr model from sklearn
model = RandomForestRegressor(
n_estimators=200,
max_depth=None,
random_state=42,
n_jobs=-1
)
#fit model
model.fit(X_train, y_train)
#predictions
y_pred = model.predict(X_test)
#Mean Absolute Error, difference between predicted and actual values
mae = mean_absolute_error(y_test, y_pred)
#R² score, predictions fit?
r2 = r2_score(y_test, y_pred)
print(f"Mean Absolute Error (MAE): {mae:.3f}")
print(f"R² Score: {r2:.3f}")
#save results
r2_mae_results = pd.DataFrame({
"Model": ["First Random Forest"],
"Mean Absolute Error (MAE)": [mae],
"R² Score/Mean R²": [r2]
})
#if stuff not null
suffix = f"_{stuff}" if stuff else ""
r2_mae_results.to_csv(f"data/r2_mae_results{suffix}.csv", index=False)
#save results
results = pd.DataFrame({
"Actual": y_test,
"Predicted": y_pred
})
results.to_csv(f"data/model_predictions{suffix}.csv", index=False)
#which features affected predictions most
importances = pd.DataFrame({
"Feature": features,
"Importance": model.feature_importances_
}).sort_values(by="Importance", ascending=False)
#save importantces to csv
importances.to_csv(f"data/feature_importance{suffix}.csv", index=False)
#print("\nFeature Importance:")
#print(importances)