keras_nn/main.py
2022-06-05 23:06:29 +02:00

36 lines
973 B
Python

from keras.models import Sequential
from keras.layers import Dense
import numpy as np
from sklearn.metrics import f1_score
if __name__ == '__main__':
print('Hello')
dataset = np.loadtxt('data.csv', delimiter=',')
np.random.shuffle(dataset)
X = dataset[:,0:8]
y = dataset[:,8]
X_train = X[0:650]
y_train = y[0:650]
X_test = X[651:]
y_test = y[651:]
model = Sequential()
model.add(Dense(12, input_dim=8, activation='relu'))
model.add(Dense(8, activation='relu'))
model.add(Dense(1, activation='sigmoid'))
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
model.fit(X_train, y_train, epochs=15, batch_size=10)
_, accuracy = model.evaluate(X_test, y_test)
print('Accuracy: %.2f' % (accuracy*100))
predictions = (model.predict(X_test) > 0.5).astype(int)
fscore = f1_score(y_test, predictions, average='macro')
print('fscore: ', fscore)