Kaggle competition: Santander Customer Transaction Prediction
<SOS> metric: AUC Kaggle data source: santander-customer-transaction-prediction<drop_column>
os.environ["CUDA_VISIBLE_DEVICES"] = "0"
random.seed(1234)
 warnings.filterwarnings('ignore')
 class StackingEstimator(BaseEstimator, TransformerMixin):
def __init__(self, estimator):
self.estimator = estimator
def fit(self, X, y=None, **fit_params):
self.estimator.fit(X, y, **fit_params)
 return self
def transform(self, X):
X = check_array(X)
 X_transformed = np.copy(X)
 if issubclass(self.estimator.__class__, ClassifierMixin)and hasattr(self.estimator, 'predict_proba'):
X_transformed = np.hstack(( self.estimator.predict_proba(X), X))
X_transformed = np.hstack(( np.reshape(self.estimator.predict(X),(-1, 1)) , X_transformed))
return X_transformed
train = pd.read_csv('.. /input/train.csv')
 test = pd.read_csv('.. /input/test.csv')
 class GaussRankScaler() :
def __init__(self):
self.epsilon = 1e-9
self.lower = -1 + self.epsilon
self.upper = 1 - self.epsilon
self.range = self.upper - self.lower
def fit_transform(self, X):
i = np.argsort(X, axis = 0)
 j = np.argsort(i, axis = 0)
 assert(j.min() == 0 ).all()
assert(j.max() == len(j)- 1 ).all()
j_range = len(j)- 1
self.divider = j_range / self.range
transformed = j / self.divider
transformed = transformed - self.upper
transformed = scipy.special.erfinv(transformed)
 return transformed
target_col='target'
id_col='ID_code'
submission = pd.read_csv('.. /input/sample_submission.csv')
 id_test = submission[id_col].values
def neural_compression_v2(train, test):
dataset = pd.concat([train.drop(target_col, axis=1), test], axis=0)
 ids = dataset[id_col]
dataset.drop(id_col, axis=1, inplace=True)
 y_train = train[target_col]
cat_vars = [c for c in dataset.columns if dataset[c].dtype == 'object']
for c in cat_vars:
t_data = pd.get_dummies(dataset[c], prefix=c)
 dataset = pd.concat([dataset, t_data], axis=1)
 dataset.drop(cat_vars, axis=1, inplace=True)
 sc = StandardScaler()
sc.fit_transform(dataset)
 dataset = sc.fit_transform(dataset)
 train = dataset[:train.shape[0]]
test = dataset[train.shape[0]:]
print("one hot encoded train shape :: {}".format(train.shape))
print("one hot encoded test shape :: {}".format(test.shape))

 train = train + 0.5 * np.random.normal(loc=0.0, scale=1.0, size=train.shape)
 test = test + 0.5 * np.random.normal(loc=0.0, scale=1.0, size=test.shape)
 l2_reg_embedding = 1e-5
print(keras.__version__)
 init_dim = train.shape[1]
