-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path13_RandomForest.py
More file actions
70 lines (50 loc) · 1.78 KB
/
13_RandomForest.py
File metadata and controls
70 lines (50 loc) · 1.78 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
58
59
60
61
62
63
64
65
66
67
68
69
70
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import accuracy_score, precision_score, recall_score, f1_score
from sklearn.metrics import confusion_matrix, ConfusionMatrixDisplay, classification_report
from sklearn.impute import SimpleImputer
df = pd.read_csv("D:/GCET/Machine Learning/Datasets/framingham.csv")
X = df.iloc[:, :-1] # all columns except last
y = df.iloc[:, -1] # target column
imputer = SimpleImputer(strategy='mean')
X = imputer.fit_transform(X)
scaler = StandardScaler()
X_scaled = scaler.fit_transform(X)
# 5. Split dataset into training and testing
X_train, X_test, y_train, y_test = train_test_split(
X_scaled, y, test_size=0.3, random_state=42
)
rf = RandomForestClassifier(
n_estimators=100, # number of trees
max_depth=5, # tree depth
random_state=42
)
rf.fit(X_train, y_train)
# Predict
y_pred = rf.predict(X_test)
#Importances
importances = rf.feature_importances_
print(importances)
#Evaluation Metrics
accuracy = accuracy_score(y_test, y_pred)
precision = precision_score(y_test, y_pred, average='weighted')
recall = recall_score(y_test, y_pred, average='weighted')
f1 = f1_score(y_test, y_pred, average='weighted')
print("\nModel Performance")
print("Accuracy:", accuracy)
print("Precision:", precision)
print("Recall:", recall)
print("F1 Score:", f1)
# 10. Classification report
print("\nClassification Report:")
print(classification_report(y_test, y_pred))
# 11. Confusion Matrix
cm = confusion_matrix(y_test, y_pred)
disp = ConfusionMatrixDisplay(confusion_matrix=cm)
disp.plot()
plt.title("Confusion Matrix - Random Forest")
plt.show()