Kaggle competition: Instant Gratification
<SOS> metric: AUC Kaggle data source: instant-gratification<set_options>
%matplotlib inline
warnings.filterwarnings("ignore")
 pd.set_option("display.max_columns", None)
 scaler_type = "no"
N_SPLITS = 10
%matplotlib inline
print(os.path.dirname(os.getcwd())+':', os.listdir(os.path.dirname(os.getcwd())));
print(os.getcwd() +':', os.listdir(os.getcwd())) ;<load_from_csv>
test = pd.read_csv(".. /input/test.csv")
 train = pd.read_csv(".. /input/train.csv")
 sample_submission = pd.read_csv(".. /input/sample_submission.csv")
 train.head()
train_df = pd.read_csv('.. /input/train.csv')
 test_df = pd.read_csv('.. /input/test.csv')
 train_df.info()
print('_'*40)
 test_df.info()<count_missing_values>
train['target'].value_counts()
print(train_df.isnull().sum())
 print('')
 print(test_df.isnull().sum() )<sort_values>
display(t_des.loc['mean'][t_des.loc['mean'] > 250] )
train_df[['Pclass', 'Survived']].groupby(['Pclass'], as_index=False ).mean().sort_values(by='Survived', ascending=False )<sort_values>
display(t_des.loc['std'][t_des.loc['std'] > 140] )
train_df[["Sex", "Survived"]].groupby(['Sex'], as_index=False ).mean().sort_values(by='Survived', ascending=False )<sort_values>
def scale(train,test):
traintest = pd.concat([train,test],axis=0,ignore_index=True ).reset_index(drop=True)
 cols = [c for c in train.columns if c not in ['id','target','wheezy-copper-turtle-magic']]
scaler = StandardScaler()
traintest[cols] = scaler.fit_transform(traintest[cols])
 train['wheezy-copper-turtle-magic'] = train['wheezy-copper-turtle-magic'].astype('category')
 train = traintest[:train.shape[0]].reset_index(drop=True)
 test = traintest[train.shape[0]:].reset_index(drop=True)
 return train,test
if scaler_type == "standard":
train,test = scale(train,test )
train_df[["SibSp", "Survived"]].groupby(['SibSp'], as_index=False ).mean().sort_values(by='Survived', ascending=False )<sort_values>
train_df[["Parch", "Survived"]].groupby(['Parch'], as_index=False ).mean().sort_values(by='Survived', ascending=False )<concatenate>
data_df = pd.concat(( train_df, test_df)).reset_index(drop=True)
 print('data_df.shape = ', data_df.shape )<data_type_conversions>
data_df['Sex'] = data_df['Sex'].map({'female': 1, 'male': 0} ).astype(int )<feature_engineering>
print(data_df.Embarked.isnull().values.sum() ,'missing values')
 freq_port = data_df.Embarked.dropna().mode() [0]
print(freq_port,'= most frequent');
data_df['Embarked'] = data_df['Embarked'].fillna(freq_port)
 data_df[['Embarked', 'Survived']].groupby(['Embarked'],
as_index=False ).mean().sort_values(by='Survived', ascending=False )<define_search_space>
print(data_df.Age.isnull().values.sum() , 'missing values')
 guess_ages = np.zeros(( 2,3)) ;
for i in range(0, 2):
for j in range(0, 3):
guess_df = data_df[(data_df['Sex'] == i)& 
(data_df['Pclass'] == j+1)]['Age'].dropna()
age_mean = guess_df.mean()
age_std = guess_df.std()
age_guess = np.random.normal(age_mean, age_std)
 guess_ages[i,j] = int(( age_guess/0.5 + 0.5)* 0.5)
 for i in range(0, 2):
for j in range(0, 3):
data_df.loc[(data_df.Age.isnull())&(data_df.Sex == i)& 
(data_df.Pclass == j+1),'Age'] = guess_ages[i,j]
data_df['Age'] = data_df['Age'].astype(int)
 print(data_df.Age.isnull().values.sum() , 'missing values')
