Kaggle competition: Titanic - Machine Learning from Disaster
dfsubmissao['nota_mat']=rf.predict(dfsubmissao[feats])
 dfsubmissao[['codigo_mun','nota_mat']].to_csv('dfresultado.csv', index=False)
<train_model>
%matplotlib inline
plt.style.use('seaborn-whitegrid')
 warnings.filterwarnings('ignore' )
<import_modules>
test = pd.read_csv('.. /input/titanic/test.csv')
 train = pd.read_csv('.. /input/titanic/train.csv' )
np.random.seed(10)
<drop_column>
ntrain = train.shape[0]
ntest = test.shape[0]
y_train = train['Survived'].values
passId = test['PassengerId']
data = pd.concat(( train, test))
print("data size is: {}".format(data.shape))
def trim_all_columns(df):
trim_strings = lambda x: x.strip() if isinstance(x, str)else x
return df.applymap(trim_strings )<define_variables>
train['Survived'].value_counts()
header_list = ["AAGE", "ACLSWKR", "ADTIND", "ADTOCC", "AHGA","AHRSPAY","AHRSCOL",
"AMARITL","AMJIND","AMJOCC","ARACE","AREORGN","ASEX","AUNMEM",
"AUNTYPE","AWKSTAT","CAPGAIN","CAPLOSS","DIVVAL", "FILESTAT",
"GRINREG","GRINST","HHDFMX","HHDREL", "MARSUPWT","MIGMTR1",
"MIGMTR3","MIGMTR4","MIGSAME", "MIGSUN","NOEMP","PARENT",
"PEFNTVTY","PEMNTVTY","PENATVTY","PRCITSHP","SEOTR",
"VETQVA","VETYN","WKSWORK","YEAR","INCCAT"] 
<load_from_csv>
data.isnull().sum()
d_train=pd.read_csv('/kaggle/input/ml-challenge-week6/census-income.data',index_col=False,names=header_list)
 d_test=pd.read_csv('/kaggle/input/ml-challenge-week6/census-income.test',index_col=False,names=header_list )<drop_column>
data.Name.value_counts()
d_test=trim_all_columns(d_test)
 d_train=trim_all_columns(d_train )<categorify>
temp = data.copy()
temp['Initial'] = 0
temp['Initial'] = data.Name.str.extract('([A-Za-z]+)\.')
label = {'50000+.':1, '- 50000.':0}
d_train['INCCAT']=d_train['INCCAT'].map(label)
 d_test['INCCAT']=d_test['INCCAT'].map(label)
 d_test['AREORGN']=d_test['AREORGN'].fillna('NA')
 d_test=d_test.fillna('?')
<create_dataframe>
temp['Initial'].value_counts()
<feature_engineering>
def survpct(col):
return temp.groupby(col)['Survived'].mean()
survpct('Initial' )
d_train.loc[(d_train['AWKSTAT']=='Children or Armed Forces')&(d_train['AAGE']<=18),'AWKSTAT']='Children'
d_test.loc[(d_test['AWKSTAT']=='Children or Armed Forces')&(d_test['AAGE']<=18),'AWKSTAT']='Children'
numb=d_train.groupby(['AWKSTAT','AMJIND'])['INCCAT'].agg({'Nr':'sum','Tot':'count'})
 numb['Ratio']=numb['Nr']/numb['Tot']
numb=numb.reset_index()
print("train=", d_train.shape," test=", d_test.shape)
<feature_engineering>
temp.loc[temp['Initial'] == 'Dona']
pd.set_option('display.max_rows', None)
 numb=d_train.groupby(['AAGE'])['INCCAT'].agg({'Nr':'sum','Tot':'count'})
 numb['Ratio']=numb['Nr']/numb['Tot']
numb=numb.reset_index() 
<feature_engineering>
temp.loc[temp['Initial'] == 'Dona', 'Initial'] = 'Mrs'
d_train.loc[d_train['ACLSWKR']=="Never worked",['INCCAT']]=0
d_train.loc[d_train['ACLSWKR']=="Without pay",['INCCAT']]=0<categorify>
temp = temp.reset_index(drop=True)
 temp['Age'] = temp.groupby('Initial')['Age'].apply(lambda x: x.fillna(x.mean()))
temp[31:50]
numb=d_train.groupby(['ACLSWKR'])['INCCAT'].agg({'Nr':'sum','Tot':'count'})
 numb['Ratio']=numb['Nr']/numb['Tot']
a=numb.copy()
dummy_le=LabelEncoder()
a=a.reset_index()
a.iloc[:, 0]=dummy_le.fit_transform(numb.iloc[:, 0])
 numb=numb.sort_values(numb.columns[2] ).reset_index()
le_ACLSWKR = LabelEncoder()
le_ACLSWKR.classes_=numb.iloc[:, 0]
numb.iloc[:, 0] = le_ACLSWKR.transform(numb.iloc[:, 0])
 plt.figure(1)
 plt.subplot(211)
 plt.plot(a['ACLSWKR'],a['Ratio'])
 plt.subplot(212)
 plt.plot(numb['ACLSWKR'],numb['Ratio'])
<categorify>
temp['Initial'].replace(['Capt', 'Col', 'Countess', 'Don', 'Dona' , 'Dr', 'Jonkheer', 'Lady', 'Major', 'Master', 'Miss' ,'Mlle', 'Mme', 'Mr', 'Mrs', 'Ms', 'Rev', 'Sir'], ['Sacrificed', 'Respected', 'Nobles', 'Mr', 'Mrs', 'Respected', 'Mr', 'Nobles', 'Respected', 'Kids', 'Miss', 'Nobles', 'Nobles', 'Mr', 'Mrs', 'Nobles', 'Sacrificed', 'Nobles'],inplace=True)
 temp['Initial'].replace(['Kids', 'Miss', 'Mr', 'Mrs', 'Nobles', 'Respected', 'Sacrificed'], [4, 4, 2, 5, 6, 3, 1], inplace=True )
d_train.loc[:, 'ACLSWKR'] = le_ACLSWKR.transform(d_train.loc[:, 'ACLSWKR'])
 d_test.loc[:, 'ACLSWKR'] = le_ACLSWKR.transform(d_test.loc[:, 'ACLSWKR'] )<feature_engineering>
