-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrf_dt.py
51 lines (40 loc) · 2.05 KB
/
rf_dt.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
import pandas as pd
import numpy as np
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.tree import DecisionTreeRegressor
from sklearn.ensemble import RandomForestRegressor
from sklearn.metrics import mean_squared_error
from sklearn.metrics import mean_absolute_error
from math import sqrt
###################################################
loc = "drop_out_002.csv"
dataset = pd.read_csv(loc)
Y = dataset[["career_len"]]
X = dataset[["total_duration","profit","average ride time","speed","average dist per ride","speed","10_to_6","9_to_5",
"prime_time",'weekend_per']]
X_train, X_test, y_train, y_test = train_test_split(X,Y,test_size=0.2, random_state=123)
# print (X_train.shape)
# print (X_test.shape)
# print (Y_train.shape)
# print (Y_test.shape)
scaler = StandardScaler()
train_scaled = scaler.fit_transform(X_train)
test_scaled = scaler.transform(X_test)
tree_model = DecisionTreeRegressor()
rf_model = RandomForestRegressor()
tree_model.fit(train_scaled, y_train)
rf_model.fit(train_scaled, y_train)
# tree_mse = mean_squared_error(y_train, tree_model.predict(train_scaled))
# tree_mae = mean_absolute_error(y_train, tree_model.predict(train_scaled))
# rf_mse = mean_squared_error(y_train, rf_model.predict(train_scaled))
# rf_mae = mean_absolute_error(y_train, rf_model.predict(train_scaled))
#
# print("Decision Tree training mse = ",tree_mse," & mae = ",tree_mae," & rmse = ", sqrt(tree_mse))
# print("Random Forest training mse = ",rf_mse," & mae = ",rf_mae," & rmse = ", sqrt(rf_mse))
tree_test_mse = mean_squared_error(y_test, tree_model.predict(test_scaled))
tree_test_mae = mean_absolute_error(y_test, tree_model.predict(test_scaled))
rf_test_mse = mean_squared_error(y_test, rf_model.predict(test_scaled))
rf_test_mae = mean_absolute_error(y_test, rf_model.predict(test_scaled))
print("Decision Tree test mse = ",tree_test_mse," & mae = ",tree_test_mae," & rmse = ", sqrt(tree_test_mse))
print("Random Forest test mse = ",rf_test_mse," & mae = ",rf_test_mae," & rmse = ", sqrt(rf_test_mse))