Kaggle competition: Costa Rican Household Poverty Level Prediction
Y_test_sub = rf_clf.predict(X_test_scaled)
 sub['surface'] = le.inverse_transform(Y_test_sub)
 sub.to_csv('submission_rf.csv', index=False )<import_modules>
warnings.filterwarnings('ignore' )
import numpy as np
import pandas as pd<load_from_csv>
n_jobs = multiprocessing.cpu_count()
n_jobs
train_data = pd.read_csv('/kaggle/input/titanic/train.csv')
 test_data = pd.read_csv('/kaggle/input/titanic/test.csv' )<define_variables>
from sklearn.metrics import classification_report
from sklearn.feature_selection import SelectFromModel
from sklearn.model_selection import KFold, GridSearchCV, cross_val_score, cross_val_predict
from sklearn.metrics import accuracy_score, recall_score, f1_score
from sklearn.metrics.scorer import make_scorer
from xgboost import plot_tree, plot_importance
women = train_data[train_data['Sex'] == 'female']['Survived']
rate_women = sum(women)/len(women)
 print('% of women who survived:', rate_women )<define_variables>
import xgboost as xgb
from sklearn.model_selection import train_test_split, cross_validate
from sklearn.feature_selection import VarianceThreshold, SelectKBest, f_classif
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import confusion_matrix
men = train_data[train_data.Sex == 'male']['Survived']
rate_men = sum(men)/len(men)
 print('% of men who survived:', rate_men )<groupby>
%matplotlib inline
train_data[['Sex', 'Survived']].groupby(['Sex'] ).mean()<groupby>
df = pd.read_csv('.. /input/train.csv' )
train_data[['Pclass', 'Survived']].groupby(['Pclass'] ).mean()<define_variables>
pd.options.display.max_columns = num_of_cols
women_count = 0
women_survived_count = 0
for idx, row in train_data.iterrows() :
if row['Sex'] == 'female':
women_count += 1
if row['Survived'] == 1:
women_survived_count += 1
women_survived_count / women_count<define_variables>
df_isna = pd.DataFrame(df.isnull().sum())
 df_isna.loc[(df_isna.loc[:, df_isna.dtypes != object] != 0 ).any(1)]
predictions = []
for idx, row in test_data.iterrows() :
if row['Sex'] == 'female':
if row['Pclass'] <3:
predictions.append(1)
 elif row['Fare'] < 25:
predictions.append(1)
 else:
predictions.append(0)
 else:
if row['Age'] < 10:
predictions.append(1)
 else:
predictions.append(0)
<feature_engineering>
hid_heads = df.groupby(['idhogar'])['parentesco1'].apply(lambda x: pd.unique(x.values.ravel() ).tolist() ).reset_index()
len(hid_heads )
test_data['Survived'] = predictions<save_to_csv>
df_hid = pd.DataFrame(hid_heads, index=None, columns=['idhogar','parentesco1'])
 df_hid.sample(5 )
test_data[['PassengerId', 'Survived']].to_csv('submission.csv', index=False )<import_modules>
df_hid['parentesco1'] = df_hid['parentesco1'].apply(lambda x: ''.join(map(str, x)) )
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn.preprocessing import LabelEncoder
from sklearn.model_selection import train_test_split
from sklearn.neighbors import KNeighborsClassifier
from sklearn.svm import SVC
from sklearn.tree import DecisionTreeClassifier
from sklearn.ensemble import RandomForestClassifier, AdaBoostClassifier, GradientBoostingClassifier
from sklearn.naive_bayes import GaussianNB
from sklearn.discriminant_analysis import LinearDiscriminantAnalysis, QuadraticDiscriminantAnalysis
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import roc_auc_score
from sklearn.model_selection import RandomizedSearchCV
from collections import Counter<load_from_csv>
df_hid.loc[df_hid.parentesco1 == '0']
train=pd.read_csv('/kaggle/input/titanic/train.csv')
 test=pd.read_csv('/kaggle/input/titanic/test.csv')
 target=train['Survived']
def detect_outlier(df,n,cols):
outlier_indices = []
for i in cols:
Q1 = np.percentile(df[i], 20)
 Q3 = np.percentile(df[i], 80)
 IQR = Q3 - Q1
outlier_step = 1.5*IQR
outlier_index_list = df[(df[i] < Q1-outlier_step)|(df[i] > Q3+outlier_step)].index
outlier_indices.extend(outlier_index_list)
 outlier_indices = Counter(outlier_indices)
 multiple_outliers = list(k for k,v in outlier_indices.items() if v>n)
 return multiple_outliers