temp['Age_Range'] = pd.qcut(temp['Age'], 10)
 survpct('Age_Range')
numb=d_train.groupby(['AHGA'])['INCCAT'].agg({'Nr':'sum','Tot':'count'})
 numb['Ratio']=numb['Nr']/numb['Tot']
numb.sort_values(numb.columns[2] ).reset_index()<categorify>
temp['Agroup'] = 0
temp.loc[temp['Age'] < 1.0, 'Agroup'] = 1
temp.loc[(temp['Age'] >= 1.0)&(temp['Age'] <= 3.0), 'Agroup'] = 2
temp.loc[(temp['Age'] > 3.0)&(temp['Age'] < 11.0), 'Agroup'] = 7
temp.loc[(temp['Age'] >= 11.0)&(temp['Age'] < 15.0), 'Agroup'] = 13
temp.loc[(temp['Age'] >= 15.0)&(temp['Age'] < 18.0), 'Agroup'] = 16
temp.loc[(temp['Age'] >= 18.0)&(temp['Age'] <= 20.0), 'Agroup'] = 18
temp.loc[(temp['Age'] > 20.0)&(temp['Age'] <= 22.0), 'Agroup'] = 21
temp.loc[(temp['Age'] > 22.0)&(temp['Age'] <= 26.0), 'Agroup'] = 24
temp.loc[(temp['Age'] > 26.0)&(temp['Age'] <= 30.0), 'Agroup'] = 28
temp.loc[(temp['Age'] > 30.0)&(temp['Age'] <= 32.0), 'Agroup'] = 31
temp.loc[(temp['Age'] > 32.0)&(temp['Age'] <= 34.0), 'Agroup'] = 33
temp.loc[(temp['Age'] > 34.0)&(temp['Age'] <= 38.0), 'Agroup'] = 36
temp.loc[(temp['Age'] > 38.0)&(temp['Age'] <= 52.0), 'Agroup'] = 45
temp.loc[(temp['Age'] > 52.0)&(temp['Age'] <= 75.0), 'Agroup'] = 60
temp.loc[temp['Age'] > 75.0, 'Agroup'] = 78
temp.head()
numb=d_train.groupby(['AHGA'])['INCCAT'].agg({'Nr':'sum','Tot':'count'})
 numb['Ratio']=numb['Nr']/numb['Tot']
numb=numb.sort_values(numb.columns[2])
 numb=numb.reset_index()
le_AHGA = LabelEncoder()
le_AHGA.classes_=numb.iloc[:, 0] 
<categorify>
temp.loc[(temp['Sex'] == 'male'), 'Sex'] = 1
temp.loc[(temp['Sex'] == 'female'), 'Sex'] = 2
temp.loc[(temp['Age'] < 1), 'Sex'] = 3
survpct('Sex' )
d_train.loc[:, 'AHGA'] = le_AHGA.transform(d_train.loc[:, 'AHGA'])
 d_test.loc[:, 'AHGA'] = le_AHGA.transform(d_test.loc[:, 'AHGA'] )<categorify>
temp.loc[(temp['SibSp'] == 0)&(temp['Parch'] == 0), 'Alone'] = 1
temp['Family'] = temp['Parch'] + temp['SibSp'] + 1
temp.head(n=10 )
numb=d_train.groupby(['AHRSCOL'])['INCCAT'].agg({'Nr':'sum','Tot':'count'})
 numb['Ratio']=numb['Nr']/numb['Tot']
numb=numb.sort_values(numb.columns[2])
 numb=numb.reset_index()
le_AHRSCOL = LabelEncoder()
le_AHRSCOL.classes_=numb.iloc[:, 0]<categorify>
bag('Parch', 'Survived', 'Survived per Parch', 'Parch Survived vs Not Survived' )
d_train.loc[:, 'AHRSCOL'] = le_AHRSCOL.transform(d_train.loc[:, 'AHRSCOL'])
 d_test.loc[:, 'AHRSCOL'] = le_AHRSCOL.transform(d_test.loc[:, 'AHRSCOL'] )<categorify>
temp.loc[(temp.Embarked.isnull())]
numb=d_train.groupby(['AMARITL'])['INCCAT'].agg({'Nr':'sum','Tot':'count'})
 numb['Ratio']=numb['Nr']/numb['Tot']
numb=numb.sort_values(numb.columns[2])
 numb=numb.reset_index()
le_AMARITL = LabelEncoder()
le_AMARITL.classes_=numb.iloc[:, 0]
d_train.loc[:, 'AMARITL'] = le_AMARITL.transform(d_train.loc[:, 'AMARITL'])
 d_test.loc[:, 'AMARITL'] = le_AMARITL.transform(d_test.loc[:, 'AMARITL'] )<feature_engineering>
temp.loc[(temp.Ticket == '113572')]
numb=d_train.groupby(['AMJIND','ADTIND'])['INCCAT'].agg({'Nr':'sum','Tot':'count'})
 numb['Ratio']=numb['Nr']/numb['Tot']
numb=numb.reset_index()
numb.sort_values(numb.columns[0])
 nu=d_train.groupby(['AMJIND'])['INCCAT'].agg({'Nr':'sum','Tot':'count'})
 nu['Ratio']=nu['Nr']/nu['Tot']
nu=nu.reset_index()
for index, row in numb.iterrows() :
if numb.loc[numb['ADTIND']==row['ADTIND'],'Tot'].values[0]<300:
numb.loc[numb['ADTIND']==row['ADTIND'],'Ratio']=nu.loc[nu['AMJIND']==row['AMJIND']]['Ratio'].values[0]
numb.loc[numb['AMJIND']=='Armed Forces','Ratio']=nu.loc[nu['AMJIND']=='Manufacturing-durable goods']['Ratio'].values[0]
for index, row in numb.iterrows() :
d_train.loc[d_train['ADTIND']==row['ADTIND'],'AMJIND']=row['Ratio']
d_test.loc[d_test['ADTIND']==row['ADTIND'],'AMJIND']=row['Ratio'] 
<feature_engineering>
temp.sort_values(['Ticket'], ascending=True)[55:70]
pd.set_option('display.max_rows', 50)
 numb=d_train.groupby(['AMJOCC','ADTOCC'])['INCCAT'].agg({'Nr':'sum','Tot':'count'})
 numb['Ratio']=numb['Nr']/numb['Tot']
