Kaggle competition: Santander Customer Transaction Prediction
<SOS> metric: AUC Kaggle data source: santander-customer-transaction-prediction<load_from_csv>
import lightgbm as lgb
import pandas as pd
import numpy as np
from datetime import datetime
from sklearn.preprocessing import MinMaxScaler
from sklearn.decomposition import PCA
import matplotlib.pyplot as plt
def MinMaxBestBaseStacking(input_folder, best_base, output_path):
sub_base = pd.read_csv(best_base)
 all_files = os.listdir(input_folder)
 outs = [pd.read_csv(os.path.join(input_folder, f), index_col=0)for f in all_files]
concat_sub = pd.concat(outs, axis=1)
 cols = list(map(lambda x: "target" + str(x), range(len(concat_sub.columns))))
concat_sub.columns = cols
concat_sub.reset_index(inplace=True)
 concat_sub['is_iceberg_max'] = concat_sub.iloc[:, 1:6].max(axis=1)
 concat_sub['is_iceberg_min'] = concat_sub.iloc[:, 1:6].min(axis=1)
 concat_sub['is_iceberg_mean'] = concat_sub.iloc[:, 1:6].mean(axis=1)
 concat_sub['is_iceberg_median'] = concat_sub.iloc[:, 1:6].median(axis=1)
 cutoff_lo = 0.73
cutoff_hi = 0.33
concat_sub['is_iceberg_base'] = sub_base['target']
concat_sub['target'] = np.where(np.all(concat_sub.iloc[:, 1:6] > cutoff_lo, axis=1),
concat_sub['is_iceberg_max'],
np.where(np.all(concat_sub.iloc[:, 1:6] < cutoff_hi, axis=1),
concat_sub['is_iceberg_min'],
concat_sub['is_iceberg_base']))
concat_sub[['image_name', 'target']].to_csv(output_path,
index=False, float_format='%.12f')
<save_to_csv>
train = pd.read_csv(".. /input/train.csv" ).drop("ID_code",axis=1)
 y_train = train['target']
X_train = train.drop(['target'], axis = 1)
 X_test = pd.read_csv(".. /input/test.csv" ).drop("ID_code",axis=1 )
MinMaxBestBaseStacking('.. /input/csv001/', '.. /input/base001/datasets_766461_1344911_submission_rank-then-blend.csv', 'submission.csv')
<load_from_csv>
sample = 0
if(sample == 1):
sample_train_0 = train[train.target == 0].sample(n = 4000, random_state = 1573456)
 sample_train_1 = train[train.target == 1].sample(n = 400, random_state = 1573456)
 train = sample_train_0.append(sample_train_1)
 y_train = sample_train['target']
X_train = sample_train.drop(['target'], axis = 1 )
def ensemble() :
stacked_0 = pd.read_csv('/kaggle/input/siim-isic-melanoma-classification/sample_submission.csv')
 stacked_1 = pd.read_csv('/kaggle/input/melanoma-submissions2/submissionImage.csv')
 stacked_2 = pd.read_csv('/kaggle/input/melanoma-submissions2/submissionTabular.csv')
 stacked_4 = pd.read_csv('/kaggle/input/melanoma-submissions2/submission_multiple_data_source.csv')
 stacked_3 = pd.read_csv('.. /input/csv001/lgbm_baseline_sub.csv')
 stacked_5 = pd.read_csv('.. /input/csv001/sub77.csv')
 stacked_6 = pd.read_csv('.. /input/csv001/submission.csv')
 stacked_7 = pd.read_csv('.. /input/csv001/submission_models_blended.csv')
 sub = pd.DataFrame()
