Kaggle competition: Tabular Playground Series - Jan 2021
tst_preds = np.mean(tst_preds, axis=0)
<create_dataframe>
!pip install python-vivid
test_pred = pd.DataFrame(tst_preds)
 test_pred['image_id'] = test.image_id<drop_column>
import seaborn as sns
import matplotlib.pyplot as plt
from vivid.estimators.base import MetaBlock
from vivid.estimators.boosting.mixins import BoostingEarlyStoppingMixin
from catboost import CatBoostRegressor
test_pred = test_pred.set_index('image_id' )<save_to_csv>
from vivid.features.base import BinningCountBlock
from vivid.features.base import CountEncodingBlock
from vivid.features.base import FilterBlock
from vivid.estimators.boosting import XGBRegressorBlock
from vivid.estimators.boosting import LGBMRegressorBlock
from vivid.estimators.boosting.block import create_boosting_seed_blocks
from vivid.estimators.linear import TunedRidgeBlock
from vivid.estimators.svm import SVRBlock
from vivid.estimators.ensumble import RFRegressorBlock
from vivid.estimators.base import EnsembleBlock, BaseBlock
test_pred.to_csv('vit_predictions.csv' )<define_variables>
train_df = pd.read_csv('/kaggle/input/tabular-playground-series-jan-2021/train.csv')
 test_df = pd.read_csv('/kaggle/input/tabular-playground-series-jan-2021/test.csv')
 sample_submission_df = pd.read_csv('/kaggle/input/tabular-playground-series-jan-2021/sample_submission.csv')
 feature_columns = [
'cont1',
'cont2',
'cont3',
'cont4',
'cont5',
'cont6',
'cont7',
'cont8',
'cont9',
'cont10',
'cont11',
'cont12',
'cont13',
'cont14',
]
y = train_df['target'].values
<import_modules>
class PCABlock(BaseBlock):
def __init__(self, n_components=3, columns=None, *args, **kwrgs):
self.n_components = n_components
if columns is None: columns = feature_columns
self.columns = columns
super().__init__(name='pca_n={}'.format(n_components), *args, **kwrgs)
 def fit(self, source_df, y, experiment=None)-> pd.DataFrame:
clf = PCA(n_components=self.n_components)
 clf.fit(source_df[self.columns].values)
 self.clf_ = clf
return self.transform(source_df)
 def transform(self, source_df):
z = self.clf_.transform(source_df[self.columns])
 out_df = pd.DataFrame(z)
 return out_df.add_prefix('PCA_')
 class GaussianMixtureBlock(BaseBlock):
def __init__(self, n_components=3, columns=None, *args, **kwrgs):
self.n_components = n_components
if columns is None: columns = feature_columns
self.columns = columns
super().__init__(name='GMM_n={}'.format(n_components), *args, **kwrgs)
 def fit(self, source_df, y, experiment=None)-> pd.DataFrame:
clf = GaussianMixture(n_components=self.n_components)
 clf.fit(source_df[self.columns].values)
 self.clf_ = clf
return self.transform(source_df)
 def transform(self, source_df):
z = self.clf_.predict_proba(source_df[self.columns])
 z = np.clip(z, 1e-6,1 - 1e-6)
 out_df = pd.DataFrame(z)
 return out_df.add_prefix('GMM_' )
<load_from_csv>
from vivid.estimators.boosting.mixins import TunedBoostingBlock
from vivid.estimators.boosting.helpers import get_boosting_parameter_suggestions
from vivid.estimators.boosting.lgbm import LGBMRegressorBlock
import lightgbm as lgbm
from vivid.runner import create_runner
<categorify>
class TunedLightGBMRegressorBlock(TunedBoostingBlock):
model_class = lgbm.LGBMRegressor
default_eval_metric = 'rmse'
initial_params = LGBMRegressorBlock.initial_params
def generate_model_class_try_params(self, trial):
param = get_boosting_parameter_suggestions(trial)
 param['n_jobs'] = -1
return param
<load_from_csv>
feature_blocks = [
BinningCountBlock(name='BINS', column=feature_columns),
CountEncodingBlock(name='CE', column=feature_columns),
FilterBlock(name='F', column=feature_columns),
PCABlock(n_components=3),
GaussianMixtureBlock(n_components=3)
 ]
runner = create_runner(blocks=[
LGBMRegressorBlock(name='normal_lgbm', parent=feature_blocks),
TunedLightGBMRegressorBlock(name='tuned_lgbm', parent=feature_blocks, n_trials=50)
 ] )
pred1 = pd.read_csv('./predictions.csv',index_col=0 ).sort_index()
pred3 = pd.read_csv('./vit_predictions.csv',index_col=0 ).sort_index()
pred1 = pred1.div(pred1.sum(axis=1),axis=0)
 pred3 = pred3.div(pred3.sum(axis=1),axis=0 )<prepare_output>
oof_results = runner.fit(train_df[feature_columns], y=y )
submission = 0.8*pred1 + 0.2*pred3<prepare_output>
test_results = runner.predict(test_df )
submission['label'] = submission.idxmax(axis=1)
 submission = submission.reset_index()
submission<save_to_csv>
oof_df = pd.DataFrame()
for result in oof_results:
oof_df[result.block.name] = result.out_df.values[:, 0]
submission[['image_id','label']].to_csv('submission.csv',index=False )<import_modules>
score_df = pd.DataFrame()
for name, pred in oof_df.T.iterrows() :
score_i = regression_metrics(y, pred)
 score_df = score_df.append(pd.Series(score_i, name=name))
<import_modules>
score_df.sort_values('rmse' )
import os
import glob
import random
import shutil
import warnings
import json
import itertools
import numpy as np
import pandas as pd
from collections import Counter
import plotly.express as px
import matplotlib.pyplot as plt
import seaborn as sns
import keras
from keras.preprocessing.image import ImageDataGenerator
import tensorflow as tf
from PIL import Image
from sklearn.model_selection import train_test_split<define_variables>
for result in test_results:
out_df = result.out_df
sub_df = sample_submission_df.copy()
sub_df['target'] = result.out_df.values[:, 0]
to = f'/kaggle/working/{str(result.block.name)}_submission.csv'
print('save to {}'.format(to))
sub_df.to_csv(to, index=False )