numb=numb.reset_index()
numb.sort_values(numb.columns[0])
 nu=d_train.groupby(['AMJOCC'])['INCCAT'].agg({'Nr':'sum','Tot':'count'})
 nu['Ratio']=nu['Nr']/nu['Tot']
nu=nu.reset_index()
for index, row in numb.iterrows() :
if numb.loc[numb['ADTOCC']==row['ADTOCC'],'Tot'].values[0]<300:
numb.loc[numb['ADTOCC']==row['ADTOCC'],'Ratio']=nu.loc[nu['AMJOCC']==row['AMJOCC']]['Ratio'].values[0]
numb.loc[numb['AMJOCC']=='Armed Forces','Ratio']=nu.loc[nu['AMJOCC']=='Technicians and related support']['Ratio'].values[0]
numb
for index, row in numb.iterrows() :
d_train.loc[d_train['ADTOCC']==row['ADTOCC'],'AMJOCC']=row['Ratio']
d_test.loc[d_test['ADTOCC']==row['ADTOCC'],'AMJOCC']=row['Ratio'] 
<categorify>
temp.loc[(temp.Embarked.isnull()), 'Embarked'] = 'S'
temp.loc[(temp.Embarked.isnull())]
numb=d_train.groupby(['ARACE'])['INCCAT'].agg({'Nr':'sum','Tot':'count'})
 numb['Ratio']=numb['Nr']/numb['Tot']
numb=numb.sort_values(numb.columns[2])
 numb=numb.reset_index()
le_ARACE = LabelEncoder()
le_ARACE.classes_=numb.iloc[:, 0]
d_train.loc[:, 'ARACE'] = le_ARACE.transform(d_train.loc[:, 'ARACE'])
 d_test.loc[:, 'ARACE'] = le_ARACE.transform(d_test.loc[:, 'ARACE'] )<categorify>
temp['Embarked'] = temp['Embarked'].factorize() [0]
temp[11:20]
numb=d_train.groupby(['AREORGN'])['INCCAT'].agg({'Nr':'sum','Tot':'count'})
 numb['Ratio']=numb['Nr']/numb['Tot']
numb=numb.sort_values(numb.columns[2])
 numb=numb.reset_index()
le_AREORGN = LabelEncoder()
le_AREORGN.classes_=numb.iloc[:, 0]
d_train.loc[:, 'AREORGN'] = le_AREORGN.transform(d_train.loc[:, 'AREORGN'])
 d_test.loc[:, 'AREORGN'] = le_AREORGN.transform(d_test.loc[:, 'AREORGN'] )<categorify>
temp['Priority'] = 0
temp.loc[(temp['Initial'] == 6), 'Priority'] = 1
temp.loc[(temp['Pclass'] == 1)&(temp['Sex'] == 2), 'Priority'] = 2
temp.loc[(temp['Age'] < 1), 'Priority'] = 3
temp.loc[(temp['Pclass'] == 1)&(temp['Age'] <= 17), 'Priority'] = 4
temp.loc[(temp['Pclass'] == 2)&(temp['Age'] <= 17), 'Priority'] = 5
numb=d_train.groupby(['ASEX'])['INCCAT'].agg({'Nr':'sum','Tot':'count'})
 numb['Ratio']=numb['Nr']/numb['Tot']
numb=numb.sort_values(numb.columns[2])
 numb=numb.reset_index()
le_ASEX = LabelEncoder()
le_ASEX.classes_=numb.iloc[:, 0]
d_train.loc[:, 'ASEX'] = le_ASEX.transform(d_train.loc[:, 'ASEX'])
 d_test.loc[:, 'ASEX'] = le_ASEX.transform(d_test.loc[:, 'ASEX'] )<categorify>
temp.Priority.value_counts()
numb=d_train.groupby(['AUNMEM','AUNTYPE'])['INCCAT'].agg({'Nr':'sum','Tot':'count'})
 numb['Ratio']=numb['Nr']/numb['Tot']
numb=numb.sort_values(numb.columns[2])
 numb=numb.reset_index()
le_AUN = LabelEncoder()
le_AUN.classes_=numb.iloc[:, 0] +" "+ numb.iloc[:, 1]
d_train.loc[:,'AUNMEM'] = le_AUN.transform(( d_train.loc[:, 'AUNMEM'] + " " + d_train.loc[:,'AUNTYPE']))
d_test.loc[:,'AUNMEM'] = le_AUN.transform(( d_test.loc[:, 'AUNMEM'] + " " + d_test.loc[:,'AUNTYPE']))
d_train=d_train.drop(['AUNTYPE'],axis=1)
 d_test=d_test.drop(['AUNTYPE'],axis=1)
<categorify>
temp['F1'] = temp['Priority']
temp['F2'] = temp['Initial']
temp['F3'] = temp['NumName']
temp['F4'] = temp['Family']
temp['F5'] = temp['Embarked']
temp['F6'] = temp['Sex']
temp['F7'] = temp['Pclass']
numb=d_train.groupby(['AWKSTAT'])['INCCAT'].agg({'Nr':'sum','Tot':'count'})
 numb['Ratio']=numb['Nr']/numb['Tot']
numb=numb.sort_values(numb.columns[2])
 numb=numb.reset_index()
le_AWKSTAT = LabelEncoder()
le_AWKSTAT.classes_=numb.iloc[:, 0]
d_train.loc[:, 'AWKSTAT'] = le_AWKSTAT.transform(d_train.loc[:, 'AWKSTAT'])
 d_test.loc[:, 'AWKSTAT'] = le_AWKSTAT.transform(d_test.loc[:, 'AWKSTAT'] )<feature_engineering>
dfl = pd.DataFrame()
good_columns = ['F1', 'F2', 'F3', 'F4', 'F5', 'F6', 'F7']
dfl[good_columns] = temp[good_columns]
dfh = dfl.copy()
dfl_enc = dfl.apply(LabelEncoder().fit_transform)
 dfl_enc.head()
