Kaggle competition: Instant Gratification
<SOS> metric: AUC Kaggle data source: instant-gratification<choose_model_class>
warnings.filterwarnings('ignore')
 df_train = pd.read_csv('.. /input/train.csv')
 df_test = pd.read_csv('.. /input/test.csv')
 wcrms = df_train['wheezy-copper-turtle-magic'].unique()
model.compile(optimizer= tf.keras.optimizers.Adam(learning_rate=1e-3),
loss="sparse_categorical_crossentropy",
metrics=["sparse_categorical_accuracy"] )<train_model>
class SSGaussianMixture(object):
def __init__(self, n_features, n_categories):
self.n_features = n_features
self.n_categories = n_categories
self.mus = np.array([np.random.randn(n_features)]*n_categories)
 self.sigmas = np.array([np.eye(n_features)]*n_categories)
 self.pis = np.array([1/n_categories]*n_categories)
 def fit(self, X_train, y_train, X_test, threshold=0.00001, max_iter=100):
Z_train = np.eye(self.n_categories)[y_train]
for i in range(max_iter):
Z_test = np.array([self.gamma(X_test, k)for k in range(self.n_categories)] ).T
Z_test /= Z_test.sum(axis=1, keepdims=True)
 datas = [X_train, Z_train, X_test, Z_test]
mus = np.array([self._est_mu(k, *datas)for k in range(self.n_categories)])
 sigmas = np.array([self._est_sigma(k, *datas)for k in range(self.n_categories)])
 pis = np.array([self._est_pi(k, *datas)for k in range(self.n_categories)])
 diff = max(np.max(np.abs(mus-self.mus)) ,
np.max(np.abs(sigmas-self.sigmas)) ,
np.max(np.abs(pis-self.pis)))
 self.mus = mus
self.sigmas = sigmas
self.pis = pis
if diff<threshold:
break
def predict_proba(self, X):
Z_pred = np.array([self.gamma(X, k)for k in range(self.n_categories)] ).T
Z_pred /= Z_pred.sum(axis=1, keepdims=True)
 return Z_pred
def gamma(self, X, k):
return stats.multivariate_normal.pdf(X, mean=self.mus[k], cov=self.sigmas[k])
 def _est_mu(self, k, X_train, Z_train, X_test, Z_test):
mu =(Z_train[:,k]@X_train + Z_test[:,k]@X_test ).T / \ 
(Z_train[:,k].sum() + Z_test[:,k].sum())
 return mu
def _est_sigma(self, k, X_train, Z_train, X_test, Z_test):
cmp1 =(X_train-self.mus[k] ).T@np.diag(Z_train[:,k])@(X_train-self.mus[k])
 cmp2 =(X_test-self.mus[k] ).T@np.diag(Z_test[:,k])@(X_test-self.mus[k])
 sigma =(cmp1+cmp2)/(Z_train[:,k].sum() + Z_test[:k].sum())
 return sigma
def _est_pi(self, k, X_train, Z_train, X_test, Z_test):
pi =(Z_train[:,k].sum() + Z_test[:,k].sum())/ \ 
(Z_train.sum() + Z_test.sum())
 return pi
lr_reduction = ReduceLROnPlateau(monitor="val_loss", patience=3, verbose=1, factor=0.5, min_lr=1e-5)
 history = model.fit(ds_train, epochs=30, validation_data=ds_eval, callbacks=[lr_reduction] )<predict_on_test>
class BaseClassifier(object):
def __init__(self):
self.preprocess = Pipeline([('vt', VarianceThreshold(threshold=2)) ,('scaler', StandardScaler())])
 def fit(self, X_train, y_train, X_test, cv_qda=2, cv_meta=2):
X_train_org = X_train
self.preprocess_tune(np.vstack([X_train, X_test]))
X_train = self.preprocess.transform(X_train)
 X_test = self.preprocess.transform(X_test)
 self.cgm = SSGaussianMixture(n_features=X_train.shape[1], n_categories=2)
 self.validation(X_train_org, y_train)
 self.cgm.fit(X_train, y_train, X_test)
 def predict(self, X):
X = self.preprocess.transform(X)
 return self.cgm.predict_proba(X)[:,1]
def preprocess_tune(self, X):
self.preprocess.fit(X)
 def validation(self, X, y):
X = self.preprocess.transform(X)
 kf = KFold(n_splits=3, shuffle=True)
 scores = []
