# ---
# 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
# ---

# ## Imports

# %reload_ext autoreload
# %autoreload 2
# %matplotlib inline

# +
import torch
import torch.nn as nn
import torch.nn.functional as F

import pytorch_lightning as pl
# -

from nam.data import *
from nam.config import defaults
from nam.models import NAM, DNN, get_num_units
from nam.engine import Engine

# ## Configuration 

config = defaults()
print(config)

pl.seed_everything(config.seed)

# ## Dataset & Dataloaders (California Housing Dataset)

# dataset = load_sklearn_housing_data(config=config) 
dataset = load_breast_data(config=config)
# dataset = load_gallup_data(
#     config=config,
#     features_columns= ["income_2", "WP1219", "WP1220", "weo_gdpc_con_ppp"]
# )

len(dataset.data)

dataset.features.shape

dataset.targets.max().item()

dataset.targets.shape

train_dls = dataset.train_dataloaders()

test_dl = dataset.test_dataloaders()

# ## NAM Model

model = NAM(
      config=config,
      name="NAMModel_Housing",
      num_inputs=len(dataset[0][0]),
      num_units=get_num_units(config, dataset.features),
      num_outputs=int(dataset.targets.max().item() + 1),
)
model

train_dl, val_dl = next(train_dls)
x, y = next(iter(train_dl))

logits, fnn_out = model(x)
logits

pl.metrics.functional.accuracy(logits, y)

# ## Training

engine = Engine(config, model)
# engine

checkpoint_callback = pl.callbacks.model_checkpoint.ModelCheckpoint(
    dirpath=f"{config.output_dir}/{model.name}/checkpoints",
    filename=model.name + "-{epoch:02d}-{val_loss:.4f}", 
    monitor='val_loss', 
    save_top_k=3,
    mode='min'
)

while True:
    try:
        train_dl, val_dl = next(train_dls)
        trainer = pl.Trainer(
            default_root_dir=f"{config.output_dir}/{model.name}",
            max_epochs=config.training_epochs,
            callbacks=[checkpoint_callback],
        )
        trainer.fit(model=engine, train_dataloader=train_dl, val_dataloaders=val_dl)
    except StopIteration:
        break

engine.metrics.compute()

# ## Testing

trainer.test(model=engine, test_dataloaders=test_dl)

# ---
# ---
# ---

# ## Dataset & Dataloaders (Breast Cancer Dataset)

dataset = load_sklearn_housing_data(config=config) 
# dataset = load_breast_data(config=config)
# dataset = load_gallup_data(
#     config=config,
#     features_columns= ["income_2", "WP1219", "WP1220", "weo_gdpc_con_ppp"]
# )

len(dataset.data)

dataset.features.shape

dataset.targets.max().item()

dataset.targets.shape

train_dls = dataset.train_dataloaders()

test_dl = dataset.test_dataloaders()

# ## NAM Model

model = NAM(
      config=config,
      name="NAMModel_Breast",
      num_inputs=len(dataset[0][0]),
      num_units=get_num_units(config, dataset.features),
#       num_outputs=int(dataset.targets.max().item() + 1),
)
model

# ## Training

engine = Engine(config, model)
# engine

checkpoint_callback = pl.callbacks.model_checkpoint.ModelCheckpoint(
    dirpath=f"{config.output_dir}/{model.name}/checkpoints",
    filename=model.name + "-{epoch:02d}-{val_loss:.4f}", 
    monitor='val_loss', 
    save_top_k=3,
    mode='min'
)

while True:
    try:
        train_dl, val_dl = next(train_dls)
        trainer = pl.Trainer(
            default_root_dir=f"{config.output_dir}/{model.name}",
            max_epochs=config.training_epochs,
            callbacks=[checkpoint_callback],
        )
        trainer.fit(model=engine, train_dataloader=train_dl, val_dataloaders=val_dl)
    except StopIteration:
        break

# ## Testing

trainer.test(model=engine, test_dataloaders=test_dl)

# ---
# ---
# ---

# ## Dataset & Dataloaders (GALLUP Dataset)

# dataset = load_sklearn_housing_data(config=config) 
# dataset = load_breast_data(config=config)
dataset = load_gallup_data(
    config=config,
    features_columns= ["income_2", "WP1219", "WP1220", "weo_gdpc_con_ppp"]
)

len(dataset.data)

dataset.features.shape

dataset.targets.max().item()

dataset.targets.shape

config.batch_size = 2048

train_dls = dataset.train_dataloaders()