numb=d_train.groupby(['CAPGAIN'])['INCCAT'].agg({'Nr':'sum','Tot':'count'})
 numb['Ratio']=numb['Nr']/numb['Tot']
numb=numb.reset_index()
numb=numb.sort_values(numb.columns[3])
 d_train.loc[d_train['CAPGAIN']<=3000,'CAPGAIN']=0
d_test.loc[d_test['CAPGAIN']<=3000,'CAPGAIN']=0
d_train.loc[(d_train['CAPGAIN']>3000)&(d_train['CAPGAIN']<10000),'CAPGAIN']=1
d_test.loc[(d_test['CAPGAIN']>3000)&(d_test['CAPGAIN']<10000),'CAPGAIN']=1
d_train.loc[d_train['CAPGAIN']>=10000,'CAPGAIN']=2
d_test.loc[d_test['CAPGAIN']>=10000,'CAPGAIN']=2 
<feature_engineering>
one_hot_cols = dfh.columns.tolist()
dfh_enc = pd.get_dummies(dfh, columns=one_hot_cols)
 dfh_enc.head()
numb=d_train.groupby(['DIVVAL'])['INCCAT'].agg({'Nr':'sum','Tot':'count'})
 numb['Ratio']=numb['Nr']/numb['Tot']
numb=numb.reset_index()
numb=numb.sort_values(numb.columns[0])
 d_train['DIVCAT']=0
d_test['DIVCAT']=0
d_train.loc[d_train['DIVVAL']<700,'DIVCAT']=0
d_test.loc[d_test['DIVVAL']<700,'DIVCAT']=0
d_train.loc[(d_train['DIVVAL']>=700)&(d_train['DIVVAL']<=11000),'DIVCAT']=1
d_test.loc[(d_test['DIVVAL']>700)&(d_test['DIVVAL']<=11000),'DIVCAT']=1
d_train.loc[(d_train['DIVVAL']>11000)&(d_train['DIVVAL']<=28000),'DIVCAT']=2
d_test.loc[(d_test['DIVVAL']>11000)&(d_test['DIVVAL']<=28000),'DIVCAT']=2
d_train.loc[d_train['DIVVAL']>28000,'DIVCAT']=3
d_test.loc[d_test['DIVVAL']>28000,'DIVCAT']=3
d_train=d_train.drop(['DIVVAL'],axis=1)
 d_test=d_test.drop(['DIVVAL'],axis=1 )<categorify>
train = dfh_enc[:ntrain]
test = dfh_enc[ntrain:]
numb=d_train.groupby(['FILESTAT'])['INCCAT'].agg({'Nr':'sum','Tot':'count'})
 numb['Ratio']=numb['Nr']/numb['Tot']
numb=numb.sort_values(numb.columns[2])
 numb=numb.reset_index()
le_FILESTAT = LabelEncoder()
le_FILESTAT.classes_=numb.iloc[:, 0]
d_train.loc[:, 'FILESTAT'] = le_FILESTAT.transform(d_train.loc[:, 'FILESTAT'])
 d_test.loc[:, 'FILESTAT'] = le_FILESTAT.transform(d_test.loc[:, 'FILESTAT'])
<categorify>
X_test = test
X_train = train
numb=d_train.groupby(['GRINREG'])['INCCAT'].agg({'Nr':'sum','Tot':'count'})
 numb['Ratio']=numb['Nr']/numb['Tot']
numb=numb.sort_values(numb.columns[2])
 numb=numb.reset_index()
numb
d_train.loc[d_train['GRINREG']=='Not in universe','GRINREG']=1
d_test.loc[d_test['GRINREG']=='Not in universe','GRINREG']=1
d_train.loc[d_train['GRINREG']!=1,'GRINREG']=0
d_test.loc[d_test['GRINREG']!=1,'GRINREG']=0
d_train=d_train.drop(['GRINST'],axis=1)
 d_test=d_test.drop(['GRINST'],axis=1)
<feature_engineering>
scaler = StandardScaler()
X_train = scaler.fit_transform(X_train)
 X_test = scaler.transform(X_test )
d_train.loc[d_train['HHDREL'].str.contains('Spouse of householder'),'HHDFMX']='Spouse of householder'
d_test.loc[d_test['HHDREL'].str.contains('Spouse of householder'),'HHDFMX']='Spouse of householder'
d_train.loc[(d_train['HHDREL'].str.contains('Householder')) & 
(d_train['HHDFMX'].str.contains('In group quarters')) ,'HHDFMX']='Nonfamily householder'
d_test.loc[(d_test['HHDREL'].str.contains('Householder')) & 
(d_test['HHDFMX'].str.contains('In group quarters')) ,'HHDFMX']='Nonfamily householder'
d_train.loc[d_train['HHDFMX'].str.contains('In group quarters'),'HHDFMX']='Other relative of householder'
d_test.loc[d_test['HHDFMX'].str.contains('In group quarters'),'HHDFMX']='Other relative of householder'
d_train.loc[d_train['HHDFMX'].str.contains('Other relative of householder'),'HHDREL']='Other relative of householder'
d_test.loc[d_test['HHDFMX'].str.contains('Other relative of householder'),'HHDREL']='Other relative of householder'
d_train.loc[d_train['HHDFMX'].str.contains('Grandchild'),'HHDFMX']='Child 18+'
d_test.loc[d_test['HHDFMX'].str.contains('Grandchild'),'HHDFMX']='Child 18+'
d_train.loc[d_train['HHDFMX'].str.contains('Child'),'HHDREL']='Child 18+'
d_test.loc[d_test['HHDFMX'].str.contains('Child'),'HHDREL']='Child 18+'
d_train.loc[d_train['HHDFMX'].str.contains('Child'),'HHDFMX']='Child 18+'
d_test.loc[d_test['HHDFMX'].str.contains('Child'),'HHDFMX']='Child 18+'
numb=d_train.groupby(['HHDFMX'])['INCCAT'].agg({'Nr':'sum','Tot':'count'})
 numb['Ratio']=numb['Nr']/numb['Tot']
numb=numb.reset_index()
numb=numb.sort_values(numb.columns[0])
 le_HHDFMX = LabelEncoder()
