Kaggle competition: Instant Gratification
<SOS> metric: AUC Kaggle data source: instant-gratification<data_type_conversions>
%matplotlib inline
np.random.seed(1111)
 warnings.filterwarnings('ignore' )
dataset["Embarked"] = dataset["Embarked"].fillna("S" )<categorify>
train = pd.read_csv('.. /input/instant-gratification/train.csv')
 test = pd.read_csv('.. /input/instant-gratification/test.csv')
 cols = [c for c in train.columns if c not in ['id', 'target', 'wheezy-copper-turtle-magic']]
dataset["Sex"] = dataset["Sex"].map({"male": 0, "female":1} )<feature_engineering>
gm_list = pickle.load(open('.. /input/models-v5/gm_models_v5.pkl', 'rb'))
index_NaN_age = list(dataset["Age"][dataset["Age"].isnull() ].index)
 for i in index_NaN_age :
age_med = dataset["Age"].median()
age_pred = dataset["Age"][(( dataset['SibSp'] == dataset.iloc[i]["SibSp"])&(dataset['Parch'] == dataset.iloc[i]["Parch"])&(dataset['Pclass'] == dataset.iloc[i]["Pclass"])) ].median()
if not np.isnan(age_pred):
dataset['Age'].iloc[i] = age_pred
else :
dataset['Age'].iloc[i] = age_med<feature_engineering>
class MyGM(GaussianMixture):
def __init__(self, n_components=1, covariance_type='full', tol=1e-3,
reg_covar=1e-6, max_iter=100, n_init=1, init_params='kmeans',
weights_init=None, means_init=None, precisions_init=None,
random_state=None, warm_start=False,
verbose=0, verbose_interval=10, init_clusters=None, y=None):
super().__init__(
 n_components=n_components, tol=tol, reg_covar=reg_covar,
max_iter=max_iter, n_init=n_init, init_params=init_params,
random_state=random_state, warm_start=warm_start,
verbose=verbose, verbose_interval=verbose_interval)
 self.init_clusters_ = np.asarray(init_clusters ).astype('int')
 self.y = y
def _initialize_parameters(self, X, random_state):

 n_samples, _ = X.shape
if self.init_params == 'kmeans':
resp = np.zeros(( n_samples, self.n_components))
label = cluster.KMeans(n_clusters=self.n_components, n_init=1,
random_state=random_state ).fit(X ).labels_
resp[np.arange(n_samples), label] = 1
elif self.init_params == 'random':
resp = random_state.rand(n_samples, self.n_components)
 resp /= resp.sum(axis=1)[:, np.newaxis]
elif self.init_params == 'clusters':
resp = np.zeros(( n_samples, self.n_components))
resp[np.arange(self.init_clusters_.shape[0]), self.init_clusters_] = 1
else:
raise ValueError("Unimplemented initialization method '%s'"
% self.init_params)
 self._initialize(X, resp)
 def estimate_log_ratio(self, X):
weighted_log_prob = self._estimate_weighted_log_prob(X)
 return logsumexp(weighted_log_prob[:, 1::2], axis=1)- logsumexp(weighted_log_prob[:, 0::2], axis=1)
dataset_title = [i.split(",")[1].split(".")[0].strip() for i in dataset["Name"]]
dataset["Title"] = pd.Series(dataset_title)
 dataset["Title"].head()<categorify>
class MyBGM(BayesianGaussianMixture):
def __init__(self, n_components=1, covariance_type='full', tol=1e-3,
reg_covar=1e-6, max_iter=100, n_init=1, init_params='kmeans',
weight_concentration_prior_type='dirichlet_process',
weight_concentration_prior=None,
mean_precision_prior=None, mean_prior=None,
degrees_of_freedom_prior=None, covariance_prior=None,
random_state=None, warm_start=False, verbose=0,
verbose_interval=10, init_clusters=None):
super().__init__(
 n_components=n_components, covariance_type=covariance_type, tol=tol,
reg_covar=reg_covar, max_iter=max_iter, n_init=n_init, init_params=init_params,
weight_concentration_prior_type=weight_concentration_prior_type,
weight_concentration_prior=weight_concentration_prior,
mean_precision_prior=mean_precision_prior, mean_prior=mean_prior,
degrees_of_freedom_prior=degrees_of_freedom_prior, covariance_prior=covariance_prior,
random_state=random_state, warm_start=warm_start, verbose=verbose,
verbose_interval=verbose_interval)
 self.init_clusters_ = np.asarray(init_clusters ).astype('int')
 def _initialize_parameters(self, X, random_state):

 n_samples, _ = X.shape
if self.init_params == 'kmeans':
resp = np.zeros(( n_samples, self.n_components))
label = cluster.KMeans(n_clusters=self.n_components, n_init=1,
random_state=random_state ).fit(X ).labels_
resp[np.arange(n_samples), label] = 1
elif self.init_params == 'random':
resp = random_state.rand(n_samples, self.n_components)
 resp /= resp.sum(axis=1)[:, np.newaxis]
elif self.init_params == 'clusters':
resp = np.zeros(( n_samples, self.n_components))
resp[np.arange(self.init_clusters_.shape[0]), self.init_clusters_] = 1
elif self.init_params == 'proba':
resp = self.init_proba_.copy()
resp[np.arange(self.init_clusters_.shape[0]), self.init_clusters_] = 1
resp /= resp.sum(axis=1)[:, np.newaxis]
else:
raise ValueError("Unimplemented initialization method '%s'"
% self.init_params)
 self._initialize(X, resp )
dataset["Title"] = dataset["Title"].replace(['Lady', 'the Countess','Countess','Capt', 'Col','Don', 'Dr', 'Major', 'Rev', 'Sir', 'Jonkheer', 'Dona'], 'Rare')
 dataset["Title"] = dataset["Title"].map({"Master":0, "Miss":1, "Ms" : 1 , "Mme":1, "Mlle":1, "Mrs":1, "Mr":2, "Rare":3})
 dataset["Title"] = dataset["Title"].astype(int )<drop_column>
roc_auc_score(train["target"], oof_preds )
<feature_engineering><EOS> 
sub = pd.read_csv('.. /input/instant-gratification/sample_submission.csv')
 sub['target'] = test_preds
sub.to_csv('submission.csv',index=False )