Skip to content

Commit f2a13df

Browse files
committed
add autoencoders for dimenstionality reduction & feature extraction tutorial with tensorflow and keras
1 parent 4d66adf commit f2a13df

File tree

4 files changed

+55
-0
lines changed

4 files changed

+55
-0
lines changed

‎README.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ This is a repository of all the tutorials of [The Python Code](https://www.thepy
9595
-[Dimensionality Reduction: Feature Extraction using Scikit-learn in Python](https://www.thepythoncode.com/article/dimensionality-reduction-using-feature-extraction-sklearn). ([code](machine-learning/dimensionality-reduction-feature-extraction))
9696
-[Dimensionality Reduction: Using Feature Selection in Python](https://www.thepythoncode.com/article/dimensionality-reduction-feature-selection). ([code](machine-learning/dimensionality-reduction-feature-selection))
9797
-[A Guide to Explainable AI Using Python](https://www.thepythoncode.com/article/explainable-ai-model-python). ([code](machine-learning/explainable-ai))
98+
-[Autoencoders for Dimensionality Reduction using TensorFlow in Python](https://www.thepythoncode.com/article/feature-extraction-dimensionality-reduction-autoencoders-python-keras). ([code](machine-learning/feature-extraction-autoencoders))
9899

99100
-### [General Python Topics](https://www.thepythoncode.com/topic/general-python-topics)
100101
-[How to Make Facebook Messenger bot in Python](https://www.thepythoncode.com/article/make-bot-fbchat-python). ([code](general/messenger-bot))
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# [Autoencoders for Dimensionality Reduction using TensorFlow in Python](https://www.thepythoncode.com/article/feature-extraction-dimensionality-reduction-autoencoders-python-keras)
2+
To run this:
3+
-`pip install -r requirements.txt`
4+
- Download the dataset (`gafgyt_danmini_doorbell_train.csv` and `gafgyt_danmini_doorbell_test.csv`) from [here](https://www.kaggle.com/datasets/saurabhshahane/anomaly-detection-using-deep-learning?resource=download)
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
importpandasaspd
2+
fromsklearn.preprocessingimportMinMaxScaler
3+
fromtensorflow.keras.modelsimportModel
4+
fromtensorflow.keras.layersimportInput, Dense, LeakyReLU
5+
6+
7+
# Reading Data
8+
df=pd.read_csv("gafgyt_danmini_doorbell_train.csv")
9+
df_test=pd.read_csv("gafgyt_danmini_doorbell_test.csv")
10+
# Keeping only features columns for the train set
11+
df_features=df.loc[:, df.columns!="target"]
12+
print(f"Shape of the train set: {df_features.shape}")
13+
y_train=df.target
14+
# Keeping only features for the test set
15+
df_features_test=df_test.loc[:, df_test.columns!="target"]
16+
y_test=df_test.target
17+
# Applying the normalization on the train then test set
18+
scaler=MinMaxScaler()
19+
df_features=scaler.fit_transform(df_features)
20+
df_features_test=scaler.transform(df_features_test)
21+
22+
# Implementation of the Autoencoder Model
23+
# input from df_features, dense64, leakyrelu, dense32, leakyrelu, dense16, tanh
24+
input=Input(shape=df_features.shape[1:])
25+
enc=Dense(64)(input)
26+
enc=LeakyReLU()(enc)
27+
enc=Dense(32)(enc)
28+
enc=LeakyReLU()(enc)
29+
# latent space with tanh
30+
latent_space=Dense(16, activation="tanh")(enc)
31+
32+
dec=Dense(32)(latent_space)
33+
dec=LeakyReLU()(dec)
34+
dec=Dense(64)(dec)
35+
dec=LeakyReLU()(dec)
36+
37+
dec=Dense(units=df_features.shape[1], activation="sigmoid")(dec)
38+
# init model
39+
autoencoder=Model(input, dec)
40+
# compile model
41+
autoencoder.compile(optimizer="adam",metrics=["mse"],loss="mse")
42+
# train model
43+
autoencoder.fit(df_features, df_features, epochs=50, batch_size=32, validation_split=0.25)
44+
encoder=Model(input, latent_space)
45+
# predict on test set
46+
test_au_features=encoder.predict(df_features_test)
47+
print(test_au_features.shape)
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
tensorflow
2+
pandas
3+
sklearn

0 commit comments

Comments
(0)