le_HHDFMX.classes_=numb.iloc[:, 0]
d_train.loc[:, 'HHDFMX'] = le_HHDFMX.transform(d_train.loc[:, 'HHDFMX'])
 d_test.loc[:, 'HHDFMX'] = le_HHDFMX.transform(d_test.loc[:, 'HHDFMX'])
 numb=d_train.groupby(['HHDREL'],)['INCCAT'].agg({'Nr':'sum','Tot':'count'})
 numb['Ratio']=numb['Nr']/numb['Tot']
numb=numb.reset_index()
numb=numb.sort_values(numb.columns[0])
 numb
le_HHDREL = LabelEncoder()
le_HHDREL.classes_=numb.iloc[:, 0]
d_train.loc[:, 'HHDREL'] = le_HHDREL.transform(d_train.loc[:, 'HHDREL'])
 d_test.loc[:, 'HHDREL'] = le_HHDREL.transform(d_test.loc[:, 'HHDREL'])
<categorify>
ran = RandomForestClassifier(random_state=1)
 knn = KNeighborsClassifier()
log = LogisticRegression()
xgb = XGBClassifier()
gbc = GradientBoostingClassifier()
svc = SVC(probability=True)
 ext = ExtraTreesClassifier()
ada = AdaBoostClassifier()
gnb = GaussianNB()
gpc = GaussianProcessClassifier()
bag = BaggingClassifier()
models = [ran, knn, log, xgb, gbc, svc, ext, ada, gnb, gpc, bag]
model_names = ['Random Forest', 'K Nearest Neighbor', 'Logistic Regression', 'XGBoost', 'Gradient Boosting', 'SVC', 'Extra Trees', 'AdaBoost', 'Gaussian Naive Bayes', 'Gaussian Process', 'Bagging Classifier']
scores = {}
for ind, mod in enumerate(models):
mod.fit(X_train, y_train)
 acc = cross_val_score(mod, X_train, y_train, scoring="accuracy", cv=10)
 scores[model_names[ind]] = acc
pd.set_option('display.max_rows', 20)
 numb=d_train.groupby(['MIGMTR3'])['INCCAT'].agg({'Nr':'sum','Tot':'count'})
 numb['Ratio']=numb['Nr']/numb['Tot']
numb=numb.sort_values(numb.columns[2])
 numb=numb.reset_index()
numb
le_MIGMTR3 = LabelEncoder()
le_MIGMTR3.classes_=numb.iloc[:, 0]
d_train.loc[:, 'MIGMTR3'] = le_MIGMTR3.transform(d_train.loc[:, 'MIGMTR3'])
 d_test.loc[:, 'MIGMTR3'] = le_MIGMTR3.transform(d_test.loc[:, 'MIGMTR3'])
 d_train=d_train.drop(['MIGMTR1','MIGMTR4'],axis=1)
 d_test=d_test.drop(['MIGMTR1','MIGMTR4'],axis=1)
<categorify>
results = pd.DataFrame(scores ).T
results['mean'] = results.mean(1)
 result_df = results.sort_values(by='mean', ascending=False)
 result_df.head(11 )
numb=d_train.groupby(['MIGSAME'])['INCCAT'].agg({'Nr':'sum','Tot':'count'})
 numb['Ratio']=numb['Nr']/numb['Tot']
numb=numb.sort_values(numb.columns[2])
 numb=numb.reset_index()
numb
le_MIGSAME = LabelEncoder()
le_MIGSAME.classes_=numb.iloc[:, 0]
d_train.loc[:, 'MIGSAME'] = le_MIGSAME.transform(d_train.loc[:, 'MIGSAME'])
 d_test.loc[:, 'MIGSAME'] = le_MIGSAME.transform(d_test.loc[:, 'MIGSAME'] )<categorify>
gbc_imp = pd.DataFrame({'Feature':train.columns, 'gbc importance':gbc.feature_importances_})
 xgb_imp = pd.DataFrame({'Feature':train.columns, 'xgb importance':xgb.feature_importances_})
 ran_imp = pd.DataFrame({'Feature':train.columns, 'ran importance':ran.feature_importances_})
 ext_imp = pd.DataFrame({'Feature':train.columns, 'ext importance':ext.feature_importances_})
 ada_imp = pd.DataFrame({'Feature':train.columns, 'ada importance':ada.feature_importances_})
 importances = gbc_imp.merge(xgb_imp, on='Feature' ).merge(ran_imp, on='Feature' ).merge(ext_imp, on='Feature' ).merge(ada_imp, on='Feature')
 importances['Average'] = importances.mean(axis=1)
 importances = importances.sort_values(by='Average', ascending=False ).reset_index(drop=True )
numb=d_train.groupby(['MIGSUN'])['INCCAT'].agg({'Nr':'sum','Tot':'count'})
 numb['Ratio']=numb['Nr']/numb['Tot']
numb=numb.sort_values(numb.columns[2])
 numb=numb.reset_index()
numb
le_MIGSUN = LabelEncoder()
le_MIGSUN.classes_=numb.iloc[:, 0]
d_train.loc[:, 'MIGSUN'] = le_MIGSUN.transform(d_train.loc[:, 'MIGSUN'])
 d_test.loc[:, 'MIGSUN'] = le_MIGSUN.transform(d_test.loc[:, 'MIGSUN'] )<drop_column>
mylist = list(importance1.index )
d_train=d_train.drop(['PARENT'],axis=1)
 d_test=d_test.drop(['PARENT'],axis=1)
<drop_column>
train1 = pd.DataFrame()
test1 = pd.DataFrame()
for i in mylist:
train1[i] = train[i]
test1[i] = test[i]
train1.head()
d_train=d_train.drop(['PEMNTVTY','PENATVTY'],axis=1)
 d_test=d_test.drop(['PEMNTVTY','PENATVTY'],axis=1 )<categorify>
train = train1
test = test1
X_train = train
X_test = test
X_train = scaler.fit_transform(X_train)
 X_test = scaler.transform(X_test )
numb=d_train.groupby(['PEFNTVTY'])['INCCAT'].agg({'Nr':'sum','Tot':'count'})
 numb['Ratio']=numb['Nr']/numb['Tot']
numb=numb.sort_values(numb.columns[2])
 numb=numb.reset_index()
