-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathrecognizer_simple_nn.py
More file actions
46 lines (32 loc) · 1.13 KB
/
Copy pathrecognizer_simple_nn.py
File metadata and controls
46 lines (32 loc) · 1.13 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
import numpy as np
import pandas as pd
import tensorflow as tf
from tensorflow.keras import layers, models
(train_images, train_labels), (test_images, test_labels) = tf.keras.datasets.mnist.load_data()
train = pd.read_csv('train.csv')
test = pd.read_csv('test.csv')
y_df = train[['label']]
x_df = train.drop(['label'], axis=1)
y = y_df.to_numpy().flatten()
X = x_df.to_numpy()
X = X.reshape(X.shape[0], 28, 28)
model = models.Sequential([
layers.Flatten(input_shape=(28, 28)),
layers.Dense(128, activation='relu'),
layers.Dense(10, activation='softmax')
])
model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])
model.fit(X, y, epochs=10)
test_loss, test_acc = model.evaluate(test_images, test_labels)
print(f'Test accuracy: {test_acc}')
model.save('digit_recognition_model.keras')
test = test.to_numpy()
test = test.reshape(test.shape[0], 28, 28)
predictions = model.predict(test)
predicted_labels = np.argmax(predictions, axis=1)
data = {
'ImageId': range(1, len(predicted_labels) + 1),
'Label': predicted_labels
}
result = pd.DataFrame(data)
result.to_csv('submission.csv', index=False)