-
Notifications
You must be signed in to change notification settings - Fork 0
/
ml_test.py
84 lines (63 loc) · 2.13 KB
/
ml_test.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
from xml.etree.ElementInclude import include
from matplotlib.pyplot import cla
from sklearn.ensemble import RandomForestClassifier
from sklearn.datasets import make_classification
from sklearn.model_selection import train_test_split
import pandas as pd
from sklearn import metrics
from sklearn.preprocessing import OneHotEncoder
from sklearn import preprocessing
from sklearn.metrics import ConfusionMatrixDisplay, confusion_matrix
import numpy as np
import matplotlib.pyplot as plt
# Load data
df_1 = pd.read_csv('KN_features.csv')
df_2 = pd.read_csv('MDF_features.csv')
# Make the number of detections for the two classes equal
df_2 = df_2[:len(df_1)]
# Merge the dataframs
df = pd.concat([df_1,df_2])
# Make the matrices
x = df[['BAND','PRE-BAND','POST-BAND']]
y = df[['CLASS']]
# One hot encoding for passband features
enc = OneHotEncoder(sparse=False)
x = enc.fit_transform(x)
# Adding the time to prev and next det as features
x_new = np.zeros((len(df), 23))
x_new[:, :20] = x
x_new[:, 20] = df['TIME-TO-PREV']
x_new[:, 21] = df['TIME-TO-NEXT']
x_new[:, 22] = df['MDF_DENSITY']
# x_new[:, 23] = df['NEXT-PHOT-FLAG']
# x_new[:, 24] = df['NUM_DETECTIONS']
features = list(enc.get_feature_names_out())
features.append('TIME-TO-PREV')
features.append('TIME-TO-NEXT')
features.append('MDF_DENSITY')
# features.append('NEXT-PHOT-FLAG')
# features.append('NUM_DETECTIONS')
x_new = pd.DataFrame(x_new, columns=features)
x = x_new
print(x_new)
# Creating binary class column
y = []
for c in df['CLASS']:
if c == 'KN':
y.append(0)
else:
y.append(1)
# Splitting data
X_train, X_test, y_train, y_test = train_test_split(x, y, test_size=0.3, random_state=42)
# Classifier
clf=RandomForestClassifier(n_estimators=1000, random_state=42)
clf.fit(X_train,y_train)
y_pred=clf.predict(X_test)
print("Accuracy:",metrics.accuracy_score(y_test, y_pred))
cm = confusion_matrix(y_test, y_pred, labels=clf.classes_)
importance = clf.feature_importances_
disp = ConfusionMatrixDisplay(confusion_matrix=cm, display_labels=['Kilo Nova', 'M Dwarf flare'])
disp.plot()
plt.show()
for i in range(len(importance)):
print(f'{features[i]}: {importance[i]}')