numb
le_PEFNTVTY = LabelEncoder()
le_PEFNTVTY.classes_=numb.iloc[:, 0]
d_train.loc[:, 'PEFNTVTY'] = le_PEFNTVTY.transform(d_train.loc[:, 'PEFNTVTY'])
 d_test.loc[:, 'PEFNTVTY'] = le_PEFNTVTY.transform(d_test.loc[:, 'PEFNTVTY'] )<categorify>
ran = RandomForestClassifier(random_state=1)
 knn = KNeighborsClassifier()
log = LogisticRegression()
xgb = XGBClassifier(random_state=1)
 gbc = GradientBoostingClassifier(random_state=1)
 svc = SVC(probability=True)
 ext = ExtraTreesClassifier(random_state=1)
 ada = AdaBoostClassifier(random_state=1)
 gnb = GaussianNB()
gpc = GaussianProcessClassifier()
bag = BaggingClassifier(random_state=1)
 models = [ran, knn, log, xgb, gbc, svc, ext, ada, gnb, gpc, bag]
model_names = ['Random Forest', 'K Nearest Neighbor', 'Logistic Regression', 'XGBoost', 'Gradient Boosting', 'SVC', 'Extra Trees', 'AdaBoost', 'Gaussian Naive Bayes', 'Gaussian Process', 'Bagging Classifier']
scores2 = {}
for ind, mod in enumerate(models):
mod.fit(X_train, y_train)
 acc = cross_val_score(mod, X_train, y_train, scoring = "accuracy", cv = 10)
 scores2[model_names[ind]] = acc
numb=d_train.groupby(['PRCITSHP'])['INCCAT'].agg({'Nr':'sum','Tot':'count'})
 numb['Ratio']=numb['Nr']/numb['Tot']
numb=numb.sort_values(numb.columns[2])
 numb=numb.reset_index()
numb
le_PRCITSHP = LabelEncoder()
le_PRCITSHP.classes_=numb.iloc[:, 0]
d_train.loc[:, 'PRCITSHP'] = le_PRCITSHP.transform(d_train.loc[:, 'PRCITSHP'])
 d_test.loc[:, 'PRCITSHP'] = le_PRCITSHP.transform(d_test.loc[:, 'PRCITSHP'] )<categorify>
results = pd.DataFrame(scores2 ).T
results['mean'] = results.mean(1)
 result_df = results.sort_values(by='mean', ascending=False)
 result_df.head(11 )
numb=d_train.groupby(['VETQVA','VETYN'])['INCCAT'].agg({'Nr':'sum','Tot':'count'})
 numb['Ratio']=numb['Nr']/numb['Tot']
numb=numb.sort_values(numb.columns[2])
 numb=numb.reset_index()
numb
le_VET = LabelEncoder()
le_VET.classes_=numb.iloc[:, 0].astype(str)+","+ numb.iloc[:, 1].astype(str)
 d_train['VET'] = le_VET.transform(d_train.loc[:, 'VETQVA'].astype(str)+ "," + d_train.loc[:,'VETYN'].astype(str))
d_test['VET'] = le_VET.transform(d_test.loc[:, 'VETQVA'].astype(str)+ "," + d_test.loc[:,'VETYN'].astype(str))
d_train=d_train.drop(['VETYN','VETQVA'],axis=1)
 d_test=d_test.drop(['VETYN','VETQVA'],axis=1)
<create_dataframe>
Cs = [0.01, 0.1, 1, 5, 10, 15, 20, 50]
gammas = [0.001, 0.01, 0.1]
hyperparams = {'C': Cs, 'gamma': gammas}
gd = GridSearchCV(estimator=SVC(probability=True), param_grid=hyperparams, verbose=True, cv=5, scoring="accuracy", n_jobs=-1)
 gd.fit(X_train, y_train)
 print(gd.best_score_)
 print(gd.best_params_ )
d_train_bckp=d_train.copy()
d_test_bckp=d_test.copy()<remove_duplicates>
learning_rate = [0.01, 0.05, 0.1, 0.2, 0.5]
n_estimators = [100, 1000, 2000]
max_depth = [3, 5, 10, 15]
hyperparams = {'learning_rate': learning_rate, 'n_estimators': n_estimators}
gd = GridSearchCV(estimator=GradientBoostingClassifier() , param_grid = hyperparams, verbose=True, cv=5, scoring="accuracy", n_jobs=-1)
 gd.fit(X_train, y_train)
 print(gd.best_score_)
 print(gd.best_params_ )
d_train=d_train.drop_duplicates()<concatenate>
penalty = ['l1', 'l2']
C = np.logspace(0, 4, 10)
 hyperparams = {'penalty': penalty, 'C': C}
gd = GridSearchCV(estimator=LogisticRegression() , param_grid=hyperparams, verbose=True, cv=5, scoring="accuracy", n_jobs=-1)
 gd.fit(X_train, y_train)
 print(gd.best_score_)
 print(gd.best_params_ )
add_tr=d_train[(d_train['INCCAT']==1)&(d_train['AAGE']>20)]
balanced_d_train=d_train.copy()
for i in range(0,round(balanced_d_train.shape[0]/add_tr.shape[0])) :
balanced_d_train=pd.concat([balanced_d_train,add_tr],axis=0,sort=False, ignore_index=True)
 i=i+1<prepare_x_and_y>
learning_rate = [0.001, 0.005, 0.01, 0.05, 0.1, 0.2]
n_estimators = [10, 50, 100, 250, 500, 1000]
hyperparams = {'learning_rate': learning_rate, 'n_estimators': n_estimators}
gd = GridSearchCV(estimator = XGBClassifier() , param_grid=hyperparams, verbose=True, cv=5, scoring="accuracy", n_jobs=-1)
 gd.fit(X_train, y_train)
 print(gd.best_score_)
 print(gd.best_params_ )
gen_y_train=d_train['INCCAT'].copy()
balanced_y_train=balanced_d_train['INCCAT'].copy()<drop_column>
max_depth = [3, 4, 5, 6, 7, 8, 9, 10]
min_child_weight = [1, 2, 3, 4, 5, 6]
hyperparams = {'max_depth': max_depth, 'min_child_weight': min_child_weight}
gd = GridSearchCV(estimator=XGBClassifier(learning_rate=0.2, n_estimators=50),
param_grid=hyperparams, verbose=True, cv=5, scoring="accuracy", n_jobs=-1)
 gd.fit(X_train, y_train)
 print(gd.best_score_)
 print(gd.best_params_ )
