Kaggle competition: Santander Customer Transaction Prediction
<count_values><EOS> 
%matplotlib inline
<SOS> metric: AUC Kaggle data source: santander-customer-transaction-prediction<groupby>
%matplotlib inline
train['Survived'].groupby(train['SibSp'] ).mean()<count_values>
plt.style.use('bmh')
 plt.rcParams['figure.figsize'] =(20, 10)
 title_config = {'fontsize': 20, 'y': 1.05}
train['SibSp'].value_counts()<groupby>
train = pd.read_csv('.. /input/train.csv')
 test = pd.read_csv('.. /input/test.csv' )
train['Survived'].groupby(train['Parch'] ).mean()<count_values>
train = pd.read_csv('.. /input/train.csv')
 test = pd.read_csv('.. /input/test.csv' )
train['Parch'].value_counts()<feature_engineering>
X_train = train.iloc[:, 2:].values.astype('float64')
 y_train = train['target'].values
X_test = test.iloc[:, 1:].values.astype('float64' )
train['Ticket_Len'] = train['Ticket'].apply(lambda x: len(x))<count_values>
class GaussianMixtureNB(BaseEstimator, ClassifierMixin):
def __init__(self, n_components=1, reg_covar=1e-06):
self.n_components = n_components
self.reg_covar = reg_covar
def fit(self, X, y):
self.log_prior_ = np.log(np.bincount(y)/ len(y))
shape =(len(self.log_prior_), X.shape[1])
 self.log_pdf_ = [[GaussianMixture(n_components=self.n_components,
reg_covar=self.reg_covar)
.fit(X[y == i, j:j + 1])
.score_samples for j in range(shape[1])]
for i in range(shape[0])]
def predict_proba(self, X):
shape =(len(self.log_prior_), X.shape[1], X.shape[0])
 log_likelihood = np.sum([[self.log_pdf_[i][j](X[:, j:j + 1])
 for j in range(shape[1])]
for i in range(shape[0])], axis=1 ).T
log_joint = self.log_prior_ + log_likelihood
return np.exp(log_joint - logsumexp(log_joint, axis=1, keepdims=True))
def predict(self, X):
return self.predict_proba(X ).argmax(axis=1 )
train['Ticket_Len'].value_counts()<feature_engineering>
i_train, i_valid = next(StratifiedShuffleSplit(n_splits=1 ).split(X_train, y_train))
train['Ticket_Lett'] = train['Ticket'].apply(lambda x: str(x)[0] )<count_values>
pipeline = make_pipeline(StandardScaler() ,
GaussianMixtureNB(n_components=10, reg_covar=0.03))
pipeline.fit(X_train[i_train], y_train[i_train])
 print('Training AUC is {}.' 
.format(roc_auc_score(y_train[i_train],
pipeline.predict_proba(X_train[i_train])[:, 1])))
 print('Validation AUC is {}.' 
.format(roc_auc_score(y_train[i_valid],
pipeline.predict_proba(X_train[i_valid])[:, 1])) )
train['Ticket_Lett'].value_counts()<groupby>
pipeline.fit(X_train, y_train )
train.groupby(['Ticket_Lett'])['Survived'].mean()<count_values>
submission = pd.read_csv('.. /input/sample_submission.csv')
 submission['target'] = pipeline.predict_proba(X_test)[:, 1]
submission.to_csv('submission.csv', index=False )