# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.decomposition import PCA, IncrementalPCA, FastICA
plt.style.use("ggplot")
# %matplotlib inline

train = pd.read_csv("data/train.csv", index_col="ID")
test = pd.read_csv("data/test.csv", index_col="ID")

train.head()

train.dtypes.value_counts()

# X0-X8 are alphanumeric
train.iloc[:, 1:12].head()

train.y.describe()

train.X0.value_counts()

train.X1.value_counts()

train.X2.value_counts()

train.X3.value_counts()

train.X4.value_counts()

train.X5.value_counts()

train.X6.value_counts()

# X7 is missing
train.X8.value_counts()

# ### Check for one hot encoding

set(train.X1.values) == set(test.X1.values)

set(train.X2.values) == set(test.X2.values)

set(train.X2.values), set(test.X2.values)

set(train.X3.values) == set(test.X3.values)

set(train.X4.values) == set(test.X4.values)

set(train.X5.values) == set(test.X5.values)

set(train.X5.values), set(test.X5.values)

set(train.X6.values) == set(test.X6.values)

set(train.X8.values) == set(test.X8.values)

# ### Dimension Reduction

# +
train["data"] = "train"
test["data"] = "test"

combined_data = pd.concat([train, test])
encoded = pd.get_dummies(combined_data[["X0", "X1", "X2", "X3", "X4", "X5", "X6", "X8"]])
drop_cat = combined_data.drop(["X0", "X1", "X2", "X3", "X4", "X5", "X6", "X8"], axis=1)
combined_data_clean = drop_cat.join(encoded)

train_data = combined_data_clean[combined_data_clean.data == "train"].copy()
test_data = combined_data_clean[combined_data_clean.data == "test"].copy()

train_data.drop("data", axis=1, inplace=True)
test_data.drop(["data", "y"], axis=1, inplace=True)
# -

y_train = train_data["y"].astype(np.float32)
x_train =  train_data.drop("y", axis=1).astype(np.float32)
x_test = test_data.astype(np.float32)

x_train.shape

n_comp = 128
pca = PCA(n_components=n_comp, random_state=42)
pca_train = pca.fit(x_train)

sum(pca_train.explained_variance_ratio_)

explained_variance = []
for i in range(0, 256, 8):
    pca = PCA(n_components=i, random_state=42)
    pca_train = pca.fit(x_train)
    explained_variance.append((i, sum(pca_train.explained_variance_ratio_)))

dict(explained_variance)

explained_variance = []
for i in range(240, 400, 8):
    pca = PCA(n_components=i, random_state=42)
    pca_train = pca.fit(x_train)
    explained_variance.append((i, sum(pca_train.explained_variance_ratio_)))

# PCA: 96 - 90%, 128 - 94%, 144 - 95%, 384 - 99%
explained_variance

n_comp = 128
ipca = IncrementalPCA(n_components=n_comp)
ipca_train = ipca.fit(x_train)

sum(ipca_train.explained_variance_ratio_)

explained_variance = []
for i in range(1, 256, 8):
    ipca = IncrementalPCA(n_components=i)
    ipca_train = ipca.fit(x_train)
    explained_variance.append((i, sum(ipca_train.explained_variance_ratio_)))

explained_variance

explained_variance = []
for i in range(240, 400, 8):
    ipca = IncrementalPCA(n_components=i)
    ipca_train = ipca.fit(x_train)
    explained_variance.append((i, sum(ipca_train.explained_variance_ratio_)))

explained_variance

#

n_comp = 128
ica = FastICA(n_components=n_comp, random_state=42)
ica_train = ica.fit_transform(x_train)

ica_train

# ### Understanding the target variable more

train.y.describe()

train.y.plot()

train.y.hist()







StarcoderdataJupyterScriptsDedupFiltered