NOTE: This Use Case is not purposed for resource constrained devices.
In [1]:
# get data file
!wget -N "https://cainvas-static.s3.amazonaws.com/media/user_data/cainvas-admin/musk.csv"
In [2]:
# Import the required libraries
import tensorflow as tf
import pandas as pd
import matplotlib.pyplot as plt
In [3]:
# read the csv file
dataset = pd.read_csv('musk.csv')
dataset.head()
Out[3]:
Data Preprocessing¶
In [4]:
X = dataset.iloc[:, 3:-1].values
y = dataset.iloc[:, -1].values
In [5]:
# Scaling
from sklearn.preprocessing import StandardScaler
ss = StandardScaler()
X = ss.fit_transform(X)
Split the data for training and testing¶
In [6]:
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.3, random_state = 42)
In [7]:
X_train.shape,y_train.shape
Out[7]:
Build and train the model¶
In [8]:
model = tf.keras.models.Sequential([
tf.keras.layers.Dense(33, input_shape=(166,),
activation=tf.nn.tanh),
tf.keras.layers.Dense(1, activation=tf.nn.sigmoid)
])
model.summary()
# compile
model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])
In [9]:
history = model.fit(X_train, y_train, validation_data = (X_test, y_test), epochs = 15)
In [10]:
# Save the model
model.save("Simple ANN.h5")
Plots¶
In [11]:
# train_loss vs Val_loss
from matplotlib import pyplot as plt
plt.plot(history.history['loss'])
plt.plot(history.history['val_loss'])
plt.title('model loss')
plt.ylabel('loss')
plt.xlabel('epoch')
plt.legend(['train', 'validation'], loc='upper left')
plt.show()
In [12]:
# train_accuracy vs val_accuracy
from matplotlib import pyplot as plt
plt.plot(history.history['accuracy'])
plt.plot(history.history['val_accuracy'])
plt.title('model accuracy')
plt.ylabel('acc')
plt.xlabel('epoch')
plt.legend(['train', 'validation'], loc='upper left')
plt.show()
Predictions and Accuracy¶
In [13]:
# accuracy and loss
model.evaluate(X_test, y_test)
Out[13]:
In [14]:
y_pred = model.predict(X_test)
y_pred[5:10]
Out[14]:
In [15]:
y_pred1 = []
for element in y_pred:
if element > 0.5:
y_pred1.append(1)
else:
y_pred1.append(0)
In [16]:
y_pred1[25:40]
Out[16]:
In [17]:
y_test[25:40]
Out[17]:
In [18]:
# print the classification report
from sklearn.metrics import classification_report, confusion_matrix
print(classification_report(y_test,y_pred1))
Heat Map¶
In [19]:
import seaborn as sn
cm = tf.math.confusion_matrix(labels = y_test, predictions = y_pred1)
plt.figure(figsize = (10,8))
sn.heatmap(cm, annot = True, fmt = 'd')
plt.xlabel("predicted")
plt.ylabel("actual")
Out[19]:
deepCC¶
In [21]:
!deepCC "Simple ANN.h5"