-
Notifications
You must be signed in to change notification settings - Fork 0
/
RF.py
47 lines (36 loc) · 1.25 KB
/
RF.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
import pandas as pd
import numpy as np
from sklearn.preprocessing import MinMaxScaler
from sklearn.metrics import ConfusionMatrixDisplay, accuracy_score
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import train_test_split
import matplotlib.pyplot as plt
import seaborn as sns
seed = 42
df = pd.read_csv("Particle_Data.csv")
df = df.sample(frac=0.5, random_state=seed)
Y = df.iloc[:,3]
X = df.iloc[:,0:3]
# Normalize features within range 0 to 1
sc = MinMaxScaler(feature_range=(0,1))
X = sc.fit_transform(X)
X = pd.DataFrame(X)
# Convert df to array values
X = X.values
Y = Y.values
# Train Test Split
x_train, x_test, y_train, y_test = train_test_split(X, Y, test_size=0.30, random_state=seed, shuffle=False)
clf = RandomForestClassifier(n_estimators=100, verbose=1)
clf.fit(x_train,y_train)
y_pred = clf.predict(x_test)
acc = accuracy_score(y_test, y_pred)
print("Results on Test Data", acc)
ConfusionMatrixDisplay.from_predictions(y_test, y_pred)
plt.title("Confusion Matrix")
plt.savefig("CM_RF.png")
# Predicted Outputs
dic = {"P" : x_test.transpose()[0], "TPC" : x_test.transpose()[1], "TOF": x_test.transpose()[2], "PID" : y_pred}
df = pd.DataFrame(data=dic)
sns.pairplot(df, hue='PID')
plt.savefig("pp_rf.png")
plt.show()