sub['image_name'] = stacked_0['image_name']
sub['target'] = np.exp(np.mean(
 [
stacked_1['target'].apply(lambda x: np.log2(x)) , \
stacked_2['target'].apply(lambda x: np.log2(x)) , \
stacked_4['target'].apply(lambda x: np.log2(x)) , \
stacked_3['target'].apply(lambda x: np.log2(x)) ,
stacked_5['target'].apply(lambda x: np.log2(x)) , \
stacked_6['target'].apply(lambda x: np.log2(x)) , \
stacked_7['target'].apply(lambda x: np.log2(x)) ,
], axis=0))
sub.to_csv('submission.csv', index=False, float_format='%.6f' )<install_modules>
mmscale = MinMaxScaler()
X_train_scaled = mmscale.fit_transform(X_train)
 X_test_scaled = mmscale.transform(X_test )
!pip install -q efficientnet<install_modules>
pca = PCA()
factors_train = pca.fit_transform(X_train_scaled)
 factors_test = pca.transform(X_test_scaled )
!python pytorch-xla-env-setup.py --version nightly --apt-packages libomp5 libopenblas-dev<set_options>
explained_variance = pca.explained_variance_ratio_
%matplotlib inline 
<train_on_grid>
pd.DataFrame(explained_variance,columns=['explained_variance'] ).plot(kind='box' )
print("Tensorflow version " + tf.__version__)
 AUTO = tf.data.experimental.AUTOTUNE
try:
tpu = tf.distribute.cluster_resolver.TPUClusterResolver()
print('Running on TPU ', tpu.master())
 except ValueError:
tpu = None
if tpu:
tf.config.experimental_connect_to_cluster(tpu)
 tf.tpu.experimental.initialize_tpu_system(tpu)
 strategy = tf.distribute.experimental.TPUStrategy(tpu)
 else:
strategy = tf.distribute.get_strategy()
print("REPLICAS: ", strategy.num_replicas_in_sync)
 DATASET = '512x512-melanoma-tfrecords-70k-images'
GCS_PATH = KaggleDatasets().get_gcs_path(DATASET )<define_variables>
sum(explained_variance[:200] )
train_img_path = '/kaggle/input/siim-isic-melanoma-classification/jpeg/train/'
test_img_path = '/kaggle/input/siim-isic-melanoma-classification/jpeg/test/'<load_from_csv>
factors_train = pd.DataFrame(factors_train)
 factors_test = pd.DataFrame(factors_test )
train = pd.DataFrame(pd.read_csv("/kaggle/input/siim-isic-melanoma-classification/train.csv"))
test = pd.DataFrame(pd.read_csv("/kaggle/input/siim-isic-melanoma-classification/test.csv"))<count_missing_values>
train_combined = train.merge(factors_train, left_index = True, right_index = True)
 test_combined = X_test.merge(factors_test, left_index = True, right_index = True )
train.isna().sum()<count_missing_values>
def augment(train, num_n = 1, num_p = 2):
newtrain=[train]
n = train[train.target == 0]
for i in range(num_n):
newtrain.append(n.apply(lambda x:x.values.take(np.random.permutation(len(n)))))
 p = train[train.target == 1]
for i in range(num_p):
newtrain.append(p.apply(lambda x:x.values.take(np.random.permutation(len(p)))))
 return pd.concat(newtrain )
test.isna().sum()<count_unique_values>
param = {
'boost_from_average':'false',
'boost': 'gbdt',
'feature_fraction': 0.041,
'learning_rate': 0.009,
'max_depth': -1,
'metric':'auc',
'min_data_in_leaf': 70,
'min_sum_hessian_in_leaf': 10.0,
'num_leaves': 15,
'num_threads': 8,
'tree_learner': 'serial',
'objective': 'binary',
'verbosity': -1
}
train['patient_id'].nunique() , test["patient_id"].nunique()<count_values>
train_augmented = augment(train_combined)
 train_data = lgb.Dataset(train_augmented.drop('target', axis = 1), train_augmented['target'])
 model = lgb.train(param, train_data, 19000 )
print(train["target"].value_counts() )<groupby>
prediction = model.predict(test_combined )
<feature_engineering><EOS> 
submission = pd.read_csv('.. /input/sample_submission.csv')
 submission['target'] = prediction
filename = "sub_{:%Y-%m-%d_%H_%M}.csv".format(datetime.now())
 submission.to_csv(filename, index=False )