test_dl = dataset.test_dataloaders()

# ## NAM Model

model = NAM(
      config=config,
      name="NAMModel_GALLUP",
      num_inputs=len(dataset[0][0]),
      num_units=get_num_units(config, dataset.features),
      num_outputs=int(dataset.targets.max().item() + 1),
)
model

# ## Training

config.training_epochs = 1

engine = Engine(config, model)
# engine

checkpoint_callback = pl.callbacks.model_checkpoint.ModelCheckpoint(
    dirpath=f"{config.output_dir}/{model.name}/checkpoints",
    filename=model.name + "-{epoch:02d}-{val_loss:.4f}", 
    monitor='val_loss', 
    save_top_k=3,
    mode='min'
)

while True:
    try:
        train_dl, val_dl = next(train_dls)
        trainer = pl.Trainer(
            default_root_dir=f"{config.output_dir}/{model.name}",
            max_epochs=config.training_epochs,
            callbacks=[checkpoint_callback],
        )
        trainer.fit(model=engine, train_dataloader=train_dl, val_dataloaders=val_dl)
    except StopIteration:
        break

# ## Testing

trainer.test(model=engine, test_dataloaders=test_dl)

# ---
# ---
# ---

from nam.data.base import NAMDataset

config.regression = False

csv_file = 'data/GALLUP.csv'
data = pd.read_csv(csv_file)
data["WP16"] = np.where(data["WP16"]<6, 0, 1)
data.head(3)

data = data.sample(frac=0.1)

data = df

data.head()

len(data)

features_columns = list(df.columns)[2:]# ["income_2", "WP1219", "WP1220", "weo_gdpc_con_ppp",  "year"]
targets_column = "WP16"
weights_column = "wgt"
dataset = NAMDataset(
  config=config,
  file_path=data,
  features_columns=features_columns,
  targets_column=targets_column,
  weights_column=weights_column,
)

dataset

len(dataset)

config.hidden_sizes = [16]

model = NAM(
      config=config,
      name="NAMModel_GALLUP",
      num_inputs=len(dataset[0][0]),
      num_units=get_num_units(config, dataset.features),
      num_outputs=int(dataset.targets.max().item() + 1),
)
model

# +
total_count = len(dataset)
train_count = int(0.7 * total_count)
valid_count = int(0.2 * total_count)
test_count = total_count - train_count - valid_count
train_dataset, valid_dataset, test_dataset = torch.utils.data.random_split(
    dataset, (train_count, valid_count, test_count)
)


train_dataset_loader = torch.utils.data.DataLoader(
    train_dataset, batch_size=config.batch_size, shuffle=True, num_workers=config.num_workers
)
valid_dataset_loader = torch.utils.data.DataLoader(
    valid_dataset, batch_size=config.batch_size, shuffle=True, num_workers=config.num_workers
)
test_dataset_loader = torch.utils.data.DataLoader(
    test_dataset, batch_size=config.batch_size, shuffle=False, num_workers=config.num_workers
)
dataloaders = {
    "train": train_dataset_loader,
    "val": valid_dataset_loader,
    "test": test_dataset_loader,
}
# -

dataloaders

engine = Engine(config, model)

config.training_epochs = 3

trainer = pl.Trainer(
    default_root_dir=f"{config.output_dir}/{model.name}",
    max_epochs=config.training_epochs,
)

trainer.fit(model=engine, train_dataloader=dataloaders['train'], val_dataloaders=dataloaders['val'])

trainer.test(model=engine, test_dataloaders=dataloaders['test'])

# ---
# ---
# ---

# +
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns

data = pd.read_csv('data/GALLUP.csv')
data.head(3)
# -

y=data.iloc[:,0].values

data = data.drop('country', axis=1)
data =(data-data.mean())/data.std()
data = data.interpolate(method='linear', axis=0)

data.head(3)

x= data.iloc[:,1:].values
# y=data.iloc[:,0].values
x, y

from sklearn.preprocessing import LabelEncoder 
ly = LabelEncoder()
y = ly.fit_transform(y)

y

data.columns

sns.set()
sns.pairplot(data[:1000], hue="WP16", diag_kind="kde")

from sklearn.model_selection import train_test_split
x_train,x_test,y_train,y_test = train_test_split(x,y,test_size=0.2)

# +
from sklearn.naive_bayes import GaussianNB
gnb = GaussianNB()
gnb.fit(x_train,y_train)

y_pred_test = gnb.predict(x_test)