outliers_to_drop = detect_outlier(train,3,['Age', 'SibSp', 'Parch', 'Fare'])
 train = train.drop(outliers_to_drop, axis = 0 ).reset_index(drop=True)
 print('Shape of train dataset:-',train.shape)
 print('Shape of test dataset:-' ,test.shape)
 print('
')
 print(train.info())
 train.describe() 
<prepare_x_and_y>
hid_wo_heads = list(df_hid['idhogar'].loc[df_hid.parentesco1 == '0'])
 len(hid_wo_heads )
total=pd.concat([train.drop('Survived',axis=1),test])
 target=train['Survived']
total.head()<data_type_conversions>
df_hwoh = df[df['idhogar'].isin(hid_wo_heads)]
print(total.isnull().sum())
 total['Age'] = total.groupby('Pclass')['Age'].transform(lambda x: x.fillna(x.median()))
total['Fare'] = total.groupby('Pclass')['Fare'].transform(lambda x: x.fillna(x.median()))
total['Embarked'].fillna('S',inplace=True)
<categorify>
print(df[['Id','v2a1','idhogar','parentesco1','Target']].loc[df.idhogar == '09b195e7a'])
 print(df[['Id','v2a1','idhogar','parentesco1','Target']].loc[df.idhogar == 'f2bfa75c4'])
encoder=LabelEncoder()
total['Sex']=encoder.fit_transform(total['Sex'])
 total['Embarked']=encoder.fit_transform(total['Embarked'])
 total=pd.get_dummies(total,columns=['Pclass','Embarked'] )<feature_engineering>
print("before removal: ", len(df))
df = df.loc[-df['idhogar'].isin(hid_wo_heads)]
print("after removal: ", len(df))
total['Fare_1_S']=total['Embarked_2']*total['Pclass_1']*total['Sex'] 
<categorify>
gc.collect()
total['Title'] =total['Name'].str.extract('([A-Za-z]+)\.', expand=False)
 total['Title'] =total['Title'].replace(['Lady', 'Countess','Capt', 'Col', 'Don', 'Dr', 'Major', 'Rev', 'Sir', 'Jonkheer', 'Dona'], 'Rare')
 total['Title'] =total['Title'].replace('Mlle', 'Miss')
 total['Title'] =total['Title'].replace('Ms', 'Miss')
 total['Title'] =total['Title'].replace('Mme', 'Mrs')
 title_mapping = {"Mr": 1, "Miss": 2, "Mrs": 2, "Master": 3, "Rare": 4}
total['Title'] =total['Title'].map(title_mapping)
 total['Title'] =total['Title'].fillna(0)
 total['Age_cat'] = pd.qcut(total['Age'],q=[0,.16,.33,.49,.66,.83, 1], labels=False, precision=1)
 def fare_category(fr):
if fr <= 7.91:
return 1
elif fr <= 14.454 and fr > 7.91:
return 2
elif fr <= 31 and fr > 14.454:
return 3
return 4
total['Fare_cat'] =total['Fare'].apply(fare_category)
 total['FamilySize'] =total['SibSp'] + total['Parch'] + 1
total['FamilySize_cat'] =total['FamilySize'].map(lambda x: 1 if x == 1
else(2 if 5 > x >= 2
else(3 if 8 > x >= 5
else 4)
)) 
<categorify>
len(df['v2a1'].unique() )
total.drop(['Name','Ticket','Cabin'],axis=1,inplace=True)
 total=pd.get_dummies(total,columns=['SibSp','Parch','Age_cat','Title','FamilySize','Fare_cat','FamilySize_cat'])
 total['Age']=total['Age'].astype(int )<split>
df[['v2a1','idhogar','parentesco1','Target']].loc[df.v2a1 > 1000000]
train=total[:len(train)]
test=total[len(train):]
np.random.seed(42)
 X_train, X_test, y_train, y_test = train_test_split(train,target, test_size = 0.2)
 models = {"KNN": KNeighborsClassifier() ,
"Logistic Regression": LogisticRegression(max_iter=10000),
"Random Forest": RandomForestClassifier() ,
"SVC" : SVC(probability=True),
"DecisionTreeClassifier" : DecisionTreeClassifier() ,
"AdaBoostClassifier" : AdaBoostClassifier() ,
"GradientBoostingClassifier" : GradientBoostingClassifier() ,
"GaussianNB" : GaussianNB() ,
"LinearDiscriminantAnalysis" : LinearDiscriminantAnalysis() ,
"QuadraticDiscriminantAnalysis" : QuadraticDiscriminantAnalysis() }
def fit_and_score(models, X_train, X_test, y_train, y_test):
np.random.seed(42)
 model_scores = {}
for name, model in models.items() :
model.fit(X_train, y_train)
 y_pred = model.predict(X_test)
 model_scores[name] = roc_auc_score(y_pred, y_test)
 return model_scores
model_scores = fit_and_score(models=models,
X_train=X_train,
X_test=X_test,
y_train=y_train,
y_test=y_test)
 model_scores 
<define_variables>
df[['v2a1','idhogar','parentesco1','Target']].loc[df.v2a1 >= 1000000]
leaks = {
897:1,
899:1,
930:1,
932:1,
949:1,
987:1,
995:1,
998:1,
999:1,
1016:1,
1047:1,
1083:1,
1097:1,
1099:1,
1103:1,
1115:1,
1118:1,
1135:1,
1143:1,
1152:1,
1153:1,
1171:1,
1182:1,
1192:1,
1203:1,
1233:1,
1250:1,
1264:1,
1286:1,
935:0,
957:0,
972:0,
988:0,
1004:0,
1006:0,
1011:0,
1105:0,
1130:0,
1138:0,
1173:0,
1284:0,
}
model=GradientBoostingClassifier()
model.fit(train,target)
 sub = pd.DataFrame()
sub['PassengerId'] = test['PassengerId']
sub['Survived'] = model.predict(test)
 sub['Survived'] = sub['Survived'].apply(lambda x: 1 if x>0.8 else 0)
 sub['Survived'] = sub.apply(lambda r: leaks[int(r['PassengerId'])] if int(r['PassengerId'])in leaks else r['Survived'], axis=1)
 sub.to_csv('sub_titan.csv', index=False )<set_options>
df[['v2a1','idhogar','parentesco1','Target']].loc[df.idhogar == '563cc81b7']
sns.set(style="darkgrid")
 for dirname, _, filenames in os.walk('/kaggle/input'):
for filename in filenames:
print(os.path.join(dirname, filename)) 
<load_from_csv>
print("before removal: ", len(df))
df.drop(df[df.idhogar == '563cc81b7'].index, inplace=True)
 print("after removal: ", len(df))
data = pd.read_csv("/kaggle/input/titanic/train.csv")
 data_test = pd.read_csv("/kaggle/input/titanic/test.csv" )<prepare_x_and_y>
col_types = {}
for col in cols:
col_types[col] = df[col].dtype
y = data["Survived"]
X = data.copy()
X_test = data_test.copy()
X_full = pd.concat([X, X_test])
 n = len(X)
 n_test = len(X_test)
 n_full = n + n_test<count_missing_values>
for key in sorted(col_types):
print(key, col_types[key] )
( X_full.drop(["Survived"], axis = 1 ).isna().sum() / n_full)* 100<categorify>
cat_cols = []
num_cols = []
for col in cols:
if df[col].dtype == 'O':
cat_cols.append(col)
 print(col, df[col].dtype)
 else:
num_cols.append(col )
def fill_na_age(row):
row["Age"] = df_median_age_class[row["Sex"]][row["Pclass"]][0] if pd.isnull(row["Age"])else row["Age"]
return row
X_full = X_full.apply(fill_na_age, axis = 1)
 X_full.info()<count_missing_values>
sorted(num_cols )
print(X_full[X_full["Fare"].isnull() ] )<categorify>
cols_electronics = ['refrig','mobilephone','television','qmobilephone','computer', 'v18q', 'v18q1', ]
cols_house_details = ['v2a1', 'area1', 'area2', 'bedrooms','rooms', 'cielorazo', 'v14a',
'tamhog', 'hacdor', 'hacapo', 'r4t3', ]
cols_person_details = ['age', 'agesq', 'female', 'male',]
cols_SQ = ['SQBage', 'SQBdependency', 'SQBedjefe', 'SQBescolari', 'SQBhogar_nin',
'SQBhogar_total', 'SQBmeaned', 'SQBovercrowding',]
cols_water = ['abastaguadentro', 'abastaguafuera', 'abastaguano',]
cols_h = [ 'hhsize', 'hogar_adul', 'hogar_mayor', 'hogar_nin', 'hogar_total',]
cols_r = ['r4h1', 'r4h2', 'r4h3', 'r4m1', 'r4m2', 'r4m3', 'r4t1', 'r4t2', 'r4t3',]
cols_tip = ['tipovivi1', 'tipovivi2', 'tipovivi3', 'tipovivi4', 'tipovivi5',]
cols_roof = ['techocane', 'techoentrepiso', 'techootro', 'techozinc',]
cols_floor = ['pisocemento', 'pisomadera', 'pisomoscer', 'pisonatur', 'pisonotiene', 'pisoother',]
cols_sanitary = [ 'sanitario1', 'sanitario2', 'sanitario3', 'sanitario5', 'sanitario6',]
cols_parents = [ 'parentesco1', 'parentesco10', 'parentesco11', 'parentesco12', 'parentesco2', 'parentesco3',
'parentesco4', 'parentesco5', 'parentesco6', 'parentesco7', 'parentesco8', 'parentesco9',]
cols_outside_wall = [ 'paredblolad', 'pareddes', 'paredfibras', 'paredmad', 'paredother',
'paredpreb', 'paredzinc', 'paredzocalo',]
cols_instlevel = [ 'instlevel1', 'instlevel2', 'instlevel3', 'instlevel4', 'instlevel5', 'instlevel6',
'instlevel7', 'instlevel8', 'instlevel9',]
cols_lugar = [ 'lugar1', 'lugar2', 'lugar3', 'lugar4', 'lugar5', 'lugar6',]
cols_estadoc = [ 'estadocivil1', 'estadocivil2', 'estadocivil3', 'estadocivil4',
'estadocivil5', 'estadocivil6', 'estadocivil7',]
cols_elim = ['elimbasu1', 'elimbasu2', 'elimbasu3', 'elimbasu4', 'elimbasu5', 'elimbasu6',]
cols_energ = ['energcocinar1', 'energcocinar2', 'energcocinar3', 'energcocinar4',]
cols_eviv = [ 'eviv1', 'eviv2', 'eviv3',]
cols_etech = [ 'etecho1', 'etecho2', 'etecho3',]
cols_pared = [ 'epared1', 'epared2', 'epared3',]
cols_unknown = [ 'dis', 'escolari', 'meaneduc',
'overcrowding', 'rez_esc', 'tamhog', 'tamviv', ]
cols_elec = ['coopele', 'noelec', 'planpri', 'public',]
total_features = cols_electronics+cols_house_details+cols_person_details+\
cols_SQ+cols_water+cols_h+cols_r+cols_tip+cols_roof+\
cols_floor+cols_sanitary+cols_parents+cols_outside_wall+\
cols_instlevel+cols_lugar+cols_estadoc+cols_elim+cols_energ+\
cols_eviv+cols_etech+cols_pared+cols_unknown+cols_elec
len(total_features )
def fill_na_fare(row):
row["Fare"] = df_median_fare_class[row["Pclass"]][0] if pd.isnull(row["Fare"])else row["Fare"]
return row
X_full = X_full.apply(fill_na_fare, axis = 1)
 X_full.info()
print(X_full[X_full["PassengerId"] == 1044] )<filter>
cols_electronics.remove('Target')
 cols_electronics
X_full[X_full["Embarked"].isnull() ]<groupby>
total_features.remove('r4t3')
 total_features.remove('tamhog')
 total_features.remove('tamviv')
 len(total_features )
embarked_survived = X.groupby(["Embarked", "Survived"])["PassengerId"].count()
embarked_survived.head(10 )<create_dataframe>
for x,y in zip(num_cols, list(es_corr)) :
if(y >= 0.75)or(y < -0.6):
print(x,y )
def display_embarked_prob() :
surv_counts = {
"C": {},
"Q": {},
"S": {}
}
classes = set(embarked_survived.index.get_level_values(0))
for c in classes:
for survive in range(0, 2):
surv_counts[str(c)][survive] = embarked_survived[c][survive]
df_surv = pd.DataFrame(surv_counts)
 surv_percentages = {}
for col in df_surv.columns:
surv_percentages[col] = [(count / df_surv[col].sum())* 100 for count in df_surv[col]]
df_survived_percentages = pd.DataFrame(surv_percentages ).transpose()
class_names =("C", "Q", "S")
 bar_count = np.arange(len(class_names))
bar_width = 0.5
not_survived = df_survived_percentages[0]
survived = df_survived_percentages[1]
plt.figure(figsize=(10, 6))
plt.bar(bar_count, not_survived, color='
plt.bar(bar_count, survived, bottom=not_survived, color='
plt.xlabel('Embarked', size=15, labelpad=20)
 plt.ylabel('Survival Percentage', size=15, labelpad=20)
 plt.xticks(bar_count, class_names)
 plt.tick_params(axis='x', labelsize=15)
 plt.tick_params(axis='y', labelsize=15)
 plt.legend(loc='upper left', bbox_to_anchor=(1, 1), prop={'size': 15})
 plt.title('Survival Percentage by Embarked', size=18, y=1.05)
 plt.show()
display_embarked_prob()<groupby>
for x,y in zip(num_cols, list(sqbes_corr)) :
if(y >= 0.5)or(y < -0.6):
print(x,y )
embarked_class_passengers = X.groupby("Pclass")["Embarked"].value_counts()
print(embarked_class_passengers )<feature_engineering>
total_features.remove('escolari')
 len(total_features )
X_full["CabinId"] = X_full["Cabin"].map(lambda cabin: cabin if pd.isnull(cabin)else cabin[0])
 X["CabinId"] = X["Cabin"].map(lambda cabin: cabin if pd.isnull(cabin)else cabin[0])
 X["CabinId"].sample(10)
<groupby>
df['meaneduc'].fillna(0, inplace=True)
 df['SQBmeaned'].fillna(0, inplace=True )
X.groupby(["CabinId", "Pclass"])["PassengerId"].count()<groupby>
total_features.remove('meaneduc')
 total_features
class_cabin_passengers = X.groupby(["Pclass", "CabinId"])["PassengerId"].count()
print(class_cabin_passengers )<groupby>
df['v18q1'] = df['v18q'].groupby(df['idhogar'] ).transform('sum' )
survival_by_gender = X.groupby("Sex" ).apply(lambda df: sum(df["Survived"] == 1))
survival_by_gender<feature_engineering>
ff = pd.DataFrame(df.isnull().sum())
 ff.loc[(ff.loc[:, ff.dtypes != object] != 0 ).any(1)]
survival_by_gender["female"] /(survival_by_gender["female"] + survival_by_gender["male"] )<groupby>
df['rez_esc'].isnull().sum()
survival_by_class = X.groupby("Pclass")["Survived"].apply(lambda x: {"Survived": sum(x == 1), "Not Survived": sum(x == 0)})
 survival_by_class<groupby>
len(df)- df['rez_esc'].isnull().sum()
gender_by_class = X.groupby("Pclass")["Sex"].apply(lambda x: {"Male": sum(x == "male"), "Female": sum(x == "female")})
 gender_by_class<groupby>
df['v2a1'].isnull().sum()
df_class_survived = X.groupby(['Pclass', "Survived", "Sex"] ).count().drop(columns=[
'Age',
'SibSp',
'Parch',
'Fare',
'Embarked',
'Cabin',
'PassengerId',
'Ticket'] ).rename(columns={'Name':'Count'} ).transpose()
df_class_survived.head(15 )<create_dataframe>
len(df)- df['v2a1'].isnull().sum()
def display_gender_by_class_prob(gender):
surv_counts = {
"1": {},
"2": {},
"3": {}
}
classes = df_class_survived.columns.levels[0]
for c in classes:
for survive in range(0, 2):
surv_counts[str(c)][survive] = df_class_survived[c][survive][gender][0]
df_surv = pd.DataFrame(surv_counts)
 surv_percentages = {}
for col in df_surv.columns:
surv_percentages[col] = [(count / df_surv[col].sum())* 100 for count in df_surv[col]]
df_survived_percentages = pd.DataFrame(surv_percentages ).transpose()
class_names =("1", "2", "3")
 bar_count = np.arange(len(class_names))
bar_width = 0.5
not_survived = df_survived_percentages[0]
survived = df_survived_percentages[1]
plt.figure(figsize=(10, 6))
plt.bar(bar_count, not_survived, color='
plt.bar(bar_count, survived, bottom=not_survived, color='
plt.xlabel('Pclass', size=15, labelpad=20)
 plt.ylabel('Survival Percentage', size=15, labelpad=20)
 plt.xticks(bar_count, class_names)
 plt.tick_params(axis='x', labelsize=15)
 plt.tick_params(axis='y', labelsize=15)
 plt.legend(loc='upper left', bbox_to_anchor=(1, 1), prop={'size': 15})
 plt.title('Survival Percentage by Class for gender {}'.format(gender), size=18, y=1.05)
 plt.show()
display_gender_by_class_prob("female")
 display_gender_by_class_prob("male" )<define_variables>
len(df.loc[(df.v2a1 >= 0)]), len(df.loc[(df.rez_esc >= 0)] )
numerical_columns = ["Age", "SibSp", "Parch", "Fare"]
categorical_columns = ["Pclass", "Embarked", "Sex"]
feature_columns = numerical_columns + categorical_columns
X = X[feature_columns]
X_test = X_test[feature_columns]
X.head()<split>
len(df.loc[(df.v2a1 >= 0)&(df.rez_esc >= 0)] )
X_train, X_valid, y_train, y_valid = train_test_split(X, y,
train_size=0.7, test_size=0.3,
random_state=0 )<count_missing_values>
len(df.loc[(df.v2a1 >= 0)|(df.rez_esc >= 0)] )
X_train.isna().sum()<count_missing_values>
df['rez_esc'].fillna(0, inplace=True )
X_train.isna().sum()<categorify>
ff = pd.DataFrame(df.isnull().sum())
 ff.loc[(ff.loc[:, ff.dtypes != object] != 0 ).any(1)]
numerical_transformer = SimpleImputer(strategy='mean')
 categorical_transformer = Pipeline(steps=[ 
('imputer', SimpleImputer(strategy='constant')) , 
('onehot', OneHotEncoder(handle_unknown='ignore'))
] )<feature_engineering>
df['Target'].value_counts()
preprocessor = ColumnTransformer(
 transformers=[ 
('num', numerical_transformer, numerical_columns), 
('cat', categorical_transformer, categorical_columns)
 ] )<choose_model_class>
df.groupby('Target' ).count() ['v2a1']
model = RandomForestClassifier(n_estimators = 150, max_depth = 5, random_state=0 )<choose_model_class>
df['hhsize'].value_counts()
pipeline = Pipeline(steps=[ 
('preprocessor', preprocessor), 
('model', model)
 ] )<train_model>
df['hogar_total'].value_counts()
pipeline.fit(X_train, y_train )<predict_on_test>
total_features.remove('hogar_total')
 len(total_features )
preds = pipeline.predict(X_valid)
 preds<compute_test_metric>
df['male'].value_counts()
accuracy_score(y_valid, preds )<compute_test_metric>
df['female'].value_counts()
recall_score(y_valid, preds )<predict_on_test>
total_features.remove('female')
 len(total_features )
preds_test = pipeline.predict(X_test)
 preds_test.shape<prepare_output>
df['r4t3'].value_counts()
output = pd.DataFrame({"PassengerId": data_test["PassengerId"],
"Survived": preds_test})
 output.head()<save_to_csv>
df['tamhog'].value_counts()
output.to_csv('submission.csv', index=False )<train_model>
df['tamviv'].value_counts()
lr_model = LogisticRegression(random_state=0)
 lr_pipeline = Pipeline(steps = [ 
('preprocessor', preprocessor), 
('model', lr_model)
 ])
 lr_pipeline.fit(X_train, y_train)
 lr_preds = lr_pipeline.predict(X_valid)
 accuracy_score(y_valid, lr_preds)
<save_to_csv>
total_features.remove('r4t3' )
lr_preds_test = lr_pipeline.predict(X_test)
 lr_output = pd.DataFrame({"PassengerId": data_test["PassengerId"],
"Survived": lr_preds_test})
 lr_output.to_csv('lr_submission.csv', index=False )<train_model>
df['dependency'].value_counts()
xgb_model = XGBClassifier(n_estimators = 1000, learning_rate = 0.1, max_depth = 5, random_state=0)
 xgb_pipeline = Pipeline(steps = [ 
('preprocessor', preprocessor), 
('model', xgb_model)
 ])
 xgb_pipeline.fit(X_train, y_train)
 xgb_preds = xgb_pipeline.predict(X_valid)
 accuracy_score(y_valid, xgb_preds )<save_to_csv>
df['SQBdependency'].value_counts()
xgb_preds_test = xgb_pipeline.predict(X_test)
 xgb_output = pd.DataFrame({"PassengerId": data_test["PassengerId"],
"Survived": xgb_preds_test})
 xgb_output.to_csv('xgb_submission.csv', index=False )<train_model>
df['edjefe'].value_counts()
svm_model = LinearSVC(dual = False, random_state=0)
 svm_pipeline = Pipeline(steps = [ 
('preprocessor', preprocessor), 
('model', svm_model)
 ])
 svm_pipeline.fit(X_train, y_train)
 svm_preds = svm_pipeline.predict(X_valid)
 accuracy_score(y_valid, svm_preds )<save_to_csv>
df['edjefa'].value_counts()
svm_preds_test = svm_pipeline.predict(X_test)
 svm_output = pd.DataFrame({"PassengerId": data_test["PassengerId"],
"Survived": svm_preds_test})
 svm_output.to_csv('svm_submission.csv', index=False )<categorify>
df.loc[df.edjefa == 'yes', 'edjefa'] = 1
df.loc[df.edjefa == 'no', 'edjefa'] = 0
doggo_images = np.empty(( len(imagenames), 224, 224, 3), dtype=np.float16)
 doggo_targets = np.empty(( len(imagenames)) , dtype=np.uint8)
 labels = pd.read_csv('/kaggle/input/dog-breed-identification/labels.csv')
 le = preprocessing.LabelEncoder()
labels['target'] = le.fit_transform(labels.breed )<count_unique_values>
df.loc[df.edjefe == 'yes', 'edjefe'] = 1
df.loc[df.edjefe == 'no', 'edjefe'] = 0
class_dogs = []
breed = []
for i in np.sort(np.unique(doggo_targets)) :
class_dogs.append(doggo_images[doggo_targets == i][np.random.randint(50)])
 breed.append(i )<data_type_conversions>
df[['edjefa','edjefe']] = df[['edjefa','edjefe']].apply(pd.to_numeric)
plt.figure(figsize=(11,11))
for i in range(len(np.unique(doggo_targets))):
plt.subplot(11, 11, i+1)
 plt.imshow(class_dogs[i].astype(np.float32))
plt.axis('off')
 plt.show()<train_on_grid>
total_features.append('edjefa')
 total_features.append('edjefe' )
keras.backend.clear_session()
model_base = keras.applications.inception_resnet_v2.InceptionResNetV2(weights='imagenet', include_top=False, input_shape=(224,224,3))
model = keras.models.Sequential()
for layer in model_base.layers:
layer.trainable = False
model.add(model_base)
 model.add(keras.layers.GlobalAveragePooling2D())
 model.add(keras.layers.Dense(120, activation='softmax'))
model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])
 history = model.fit(doggo_images, doggo_targets, validation_split=0.05, epochs=20, batch_size=1024 )<predict_on_test>
df['abastaguadentro'].value_counts()
predictions = pd.DataFrame(model.predict(doggo_test_images))
predictions.columns = list(le.inverse_transform([int(col)for col in predictions.columns]))
predictions['id'] = [testname.rstrip('.jpg')for testname in testnames]<save_to_csv>
df['abastaguafuera'].value_counts()
predictions.to_csv('submission.csv', index=False )<set_options>
df['abastaguano'].value_counts()
%reload_ext autoreload
%autoreload 2
%matplotlib inline<import_modules>
df_water_target = df.groupby('Target')[cols_water].sum().reset_index()
df_water_target
from fastai import *
from fastai.vision import *<set_options>
total_features.remove('abastaguano')
 total_features.remove('abastaguafuera')
 len(total_features )
print(torch.cuda.is_available() , torch.backends.cudnn.enabled )<load_from_csv>
df['pisocemento'].value_counts()
path_model='/kaggle/working/'
path_input="/kaggle/input/"
label_df = pd.read_csv(f"{path_input}labels.csv")
 label_df.head()<create_dataframe>
df_floor_target = df.groupby('Target')[cols_floor].sum().reset_index()
df_floor_target
label_df.pivot_table(index='breed',aggfunc=len ).sort_values('id',ascending=False )<load_from_csv>
total_features.remove('pisonatur')
 total_features.remove('pisonotiene')
 total_features.remove('pisoother')
 len(total_features )
data = ImageDataBunch.from_csv(
 path_input,
folder='train',
valid_pct=0.2,
ds_tfms=get_transforms(flip_vert=True,max_rotate=20., max_zoom=1.1),
size=224,
test='/kaggle/input/test/test',
suffix='.jpg',
bs=64,
num_workers=0 ).normalize(imagenet_stats )<define_variables>
df_wall_target = df.groupby('Target')[cols_outside_wall].sum().reset_index()
df_wall_target
data.show_batch(rows=3, figsize=(8,8))<define_variables>
df_roof_target = df.groupby('Target')[cols_roof].sum().reset_index()
df_roof_target
[print(len(e)) for e in [data.train_ds, data.valid_ds, data.test_ds]]<train_model>
df_sani_target = df.groupby('Target')[cols_sanitary].sum().reset_index()
df_sani_target
files = os.listdir(f'{path_input}train/')[:5]
img = plt.imread(f'{path_input}train/{files[0]}')
 plt.imshow(img )<train_model>
df['v2a1'].isna().sum()
learner.fit_one_cycle(3 )<predict_on_test>
df['tipovivi3'].value_counts()
np.set_printoptions(precision=6, suppress=True)
 test_result = learner.get_preds(ds_type=DatasetType.Test )<define_variables>
df.loc[(df['v2a1'].isna())&(df.tipovivi3 == 1)]
for i in range(0, 12):
print(np.array(test_result[0][1][i*10:i*10+10]))<prepare_output>
df[['v2a1','idhogar','parentesco1']].loc[(df.parentesco1 == 1)&(-df.v2a1.isna())&(df.tipovivi3==1)&(df.tipovivi1==0)].mean()
pd.options.display.float_format = '{:.6f}'.format
df = pd.DataFrame(np.array(test_result[0]))
df.columns = data.classes
df.head()<prepare_output>
df[['v2a1','idhogar','parentesco1']].loc[(df.parentesco1 != 1)&(-df.v2a1.isna())&(df.tipovivi3==1)&(df.tipovivi1==0)].mean()
df.insert(0, "id", [e.name[:-4] for e in data.test_ds.x.items] )<save_to_csv>
df['v2a1'].fillna(120000, inplace=True )
df.to_csv(f"dog-breed-identification-submission.csv", index=False )<load_from_csv>
df['v2a1'].isna().sum()
X_full = pd.read_csv('.. /input/titanic/train.csv', index_col='PassengerId' )<count_missing_values>
X, y = df[total_features], df['Target']
np.random.seed(0)
 missing_values_count = X_full.isnull().sum()
total_cells = np.product(X_full.shape)
 total_missing = missing_values_count.sum()
percent_missing =(total_missing/total_cells)
 print(percent_missing)
 print(total_cells)
 print(total_missing )<load_from_csv>
seed = 42
test_size = 0.3
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=test_size, random_state=seed )
X_test_full = pd.read_csv('.. /input/titanic/test.csv', index_col='PassengerId' )<split>
model1 = xgb.XGBClassifier(n_jobs=n_jobs)
 model1
X_full.dropna(axis=0, subset=['Survived'], inplace=True)
 y = X_full.Survived
X_full.drop(['Survived'], axis=1, inplace=True)
 X_full.drop(['Name', 'Ticket', 'Fare', 'SibSp'], axis=1, inplace=True)
 X_train_full, X_valid_full, y_train, y_valid = train_test_split(X_full, y, train_size=0.8, test_size=0.2, random_state=0)
 categorical_cols = [cname for cname in X_train_full.columns if
X_train_full[cname].dtype == 'object']
numerical_cols = [cname for cname in X_train_full.columns if
X_train_full[cname].dtype in ['int64', 'float64']]
my_cols = categorical_cols + numerical_cols
X_train = X_train_full[my_cols].copy()
X_valid = X_valid_full[my_cols].copy()
X_test = X_test_full[my_cols].copy()
X_train.head()<categorify>
train_model1 = model1.fit(X_train, y_train )
numerical_transformer = SimpleImputer(strategy='most_frequent')
 categorical_transformer = Pipeline(steps=[ 
('imputer', SimpleImputer(strategy='most_frequent')) , 
('onehot', OneHotEncoder(handle_unknown='ignore'))
])
 preprocessor = ColumnTransformer(
 transformers=[ 
('num', numerical_transformer, numerical_cols), 
('cat', categorical_transformer, categorical_cols)
 ])
 n_estimators = [24, 25, 26]
max_depth = [8, 9, 10]
min_samples_split = [11, 12, 13]
min_samples_leaf = [1, 2, 3]
criterion = ['gini', 'entropy']
model = RandomForestClassifier(n_estimators=100, max_depth=3, random_state=0)
 model = RandomForestClassifier(n_estimators=25, max_depth=9, min_samples_leaf=1, min_samples_split=12, random_state=0)
 hyperF = dict(n_estimators = n_estimators, max_depth = max_depth, min_samples_split = min_samples_split, min_samples_leaf = min_samples_leaf, criterion = criterion)
 gridF = GridSearchCV(model, hyperF, cv = 3, verbose = 1, n_jobs = -1)
 clf = Pipeline(steps=[('preprocessor', preprocessor), 
('model', model)
 ])
 grid_test = Pipeline(steps=[('preprocessor', preprocessor), 
('model', gridF)
 ])
 grid_test.fit(X_train, y_train)
 clf.fit(X_train, y_train)
 preds = clf.predict(X_valid)
 Fpreds = grid_test.predict(X_valid)
 print('acc:', accuracy_score(y_valid, preds))
print('acc:', accuracy_score(y_valid, Fpreds))<find_best_params>
model2 = xgb.XGBClassifier(n_estimators=100, max_depth=8, learning_rate=0.1, subsample=0.5, n_jobs=n_jobs)
 model2
print(gridF.best_params_ )<save_to_csv>
train_model2 = model2.fit(X_train, y_train )
preds_test = clf.predict(X_test)
 output = pd.DataFrame({'PassengerId': X_test.index, 'Survived': preds_test})
 output.to_csv('my_submission.csv', index=False)
 print("Your submission was successfully saved!" )<import_modules>
pred1 = train_model1.predict(X_test)
 pred2 = train_model2.predict(X_test)
 print('Model 1 XGboost Report %r' %(classification_report(y_test, pred1)))
 print('Model 2 XGboost Report %r' %(classification_report(y_test, pred2)) )
import numpy as np
import pandas as pd
import category_encoders as ce
from xgboost import XGBClassifier
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import LabelEncoder
from sklearn.impute import SimpleImputer 
<load_from_csv>
print("Accuracy for model 1: %.2f" %(accuracy_score(y_test, pred1)* 100))
print("Accuracy for model 2: %.2f" %(accuracy_score(y_test, pred2)* 100))
data = pd.read_csv('/kaggle/input/titanic/train.csv')
 print(data.columns)
 X_test = pd.read_csv('/kaggle/input/titanic/test.csv')
 data.dropna(axis=0, subset=["Embarked"], inplace=True)
 X = data.drop(columns="Survived")
 y = data.Survived.copy()
X_train, X_valid, y_train, y_valid = train_test_split(X, y, test_size=0.30, random_state=42)
 missing = [col for col in data.columns if data[col].isna().any() ]
print("Missing:", missing)
 print("no_missing:", [(col, str(sum(data[col].isna())) + '/' + str(data.size)) for col in missing])
 test_missing = [col for col in X_test.columns if X_test[col].isna().any() ]
print("Test Missing:", test_missing)
 print("no_missing:", [(col, str(sum(X_test[col].isna())) + '/' + str(X_test.size)) for col in test_missing])
 X_train["Age"] = X_train["Age"].fillna(X_train["Age"].median())
 X_valid["Age"] = X_valid["Age"].fillna(X_train["Age"].median())
 X_test["Age"] = X_test["Age"].fillna(X_train["Age"].median())
 X_train["Cabin"] = X_train["Cabin"].fillna("NO_CABIN")
 X_valid["Cabin"] = X_valid["Cabin"].fillna("NO_CABIN")
 X_test["Cabin"] = X_test["Cabin"].fillna("NO_CABIN")
 X_test["Fare"] = X_test["Fare"].fillna(X_train["Fare"].median())
 def extract_name_title(name):
last_name, rest = name.split(", ", 1)
 title, rest = rest.split(".", 1)
 return title
X_train["Name"] = X_train["Name"].apply(extract_name_title)
 X_valid["Name"] = X_valid["Name"].apply(extract_name_title)
 X_test["Name"] = X_test["Name"].apply(extract_name_title)
 print("Unique train titles:", X_train["Name"].unique())
 print("Unique valid titles:", X_valid["Name"].unique())
 X_train["Sex"] = X_train["Sex"].apply(lambda x: 1 if x == "male" else 0, convert_dtype=True)
 X_valid["Sex"] = X_valid["Sex"].apply(lambda x: 1 if x == "male" else 0, convert_dtype=True)
 X_test["Sex"] = X_test["Sex"].apply(lambda x: 1 if x == "male" else 0, convert_dtype=True)

print(X_train.head(20))
categorical = [col for col in X_train.columns if X_train[col].dtype == "object"]
print("Categorical:", categorical)
 target_enc = ce.TargetEncoder(cols=categorical)
 X_train = target_enc.fit_transform(X_train, y_train)
 X_valid = target_enc.transform(X_valid, y_valid)
 X_test = target_enc.transform(X_test, None)
 print(X_train.head(20))<compute_train_metric>
model3 = xgb.XGBClassifier(
 learning_rate =0.1,
n_estimators=1000,
max_depth=5,
min_child_weight=1,
gamma=0,
subsample=0.8,
colsample_bytree=0.8,
objective= 'binary:logistic',
n_jobs=n_jobs,
scale_pos_weight=1,
seed=27 )
model = XGBClassifier(n_estimators=500)
 model.fit(X_train, y_train)
 predictions = model.predict(X_valid)
 print(y_valid.head(20))
print(predictions[:20])
 print("Accuracy:" , 1-mean_absolute_error(predictions, y_valid))
print("F1:", f1_score(predictions, y_valid))
selector = SelectKBest(f_classif, k=5)
 X_new = selector.fit_transform(X_train, y_train)
 selected_features = pd.DataFrame(selector.inverse_transform(X_new),
index=X_train.index,
columns=X_train.columns)
 selected_features.head(30)
 test_predictions = model.predict(X_test)
 submission = pd.DataFrame({
"PassengerId": X_test["PassengerId"],
"Survived": test_predictions
})
 submission.to_csv('submission.csv', index=False )<set_options>
train_model3 = model3.fit(X_train, y_train)
 pred3 = train_model3.predict(X_test)
 print("Accuracy for model 3: %.2f" %(accuracy_score(y_test, pred3)* 100))
SEED = 7
print("Setup complete." )<load_from_csv>
print('Model 3 XGboost Report %r' %(classification_report(y_test, pred3)) )
train = pd.read_csv(".. /input/titanic/train.csv")
 test = pd.read_csv(".. /input/titanic/test.csv")
 datasets = [train, test]
train<feature_engineering>
gc.collect()
for ds in datasets:
def rand_ages() :
np.random.seed(SEED)
 return np.random.randint(low=ds['Age'].mean() - ds['Age'].std() ,
high=ds['Age'].mean() + ds['Age'].std() ,
size=ds['Age'].isnull().sum())
 ds.loc[ds['Age'].isnull() , 'Age'] = rand_ages()
ds['Age'] = pd.cut(ds['Age'], 5, labels=range(5))
ds.loc[:, 'Age'] = ds['Age'].astype(int)
 ax = sns.countplot(data=train, x='Age', hue='Survived')
 ax.set_xlabel('Age category')
 plt.show()<categorify>
parameters = {
'n_estimators': [100],
'max_depth': [6, 9],
'subsample': [0.9, 1.0],
'colsample_bytree': [0.9, 1.0],
}
def encode_freq_sorted(feature):
sorted_indices = feature.value_counts().index
sorted_dict = dict(zip(sorted_indices, range(len(sorted_indices))))
return feature.map(sorted_dict ).astype(int)
 for ds in datasets:
ds['Sex'] = encode_freq_sorted(ds['Sex'])
 ds['Embarked'] = encode_freq_sorted(ds['Embarked'])
 ds['Title'] = encode_freq_sorted(ds['Title'])
 train.head()<define_variables>
grid = GridSearchCV(model3,
parameters, n_jobs=n_jobs,
scoring="neg_log_loss",
cv=3)
 grid
drop_features = ['Name', 'SibSp', 'Parch', 'Ticket', 'Cabin']<prepare_x_and_y>
drop_features.extend(['Pclass'])
 train = train.drop(columns=drop_features)
 test = test.drop(columns=drop_features)
 X = train.drop(columns=['PassengerId', 'Survived'])
 y = train['Survived']
X.head()<split>
gc.collect()
X_train, X_val, y_train, y_val = train_test_split(X, y, train_size=0.8,
random_state=SEED)
 X_test = test.drop(columns=['PassengerId'] )<train_on_grid>
less_imp_features = ['estadocivil1','instlevel9','techocane','parentesco10','v14a',
'parentesco11','parentesco5','paredother','parentesco7','noelec',
'elimbasu4','elimbasu6']
cross_valid = StratifiedKFold(n_splits=5, shuffle=True, random_state=SEED)
 def random_search(X, y, estimator, params, score="accuracy", cv=cross_valid,
n_iter=100, random_state=SEED, n_jobs=-1):

 print("
classifier = RandomizedSearchCV(estimator=estimator, param_distributions=params,
scoring=score, cv=cv, n_iter=n_iter, n_jobs=n_jobs,
random_state=random_state)
 classifier.fit(X, y)
 print("Best parameters by random search:
", classifier.best_params_)
 return classifier<find_best_params>
for f in less_imp_features:
if f in total_features:
total_features.remove(f)
 len(total_features )
random_forest = RandomForestClassifier(random_state=SEED)
 random_forest.get_params()<init_hyperparams>
X, y = df[total_features], df['Target']
params = {
'bootstrap': [True, False],
'max_depth': [int(x)for x in np.linspace(10, 110, num = 11)],
'max_features': ['auto', 'sqrt'],
'min_samples_leaf': [1, 2, 4],
'min_samples_split': [2, 5, 10],
'n_estimators': [int(x)for x in np.linspace(200, 2000, num = 10)]
}
random_forest_tuned = random_search(
 X_train, y_train, estimator=random_forest, params=params )<predict_on_test>
seed = 43
test_size = 0.3
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=test_size, random_state=seed )
y_pred = random_forest_tuned.predict(X_val)
 accuracy_random_forest = accuracy_score(y_val, y_pred)
 print("Accuracy:", accuracy_random_forest )<find_best_params>
train_model5 = model3.fit(X_train, y_train)
 pred5 = train_model5.predict(X_test)
 print("Accuracy for model 5: %.2f" %(accuracy_score(y_test, pred5)* 100))
svc = SVC(probability=True, random_state=SEED)
 svc.get_params()<train_on_grid>
print('Model 5 XGboost Report %r' %(classification_report(y_test, pred5)) )
params = {
'C': scipy.stats.expon(scale=100),
'class_weight':['balanced', None],
'gamma': scipy.stats.expon(scale=.1),
'kernel':['rbf', 'linear']
}
svc_tuned = random_search(X_train, y_train, estimator=svc, params=params )<compute_test_metric>
train_model6 = train_model5.fit(X, y)
y_pred = svc_tuned.predict(X_val)
 accuracy_svc = accuracy_score(y_val, y_pred)
 print("Accuracy:", accuracy_svc )<find_best_params>
np.unique(thresholds ).shape
xgb = XGBClassifier(random_state=SEED, verbosity=0)
 xgb.get_params()<train_on_grid>
model4 = RandomForestClassifier(n_jobs=n_jobs)
 model4
params = {
'colsample_bytree': list(np.arange(0.6, 1.0, step=0.05)) ,
'gamma': list(np.arange(0.1, 15, step=0.2)) ,
'learning_rate': [0.01, 0.05, 0.1, 0.15, 0.2],
'max_depth': list(range(2, 12)) ,
'min_child_weight': list(range(1, 12)) ,
'n_estimators': [10, 100, 500, 1000],
'reg_alpha': [10**i for i in range(-5, 1)],
'subsample': list(np.arange(0.6, 1.0, step=0.05))
}
xgb_tuned = random_search(X_train, y_train, estimator=xgb, params=params )<predict_on_test>
gc.collect()
y_pred = xgb_tuned.predict(X_val)
 accuracy_xgboost = accuracy_score(y_val, y_pred)
 print("Accuracy:", accuracy_xgboost )<find_best_params>
train_model4 = model4.fit(X_train, y_train)
 pred4 = train_model4.predict(X_test)
 print("Accuracy for model 4: %.2f" %(accuracy_score(y_test, pred4)* 100))
decision_tree = DecisionTreeClassifier(random_state=SEED)
 decision_tree.get_params()<train_on_grid>
print('Model 4 XGboost Report %r' %(classification_report(y_test, pred4)) )
params = {
'criterion': ["gini", "entropy"],
'max_depth': list(range(1, 32)) ,
'max_features': list(range(1, X_train.shape[1]+1)) ,
'min_samples_leaf': list(range(1, 9)) ,
'min_samples_split': list(np.arange(0.1, 1.1, step=0.1))
}
decision_tree_tuned = random_search(
 X_train, y_train, estimator=decision_tree, params=params )<predict_on_test>
confusion_matrix(y_test, pred4 )
y_pred = decision_tree_tuned.predict(X_val)
 accuracy_decision_tree = accuracy_score(y_val, y_pred)
 print("Accuracy:", accuracy_decision_tree )<find_best_params>
df_test = pd.read_csv('.. /input/test.csv' )
knn = KNeighborsClassifier()
knn.get_params()<train_on_grid>
df_test.loc[df_test.edjefa == 'yes', 'edjefa'] = 1
df_test.loc[df_test.edjefa == 'no', 'edjefa'] = 0
df_test.loc[df_test.edjefe == 'yes', 'edjefe'] = 1
df_test.loc[df_test.edjefe == 'no', 'edjefe'] = 0
df_test[['edjefa','edjefe']] = df_test[['edjefa','edjefe']].apply(pd.to_numeric)
 df_test[['edjefa','edjefe']].dtypes
params = {
'leaf_size': list(range(20, 50)) ,
'n_neighbors': list(range(3, 30)) ,
'p': [1, 2]
}
knn_tuned = random_search(X_train, y_train, estimator=knn, params=params )<predict_on_test>
X_actual_test = df_test[total_features]
y_pred = knn_tuned.predict(X_val)
 accuracy_knn = accuracy_score(y_val, y_pred)
 print("Accuracy:", accuracy_knn )<find_best_params>
pred_actual = train_model6.predict(X_actual_test)
 pred_actual
logistic_regression = LogisticRegression(random_state=SEED)
 logistic_regression.get_params()<train_on_grid>
df_final = pd.DataFrame(df['Id'], pred_actual ).reset_index()
df_final.columns = ['Target','Id']
params = {
'C': scipy.stats.loguniform(1e-5, 100),
'penalty': ['l1', 'l2', 'elasticnet'],
'solver': ['newton-cg', 'lbfgs', 'liblinear']
}
logistic_regression_tuned = random_search(
 X_train, y_train, estimator=logistic_regression, params=params )<compute_train_metric>
cols = cols[-1:] + cols[:-1]
cols
y_pred = logistic_regression_tuned.predict(X_val)
 accuracy_logistic_regression = accuracy_score(y_val, y_pred)
 print("Accuracy:", accuracy_logistic_regression )<set_options>
df_final.index.name = None
df_final.head(7 )
naive_bayes = GaussianNB()
naive_bayes.get_params()<train_model>
df_final['Target'].value_counts()
<compute_train_metric><EOS> 
df_final[cols].to_csv('sample_submission.csv', index=False )