d_train = d_train.drop('INCCAT', axis=1)
 d_test = d_test.drop('INCCAT', axis=1)
 balanced_d_train=balanced_d_train.drop('INCCAT', axis=1 )<prepare_x_and_y>
gamma = [i*0.1 for i in range(0,5)]
hyperparams = {'gamma': gamma}
gd = GridSearchCV(estimator= XGBClassifier(learning_rate=0.2, n_estimators=50, max_depth=8, min_child_weight=2),
param_grid=hyperparams, verbose=True, cv=5, scoring="accuracy", n_jobs=-1)
 gd.fit(X_train, y_train)
 print(gd.best_score_)
 print(gd.best_params_ )
X=balanced_d_train.copy()
y=balanced_y_train.copy()<normalization>
subsample = [0.6, 0.65, 0.7, 0.75, 0.8, 0.85, 0.9, 0.95, 1]
colsample_bytree = [0.6, 0.65, 0.7, 0.75, 0.8, 0.85, 0.9, 0.95, 1]
hyperparams = {'subsample': subsample, 'colsample_bytree': colsample_bytree}
gd = GridSearchCV(estimator=XGBClassifier(learning_rate=0.2, n_estimators=50, max_depth=8, min_child_weight=2, gamma=0),
param_grid=hyperparams, verbose=True, cv=5, scoring="accuracy", n_jobs=-1)
 gd.fit(X_train, y_train)
 print(gd.best_score_)
 print(gd.best_params_ )
train_norm = X
std_scale = preprocessing.StandardScaler().fit(train_norm)
 x_train_norm = std_scale.transform(train_norm)
 training_norm_col = pd.DataFrame(x_train_norm, index=train_norm.index, columns=train_norm.columns)
 X.update(training_norm_col )<choose_model_class>
reg_alpha = [1e-5, 1e-2, 0.1, 1, 100]
hyperparams = {'reg_alpha': reg_alpha}
gd = GridSearchCV(estimator=XGBClassifier(learning_rate=0.2, n_estimator=50, max_depth=8, min_child_weight=2, gamma=0, subsample=0.7, colsample_bytree=0.65),
param_grid=hyperparams, verbose=True, cv=5, scoring="accuracy", n_jobs=-1)
 gd.fit(X_train, y_train)
 print(gd.best_score_)
 print(gd.best_params_ )
model = Sequential()
model.add(Dense(90, input_dim=X.shape[1], activation='relu'))
model.add(Dropout(rate=0.25, name='dropout_one'))
model.add(Dense(20, activation='sigmoid'))
model.add(Dense(7, activation='sigmoid'))
model.add(Dropout(rate=0.1, name='dropout_two'))
model.add(Dense(1, activation='sigmoid')) 
<choose_model_class>
n_restarts_optimizer = [0, 1, 2, 3]
max_iter_predict = [1, 2, 5, 10, 20, 35, 50, 100]
warm_start = [True, False]
hyperparams = {'n_restarts_optimizer': n_restarts_optimizer, 'max_iter_predict': max_iter_predict, 'warm_start': warm_start}
gd = GridSearchCV(estimator=GaussianProcessClassifier() , param_grid=hyperparams, verbose=True, cv=5, scoring="accuracy", n_jobs=-1)
 gd.fit(X_train, y_train)
 print(gd.best_score_)
 print(gd.best_params_ )
opt=keras.optimizers.RMSprop(lr=0.003, rho=0.9)
 model.compile(loss='binary_crossentropy', optimizer=opt, metrics=['binary_accuracy'])
<train_model>
n_estimators = [10, 100, 200, 500]
learning_rate = [0.001, 0.01, 0.1, 0.5, 1, 1.5, 2]
hyperparams = {'n_estimators': n_estimators, 'learning_rate': learning_rate}
gd = GridSearchCV(estimator=AdaBoostClassifier() , param_grid=hyperparams, verbose=True, cv=5, scoring="accuracy", n_jobs=-1)
 gd.fit(X_train, y_train)
 print(gd.best_score_)
 print(gd.best_params_ )
np.random.seed(10)
 kfold = StratifiedKFold(n_splits=10, shuffle=True)
 cvscores = []
kfold.get_n_splits(X,y)
 i=1
for train_index, test_index in kfold.split(X, y):
print("split Nr %d" % i)
 X_tr_m1, X_te_m1 = X.iloc[train_index], X.iloc[test_index]
y_tr_m1, y_te_m1 = y.iloc[train_index], y.iloc[test_index]
model.fit(X_tr_m1, y_tr_m1, epochs=3, batch_size=6000, verbose=1)
 scores = model.evaluate(X_te_m1, y_te_m1, verbose=0)
 print("%s: %.2f%%" %(model.metrics_names[1], scores[1]*100))
cvscores.append(scores[1] * 100)
 i=i+1
print("%.2f%%(+/- %.2f%%)" %(np.mean(cvscores), np.std(cvscores)) )<train_model>
n_neighbors = [1, 2, 3, 4, 5]
algorithm = ['auto']
weights = ['uniform', 'distance']
leaf_size = [1, 2, 3, 4, 5, 10]
hyperparams = {'algorithm':algorithm, 'weights': weights, 'leaf_size': leaf_size, 'n_neighbors': n_neighbors}
gd=GridSearchCV(estimator=KNeighborsClassifier() , param_grid=hyperparams, verbose=True, cv=5, scoring="accuracy", n_jobs=-1)
 gd.fit(X_train, y_train)
 print(gd.best_score_)
 print(gd.best_params_ )
opt = keras.optimizers.Adam(lr=0.03, beta_1=0.9, beta_2=0.999, amsgrad=False)
 model.compile(loss='binary_crossentropy', optimizer=opt, metrics=['binary_accuracy'])
 model.fit(X, y, epochs=80, batch_size=8000, verbose=1 )<compute_train_metric>
