I am trying to apply an auto-encoder for dimensionality reduction. I wonder how it will be applied on a large dataset.
I have tried this code below. I have total of 8 features in my data and I want to reduce it to 3.
from keras.models import Model
from keras.layers import Input, Dense
from keras import regularizers
from sklearn.preprocessing import MinMaxScaler
import pandas as pd
data = pd.read_csv('C:/user/python/HR.csv')
columns_names=data.columns.tolist()
print("Columns names:", columns_names)
print(data.shape)
data.head()
print(data.dtypes)
# Normalise
scaler = MinMaxScaler()
data_scaled = scaler.fit_transform(data)
# Fixed dimensions
input_dim = data.shape[1] # 8
encoding_dim = 3
# Number of neurons in each Layer [8, 6, 4, 3, ...] of encoders
input_layer = Input(shape=(input_dim, ))
encoder_layer_1 = Dense(6, activation="tanh", activity_regularizer=regularizers.l1(10e-5))(input_layer)
encoder_layer_2 = Dense(4, activation="tanh")(encoder_layer_1)
encoder_layer_3 = Dense(encoding_dim, activation="tanh")(encoder_layer_2)
# Crear encoder model
encoder = Model(inputs=input_layer, outputs=encoder_layer_3)
# Use the model to predict the factors which sum up the information of interest rates.
encoded_data = pd.DataFrame(encoder.predict(data_scaled))
encoded_data.columns = ['factor_1', 'factor_2', 'factor_3']
I have read in this tutorial that, if you have 8 features and your aim is to get 3 components, in order to set up a relationship with PCA, we need to create four layers of 8 (the original amount of series), 6, 4, and 3 (the number of components we are looking for) neurons, respectively. How does it make sense?
Now, let's say that I initially have 500 features and I want to reduce them to 20, what should I do?
According to my understanding, I need to reduce the number of neurons from the first to the last layer. So,
- in the first layer, I have 500 neurons
- in the second layer, it will be 250
- in the third layer, it will be 130
- in the fourth layer, it will be 60
- in the fifth layer, it will be 20
Is this correct, and why?
And can I get matrix-like PCA at the end to see the components I got?