-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample models.py
More file actions
149 lines (102 loc) · 4.77 KB
/
Copy pathexample models.py
File metadata and controls
149 lines (102 loc) · 4.77 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from sklearn.preprocessing import StandardScaler
from imblearn.over_sampling import RandomOverSampler
import tensorflow as tf
#neural network -> könnt es einfach mal durchjagen und schauen ob es ein gute ergebnis gibt,
def plot_history(history):
fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(10, 4))
ax1.plot(history.history['loss'], label='loss')
ax1.plot(history.history['val_loss'], label='val_loss')
ax1.set_xlabel('Epoch')
ax1.set_ylabel('Binary crossentropy')
ax1.grid(True)
ax2.plot(history.history['accuracy'], label='accuracy')
ax2.plot(history.history['val_accuracy'], label='val_accuracy')
ax2.set_xlabel('Epoch')
ax2.set_ylabel('Accuracy')
ax2.grid(True)
plt.show()
X_train = 0 #TODO
y_train = 0 #TODO
X_valid = 0 #TODO
y_valid = 0 #TODO
# lr = learning rate
# epochs = number of iterations
# batch_size = number of samples per gradient update
# num_nodes = number of nodes in each hidden layer
# dropout_prob = dont train a certain percentage of nodes in each hidden layer to prevent overfitting
#activation function = relu, sigmoid, tanh, softmax -> relu is most common and I thought it would fit best but try other ones, if solution isnt good
def train_model(X_train, y_train, num_nodes, dropout_prob, lr, batch_size):
nn_model = tf.keras.Sequential([
tf.keras.layers.Dense(num_nodes, activation='relu', input_shape=(10,)),
tf.keras.layers.Dropout(dropout_prob),
tf.keras.layers.Dense(num_nodes, activation='relu'),
tf.keras.layers.Dropout(dropout_prob),
tf.keras.layers.Dense(1, activation='sigmoid')
])
nn_model.compile(optimizer=tf.keras.optimizers.Adam(lr), loss='binary_crossentropy',
metrics=['accuracy'])
history = nn_model.fit(
X_train, y_train, epochs=100, batch_size=batch_size, validation_split=0.2, verbose=0
)
return nn_model, history
# TODO: find best hyperparameters
# Ich habe hier einfach mal ein paar hyperparameter kombinationen ausprobiert, aber ihr könnt auch gerne andere ausprobieren
# am ende nur den besten in der methode train_model einsetzen
least_val_loss = float('inf')
least_loss_model = None
epochs=100
for num_nodes in [16, 32, 64]:
for dropout_prob in[0, 0.2]:
for lr in [0.01, 0.005, 0.001]:
for batch_size in [32, 64, 128]:
print(f"{num_nodes} nodes, dropout {dropout_prob}, lr {lr}, batch size {batch_size}")
model, history = train_model(X_train, y_train, num_nodes, dropout_prob, lr, batch_size, epochs)
plot_history(history)
val_loss = model.evaluate(X_valid, y_valid)[0]
if val_loss < least_val_loss:
least_val_loss = val_loss
least_loss_model = model
# using ada boost
from sklearn.ensemble import AdaBoostClassifier
from sklearn.tree import DecisionTreeClassifier
from sklearn.datasets import make_classification
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score, classification_report
# test data, replace with our data
X, y = make_classification(n_samples=1000, n_features=20, n_informative=10, n_classes=2, random_state=42)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
base_classifier = DecisionTreeClassifier(max_depth=1)
adaboost_classifier = AdaBoostClassifier(base_classifier, n_estimators=50, random_state=42)
adaboost_classifier.fit(X_train, y_train)
y_pred = adaboost_classifier.predict(X_test)
accuracy = accuracy_score(y_test, y_pred)
print(f'Accuracy: {accuracy}')
# using svm
from sklearn.svm import SVC
svm_model = SVC()
svm_model = svm_model.fit(X_train, y_train)
y_pred = svm_model.predict(X_test)
print(classification_report(y_test, y_pred))
#using knn
from sklearn.neighbors import KNeighborsClassifier
from sklearn.metrics import classification_report
knn_model = KNeighborsClassifier(n_neighbors=5)
knn_model.fit(X_train, y_train)
y_pred = knn_model.predict(X_test)
print(classification_report(y_test, y_pred))
# using random forest
from sklearn.ensemble import RandomForestClassifier
from sklearn.datasets import make_classification
from sklearn.metrics import accuracy_score
# random data, replace with our data
X, y = make_classification(n_samples=1000, n_features=20, n_informative=10, n_classes=2, random_state=42)
# Split the dataset into training and testing sets
train, valid, test = np.split(df.sample(frac=1), [int(0.6*len(df)), int(0.8*len(df))])
random_forest_classifier = RandomForestClassifier(n_estimators=100, random_state=42)
random_forest_classifier.fit(X_train, y_train)
y_pred = random_forest_classifier.predict(X_test)
accuracy = accuracy_score(y_test, y_pred)
print(f'Accuracy: {accuracy}')