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

# +
import numpy as np
import pandas as pd
import warnings
import matplotlib.pyplot as plt
from pandas.tools.plotting import scatter_matrix
from sklearn.cluster import KMeans
from sklearn.decomposition import PCA
from sklearn.linear_model import LinearRegression, Lasso, ElasticNet
from sklearn.model_selection import cross_val_score
from sklearn.ensemble import RandomForestRegressor

warnings.filterwarnings("ignore")
# %matplotlib inline
plt.style.use("ggplot")
plt.rcParams["figure.figsize"] = (12, 8)
# -

fl1 = "data_files/train.csv"
fl2 = "data_files/test.csv"

train_df = pd.read_csv(fl1, header=0)
test_df = pd.read_csv(fl2, header=0)

print(train_df.shape)
print(test_df.shape)

tr_nulls = train_df.isnull().sum().to_frame().transpose()
te_nulls = test_df.isnull().sum().to_frame().transpose()

# +
# for c in te_nulls.columns:
#     print("{}: {}".format(c, tr_nulls[c][0]))
# -

train_df.corr()

# +
# scatter_matrix(train_df, alpha=0.2, diagonal='kde')
# -

print(train_df['loss'].min())
print(train_df['loss'].max())

train_df['loss'].plot.hist(bins=100, color="darkred")

np.log(train_df['loss']).plot.hist(bins=100, color="darkred")

train_df2 = train_df.copy()

train_df2['log_loss'] = train_df['loss'].map(lambda x: np.log(x))

train_df2.corr()

cont_vars = ["cont{}".format(i) for i in range(1, 15)]

X_cl = np.asarray(train_df2[cont_vars])

km = KMeans(n_clusters=7)

cont_labels = km.fit_predict(X_cl)

pca = PCA()

pca.fit(X_cl)

plt.figure(1)
plt.clf()
plt.axes([.2, .2, .7, .7])
plt.plot(pca.explained_variance_, linewidth=2)
plt.axis('tight')
plt.xlabel('n_components')
plt.ylabel('explained_variance_')

cat_vars = ["cat{}".format(i) for i in range(1, 117)]

train_df[cat_vars].head()

# +
# for c in cat_vars:
#     print("{}: {}".format(c, len(train_df[c].unique())))
# -

train_df.shape

# +
num_cols_w_dums = 131

for c in cat_vars:
    num_cols_w_dums += len(train_df[c].unique()) - 1
# -

num_cols_w_dums

train_df3 = train_df.copy()

train_df3 = pd.get_dummies(train_df3, columns=cat_vars, drop_first=True)

train_df3.shape

train_df3.head()

pca2 = PCA()

pca2.fit(np.asarray(train_df3.drop(['id', 'loss'], axis=1)))

plt.figure(1)
plt.clf()
plt.axes([.2, .2, .7, .7])
plt.plot(pca2.explained_variance_, linewidth=2)
plt.axis('tight')
plt.xlabel('n_components')
plt.ylabel('explained_variance_')

pca_200_comps = PCA(n_components=200)

X_pca_200 = pca_200_comps.fit_transform(np.asarray(train_df3.drop(['id', 'loss'], axis=1)))

X_pca_200.shape

y_pca_200 = np.asarray(train_df3['loss']).reshape(-1, 1)

lm1 = LinearRegression()

lm1.fit(X_pca_200, y_pca_200)

lm1.score(X_pca_200, y_pca_200)

ls = Lasso()

cross_val_score(ls, np.asarray(train_df3.drop(['id', 'loss'], axis=1)), np.asarray(train_df3['loss']).reshape(-1, 1), 
                scoring="neg_mean_absolute_error")

rf = RandomForestRegressor(n_estimators=20, max_depth=10)

# +
# cross_val_score(rf, np.asarray(train_df3.drop(['id', 'loss'], axis=1)), np.asarray(train_df3['loss']).reshape(-1, 1), 
#                 scoring="neg_mean_absolute_error")
# -

l1_mixes = np.arange(0.1, 1.0, 0.1)

# +
mx_dict = dict()

for l in l1_mixes:
    en = ElasticNet(alpha=0.1, l1_ratio=l)
    scs = cross_val_score(en, np.asarray(train_df3.drop(['id', 'loss'], axis=1)), 
                          np.asarray(train_df3['loss']).reshape(-1, 1), scoring="neg_mean_absolute_error")
    mx_dict[str(l)] = np.absolute(np.mean(scs))
# -

mx_dict



StarcoderdataJupyterScriptsDedupFiltered