Kaggle competition: Costa Rican Household Poverty Level Prediction
<SOS> metric: MacroFScore Kaggle data source: costa-rican-household-poverty-level-prediction<count_values>
%matplotlib inline
plt.style.use('fivethirtyeight')
 plt.rcParams['font.size'] = 18
plt.rcParams['patch.edgecolor'] = 'k'
pd.set_option('display.max_rows', 500)
 pd.set_option('display.max_columns', 500)
 pd.set_option('display.width', 1000)
 sns.set(style = 'darkgrid')
 scorer = make_scorer(f1_score, greater_is_better=True, average = 'macro')
train.groupby('family' ).Survived.value_counts()<feature_engineering>
for i in range(len(train)) :
if(train['family'][i] > 1):
train['family'][i] = 1
else:
train['family'][i] = 0<count_values>
train.groupby('Cabin' ).Survived.value_counts()<data_type_conversions>
df_train = pd.read_csv('.. /input/train.csv', sep=',', decimal='.')
 df_test = pd.read_csv('.. /input/test.csv', sep=',', decimal='.')
 df_submit = pd.read_csv('.. /input/sample_submission.csv', sep=',', decimal='.' )
train['Cabin'].fillna('S',inplace=True )<feature_engineering>
df_test['Target'] = np.nan
data = df_train.append(df_test, ignore_index = True )
for i in range(len(train)) :
train['Cabin'][i] = train['Cabin'][i][0]<count_values>
data['Target'].isnull().sum()
train.groupby('Cabin' ).Survived.value_counts()<feature_engineering>
map_dict = {'yes':1,'no':0}
data["dependency"] = data["dependency"].replace(map_dict ).astype('float64')
 data["edjefe"] = data["edjefe"].replace(map_dict ).astype('float64')
 data["edjefa"] = data["edjefa"].replace(map_dict ).astype('float64' )
train['fare_val'] = 0
for i in range(len(train)) :
if(train['Fare'][i] > 32.0):
train['fare_val'][i] = 1<count_values>
data["dependency"] = data["dependency"].astype('float64')
 data["edjefe"] = data["edjefe"].astype('float64')
 data["edjefa"] = data["edjefa"].astype('float64' )
train.groupby('fare_val' ).Survived.value_counts()<feature_engineering>
data[data["Target"].notnull() ].groupby("idhogar")["Target"].nunique().value_counts()
test['family'] = test['SibSp'] + test['Parch'] + 1
for i in range(len(test)) :
if(test['family'][i] > 1):
test['family'][i] = 1
else:
test['family'][i] = 0
test['Name'] = test['Name'].apply(lambda x: x if x in top6 else 'Other')
 test['Cabin'].fillna('S',inplace=True)
 for i in range(len(test)) :
test['Cabin'][i] = test['Cabin'][i][0]
test['fare_val'] = 0
for i in range(len(test)) :
if(test['Fare'][i] > 32.0):
test['fare_val'][i] = 1<define_variables>
data_multi_target = data[data["Target"].notnull() ].groupby("idhogar")["Target"].apply(lambda x : x.nunique() ==1)
 data_multi_target = data_multi_target[data_multi_target != True]
features = [
'Pclass',
'Sex',
'Age',
'family',
'fare_val',
'Embarked'
]
target = 'Survived'<count_missing_values>
data[data["idhogar"] == data_multi_target.index[0]][['idhogar', 'parentesco1', 'Target']]
train[features].isnull().sum()<count_missing_values>
for id in data_multi_target.index:
True_label = data[(data['idhogar'] == id)&(data['parentesco1'] == 1)]["Target"]
data.loc[data['idhogar'] == id, "Target"] = np.where(True_label.notnull() , True_label, data.loc[data['idhogar'] == id, "Target"] )
test[features].isnull().sum()<feature_engineering>
data['Target'].isnull().sum()
;<data_type_conversions>
missing = pd.DataFrame(data.isnull().sum() ).rename(columns = {0: 'total'})
 missing['percent'] = missing['total'] / len(data)
 missing.sort_values('percent', ascending = False ).head(10 ).drop('Target' )
Age_std = train['Age'].std()
train['Age'] = train['Age'].fillna(value = Age_std)
 Age_std_t = test['Age'].std()