input_row = Input(shape=(init_dim,))
encoded = Dense(512, activation='elu',kernel_regularizer=l2(l2_reg_embedding))(input_row)
 encoded = Dropout(0.2 )(encoded)
 encoded=BatchNormalization()(encoded)
 encoded = Dense(256, activation='elu',kernel_regularizer=l2(l2_reg_embedding))(encoded)
 encoded = Dropout(0.2 )(encoded)
 encoded = Dense(128, activation='elu',kernel_regularizer=l2(l2_reg_embedding))(encoded)
 encoded = Dropout(0.2 )(encoded)
 encoded = Dense(64, activation='elu',kernel_regularizer=l2(l2_reg_embedding))(encoded)
 encoded = Dropout(0.2 )(encoded)
 encoded = Dense(32, activation='elu',kernel_regularizer=l2(l2_reg_embedding))(encoded)
 encoded = Dense(16, activation='elu' )(encoded)
 decoded = Dense(32, activation='elu',kernel_regularizer=l2(l2_reg_embedding))(encoded)
 encoded = Dropout(0.2 )(encoded)
 decoded = Dense(64, activation='elu',kernel_regularizer=l2(l2_reg_embedding))(decoded)
 encoded = Dropout(0.2 )(encoded)
 decoded = Dense(128, activation='elu',kernel_regularizer=l2(l2_reg_embedding))(decoded)
 encoded = Dropout(0.2 )(encoded)
 decoded = Dense(256, activation='elu',kernel_regularizer=l2(l2_reg_embedding))(decoded)
 encoded = Dropout(0.2 )(encoded)
 encoded = BatchNormalization()(encoded)
 decoded = Dense(512, activation='elu',kernel_regularizer=l2(l2_reg_embedding))(decoded)
 decoded = Dense(init_dim, activation='sigmoid' )(decoded)
 autoencoder = Model(inputs=input_row, outputs=decoded)
 autoencoder.compile(optimizer='rmsprop', loss='binary_crossentropy')
 autoencoder.fit(train, train,
batch_size=512,verbose=2,
shuffle=True, validation_data=(test, test), epochs=4)
 encoder = Model(inputs=input_row, outputs=encoded)
 train_compress = encoder.predict(train,batch_size=4048)
 test_compress = encoder.predict(test,batch_size=4048)
 denoised_train = autoencoder.predict(train,batch_size=4048)
 denoised_test = autoencoder.predict(test,batch_size=4048)
 return train_compress, test_compress, denoised_train, denoised_test
train_compress, test_compress, denoised_train, denoised_test = neural_compression_v2(train, test)
 for c in train.columns:
if train[c].dtype == 'object':
lbl = LabelEncoder()
lbl.fit(list(train[c].values)+ list(test[c].values))
train[c] = lbl.transform(list(train[c].values))
test[c] = lbl.transform(list(test[c].values))
n_comp = 12
tsvd = TruncatedSVD(n_components=n_comp, random_state=420)
 tsvd_results_train = tsvd.fit_transform(train.drop([target_col], axis=1))
tsvd_results_test = tsvd.transform(test)
 pca = PCA(n_components=n_comp, random_state=420)
 pca2_results_train = pca.fit_transform(train.drop([target_col], axis=1))
pca2_results_test = pca.transform(test)
 ica = FastICA(n_components=n_comp, random_state=420)
 ica2_results_train = ica.fit_transform(train.drop([target_col], axis=1))
ica2_results_test = ica.transform(test)
 grp = GaussianRandomProjection(n_components=n_comp, eps=0.1, random_state=420)
 grp_results_train = grp.fit_transform(train.drop([target_col], axis=1))
grp_results_test = grp.transform(test)
 srp = SparseRandomProjection(n_components=n_comp, dense_output=True, random_state=420)
 srp_results_train = srp.fit_transform(train.drop([target_col], axis=1))
srp_results_test = srp.transform(test)
 fag = FeatureAgglomeration(n_clusters=n_comp, linkage='ward')
 fag_results_train = fag.fit_transform(train.drop([target_col], axis=1))
fag_results_test = fag.transform(test)
 usable_columns = list(set(train.columns)- set([target_col]))
for i in range(1, n_comp + 1):
train['pca_' + str(i)] = pca2_results_train[:, i - 1]
test['pca_' + str(i)] = pca2_results_test[:, i - 1]
train['ica_' + str(i)] = ica2_results_train[:, i - 1]
test['ica_' + str(i)] = ica2_results_test[:, i - 1]
train['tsvd_' + str(i)] = tsvd_results_train[:, i - 1]
test['tsvd_' + str(i)] = tsvd_results_test[:, i - 1]
train['grp_' + str(i)] = grp_results_train[:, i - 1]
test['grp_' + str(i)] = grp_results_test[:, i - 1]
train['srp_' + str(i)] = srp_results_train[:, i - 1]
test['srp_' + str(i)] = srp_results_test[:, i - 1]
for j in range(1, train_compress.shape[1]):
train['aen_' + str(j)] = denoised_train[:, j-1]
test['aen_' + str(j)] = denoised_test[:, j-1]
y = train[target_col].values
finaltrainset = train[usable_columns].values
finaltestset = test[usable_columns].values
X=train.drop(target_col, axis=1)