n_estimators = [10, 50, 100, 200]
max_depth = [3, None]
max_features = [0.1, 0.2, 0.5, 0.8]
min_samples_split = [2, 6]
min_samples_leaf = [2, 6]
hyperparams = {'n_estimators': n_estimators, 'max_depth': max_depth, 'max_features': max_features,
'min_samples_split': min_samples_split, 'min_samples_leaf': min_samples_leaf}
gd = GridSearchCV(estimator=RandomForestClassifier() , param_grid=hyperparams, verbose=True, cv=5, scoring="accuracy", n_jobs=-1)
 gd.fit(X_train, y_train)
 print(gd.best_score_)
 print(gd.best_params_ )
y_T=model.predict(X)
 matrix = confusion_matrix(balanced_y_train, y_T.round())
 matrix<compute_train_metric>
n_estimators = [10, 25, 50, 75, 100]
max_depth = [3, None]
max_features = [0.1, 0.2, 0.5, 0.8]
min_samples_split = [2, 10]
min_samples_leaf = [2, 10]
hyperparams = {'n_estimators': n_estimators, 'max_depth': max_depth, 'max_features': max_features, 'min_samples_split': min_samples_split, 'min_samples_leaf': min_samples_leaf}
gd = GridSearchCV(estimator=ExtraTreesClassifier() , param_grid=hyperparams, verbose=True, cv=5, scoring="accuracy", n_jobs=-1)
 gd.fit(X_train, y_train)
 print(gd.best_score_)
 print(gd.best_params_ )
roc_auc_score(balanced_y_train,y_T.round() )<train_model>
n_estimators = [10, 50, 75, 100, 200]
max_samples = [0.1, 0.2, 0.5, 0.8, 1.0]
max_features = [0.1, 0.2, 0.5, 0.8, 1.0]
hyperparams = {'n_estimators': n_estimators, 'max_samples': max_samples, 'max_features': max_features}
gd = GridSearchCV(estimator=BaggingClassifier() , param_grid=hyperparams, verbose=True, cv=5, scoring="accuracy", n_jobs=-1)
 gd.fit(X_train, y_train)
 print(gd.best_score_)
 print(gd.best_params_ )
<define_variables>
ran = RandomForestClassifier(max_depth=None, max_features=0.1, min_samples_leaf=2, min_samples_split=6, n_estimators=100, random_state=1)
 knn = KNeighborsClassifier(leaf_size=1, n_neighbors=5, weights='distance')
 log = LogisticRegression(C=1.0, penalty='l2')
 xgb = XGBClassifier(learning_rate=0.2, n_estimators=50, max_depth=8, min_child_weight=2, gamma=0, subsample=0.7, colsample_bytree=0.65, reg_alpha=1)
 gbc = GradientBoostingClassifier(learning_rate=0.2, max_depth=3, n_estimators=1000)
 svc = SVC(probability=True, gamma=0.001, C=20)
 ext = ExtraTreesClassifier(max_depth=None, max_features=0.1, min_samples_leaf=2, min_samples_split=2, n_estimators=50, random_state=1)
 ada = AdaBoostClassifier(learning_rate=0.5, n_estimators=200, random_state=1)
 gpc = GaussianProcessClassifier(max_iter_predict=1, n_restarts_optimizer=0, warm_start=True)
 bag = BaggingClassifier(max_features=0.5, max_samples=1.0, n_estimators=75, random_state=1)
 models = [ran, knn, log, xgb, gbc, svc, ext, ada, gnb, gpc, bag]
model_names = ['Random Forest', 'K Nearest Neighbour', 'Logistic Regression', 'XGBoost', 'Gradient Boosting', 'SVC', 'Extra Trees', 'AdaBoost', 'Gaussian Naive Bayes', 'Gaussian Process', 'Bagging Classifier']
scores3 = {}
for ind, mod in enumerate(models):
mod.fit(X_train, y_train)
 acc = cross_val_score(mod, X_train, y_train, scoring = "accuracy", cv = 10)
 scores3[model_names[ind]] = acc
<prepare_x_and_y>
grid_hard = VotingClassifier(estimators = [('Random Forest', ran), 
('Logistic Regression', log), 
('XGBoost', xgb), 
('Gradient Boosting', gbc), 
('Extra Trees', ext), 
('AdaBoost', ada), 
('Gaussian Process', gpc), 
('SVC', svc), 
('K Nearest Neighbor', knn), 
('Bagging Classifier', bag)], voting='hard')
 grid_hard_cv = model_selection.cross_validate(grid_hard, X_train, y_train, cv=10)
 grid_hard.fit(X_train, y_train)
 print("Hard voting on test set score mean: {:.2f}".format(grid_hard_cv['test_score'].mean() *100))
X_T=d_test.copy()
test_norm = X_T
x_test_norm = std_scale.transform(test_norm)
 testing_norm_col = pd.DataFrame(x_test_norm, index=test_norm.index, columns=test_norm.columns)
 X_T.update(testing_norm_col )<predict_on_test>
grid_soft = VotingClassifier(estimators = [('Random Forest', ran), 
('Logistic Regression', log), 
('XGBoost', xgb), 
('Gradient Boosting', gbc), 
('Extra Trees', ext), 
('AdaBoost', ada), 
('Gaussian Process', gpc), 
('SVC', svc), 
('K Nearest Neighbor', knn), 
('Bagging Classifier', bag)], voting='soft')
 grid_soft_cv = model_selection.cross_validate(grid_soft, X_train, y_train, cv=10)
 grid_soft.fit(X_train, y_train)
 print("Soft voting on test set score mean: {:.2f}".format(grid_soft_cv['test_score'].mean() *100))
y_Test_lev1_pred=model.predict(X_T )<feature_engineering>
predictions = grid_hard.predict(X_test)
 submission = pd.concat([pd.DataFrame(passId), pd.DataFrame(predictions)], axis='columns')
 submission.columns = ["PassengerId", "Survived"]
submission.to_csv('titanic_submission1.csv', header=True, index=False )
<feature_engineering><EOS> 
predictions = grid_soft.predict(X_test)
 submission = pd.concat([pd.DataFrame(passId), pd.DataFrame(predictions)], axis='columns')
 submission.columns = ["PassengerId", "Survived"]
submission.to_csv('titanic_submission2.csv', header=True, index=False )