text_plain_output_2.png text_plain_output_1.png
from keras.utils import to_categorical

from pathlib import Path

from sklearn.preprocessing import OrdinalEncoder

from tensorflow import keras

from tensorflow.keras import layers, models, Sequential

import numpy as np

import pandas as pd

import tensorflow as tf

INPUT_DIR = Path('/kaggle/input/playground-series-s3e13/')
train_data = pd.read_csv(INPUT_DIR / 'train.csv')
train_data['data_type'] = 0
train_data['prognosis'] = train_data['prognosis'].str.replace(' ', '_')
test_data = pd.read_csv(INPUT_DIR / 'test.csv')
test_data['data_type'] = 0
features = sorted(list(set(test_data.columns) - set(['id', 'data_type'])))
if INCLUDE_ORIGINAL:
    df_original = pd.read_csv('/kaggle/input/vector-borne-disease-prediction/trainn.csv')
    df_original['id'] = -1 - np.arange(len(df_original))
    df_original['prognosis'] = df_original['prognosis'].str.replace(' ', '_')
    df_original['data_type'] = 1
    train_data = pd.concat([df_original, train_data]).reset_index(drop=True)
    features = sorted(list(set(test_data.columns) - set(['id'])))

from sklearn.preprocessing import OrdinalEncoder
from keras.utils import to_categorical
enc = OrdinalEncoder()
y_enc = enc.fit_transform(train_data.filter(['prognosis']))
y = to_categorical(y_enc)
prognosis_classes = list(enc.categories_[0])
N_CLASSES = len(prognosis_classes)

feats = list(features)
X = train_data.filter(feats).values
X_train = train_data.drop('data_type', axis=1).filter(feats).values
X_data_type = train_data['data_type'].values
X_test = test_data.drop('data_type', axis=1).filter(feats).values
X_tst = test_data.filter(feats).values

encoder_dim = 12
dim_input = layers.Input(shape=(64,))
encoded_layer_0 = tf.keras.layers.GaussianNoise(0.1)(dim_input)
encoded_layer_1 = layers.Dense(60, activation='relu')(encoded_layer_0)
encoded_layer_2 = layers.Dense(50, activation='relu')(encoded_layer_1)
encoded_layer_3 = layers.Dense(40, activation='relu')(encoded_layer_2)
encoded_layer_4 = layers.Dense(30, activation='relu')(encoded_layer_3)
encoded_layer_5 = layers.Dense(20, activation='relu')(encoded_layer_4)
encoded_layer_6 = layers.Dense(encoder_dim, activation='softmax')(encoded_layer_5)
decoded_layer_1 = layers.Dense(20, activation='relu')(encoded_layer_6)
decoded_layer_2 = layers.Dense(30, activation='relu')(decoded_layer_1)
decoded_layer_3 = layers.Dense(40, activation='relu')(decoded_layer_2)
decoded_layer_4 = layers.Dense(50, activation='relu')(decoded_layer_3)
decoded_layer_5 = layers.Dense(60, activation='relu')(decoded_layer_4)
decoded_layer_6 = layers.Dense(64, activation='relu')(decoded_layer_5)
autoencoder = keras.Model(inputs=dim_input, outputs=decoded_layer_6)
autoencoder.compile(loss='categorical_crossentropy', metrics='categorical_accuracy')

encoder = keras.Model(inputs=dim_input, outputs=encoded_layer_6)
encoded_input = layers.Input(shape=(encoder_dim,))

encoded_train = pd.DataFrame(encoder.predict(X_train))
encoded_train = encoded_train.add_prefix('feature_')
encoded_test = pd.DataFrame(encoder.predict(X_test))
encoded_test = encoded_test.add_prefix('feature_')