-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_random_forest.py
More file actions
35 lines (26 loc) · 1007 Bytes
/
test_random_forest.py
File metadata and controls
35 lines (26 loc) · 1007 Bytes
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
import os
import pickle
import pandas as pd
from sklearn.metrics import mean_absolute_error, r2_score, mean_squared_error, root_mean_squared_error
from sklearn.model_selection import train_test_split
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn.tree import plot_tree
from train_random_forest import load_data, build_features, load_pipeline
def main():
pipeline = load_pipeline()
X, y = load_data()
Xf, yf = build_features(X, y)
if len(Xf) == 0:
print('No valid rows after feature construction. Cannot evaluate.')
return
_, X_test, _, y_test = train_test_split(Xf, yf, test_size=0.2, random_state=42)
preds = pipeline.predict(X_test)
mae = mean_absolute_error(y_test, preds)
rmse = root_mean_squared_error(y_test, preds)
r2 = r2_score(y_test, preds)
print(f"Test MAE: {mae:.2f}")
print(f"Test RMSE: {rmse:.2f}")
print(f"Test R2: {r2:.3f}")
if __name__ == '__main__':
main()