test['Age'] = test['Age'].fillna(value = Age_std_t)
 f"'train',{Age_std}, 'test',{Age_std_t}"<categorify>
data[data['parentesco1'] == 1]['v18q1'].value_counts()
lbl = LabelEncoder()
train['Sex'] = lbl.fit_transform(train[['Sex']].values.ravel())
 test['Sex'] = lbl.fit_transform(test[['Sex']].values.ravel() )<categorify>
data[data['parentesco1'] == 1].groupby('v18q1')['v18q'].sum()
<data_type_conversions>
data['v18q1'] = data['v18q1'].fillna(0 )
train['Embarked'] = train['Embarked'].fillna(value=train['Embarked'].mode() [0])
 test['Embarked'] = test['Embarked'].fillna(value=test['Embarked'].mode() [0])
<define_variables>
tipovivi = data.columns.str.startswith('tipovivi' )
train_ds = train[features]
test_ds = test[features]<categorify>
data.loc[(data['tipovivi1'] == 1 & data['v2a1'].isnull()), 'v2a1'] = 0
train_ds = pd.get_dummies(columns = ['Embarked','Pclass'],data=train_ds,drop_first = True)
 test_ds = pd.get_dummies(columns = ['Embarked','Pclass'],data=test_ds,drop_first = True )<prepare_x_and_y>
data.loc[data['v2a1'].isnull() , 'v2a1'] = 0
y_train = train[target]<split>
data.loc[(( data['age'] > 19)|(data['age'] < 7)) &(data['rez_esc'].isnull()),'rez_esc'] = 0
X_train, X_valid, y_train, y_valid = train_test_split(train_ds, y_train, test_size=0.30 )<import_modules>
data.loc[data['rez_esc'] > 5, 'rez_esc'] = 5
from sklearn.model_selection import train_test_split,cross_val_score,RandomizedSearchCV,GridSearchCV
from sklearn.metrics import confusion_matrix, accuracy_score, classification_report
from sklearn.metrics import roc_auc_score, roc_curve
from sklearn.ensemble import RandomForestClassifier
from xgboost import XGBClassifier<train_on_grid>
data.loc[data['rez_esc'].isnull() ,'rez_esc' ] = 0
;<train_on_grid>
data.loc[data['meaneduc'].isnull() ,'meaneduc' ] = 0
rfc = XGBClassifier()
params = {'n_estimators': [200,500,800,1000,1200],
'max_depth': [3,5,7],
'objective' : ['binary:logistic'],
'min_samples_leaf' : [1, 2, 3, 4, 5],
'max_leaf_nodes':[2,3,5,7],
'min_child_weight': [1, 5, 10],
'gamma': [0.5, 1, 1.5, 2, 5],
}
rfc_cv = RandomizedSearchCV(rfc, params, cv = 10, n_jobs=-1, verbose=2 ).fit(X_train, y_train )<find_best_params>
data.loc[data['SQBmeaned'].isnull() , 'SQBmeaned'] = 0
rfc_cv.best_params_
best_model = rfc_cv.best_estimator_
print(best_model)
 print(rfc_cv.best_score_ )<compute_test_metric>
data.isnull().sum()
rfc_pred = best_model.predict(X_valid)
 print("Accuracy: ", accuracy_score(y_valid, rfc_pred))
print("
Confusion Matrix
")
 print(confusion_matrix(y_valid, rfc_pred))<load_pretrained>
data.isnull().sum()
filename = 'Titanic_model.sav'
pickle.dump(best_model, open(filename, 'wb'))<load_pretrained>
head = df_train.loc[(data["parentesco1"] ==1)&(data["Target"].notnull())].copy()
loaded_model = pickle.load(open(filename, 'rb'))
result = loaded_model.score(X_valid, y_valid)
 print(result )<define_variables>
distrib_target = head['Target'].value_counts().reset_index().rename(columns = {'index' : 'level' } )
passId = test[['PassengerId']].values<predict_on_test>
distrib_target['Household_type'] = distrib_target['level'].map(poverty_mapping )
final_pred = best_model.predict(test_ds )<prepare_output>
ids = ['Id', 'idhogar']
target = ['Target']
sub = {'PassengerId':passId.ravel() , 'Survived':final_pred}<create_dataframe>
ind_bool = ['v18q', 'dis', 'male', 'female', 'estadocivil1', 'estadocivil2', 'estadocivil3',
'estadocivil4', 'estadocivil5', 'estadocivil6', 'estadocivil7',
'parentesco1', 'parentesco2', 'parentesco3', 'parentesco4', 'parentesco5',
'parentesco6', 'parentesco7', 'parentesco8', 'parentesco9', 'parentesco10',
'parentesco11', 'parentesco12', 'instlevel1', 'instlevel2', 'instlevel3',
'instlevel4', 'instlevel5', 'instlevel6', 'instlevel7', 'instlevel8',
'instlevel9', 'mobilephone']
ind_ordered = ['rez_esc', 'escolari', 'age']
submission_csv = pd.DataFrame(sub )<save_to_csv>
hh_bool = ['hacdor', 'hacapo', 'v14a', 'refrig', 'paredblolad', 'paredzocalo',
'paredpreb','pisocemento', 'pareddes', 'paredmad',
'paredzinc', 'paredfibras', 'paredother', 'pisomoscer', 'pisoother',
'pisonatur', 'pisonotiene', 'pisomadera',
'techozinc', 'techoentrepiso', 'techocane', 'techootro', 'cielorazo',
'abastaguadentro', 'abastaguafuera', 'abastaguano',
'public', 'planpri', 'noelec', 'coopele', 'sanitario1',
'sanitario2', 'sanitario3', 'sanitario5', 'sanitario6',
'energcocinar1', 'energcocinar2', 'energcocinar3', 'energcocinar4',
'elimbasu1', 'elimbasu2', 'elimbasu3', 'elimbasu4',
'elimbasu5', 'elimbasu6', 'epared1', 'epared2', 'epared3',
'etecho1', 'etecho2', 'etecho3', 'eviv1', 'eviv2', 'eviv3',
'tipovivi1', 'tipovivi2', 'tipovivi3', 'tipovivi4', 'tipovivi5',
'computer', 'television', 'lugar1', 'lugar2', 'lugar3',
'lugar4', 'lugar5', 'lugar6', 'area1', 'area2']
hh_ordered = [ 'rooms', 'r4h1', 'r4h2', 'r4h3', 'r4m1','r4m2','r4m3', 'r4t1', 'r4t2',
'r4t3', 'v18q1', 'tamhog','tamviv','hhsize','hogar_nin',
'hogar_adul','hogar_mayor','hogar_total', 'bedrooms', 'qmobilephone']
hh_cont = ['v2a1', 'dependency', 'edjefe', 'edjefa', 'meaneduc', 'overcrowding']
submission_csv.to_csv('final_sub_titanic_xgb_cv_10.csv',index = False )<load_from_csv>
sqr_ = ['SQBescolari', 'SQBage', 'SQBhogar_total', 'SQBedjefe',
'SQBhogar_nin', 'SQBovercrowding', 'SQBdependency', 'SQBmeaned', 'agesq']
x = pd.read_csv("./final_sub_titanic_xgb_cv_10.csv" )<load_from_csv>
head = data.loc[(data["parentesco1"] ==1), ids + hh_bool + hh_ordered + hh_cont + target].copy()
df_train = pd.read_csv('/kaggle/input/titanic/train.csv')
 df_train.head()<count_missing_values>
head.drop(labels = 'area2', axis = 1, inplace = True )
df_train.isnull().sum()<count_values>
head['epared_argmax'] = np.argmax(np.array(head[['epared1', 'epared2', 'epared3']]),
axis = 1 )
df_train['Survived'].value_counts()<count_values>
head['etecho_argmax'] = np.argmax(np.array(head[['etecho1', 'etecho2', 'etecho3']]),
axis = 1 )
df_train['Survived'].value_counts() /len(df_train)*100<drop_column>
head['eviv_argmax'] = np.argmax(np.array(head[['eviv1', 'eviv2', 'eviv3']]),
axis = 1 )
df_train = df_train.drop(['PassengerId','Ticket','Name','Cabin'],axis=1 )<count_values>
head['abastagua_argmax'] = np.argmax(np.array(head[[ 'abastaguano', 'abastaguafuera','abastaguadentro']]),
axis = 1 )
df_train['Sex'].value_counts() /len(df_train)*100<count_values>
head['elec_argmax'] = np.argmax(np.array(head[['noelec','coopele', 'public', 'planpri']]),
axis = 1 )
df_train[df_train['Survived']==0]['Sex'].value_counts() /len(df_train[df_train['Survived']==0])*100<count_values>
head['tipovivi_argmax'] = np.argmax(np.array(head[['tipovivi5','tipovivi4', 'tipovivi3', 'tipovivi2','tipovivi1']]),
axis = 1 )
df_train[df_train['Survived']==1]['Sex'].value_counts() /len(df_train[df_train['Survived']==1])*100<count_values>
head['electro_house'] = head['refrig'] + head['computer'] + head['television']
df_train[df_train['Survived']==1]['Pclass'].value_counts() /len(df_train[df_train['Survived']==1])*100<count_values>
head['diff_tamviv_hhsize'] = head['tamviv'] - head['hhsize']
df_train[df_train['Survived']==0]['Pclass'].value_counts() /len(df_train[df_train['Survived']==0])*100<feature_engineering>
head.drop(labels = [ 'epared1', 'epared2', 'epared3','etecho1', 'etecho2', 'etecho3',
'eviv1', 'eviv2', 'eviv3',
'abastaguano', 'abastaguafuera','abastaguadentro',
'noelec','coopele', 'public', 'planpri',
'refrig', 'computer','television',
'tipovivi5','tipovivi4', 'tipovivi3', 'tipovivi2','tipovivi1',
'tamviv']
, axis = 1, inplace = True )
df_train['Family']=df_train['SibSp']+df_train['Parch']<count_values>
corr_matrix = indiv.corr()
upper = corr_matrix.where(np.triu(np.ones(corr_matrix.shape), k=1 ).astype(np.bool))
to_drop = [column for column in upper.columns if any(abs(upper[column])> 0.95)]
to_drop
df_train['Embarked'].value_counts() /len(df_train )<count_values>
indiv.drop(labels = 'male', axis = 1, inplace = True )
df_train[df_train['Survived']==1]['Embarked'].value_counts() /len(df_train[df_train['Survived']==1])*100<count_values>
indiv['instlevel_argmax'] = np.argmax(np.array(indiv[['instlevel1', 'instlevel2','instlevel3','instlevel4','instlevel5','instlevel6','instlevel7','instlevel8','instlevel9']]),
axis = 1 )
df_train[df_train['Survived']==0]['Embarked'].value_counts() /len(df_train[df_train['Survived']==0])*100<categorify>
indiv['indiv_digital'] = indiv['mobilephone'] + indiv['v18q']
df_train['Sex'] = pd.get_dummies(df_train['Sex'])
 df_train['Age'] = df_train['Age'].fillna(df_train['Age'].median())
 df_train['Embarked'] = df_train['Embarked'].map({'C':0,'Q':1,'S':2} )<count_missing_values>
indiv.drop(labels = ['instlevel1', 'instlevel2','instlevel3','instlevel4','instlevel5',
'instlevel6','instlevel7','instlevel8','instlevel9',
'mobilephone', 'v18q'
]
, axis = 1, inplace = True )
df_train.isnull().sum()<correct_missing_values>
indiv_pivot = pd.pivot_table(indiv,index=["idhogar"],
values=[
'age',
'rez_esc',
'escolari',
'dis',
'female',
'instlevel_argmax',
'indiv_digital',
'tamviv',
'hhsize'
],
aggfunc={
'age':np.sum,
'rez_esc':np.sum,
'escolari':np.sum,
'dis' : np.sum,
'female' : np.sum,
'instlevel_argmax':[np.max,np.min,np.mean] ,
'indiv_digital':[np.max,np.min,np.mean] ,
'tamviv':[np.max,np.min] ,
'hhsize':[np.max,np.min]
},
fill_value=0 )
df_train = df_train.dropna()<import_modules>
indiv_toframe = pd.DataFrame(indiv_pivot.to_records() )
from sklearn.preprocessing import StandardScaler
from sklearn.pipeline import make_pipeline
from sklearn.impute import SimpleImputer
from sklearn.model_selection import train_test_split, cross_validate, GridSearchCV
from sklearn.dummy import DummyClassifier
from sklearn.naive_bayes import GaussianNB
from sklearn.linear_model import LogisticRegression
from sklearn.neighbors import KNeighborsClassifier
from sklearn.ensemble import RandomForestClassifier
from sklearn import metrics
from sklearn.metrics import classification_report, confusion_matrix, roc_auc_score, roc_curve, accuracy_score, precision_score, recall_score, precision_recall_curve
import time
from sklearn import __version__ as sklearn_version<split>
indiv_toframe.columns = [hdr.replace("('", "" ).replace("', '", "_" ).replace("')", "")\
for hdr in indiv_toframe.columns]
X = df_train.drop(['Survived'],axis=1)
 y = df_train['Survived']
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=.2,stratify=y, random_state=42 )<create_dataframe>
indiv_toframe['mean_age_hhsize'] = indiv_toframe['age_sum'] / indiv_toframe['hhsize_amax']
indiv_toframe['mean_age_tamviv'] = indiv_toframe['age_sum'] / indiv_toframe['tamviv_amax']
columns = ['Model Name', 'accuracy','precision','recall','ROC AUC score','run time']
results = pd.DataFrame(columns=columns )<compute_test_metric>
indiv_toframe['mean_escolari_hhsize'] = indiv_toframe['escolari_sum'] / indiv_toframe['hhsize_amax']
indiv_toframe['mean_escolari_tamviv'] = indiv_toframe['escolari_sum'] / indiv_toframe['tamviv_amax']
def metrics(model_name,y_test,y_pred):
accuracy = accuracy_score(y_test,y_pred)
 roc_auc =roc_auc_score(y_test, y_pred)
 precision = precision_score(y_pred=y_pred, y_true=y_test,zero_division=1)
 recall = recall_score(y_pred=y_pred, y_true=y_test,zero_division=1)
 print(classification_report(y_test, y_pred,zero_division=1))
cf_matrix = confusion_matrix(y_test, y_pred)
 sns.heatmap(cf_matrix, annot=True,fmt='3', cmap='Blues')
 plt.xlabel('Predicted user status',fontsize=12)
 plt.ylabel('True user status',fontsize=12)
 plt.title('%s Confusion Matrix' % model_name,fontsize=20)
 plt.show()
fpr, tpr, threshold = roc_curve(y_test, y_pred)
 plt.plot([0,1], [0,1], 'k--')
 plt.plot(fpr, tpr, label=model_name)
 plt.xlabel('False Positive Rate',fontsize=12)
 plt.ylabel('True Positive Rate',fontsize=12)
 plt.title('%s ROC Curve'% model_name,fontsize=20)
 plt.legend(fontsize=12)
 plt.show()
return pd.DataFrame([[model_name,accuracy, precision, recall,roc_auc,t1]],columns=columns )<choose_model_class>
indiv_toframe['mean_rez_esc_hhsize'] = indiv_toframe['rez_esc_sum'] / indiv_toframe['hhsize_amax']
indiv_toframe['mean_rez_esc_tamviv'] = indiv_toframe['rez_esc_sum'] / indiv_toframe['tamviv_amax']
model_name = 'Dummy'
model = DummyClassifier(strategy='most_frequent')
 pipe_dummy = make_pipeline(
 SimpleImputer(strategy='median'),
StandardScaler() ,
model)
 t0 = time.time()
pipe_dummy.fit(X_train,y_train)
 t1 = time.time() - t0
y_pred = pipe_dummy.predict(X_test)
 print('time to run in seconds: ',format(t1))
results = results.append(metrics(model_name,y_test,y_pred))<train_on_grid>
indiv_toframe['mean_female_hhsize'] = indiv_toframe['female_sum'] / indiv_toframe['hhsize_amax']
indiv_toframe['mean_female_tamviv'] = indiv_toframe['female_sum'] / indiv_toframe['tamviv_amax']
model_name = 'Naive Bayes'
NB = GaussianNB()
params_NB = {'var_smoothing': np.logspace(0,-9, num=100)}
grid = GridSearchCV(estimator=NB, param_grid=params_NB, cv=5)
 grid = grid.fit(X_train,y_train)
 model = grid.best_estimator_
pipe_NB = make_pipeline(
 SimpleImputer(strategy='median'),
StandardScaler() ,
model)
 t0 = time.time()
pipe_NB.fit(X_train,y_train)
 t1 = time.time() - t0
y_pred = pipe_NB.predict(X_test)
 print('time to run in seconds: ',format(t1))
results = results.append(metrics(model_name,y_test,y_pred))<train_on_grid>
indiv_toframe['mean_dis_hhsize'] = indiv_toframe['dis_sum'] / indiv_toframe['hhsize_amax']
indiv_toframe['mean_dis_tamviv'] = indiv_toframe['dis_sum'] / indiv_toframe['tamviv_amax']
model_name = 'Logistic Regression'
param_grid = [{'penalty' : ['l1', 'l2'],
'C' : np.logspace(0, 4, 10),
'solver' : ['liblinear']}]
LR = LogisticRegression()
grid = GridSearchCV(estimator=LR, param_grid=param_grid, cv=5)
 grid = grid.fit(X_train,y_train)
 model = grid.best_estimator_
pipe_LR = make_pipeline(
 SimpleImputer(strategy='median'),
StandardScaler() ,
model)
 t0 = time.time()
pipe_LR.fit(X_train,y_train)
 t1 = time.time() - t0
y_pred = pipe_LR.predict(X_test)
 print('time to run in seconds: ',format(t1))
results = results.append(metrics(model_name,y_test,y_pred))<train_on_grid>
head_agg = head.merge(indiv_toframe, on = 'idhogar',
how = 'left' )
model_name = 'kNN'
knn = KNeighborsClassifier()
param_grid = {'n_neighbors': [3, 5, 7, 9, 11],
'weights': ['uniform', 'distance']
}
grid = GridSearchCV(estimator=knn, param_grid=param_grid, cv=5)
 grid = grid.fit(X_train,y_train)
 model = grid.best_estimator_
pipe_kNN = make_pipeline(
 SimpleImputer(strategy='median'),
StandardScaler() ,
model)
 t0 = time.time()
pipe_kNN.fit(X_train,y_train)
 t1 = time.time() - t0
y_pred = pipe_kNN.predict(X_test)
 print('time to run in seconds: ',format(t1))
results = results.append(metrics(model_name,y_test,y_pred))<train_on_grid>
train_labels = np.array(list(head_agg[head_agg['Target'].notnull() ]['Target'].astype(np.uint8)))
 train_set = head_agg[head_agg['Target'].notnull() ].drop(labels = ['Id', 'idhogar', 'Target'], axis = 1)
 test_set = head_agg[head_agg['Target'].isnull() ].drop(labels = ['Id', 'idhogar', 'Target'], axis = 1)
 submission_base = df_test[['Id', 'idhogar']].copy()
model_name = 'Random Forest'
rfc=RandomForestClassifier(random_state=1234)
 param_grid = {'n_estimators': [100,200],
'max_features': ['auto', 'sqrt', 'log2'],
'max_depth' : [5,10,15,20,25,50],
'criterion' :['gini', 'entropy']}
grid = GridSearchCV(estimator=rfc, param_grid=param_grid, cv= 5)
 grid = grid.fit(X_train,y_train)
 model = grid.best_estimator_
pipe_rfc = make_pipeline(
 SimpleImputer(strategy='mean'),
StandardScaler() ,
model)
 t0 = time.time()
pipe_rfc.fit(X_train,y_train)
 t1 = time.time() - t0
y_pred = pipe_rfc.predict(X_test)
 print('time to run in seconds: ',format(t1))
results = results.append(metrics(model_name,y_test,y_pred)) 
<load_from_csv>
sm = SMOTE(random_state = 33 )
df_test = pd.read_csv('/kaggle/input/titanic/test.csv')
 df_test.head()<count_missing_values>
train_set_balanced, train_labels_balanced = sm.fit_sample(train_set, train_labels )
df_test.isnull().sum()<drop_column>
features = list(train_set.columns)
 pipeline = Pipeline([('imputer', Imputer(strategy = 'median')) , 
('scaler', MinMaxScaler())])
 train_set = pipeline.fit_transform(train_set)
 test_set = pipeline.transform(test_set )
passengerId = df_test['PassengerId']
df_test['Family'] = df_test['SibSp'] + df_test['Parch']
df_test = df_test.drop(['Cabin','Name','Ticket','PassengerId'],axis=1 )<feature_engineering>
train_set_balanced = pipeline.fit_transform(train_set_balanced )
df_test['Age'] = df_test['Age'].fillna(df_test['Age'].median())
 df_test['Fare'] = df_test['Fare'].fillna(df_test['Fare'].median() )<categorify>
model_no_smote = RandomForestClassifier(n_estimators=100, random_state=10,
n_jobs = -1)
 cv_score_no_smote = cross_val_score(model_no_smote, train_set, train_labels, cv = 10, scoring = scorer)
 print(f'10 Fold Cross Validation F1 Score = {round(cv_score_no_smote.mean() , 4)} with std = {round(cv_score_no_smote.std() , 4)}' )
df_test['Embarked'] = df_test['Embarked'].map({'C':0,'Q':1,'S':2})
 df_test['Sex'] = pd.get_dummies(df_test['Sex'] )<predict_on_test>
model_smote = RandomForestClassifier(n_estimators=100, random_state=10,
n_jobs = -1)
 cv_score_smote = cross_val_score(model_smote, train_set_balanced, train_labels_balanced, cv = 10, scoring = scorer)
 print(f'10 Fold Cross Validation F1 Score = {round(cv_score_smote.mean() , 4)} with std = {round(cv_score_smote.std() , 4)}' )
prediction = []
for i in range(len(df_test)) :
test = pipe_rfc.predict([df_test.loc[i]])
 prediction.append(test )<create_dataframe>
model_no_smote.fit(train_set, train_labels)
 feature_importances_no_smote = pd.DataFrame({'feature': features, 'importance': model_no_smote.feature_importances_})
 feature_importances_no_smote.head()
prediction = pd.DataFrame(prediction, columns = ['Survived'] )<concatenate>
model_smote.fit(train_set_balanced, train_labels_balanced)
 feature_importances_smote = pd.DataFrame({'feature': features, 'importance': model_smote.feature_importances_})
 feature_importances_smote.head()
test_data_predictions = pd.concat([passengerId,prediction],axis=1 )<save_to_csv>
test_ids = list(head.loc[head['Target'].isnull() , 'idhogar'] )
test_data_predictions.to_csv('Titanic_survivor_prediction.csv',index=False )<load_from_csv>
def macro_f1_score(labels, predictions):
predictions = predictions.reshape(len(np.unique(labels)) , -1 ).argmax(axis = 0)
 metric_value = f1_score(labels, predictions, average = 'macro')
 return 'macro_f1', metric_value, True
pd.read_csv('Titanic_survivor_prediction.csv' )<load_from_csv>
params = {'boosting_type': 'dart',
'colsample_bytree': 0.88,
'learning_rate': 0.028,
'min_child_samples': 10,
'num_leaves': 36, 'reg_alpha': 0.76,
'reg_lambda': 0.43,
'subsample_for_bin': 40000,
'subsample': 0.54,
'class_weight': 'balanced'}
model = lgb.LGBMClassifier(**params, objective = 'multiclass',
n_jobs = -1, n_estimators = 10000,
random_state = 10)
 strkfold = StratifiedKFold(n_splits = 5, shuffle = True )
train_data = pd.read_csv('/kaggle/input/titanic/train.csv')
 train_data.head()<load_from_csv>
strkfold.get_n_splits(train_set_balanced, train_labels_balanced )
test_data = pd.read_csv("/kaggle/input/titanic/test.csv")
 test_data.head()<data_type_conversions>
for train_index, test_index in strkfold.split(train_set, train_labels):
print("TRAIN:", train_index, "TEST:", test_index)
 X_train, X_test = train_set[train_index], train_set[test_index]
y_train, y_test = train_labels[train_index], train_labels[test_index]
data = [train_data, test_data]
for dataset in data:
mean = train_data['Age'].mean()
dataset['Age'].fillna(mean, inplace = True)
 dataset["Age"] = dataset["Age"].astype(int)
<feature_engineering>
for train_index, test_index in strkfold.split(train_set_balanced, train_labels_balanced):
print("TRAIN:", train_index, "TEST:", test_index)
 X_train_balanced, X_test_balanced = train_set_balanced[train_index], train_set_balanced[test_index]
