In [18]:
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
from sklearn import preprocessing
from sklearn.model_selection import train_test_split
In [19]:
bank_note_data = pd.read_csv("https://raw.githubusercontent.com/dphi-official/Datasets/master/bank_note_data/training_set_label.csv" )
test_data = pd.read_csv('https://raw.githubusercontent.com/dphi-official/Datasets/master/bank_note_data/testing_set_label.csv')
In [20]:
bank_note_data.head()
Out[20]:
VWTI | SWTI | CWTI | EI | Class | |
---|---|---|---|---|---|
0 | 2.2634 | -4.4862 | 3.6558 | -0.612510 | 0 |
1 | 3.2718 | 1.7837 | 2.1161 | 0.613340 | 0 |
2 | -3.9411 | -12.8792 | 13.0597 | -3.312500 | 1 |
3 | 0.5195 | -3.2633 | 3.0895 | -0.984900 | 0 |
4 | 2.5698 | -4.4076 | 5.9856 | 0.078002 | 0 |
In [21]:
test_data.head()
Out[21]:
VWTI | SWTI | CWTI | EI | |
---|---|---|---|---|
0 | -0.40804 | 0.54214 | -0.52725 | 0.65860 |
1 | -3.71810 | -8.50890 | 12.36300 | -0.95518 |
2 | 5.50400 | 10.36710 | -4.41300 | -4.02110 |
3 | 1.68490 | 8.74890 | -1.26410 | -1.38580 |
4 | 4.74320 | 2.10860 | 0.13680 | 1.65430 |
In [22]:
print(bank_note_data.shape)
print(test_data.shape)
(1096, 5) (275, 4)
In [23]:
bank_note_data.info()
<class 'pandas.core.frame.DataFrame'> RangeIndex: 1096 entries, 0 to 1095 Data columns (total 5 columns): # Column Non-Null Count Dtype --- ------ -------------- ----- 0 VWTI 1096 non-null float64 1 SWTI 1096 non-null float64 2 CWTI 1096 non-null float64 3 EI 1096 non-null float64 4 Class 1096 non-null int64 dtypes: float64(4), int64(1) memory usage: 42.9 KB
In [24]:
test_data.info()
<class 'pandas.core.frame.DataFrame'> RangeIndex: 275 entries, 0 to 274 Data columns (total 4 columns): # Column Non-Null Count Dtype --- ------ -------------- ----- 0 VWTI 275 non-null float64 1 SWTI 275 non-null float64 2 CWTI 275 non-null float64 3 EI 275 non-null float64 dtypes: float64(4) memory usage: 8.7 KB
In [25]:
X=bank_note_data.drop('Class', axis=1)
y=bank_note_data.Class
In [26]:
scaler = preprocessing.StandardScaler()
scaled_X = scaler.fit_transform(X)
X = pd.DataFrame(scaled_X, columns =['VWTI', 'SWTI', 'CWTI', 'EI'])
In [27]:
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)
Building a model
In [46]:
model = tf.keras.Sequential([
tf.keras.layers.Dense(100, input_shape=(4,), activation='relu'),
tf.keras.layers.Dense(50, activation='relu'),
tf.keras.layers.Dense(10, activation='relu'),
tf.keras.layers.Dense(1, activation='sigmoid')
])
model.summary()
Model: "sequential_6" _________________________________________________________________ Layer (type) Output Shape Param # ================================================================= dense_16 (Dense) (None, 100) 500 _________________________________________________________________ dense_17 (Dense) (None, 50) 5050 _________________________________________________________________ dense_18 (Dense) (None, 10) 510 _________________________________________________________________ dense_19 (Dense) (None, 1) 11 ================================================================= Total params: 6,071 Trainable params: 6,071 Non-trainable params: 0 _________________________________________________________________
In [47]:
# Compiling the model
model.compile(optimizer='adam',
loss='binary_crossentropy',
metrics=['accuracy'])
# Fit the model
model.fit(X_train, y_train, batch_size=32, epochs=20)
Epoch 1/20 24/24 [==============================] - 0s 1ms/step - loss: 0.5870 - accuracy: 0.8110 Epoch 2/20 24/24 [==============================] - 0s 1ms/step - loss: 0.3715 - accuracy: 0.9465 Epoch 3/20 24/24 [==============================] - 0s 2ms/step - loss: 0.1762 - accuracy: 0.9752 Epoch 4/20 24/24 [==============================] - 0s 2ms/step - loss: 0.0802 - accuracy: 0.9831 Epoch 5/20 24/24 [==============================] - 0s 880us/step - loss: 0.0440 - accuracy: 0.9883 Epoch 6/20 24/24 [==============================] - 0s 979us/step - loss: 0.0304 - accuracy: 0.9909 Epoch 7/20 24/24 [==============================] - 0s 1ms/step - loss: 0.0198 - accuracy: 0.9948 Epoch 8/20 24/24 [==============================] - 0s 1ms/step - loss: 0.0148 - accuracy: 0.9987 Epoch 9/20 24/24 [==============================] - 0s 1ms/step - loss: 0.0109 - accuracy: 1.0000 Epoch 10/20 24/24 [==============================] - 0s 1ms/step - loss: 0.0084 - accuracy: 1.0000 Epoch 11/20 24/24 [==============================] - 0s 1ms/step - loss: 0.0065 - accuracy: 1.0000 Epoch 12/20 24/24 [==============================] - 0s 1ms/step - loss: 0.0052 - accuracy: 1.0000 Epoch 13/20 24/24 [==============================] - 0s 1ms/step - loss: 0.0042 - accuracy: 1.0000 Epoch 14/20 24/24 [==============================] - ETA: 0s - loss: 0.0047 - accuracy: 1.00 - 0s 973us/step - loss: 0.0035 - accuracy: 1.0000 Epoch 15/20 24/24 [==============================] - ETA: 0s - loss: 0.0010 - accuracy: 1.00 - 0s 922us/step - loss: 0.0029 - accuracy: 1.0000 Epoch 16/20 24/24 [==============================] - 0s 933us/step - loss: 0.0025 - accuracy: 1.0000 Epoch 17/20 24/24 [==============================] - ETA: 0s - loss: 0.0033 - accuracy: 1.00 - 0s 995us/step - loss: 0.0021 - accuracy: 1.0000 Epoch 18/20 24/24 [==============================] - ETA: 0s - loss: 0.0016 - accuracy: 1.00 - 0s 1ms/step - loss: 0.0018 - accuracy: 1.0000 Epoch 19/20 24/24 [==============================] - ETA: 0s - loss: 0.0010 - accuracy: 1.00 - 0s 1ms/step - loss: 0.0016 - accuracy: 1.0000 Epoch 20/20 24/24 [==============================] - 0s 982us/step - loss: 0.0014 - accuracy: 1.0000
Out[47]:
<tensorflow.python.keras.callbacks.History at 0x237b03e52b0>
In [48]:
# Evaluate the model
model.evaluate(X_test,y_test)
11/11 [==============================] - 0s 1ms/step - loss: 0.0025 - accuracy: 1.0000
Out[48]:
[0.0024841674603521824, 1.0]
In [41]:
scaled_test = scaler.fit_transform(test_data)
test_data = pd.DataFrame(scaled_test, columns =['VWTI', 'SWTI', 'CWTI', 'EI'])
In [49]:
y_pred = model.predict(test_data)
y_pred = np.round(pd.DataFrame(y_pred,columns = ['prediction']))
In [50]:
y_pred.to_csv('submission.csv',index = False)
In [ ]: