Dimensionality Reduction using Autoencoders — Easy Explanation — with source code

Dimensionality reduction
Dimensionality reduction

Let’s do it…

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from tensorflow.keras.layers import Dense
from tensorflow.keras.optimizers import Adam
from tensorflow.keras.models import Sequential,Model
from sklearn.preprocessing import MinMaxScaler
import seaborn as sns

%matplotlib inline
data = pd.read_csv('anonymized_data.csv')
data.head()
data.info()
scaler = MinMaxScaler()
scaled_data = scaler.fit_transform(data.drop('Label',axis=1))
scaled_data.shape
num_inputs = 30
num_hidden = 2
num_outputs = num_inputs # Must be true for an autoencoder!
model = Sequential()

model.add(Dense(num_inputs, input_shape=[num_inputs]))
model.add(Dense(num_hidden))
model.add(Dense(num_outputs))

model.compile(optimizer=Adam(0.001), metrics=['accuracy'], loss='mae')

print(model.summary())
model.fit(x=scaled_data, y=scaled_data, epochs=1000, batch_size=32)
intermediate_layer_model = Model(inputs=model.input, outputs=model.get_layer(index=1).output)
intermediate_output = intermediate_layer_model.predict(scaled_data)
intermediate_output.shape
sns.scatterplot(intermediate_output[:,0],intermediate_output[:,1],hue=data[‘Label’])
Dimensionality reduction
Dimensionality reduction

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store