y_train_balanced, y_test_balanced = train_labels_balanced[train_index], train_labels_balanced[test_index]
for dataset in data:
dataset['relatives'] = dataset['SibSp'] + dataset['Parch']
dataset.loc[dataset['relatives'] > 0, 'relatives'] = 1
dataset.loc[dataset['relatives'] == 0, 'relatives'] = 0<categorify>
model_lgb = model.fit(X_train, y_train, early_stopping_rounds =20,
eval_metric = macro_f1_score,
eval_set = [(X_train, y_train),(X_test, y_test)],
eval_names = ['train', 'valid'],
verbose = 200 )
for dataset in data:
dataset['Embarked'].fillna("S", inplace = True )<define_variables>
predictions = model_lgb.predict(test_set)
 predictions = pd.DataFrame({'idhogar': test_ids,
'Target': predictions})
 submission = submission_base.merge(predictions,
on = 'idhogar',
how = 'left' ).drop(labels = ['idhogar'], axis = 1)
 lgb_submission = submission['Target'] = submission['Target'].fillna(4 ).astype(np.int8)
 lgb_submission.to_csv('lgb_submission.csv', index = False )
women = train_data.loc[train_data.Sex == 'female']["Survived"]
rate_women = sum(women)/len(women)
 print("女性生还率:", rate_women)
 men = train_data.loc[train_data.Sex == 'male']["Survived"]
rate_men = sum(men)/len(men)
 print("男性生还率:", rate_men )<save_to_csv>
model_lgb_balanced = model.fit(X_train_balanced, y_train_balanced, early_stopping_rounds = 20,
eval_metric = macro_f1_score,
eval_set = [(X_train_balanced, y_train_balanced),(X_test_balanced, y_test_balanced)],
eval_names = ['train_balanced', 'valid_balanced'],
verbose = 200 )
y = train_data["Survived"]
features = ["Pclass", "Sex", "Age", "Embarked", "relatives"]
X = pd.get_dummies(train_data[features])
 X_test = pd.get_dummies(test_data[features])
 model = RandomForestClassifier(n_estimators=100, max_depth=5, random_state=1)
 model.fit(X, y)
 predictions = model.predict(X_test)
 output = pd.DataFrame({'PassengerId': test_data.PassengerId, 'Survived': predictions})
 output.to_csv('my_submission.csv', index=False)
 print("Your submission was successfully saved!" )<install_modules>
predictions = model_lgb_balanced.predict(test_set)
 predictions = pd.DataFrame({'idhogar': test_ids,
'Target': predictions})
 submission = submission_base.merge(predictions,
on = 'idhogar',
how = 'left' ).drop(labels = ['idhogar'], axis = 1)
 lgb_balanced_submission = submission['Target'] = submission['Target'].fillna(4 ).astype(np.int8)
 lgb_balanced_submission.to_csv('lgb_submission.csv', index = False )
pip install -U lightautoml<import_modules>
test_ids = list(head.loc[head['Target'].isnull() , 'idhogar'] )
import os
import time
import re
import numpy as np
import pandas as pd
from sklearn.metrics import accuracy_score
from sklearn.model_selection import train_test_split
import torch
from lightautoml.automl.presets.tabular_presets import TabularAutoML, TabularUtilizedAutoML
from lightautoml.dataset.roles import DatetimeRole
from lightautoml.tasks import Task
from lightautoml.utils.profiler import Profiler<define_variables>
def submit(model, train, train_labels, test, test_ids):

 model.fit(train, train_labels)
 predictions = model.predict(test)
 predictions = pd.DataFrame({'idhogar': test_ids,
'Target': predictions})
 submission = submission_base.merge(predictions,
on = 'idhogar',
how = 'left' ).drop(labels = ['idhogar'], axis = 1)
 submission['Target'] = submission['Target'].fillna(4 ).astype(np.int8)
 return submission
N_THREADS = 4
N_FOLDS = 5
RANDOM_STATE = 42
TEST_SIZE = 0.2
TIMEOUT = 600<set_options>
rf_submission = submit(RandomForestClassifier(n_estimators = 100,
random_state=10, n_jobs = -1),
train_set, train_labels, test_set, test_ids)
 rf_submission.to_csv('rf_submission.csv', index = False )
np.random.seed(RANDOM_STATE)
 torch.set_num_threads(N_THREADS )<load_from_csv>
rf_balanced_submission = submit(RandomForestClassifier(n_estimators = 100,
random_state=10, n_jobs = -1),
train_set_balanced, train_labels_balanced, test_set, test_ids)
 rf_balanced_submission.to_csv('rf_balanced_submission.csv', index = False )