for train_index, test_index in kf.split(X):
X_train, X_test = X[train_index], X[test_index]
y_train, y_test = y[train_index], y[test_index]
self.cgm.fit(X_train, y_train, X_test)
 y_pred = self.cgm.predict_proba(X_test)[:,1]
scores.append(roc_auc_score(y_test, y_pred))
self.score = np.array(scores ).mean()
print('validation score = ', self.score )
predictions = model.predict(ds_test )<save_to_csv>
df_train_sample = df_train[df_train['wheezy-copper-turtle-magic']==wcrms[3]]
X_train_sample = df_train_sample.drop(['id', 'target', 'wheezy-copper-turtle-magic'], axis=1 ).values
y_train_sample = df_train_sample['target'].values
df_test_sample = df_test[df_test['wheezy-copper-turtle-magic']==wcrms[3]]
X_test_sample = df_test_sample.drop(['id', 'wheezy-copper-turtle-magic'], axis=1 ).values
submission = pd.concat([pd.Series(range(1,28001), name="ImageId"), results], axis=1)
 submission.to_csv("cnn_submission.csv", index=False )<set_options>
bc = BaseClassifier()
bc.fit(X_train_sample, y_train_sample, X_test_sample )
%matplotlib inline
print(torch.__version__ )<load_from_csv>
class ConsolEstimator(object):
def __init__(self, ids):
self.clfs = {}
self.id_column = 'wheezy-copper-turtle-magic'
self.ids = ids
def predict(self, df_X):
y_pred = np.zeros(shape=(len(df_X)))
 for id in df_X[self.id_column].unique() :
id_rows =(df_X[self.id_column]==id)
 X = df_X.drop(['id', self.id_column], axis=1 ).values[id_rows]
y_pred[id_rows] = self.clfs[id].predict(X)
 return y_pred
def fit(self, df_train, df_test):
for i, id in enumerate(self.ids):
print(i, 'th training...')
 df_train_id = df_train[df_train[self.id_column]==id]
df_test_id = df_test[df_test[self.id_column]==id]
if len(df_train_id)==0 or len(df_test_id)==0:
continue
X_train = df_train_id.drop(['id', 'target', self.id_column], axis=1 ).values
y_train = df_train_id['target'].values
X_test = df_test_id.drop(['id', self.id_column], axis=1 ).values
self.clfs[id] = BaseClassifier()
self.clfs[id].fit(X_train, y_train, X_test)
 print('mean score = ', np.array([clf.score for clf in self.clfs.values() ] ).mean() )
random_seed = 42
torch.backends.cudnn.enabled = False
torch.manual_seed(random_seed)
 train_df = pd.read_csv(".. /input/digit-recognizer/train.csv")
 test_df = pd.read_csv(".. /input/digit-recognizer/test.csv")
 y = train_df["label"]
x = train_df.drop("label", axis = 1)
 X_train, X_valid, y_train, y_valid = train_test_split(x, y, test_size=0.15, shuffle=True)
 num_epoch = 25
batch_size_train = 32
batch_size_test = 32
learning_rate = 0.002
momentum = 0.9
log_interval = 100<train_model>
ce = ConsolEstimator(ids=wcrms)
 ce.fit(df_train, df_test )
class MNISTDataset(Dataset):
def __init__(self, data, target, train=True, transform=None):

 self.train = train
if self.train :
self.data = data
self.labels = np.asarray(target.iloc[:])
 else:
self.data = data
self.labels = None
self.height = 28
self.width = 28
self.transform = transform
def __getitem__(self, index):
img_as_np = np.asarray(self.data.iloc[index][0:] ).reshape(self.height, self.width ).astype('uint8')
 img_as_img = Image.fromarray(img_as_np)
 img_as_img = img_as_img.convert('L')
 img_as_tensor = img_as_img
if self.train:
single_image_label = self.labels[index]
else:
single_image_label = None
if self.transform is not None:
img_as_tensor = self.transform(img_as_img)
 if self.train:
return(img_as_tensor, single_image_label)
 else:
return img_as_tensor
def __len__(self):
return len(self.data.index )<normalization>
y_pred = ce.predict(df_test )
<normalization><EOS> 
df_submission = pd.concat([df_test['id'], pd.Series(y_pred, name='target')], axis=1)
 df_submission.to_csv('submission.csv', index=False )