from sklearn.metrics import accuracy_score
acc = accuracy_score(y_test,y_pred_test)
# -

acc

# +
from sklearn.tree import DecisionTreeClassifier

dt = DecisionTreeClassifier()
dt.fit(x_train,y_train)

y_pred2 = dt.predict(x_test)
acc2 = accuracy_score(y_test,y_pred2)
acc2

# +
from sklearn.neighbors import KNeighborsClassifier
clf = KNeighborsClassifier(n_neighbors=3,algorithm='ball_tree')

clf.fit(x_train,y_train)
y_pred3 = clf.predict(x_test)
acc3 =   accuracy_score(y_test,y_pred3)
acc3

# +
from sklearn.svm import SVC
svc1 = SVC(C=50,kernel='rbf',gamma=1)     

svc1.fit(x_train,y_train)
y_pred4 = svc1.predict(x_test)

from sklearn.metrics import accuracy_score
acc4=    accuracy_score(y_test,y_pred4)
acc4

# +
import numpy as np
from deeptables.models import deeptable, deepnets
from deeptables.datasets import dsutils
from sklearn.model_selection import train_test_split

# #loading data
# df = dsutils.load_bank()
# df_train, df_test = train_test_split(df, test_size=0.2, random_state=42)

# y = df_train.pop('y')
# y_test = df_test.pop('y')

# #training
# config = deeptable.ModelConfig(nets=deepnets.DeepFM)
# dt = deeptable.DeepTable(config=config)
# model, history = dt.fit(df_train, y, epochs=10)

# #evaluation
# result = dt.evaluate(df_test,y_test, batch_size=512, verbose=0)
# print(result)

# #scoring
# preds = dt.predict(df_test)

# +
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns

data = pd.read_csv('data/GALLUP.csv')
data.head(3)
# -

y=data.iloc[:,0].values

# data = data.drop('country', axis=1)
# data =(data-data.mean())/data.std()
data = data.interpolate(method='linear', axis=0)

data.head(3)

data = df

x= data.iloc[:,1:].values
y=data.iloc[:,0].values
x, y

from sklearn.preprocessing import LabelEncoder 
ly = LabelEncoder()
y = ly.fit_transform(y)

y

from sklearn.model_selection import train_test_split
x_train,x_test,y_train,y_test = train_test_split(x,y,test_size=0.2)

x_train.shape, y_train.shape

# +
#training
config = deeptable.ModelConfig(nets=deepnets.DeepFM, 
                              categorical_columns='auto', # or categorical_columns=['x1', 'x2', 'x3', ...]
                                metrics=['AUC', 'accuracy'], # can be `metrics=['RootMeanSquaredError']` for regression task
                                auto_categorize=True,
                                auto_discrete=False,
                                embeddings_output_dim=20,
                                embedding_dropout=0.3,)
dt = deeptable.DeepTable(config=config)
model, history = dt.fit(x_train, y_train, batch_size=2048, epochs=3)

#evaluation
result = dt.evaluate(x_test,y_test, batch_size=1024, verbose=0)
print(result)

#scoring
preds = dt.predict(x_test)
# -

# # DATA PROCESSING

import pandas as pd
df = pd.read_csv('data/GALLUP.csv')
df.head(10)

df.isna().sum()

df = df.interpolate(method ='linear', limit_direction ='forward')

# +
# df = df.drop(columns=['year', 'country'])
# -

df.head()

df = df.drop_duplicates(keep=False)


def encode_and_bind(original_dataframe, feature_to_encode):
    dummies = pd.get_dummies(original_dataframe[[feature_to_encode]])
    res = pd.concat([original_dataframe, dummies], axis=1)
    res = res.drop([feature_to_encode], axis=1)
    return(res) 


df2 = encode_and_bind(df, 'year')
df2 = encode_and_bind(df2, 'country')
df2.head()

df = df2

columns = ['wgt', 'income_2', 'WP1219', 'WP1220', 'weo_gdpc_con_ppp']
df[columns] = df[columns].apply(lambda x: (x - x.min()) / (x.max() - x.min()))

df.head()

df["WP16"] = np.where(df["WP16"]<6, 0, 1)

df.head()

list(df.columns)[2:]

from sklearn.preprocessing import LabelEncoder 
ly = LabelEncoder()
# y = ly.fit_transform(y)

columns = ['year', 'country']
df[columns] = df[columns].apply(lambda x: ly.fit_transform(x))

df.head()



StarcoderdataJupyterScriptsDedupFiltered