<jupyter_start><jupyter_text>pytorch-tabnet* good result for single model, fixed split train/valid<jupyter_code>import pandas as pd
import numpy as np
from sklearn.preprocessing import LabelEncoder

from pytorch_tabnet.tab_model import TabNetRegressor

import seaborn as sns
import matplotlib.pyplot as plt
# globals
FIGSIZE = (9, 6)

SEED = 42

FILE_TRAIN = "train.csv"
#
# my split in train, test set
#
def my_train_test_split(df, frac):
    # frac: the fraction used for train
    # df: the dataframe
    
    # shuffle before split
    df = df.sample(frac=1., random_state=SEED)

    # FRAC = 0.90
    tot_rec = df.shape[0]
    NUM_TRAIN = int(frac*tot_rec)
    NUM_TEST = tot_rec - NUM_TRAIN

    data_train = df[:NUM_TRAIN]
    data_test = df[NUM_TRAIN:]

    print()
    print('Numero totale di campioni:', tot_rec)
    print('Numero di campioni nel TRAIN SET:', data_train.shape[0])
    print('Numero di campioni nel Validation SET:', data_test.shape[0])
    
    return data_train, data_test
data_orig = pd.read_csv(FILE_TRAIN)

# feature engineering
data_orig['datetime'] = pd.to_datetime(data_orig['datetime'])

# this way I add 3 engineered features
data_orig['hour'] = data_orig['datetime'].dt.hour

# mese di fatto è duplicato di season. rimuovo
#data_orig['month'] = data_orig['datetime'].dt.month
# data_orig['day'] = data_orig['datetime'].dt.day
data_orig['year'] = data_orig['datetime'].dt.year
data_orig.tail()
all_columns = data_orig.columns

# colonne da ignorare
# atemp and temp are strongly correlated (0.98) taking only one
del_columns = ['datetime', 'casual', 'registered', 'temp']

TARGET = "count"

cat_cols = ['season', 'holiday','workingday', 'weather', 'hour', 'year']

num_cols = list(set(all_columns) - set([TARGET]) - set(del_columns) - set(cat_cols))

features = sorted(cat_cols + num_cols)

print('Tutte le colonne:', len(all_columns))
print('Colonne ignorate:', len(del_columns))
print('target:', len([TARGET]))
print('Colonne cat:', len(cat_cols))
print('Colonne num:', len(num_cols))
print('Num. features', len(features))
data_used = data_orig.drop(del_columns, axis=1)
FRAC = 0.90

data_train, data_valid = my_train_test_split(data_used, frac=FRAC)
categorical_columns = cat_cols
categorical_dims =  {}
vet_lenc = []

for col in cat_cols:
    print(col, data_train[col].nunique(), data_train[col].unique())
    l_enc = LabelEncoder()
    data_train[col] = l_enc.fit_transform(data_train[col].values)
    data_valid[col] = l_enc.transform(data_valid[col].values)
    vet_lenc.append(l_enc)
    categorical_dims[col] = len(l_enc.classes_)
cat_idxs = [ i for i, f in enumerate(features) if f in categorical_columns]

cat_dims = [ categorical_dims[f] for i, f in enumerate(features) if f in categorical_columns]
# separo X ed y
x_train = data_train[features].values
y_train = data_train[TARGET].values

y_train = y_train.reshape(-1, 1)

x_valid = data_valid[features].values
y_valid = data_valid[TARGET].values

y_valid = y_valid.reshape(-1, 1)
%%time

params = {'n_steps':2,
          'cat_dims':cat_dims,
          'cat_idxs':cat_idxs,
          'verbose':50
         }

# clf = TabNetRegressor(n_steps = 2, cat_dims=cat_dims, cat_idxs=cat_idxs, verbose=50)
clf = TabNetRegressor(**params)

# provo a cercare rmsle
clf.fit(x_train, y_train, eval_set=[(x_valid, y_valid)], eval_metric=['rmse', 'rmsle'], max_epochs=1000, patience=100, batch_size=256)
%matplotlib inline

# eXplainability: Feature importance


plt.figure(figsize = FIGSIZE)
plt.grid(True)
sns.barplot(x = clf.feature_importances_, y = features);<jupyter_output><empty_output><jupyter_text>Submission<jupyter_code>test_orig = pd.read_csv("test.csv")
# add engineered features
# feature engineering
test_orig['datetime'] = pd.to_datetime(test_orig['datetime'])

# this way I add 3 engineered features
test_orig['hour'] = test_orig['datetime'].dt.hour
#test_orig['month'] = test_orig['datetime'].dt.month
# test_orig['day'] = test_orig['datetime'].dt.day
test_orig['year'] = test_orig['datetime'].dt.year
# code categorical
for i, col in enumerate(cat_cols):
    print(col, test_orig[col].nunique(), test_orig[col].unique())
    l_enc = vet_lenc[i]
    test_orig[col] = l_enc.transform(test_orig[col].values)
v_test = test_orig[features].values
score_test = clf.predict(v_test)
df_sub = pd.read_csv("sampleSubmission.csv")
df_sub["count"] = score_test
condition = df_sub["count"] < 0

df_sub.loc[condition, "count"] = 0
FILE_SUB = "submission32.csv"

df_sub.to_csv(FILE_SUB, index=False)
!kaggle competitions submit -c "bike-sharing-demand" -f $FILE_SUB -m "sub32 tabnet 2"<jupyter_output>100%|█████████████████████████████████████████| 188k/188k [00:01<00:00, 118kB/s]
Successfully submitted to Bike Sharing Demand
{'fraction_non_alphanumeric': 0.1440084835630965, 'fraction_numerical': 0.014209968186638389, 'mean_word_length': 5.522821576763485, 'pattern_counts': {'":': 0, '<': 10, '<?xml version=': 0, '>': 8, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '46801', 'license': 'MIT', 'n_tokens_mistral': 1813, 'n_tokens_neox': 1726, 'n_words': 367, 'path': '.ipynb_checkpoints/tabnet1-checkpoint.ipynb', 'repo_name': 'luigisaetta/bike-sharing-forecast', 'size': '4715'}