Kaggle competition: EC524: Heart-disease classification
warnings.filterwarnings('ignore')
 RS=81<load_from_csv>
train=pd.read_csv('/kaggle/input/ec524-heart-disease/train.csv')
 test=pd.read_csv('/kaggle/input/ec524-heart-disease/test.csv' )
train = pd.read_csv('.. /input/titanic/train.csv')
 test = pd.read_csv('.. /input/titanic/test.csv' )<concatenate>
train.isna().sum()
data=pd.concat([train, test], sort = True ).reset_index(drop=True)
 data.info()<categorify>
train.fillna(train.mean() ,inplace=True )
def replace_titles(x):
title=x['Title']
if title in ['Don', 'Major', 'Capt', 'Jonkheer', 'Rev', 'Col', 'Sir']:
return 'Mr'
elif title in ['Countess', 'Mme', 'Lady', 'Dona']:
return 'Mrs'
elif title in ['Mlle', 'Ms']:
return 'Miss'
elif title =='Dr':
if x['Sex']=='Male':
return 'Mr'
else:
return 'Mrs'
else:
return title
data['Title'] = data['Name'].str.extract('([A-Za-z]+)\.', expand=False)
 data['Title']=data.apply(replace_titles, axis=1 )<feature_engineering>
train.isna().sum()
data['Age']=data.groupby(['Title', 'Pclass'])['Age'].apply(lambda x: x.fillna(x.median()))
data['Fare'].fillna(data['Fare'].median() , inplace=True )<feature_engineering>
test.isna().sum()
data['Family_Size']=data['SibSp']+data['Parch']
data['Alone']=[1 if x==0 else 0 for x in data['SibSp']+data['Parch']]<merge>
train=train.drop(['id'],axis=1 )
data['Surname'] =data.Name.str.extract(r'([A-Za-z]+),', expand=False)
 df=data[data.duplicated(['Ticket','Surname'], keep=False)]
dfg=df.groupby(['Ticket','Surname'])[['Survived']].max().fillna(0.5)
 data=pd.merge(data, dfg.rename(columns={'Survived':'FamSurvived'}), how='left', on=['Ticket','Surname'])
 data['FamSurvived'].fillna(0.5, inplace=True )<categorify>
x=train.drop(['heart_disease'],axis=1)
 y=train['heart_disease'].values
data['Age_T']=PowerTransformer().fit_transform(data[['Age']])
 data['Fare_T']=PowerTransformer().fit_transform(data[['Fare']] )<categorify>
scaler=StandardScaler()
x=scaler.fit_transform(x)
data = pd.get_dummies(data, columns=['Title'], drop_first=True )<drop_column>
lr=LogisticRegression()
rf=RandomForestClassifier()
vote=VotingClassifier(estimators=[('lr',lr),('rf',rf)] )
train = data[:len(train)]
test = data[len(train):].drop(['Survived'],axis=1 )<groupby>
vote.fit(x,y )
data.groupby(['Sex', 'Pclass'])['Survived'].mean()<filter>
vote.score(x,y )
train_1=train.loc[(train.Sex=='male')]
test_1=test.loc[(test.Sex=='male')]
train_2=train.loc[(( train.Pclass<=2)&(train.Sex=='female')) ]
test_2=test.loc[(( test.Pclass<=2)&(test.Sex=='female')) ]
train_3=train.loc[(( train.Pclass>2)&(train.Sex=='female')) ]
test_3=test.loc[(( test.Pclass>2)&(test.Sex=='female')) ]<define_variables>
test_=test.drop(['id'],axis=1 )
col_final=['Age_T', 'Fare_T', 'Alone', 'Family_Size', 'FamSurvived', 'Title_Miss', 'Title_Mr', 'Title_Mrs']<define_search_space>
test_scaled=scaler.fit_transform(test_ )
param_lgb ={'n_estimators': [100, 500, 1000, 2000],
'max_depth':[1,2,3,4,5],
'num_leaves': [2,4,6,8,10],
'min_child_samples ': [2,5,10,15,20],
'min_child_weight': [1e-5, 1e-3, 1e-2, 1e-1, 1, 1e1, 1e2, 1e3, 1e4],
'subsample':[0.4,0.5,0.6,0.7,0.8,0.9,1],
'colsample_bytree':[0.4,0.5,0.6,0.7,0.8,0.9,1],
'reg_alpha': [0, 1e-1, 1, 2, 5, 7, 10, 50, 100],
'reg_lambda': [0, 1e-1, 1, 5, 10, 20, 50, 100]}
param_log={'C': [0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1, 3, 5, 7, 10, 15, 20, 25, 30, 50],
'penalty':['l1', 'l2', 'elasticnet', 'none'],
'class_weight': [1, 3, 10],
'max_iter': [10000]}
param_rf= {'n_estimators': [10 ,25,50,100,125,250,500,1000],
'max_features': ['auto', 'sqrt'],
'max_depth': [1,2,3,4,5,10],
'min_samples_split': [2, 5, 10, 15, 25],
'min_samples_leaf': [1, 2, 5, 10]}
paramsStack= {'final_estimator__multi_class':['ovr'],
'final_estimator__max_iter':[10000],
'final_estimator__solver':['saga'],
'final_estimator__penalty':['l1', 'l2', 'elasticnet', 'none'],
'final_estimator__l1_ratio':[0.5],
'final_estimator__C':np.linspace(start = 0.0001, stop = 10, num = 100)}
searchParamsModels={'n_iter':50,'scoring':'accuracy','cv':10,'refit':True,'random_state':RS,'verbose':0,'n_jobs':-1}
clf_lgb=lgb.LGBMClassifier(random_state=RS, silent=True, metric='None')
 clf_log=LogisticRegression(random_state = RS, solver='saga', l1_ratio=0.5)
 clf_rf=RandomForestClassifier(random_state = RS)
 stackM=LogisticRegression(random_state=RS)
 l_param=[param_log,param_rf,param_lgb]
l_clf=[clf_log,clf_rf,clf_lgb]<train_on_grid>
y_pred=vote.predict(test_scaled )
def tune_stack_predict(X_train,y_train,X_test,listModels,listSearchParamM,stackModel,searchParamsModels,listSearchParamS):
models_local=[]
X_test_c=X_test.copy()
for clf, param in zip(listModels,listSearchParamM):
rs_clf=RandomizedSearchCV(estimator=clf, param_distributions=param, **searchParamsModels)
 rs_clf.fit(X_train, y_train)
 rs_clf_name=type(clf ).__name__[0:5]
y_pred=rs_clf.best_estimator_.predict(X_test)
 X_test_c[rs_clf_name]=np.asarray(y_pred)
 models_local.append([rs_clf_name,rs_clf.best_estimator_])
 stack_clf=StackingClassifier(estimators=models_local, final_estimator=stackModel,cv=10, verbose=0)
 rs_stack=RandomizedSearchCV(estimator=stack_clf, param_distributions=listSearchParamS, **searchParamsModels)
 rs_stack.fit(X_train,y_train)
 rs_stack_name=type(stack_clf ).__name__[0:5]
y_pred=rs_stack.best_estimator_.predict(X_test)
 X_test_c[rs_stack_name]=np.asarray(y_pred)
 models_local.append([rs_stack_name,rs_stack.best_estimator_])
 return X_test_c, models_local<predict_on_test>
sub=pd.DataFrame()
sub['id']=test['id']
sub['heart_disease']=y_pred
<choose_model_class>
sub.to_csv('sub.csv',index=False )