<feature_engineering>
%%time
cols = [c for c in train.columns if c not in ['id', 'target']]
cols.remove('wheezy-copper-turtle-magic')
 oof = np.zeros(len(train))
preds = np.zeros(len(test))
for i in range(512):
train2 = train[train['wheezy-copper-turtle-magic']==i]
test2 = test[test['wheezy-copper-turtle-magic']==i]
idx1 = train2.index; idx2 = test2.index
train2.reset_index(drop=True,inplace=True)
 sel = VarianceThreshold(threshold=1.5 ).fit(train2[cols])
 train3 = sel.transform(train2[cols])
 test3 = sel.transform(test2[cols])
 skf = StratifiedKFold(n_splits=11, random_state=42, shuffle=True)
 for train_index, test_index in skf.split(train3, train2['target']):
clf = QuadraticDiscriminantAnalysis(reg_param=0.5)
 clf.fit(train3[train_index,:],train2.loc[train_index]['target'])
 oof[idx1[test_index]] = clf.predict_proba(train3[test_index,:])[:,1]
preds[idx2] += clf.predict_proba(test3)[:,1] / skf.n_splits
auc = roc_auc_score(train['target'],oof)
 print('QDA scores CV =',round(auc,5))
data_df.loc[data_df['Age'] <= 16, 'AgeBin'] = 0
data_df.loc[(data_df['Age'] > 16)&(data_df['Age'] <= 32), 'AgeBin'] = 1
data_df.loc[(data_df['Age'] > 32)&(data_df['Age'] <= 48), 'AgeBin'] = 2
data_df.loc[(data_df['Age'] > 48)&(data_df['Age'] <= 64), 'AgeBin'] = 3
data_df.loc[data_df['Age'] > 64, 'AgeBin']
data_df = data_df.drop(['AgeBand'], axis=1)
 print(data_df.AgeBin.isnull().values.sum() , 'missing values')
<feature_engineering>
test['target'] = preds
oof = np.zeros(len(train))
preds = np.zeros(len(test))
for k in range(512):
train2 = train[train['wheezy-copper-turtle-magic']==k]
train2p = train2.copy() ; idx1 = train2.index
test2 = test[test['wheezy-copper-turtle-magic']==k]
test2p = test2[(test2['target']<=0.01)|(test2['target']>=0.99)].copy()
test2p.loc[ test2p['target']>=0.5, 'target' ] = 1
test2p.loc[ test2p['target']<0.5, 'target' ] = 0
train2p = pd.concat([train2p,test2p],axis=0)
 train2p.reset_index(drop=True,inplace=True)
 sel = VarianceThreshold(threshold=1.5 ).fit(train2p[cols])
 train3p = sel.transform(train2p[cols])
 train3 = sel.transform(train2[cols])
 test3 = sel.transform(test2[cols])
 skf = StratifiedKFold(n_splits=11, random_state=42, shuffle=True)
 for train_index, test_index in skf.split(train3p, train2p['target']):
test_index3 = test_index[ test_index<len(train3)]
clf = QuadraticDiscriminantAnalysis(reg_param=0.5)
 clf.fit(train3p[train_index,:],train2p.loc[train_index]['target'])
 oof[idx1[test_index3]] = clf.predict_proba(train3[test_index3,:])[:,1]
preds[test2.index] += clf.predict_proba(test3)[:,1] / skf.n_splits
auc = roc_auc_score(train['target'],oof)
 print('Pseudo Labeled QDA scores CV =',round(auc,5))
<feature_engineering><EOS> 
sub = pd.read_csv('.. /input/sample_submission.csv')
 sub['target'] = preds
sub.to_csv('submission.csv',index=False)
 plt.hist(preds,bins=100)
 plt.title('Final Test.csv predictions')
 plt.show()