import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
%matplotlib inline
import seaborn as sns
import tensorflow as tf
from sklearn.preprocessing import StandardScaler
from sklearn.decomposition import PCA
from sklearn.preprocessing import LabelEncoder
from tensorflow import keras
from sklearn.metrics import accuracy_score, confusion_matrix, classification_report
pd.set_option('display.max_columns', None)
sns.set_style('darkgrid')
## reading training data
train_df = pd.read_csv()
##reading test data
test_df = pd.read_csv()
train_df.head()
test_df.head()
print("train data shape: ", train_df.shape)
print("test data shape: ", test_df.shape)
train data shape: (7352, 563)
test data shape: (2947, 563)
## train and test data
X_train = train_df.iloc[:, :-2]
y_train = train_df.iloc[:, -1]
X_test = test_df.iloc[:, :-2]
y_test = test_df.iloc[:, -1]
## unique classes
y_train.unique()
## value counts of unique classes
class_label = y_train.value_counts()
plt.figure(figsize=(10, 10))
plt.xticks(rotation=75)
sns.barplot(class_label.index, class_label);
C:\Users\jgaur\anaconda3\lib\site-packages\seaborn\_decorators.py:36: FutureWarning: Pass the following variables as keyword args: x, y. From version 0.12, the only valid positional argument will be `data`, and passing other arguments without an explicit keyword will result in an error or misinterpretation.
warnings.warn(
## Standard Scaler
sc = StandardScaler()
X_train = sc.fit_transform(X_train)
X_test = sc.transform(X_test)
## LabelEncoder
label_encoder = LabelEncoder()
y_train = label_encoder.fit_transform(y_train)
y_train=pd.get_dummies(y_train).values
y_test = label_encoder.fit_transform(y_test)
y_test=pd.get_dummies(y_test).values
## converting into numpy array
y_train = np.array(y_train)
y_test = np.array(y_test)
y_train
## PCA
pca = PCA(n_components=None)
X_train = pca.fit_transform(X_train)
X_test = pca.transform(X_test)
X_train
## Model
model = keras.models.Sequential()
model.add(keras.layers.Dense(units=64,activation='relu'))
model.add(keras.layers.Dense(units=128,activation='relu'))
model.add(keras.layers.Dense(units=64,activation='relu'))
model.add(keras.layers.Dense(units=6,activation='softmax'))
model.compile(optimizer='adam',loss='categorical_crossentropy',metrics=['accuracy'])
history = model.fit(X_train, y_train, batch_size=128, epochs=15, validation_split=0.2)
Epoch 1/15
46/46 [==============================] - 6s 24ms/step - loss: 1.1816 - accuracy: 0.5844 - val_loss: 0.2190 - val_accuracy: 0.9252
Epoch 2/15
46/46 [==============================] - 0s 7ms/step - loss: 0.1102 - accuracy: 0.9690 - val_loss: 0.1347 - val_accuracy: 0.9443
Epoch 3/15
46/46 [==============================] - 0s 7ms/step - loss: 0.0424 - accuracy: 0.9875 - val_loss: 0.1308 - val_accuracy: 0.9470
Epoch 4/15
46/46 [==============================] - 0s 7ms/step - loss: 0.0234 - accuracy: 0.9926 - val_loss: 0.1299 - val_accuracy: 0.9524
Epoch 5/15
46/46 [==============================] - ETA: 0s - loss: 0.0159 - accuracy: 0.99 - 0s 6ms/step - loss: 0.0166 - accuracy: 0.9962 - val_loss: 0.1541 - val_accuracy: 0.9524
Epoch 6/15
46/46 [==============================] - 0s 6ms/step - loss: 0.0146 - accuracy: 0.9960 - val_loss: 0.1600 - val_accuracy: 0.9524
Epoch 7/15
46/46 [==============================] - 0s 6ms/step - loss: 0.0088 - accuracy: 0.9980 - val_loss: 0.1545 - val_accuracy: 0.9551
Epoch 8/15
46/46 [==============================] - 0s 7ms/step - loss: 0.0065 - accuracy: 0.9988 - val_loss: 0.1599 - val_accuracy: 0.9572
Epoch 9/15
46/46 [==============================] - 0s 10ms/step - loss: 0.0041 - accuracy: 0.9993 - val_loss: 0.1796 - val_accuracy: 0.9551
Epoch 10/15
46/46 [==============================] - 1s 11ms/step - loss: 0.0026 - accuracy: 1.0000 - val_loss: 0.1829 - val_accuracy: 0.9565
Epoch 11/15
46/46 [==============================] - 1s 13ms/step - loss: 0.0020 - accuracy: 1.0000 - val_loss: 0.1980 - val_accuracy: 0.9572
Epoch 12/15
46/46 [==============================] - 0s 8ms/step - loss: 0.0015 - accuracy: 1.0000 - val_loss: 0.2027 - val_accuracy: 0.9572
Epoch 13/15
46/46 [==============================] - 1s 17ms/step - loss: 9.2582e-04 - accuracy: 1.0000 - val_loss: 0.1992 - val_accuracy: 0.9565
Epoch 14/15
46/46 [==============================] - 0s 9ms/step - loss: 7.3718e-04 - accuracy: 1.0000 - val_loss: 0.2063 - val_accuracy: 0.9572
Epoch 15/15
46/46 [==============================] - 0s 8ms/step - loss: 5.8906e-04 - accuracy: 1.0000 - val_loss: 0.2086 - val_accuracy: 0.9572
## Loss Vs. Epochs
plt.figure(figsize=(10, 5))
plt.plot(history.history['loss'], label='Train Loss')
plt.plot(history.history['val_loss'], label='Test Loss')
plt.xlabel('Epochs')
plt.ylabel('Loss')
plt.legend(['Train Loss', 'Validation Loss']);
## Accuracy Vs. Epochs
plt.figure(figsize=(10, 5))
plt.plot(history.history['accuracy'], label='Train Accuracy')
plt.plot(history.history['val_accuracy'], label='Test Accuracy')
plt.xlabel('Epochs')
plt.ylabel('Accuracy')
plt.legend(['Train Accuracy', 'Validation Accuracy']);
## prediction
pred = model.predict(X_test)
predic = []
for p in pred:
p = np.argmax(p)
predic.append(p)
predic = np.array(predic)
y_test.shape
y_test[0]
y_test_label = []
for i in range(len(y_test)):
for ind, j in enumerate(y_test[i]):
if j == 1:
y_test_label.append(ind)
y_test_label = np.array(y_test_label)
y_test_label
predic
print("Classification Report: \n", classification_report(y_test_label, predic))
print("-" * 100)
print()
print("Accuracy Score: ", accuracy_score(y_test_label, predic))
print("-" * 100)
print()
plt.figure(figsize=(10, 10))
sns.heatmap(confusion_matrix(y_test_label, predic), annot=True, fmt='g')
Classification Report:
precision recall f1-score support
0 1.00 0.94 0.97 537
1 0.97 0.86 0.91 491
2 0.84 0.98 0.90 532
3 0.92 0.99 0.95 496
4 0.97 0.90 0.94 420
5 0.93 0.92 0.92 471
accuracy 0.93 2947
macro avg 0.94 0.93 0.93 2947
weighted avg 0.94 0.93 0.93 2947
----------------------------------------------------------------------------------------------------
Accuracy Score: 0.9328130302002036
----------------------------------------------------------------------------------------------------