Kaggle competition: Costa Rican Household Poverty Level Prediction
train = data[:len(train)]
test = data[len(train):]<prepare_x_and_y>
df = pd.read_csv('/kaggle/input/costa-rican-household-poverty-prediction/train.csv')
 test = pd.read_csv('/kaggle/input/costa-rican-household-poverty-prediction/test.csv')
 df.shape, test.shape
y_train0 = train['Survived']
X_train0 = train.drop('Survived', axis = 1)
 X_test0 = test.drop('Survived', axis = 1 )<prepare_x_and_y>
df_all = df.append(test)
 df_all.shape
X = np.array(X_train0)
 y = np.array(y_train0 )<choose_model_class>
df['Target'].isnull().sum()
clf = XGBClassifier(max_depth=3, n_estimators=1000, learning_rate=0.01 )<train_model>
df_all['Target'].value_counts()
ss = ShuffleSplit(n_splits=5,
train_size=0.8,
test_size =0.2,
random_state=0)
 for train_index, test_index in ss.split(X):
X_train, X_test = X[train_index], X[test_index]
Y_train, Y_test = y[train_index], y[test_index]
clf.fit(X_train, Y_train)
 print(clf.score(X_test, Y_test))<predict_on_test>
df.dtypes.value_counts()
y_pred = clf.predict(np.array(X_test0))<save_to_csv>
df_all['dependency'].value_counts()
sub = gender_submission
sub['Survived'] = list(map(int, y_pred))
sub.to_csv("submission.csv", index=False )<load_from_csv>
mapeamento = {'yes': 1, 'no': 0}
df_all['dependency'] = df_all['dependency'].replace(mapeamento ).astype(float )
training = pd.read_csv('/kaggle/input/titanic/train.csv')
 test = pd.read_csv('/kaggle/input/titanic/test.csv')
 training['train_test'] = 1
test['train_test'] = 0
test['Survived'] = np.NaN
all_data = pd.concat([training,test])
<define_variables>
df_all['edjefa'].value_counts()
df_num = training[['Age','SibSp', 'Parch', 'Fare']]
df_cat = training[['Survived', 'Pclass','Sex','Ticket', 'Cabin', 'Embarked']]<create_dataframe>
df_all['edjefe'].value_counts()
pd.pivot_table(training, index = 'Survived', values = ['Age','Fare','SibSp','Parch'] )<create_dataframe>
mapeamento = {'yes': 1, 'no': 0}
df_all['edjefa'] = df_all['edjefa'].replace(mapeamento ).astype(int)
 df_all['edjefe'] = df_all['edjefe'].replace(mapeamento ).astype(int )
for i in ['Pclass', 'Sex', 'Embarked']:
print(pd.pivot_table(training, index = 'Survived', columns = i, values = 'Ticket', aggfunc = 'count'), '
' )<feature_engineering>
df_all.isnull().sum()
df_cat.Cabin
training['cabin_multiple'] = training.Cabin.apply(lambda x: 0 if pd.isna(x)else len(x.split(' ')))
 training['cabin_multiple'].value_counts() 
<create_dataframe>
df_all[df_all['parentesco1'] == 1]['v2a1'].isnull().sum()
pd.pivot_table(training, index = 'Survived', columns = 'cabin_multiple', values = 'Ticket', aggfunc = 'count' )<feature_engineering>
df_all['v18q'].value_counts()
training['cabin_adv'] = training.Cabin.apply(lambda x : str(x)[0] )<create_dataframe>
df_all.isnull().sum().sort_values()
print(training.cabin_adv.value_counts())
 pd.pivot_table(training, index='Survived', columns='cabin_adv', values='Name', aggfunc='count' )<feature_engineering>
df_all['SQBmeaned'].fillna(-1, inplace=True)
 df_all['meaneduc'].fillna(-1, inplace=True)
 df_all['rez_esc'].fillna(-1, inplace=True )
training['numeric_tickets'] = training.Ticket.apply(lambda x : 1 if x.isnumeric() else 0)
 training['ticket_letters'] = training.Ticket.apply(lambda x : ''.join(x.split(' ')[:-1] ).replace('.','' ).replace('/','' ).lower()
if len(x.split(' ')[:-1])> 0 else 0)
 print(training['numeric_tickets'].value_counts() )<create_dataframe>
feats = [c for c in df_all.columns if c not in ['Id', 'idhogar', 'Target']]
pd.pivot_table(training, index='Survived', columns='numeric_tickets', values='Name', aggfunc='count' )<count_values>
rf = RandomForestClassifier(n_jobs=-1, n_estimators=200, random_state=42 )
training['ticket_letters'].value_counts()<count_values>
rf.fit(train[feats], train['Target'] )
training.Name.head(50)
 training['name_title'] = training.Name.apply(lambda x : x.split(',')[1].split('.')[0].strip())
 training['name_title'].value_counts()<feature_engineering>
test['Target'] = rf.predict(test[feats] ).astype(int )
all_data['cabin_multiple'] = all_data.Cabin.apply(lambda x: 0 if pd.isna(x)else len(x.split(' ')))
 all_data['cabin_adv'] = all_data.Cabin.apply(lambda x : str(x)[0])
 all_data['numeric_tickets'] = all_data.Ticket.apply(lambda x : 1 if x.isnumeric() else 0)
 all_data['ticket_letters'] = all_data.Ticket.apply(lambda x : ''.join(x.split(' ')[:-1] ).replace('.','')
.replace('/','' ).lower() if len(x.split(' ')[:-1])> 0 else 0)
 all_data['name_title'] = all_data.Name.apply(lambda x : x.split(',')[1].split('.')[0].strip())
 all_data.Age = all_data.Age.fillna(training.Age.mean())
 all_data.Fare = all_data.Fare.fillna(training.Fare.mean())
 all_data.dropna(subset=['Embarked'], inplace=True)
 all_data['norm_fare'] = np.log(all_data.Fare+1)
 plt.hist(all_data.Fare, bins=50)
 plt.title('Fare - Antes da normalização')
 plt.show()
plt.hist(all_data.norm_fare, bins=50)
 plt.title('Fare - Depois da normalização')
 plt.show()
all_data.Pclass = all_data.Pclass.astype(str)
 all_dummies = pd.get_dummies(all_data[['Pclass', 'Sex', 'Age', 'SibSp', 'Parch', 'norm_fare', 'Embarked',
'cabin_adv', 'cabin_multiple','numeric_tickets','name_title', 'train_test']])
 x_train = all_dummies[all_dummies.train_test == 1].drop(['train_test'], axis = 1)
 x_test = all_dummies[all_dummies.train_test == 1].drop(['train_test'], axis = 1)
 y_train = all_data[all_data.train_test == 1].Survived
y_train.shape<prepare_x_and_y>
test['Target'].value_counts(normalize=True )
Scale = StandardScaler()
all_dummies_scaled = all_dummies.copy()
all_dummies_scaled[['Age', 'SibSp', 'Parch', 'norm_fare']] = Scale.fit_transform(all_dummies_scaled[['Age', 'SibSp', 'Parch', 'norm_fare']])
 x_train_scaled = all_dummies_scaled[all_dummies_scaled.train_test == 1].drop(['train_test'], axis = 1)
 x_test_scaled = all_dummies_scaled[all_dummies_scaled.train_test == 1].drop(['train_test'], axis = 1)
<import_modules>
test[['Id', 'Target']].to_csv('submission.csv', index=False )
from sklearn.model_selection import cross_validate
from sklearn.naive_bayes import GaussianNB
from sklearn.linear_model import LogisticRegression
from sklearn import tree
from sklearn.neighbors import KNeighborsClassifier
from sklearn.ensemble import RandomForestClassifier
from sklearn.svm import SVC<define_variables>
train['Target'].value_counts(normalize=True )
classifiers = {}<compute_train_metric>
heads = train[train['parentesco1'] == 1]
gnb = GaussianNB()
cv = cross_validate(gnb, x_train, y_train, cv = 5, return_train_score = True, return_estimator = True)
 classifiers['GaussianNBNotScaled'] = cv
print('Average performance on training set:
')
 print(cv['train_score'].mean())
 print('
Average performance on test set:
')
 print(cv['test_score'].mean() )<compute_train_metric>
rf2 = RandomForestClassifier(n_jobs=-1, n_estimators=200, random_state=42)
 rf2.fit(heads[feats], heads['Target'])
 test['Target'] = rf2.predict(test[feats] ).astype(int)
 test[['Id', 'Target']].to_csv('submission.csv', index=False )
gnb = GaussianNB()
cv = cross_validate(gnb, x_train_scaled, y_train, cv = 5, return_train_score = True, return_estimator = True)
 classifiers['GaussianNBScaled'] = cv
print('Average performance on training set:
')
 print(cv['train_score'].mean())
 print('
Average performance on test set:
')
 print(cv['test_score'].mean() )<compute_train_metric>
train['hhsize'].value_counts()
lr = LogisticRegression(max_iter = 2000)
 cv = cross_validate(lr, x_train, y_train, cv = 5, return_train_score = True, return_estimator = True)
 classifiers['LogisticRegressionNotScaled'] = cv
print('Average performance on training set:
')
 print(cv['train_score'].mean())
 print('
Average performance on test set:
')
 print(cv['test_score'].mean() )<compute_train_metric>
train['tamviv'].value_counts()
lr = LogisticRegression(max_iter = 2000)
 cv = cross_validate(lr, x_train_scaled, y_train, cv = 5, return_train_score = True, return_estimator = True)
 classifiers['LogisticRegressionScaled'] = cv
print('Average performance on training set:
')
 print(cv['train_score'].mean())
 print('
Average performance on test set:
')
 print(cv['test_score'].mean() )<compute_train_metric>
train['tamhog'].value_counts()
dt = tree.DecisionTreeClassifier(random_state = 10)
 cv = cross_validate(dt,x_train,y_train,cv=5, return_train_score = True, return_estimator = True)
 classifiers['DecisionTreeNotScaled'] = cv
print('Average performance on training set:
')
 print(cv['train_score'].mean())
 print('
Average performance on test set:
')
 print(cv['test_score'].mean() )<compute_train_metric>
df_all['hhsize-pc'] = df_all['hhsize'] / df_all['tamviv']
df_all['mobile-pc'] = df_all['qmobilephone'] / df_all['tamviv']
df_all['tablet-pc'] = df_all['v18q1'] / df_all['tamviv']
df_all['rooms-pc'] = df_all['rooms'] / df_all['tamviv']
dt = tree.DecisionTreeClassifier(random_state = 10)
 cv = cross_validate(dt,x_train_scaled,y_train,cv=5, return_train_score = True, return_estimator = True)
 classifiers['DecisionTreeScaled'] = cv
print('Average performance on training set:
')
 print(cv['train_score'].mean())
 print('
Average performance on test set:
')
 print(cv['test_score'].mean() )<compute_train_metric>
feats = [c for c in df_all.columns if c not in ['Id', 'idhogar', 'Target']]
knn = KNeighborsClassifier()
cv = cross_validate(knn,x_train,y_train,cv=5, return_train_score = True, return_estimator = True)
 classifiers['KNeighborsNotScaled'] = cv
print('Average performance on training set:
')
 print(cv['train_score'].mean())
 print('
Average performance on test set:
')
 print(cv['test_score'].mean() )<compute_train_metric>
rf3 = RandomForestClassifier(n_jobs=-1, n_estimators=200, random_state=42)
 rf3.fit(train[feats], train['Target'])
 test['Target'] = rf3.predict(test[feats] ).astype(int)
 test[['Id', 'Target']].to_csv('submission.csv', index=False )
knn = KNeighborsClassifier()
cv = cross_validate(knn,x_train_scaled,y_train,cv=5, return_train_score = True, return_estimator = True)
 classifiers['KNeighborsScaled'] = cv
print('Average performance on training set:
')
 print(cv['train_score'].mean())
 print('
Average performance on test set:
')
 print(cv['test_score'].mean() )<compute_train_metric>
heads2 = train[train['parentesco1'] == 1]
rf = RandomForestClassifier(random_state = 10)
 cv = cross_validate(rf,x_train,y_train,cv=5, return_train_score = True, return_estimator = True)
 classifiers['RandomForestNotScaled'] = cv
print('Average performance on training set:
')
 print(cv['train_score'].mean())
 print('
Average performance on test set:
')
 print(cv['test_score'].mean() )<compute_train_metric>
rf4 = RandomForestClassifier(n_jobs=-1, n_estimators=200, random_state=42)
 rf4.fit(heads2[feats], heads2['Target'])
 test['Target'] = rf4.predict(test[feats] ).astype(int)
 test[['Id', 'Target']].to_csv('submission.csv', index=False )
rf = RandomForestClassifier(random_state = 10)
 cv = cross_validate(rf,x_train_scaled,y_train,cv=5, return_train_score = True, return_estimator = True)
 classifiers['RandomForestScaled'] = cv
print('Average performance on training set:
')
 print(cv['train_score'].mean())
 print('
Average performance on test set:
')
 print(cv['test_score'].mean() )<compute_train_metric>
rf5 = RandomForestClassifier(n_jobs=-1, n_estimators=200, random_state=42, oob_score=True, class_weight='balanced')
 rf5.fit(train[feats], train['Target'])
 test['Target'] = rf5.predict(test[feats] ).astype(int)
 test[['Id', 'Target']].to_csv('submission.csv', index=False)
svc = SVC(probability = True)
 cv = cross_validate(svc, x_train, y_train,cv=5, return_train_score = True, return_estimator = True)
 classifiers['SVCNotScaled'] = cv
print('Average performance on training set:
')
 print(cv['train_score'].mean())
 print('
Average performance on test set:
')
 print(cv['test_score'].mean() )<compute_train_metric>
df = pd.read_csv('/kaggle/input/costa-rican-household-poverty-prediction/train.csv')
 test = pd.read_csv('/kaggle/input/costa-rican-household-poverty-prediction/test.csv')
 df.shape, test.shape
svc = SVC(probability = True)
 cv = cross_validate(svc,x_train_scaled,y_train,cv=5, return_train_score = True, return_estimator = True)
 classifiers['SVCScaled'] = cv
print('Average performance on training set:
')
 print(cv['train_score'].mean())
 print('
Average performance on test set:
')
 print(cv['test_score'].mean() )<compute_test_metric>
df_all = df.append(test)
 df_all.shape
for i in classifiers:
print(i + ": "+"train: "+str(classifiers.get(i)['train_score'].mean())+" - test: " + str(classifiers.get(i)['test_score'].mean()), end='
' )<import_modules>
df_all['edjefa'] = df_all['edjefa'].replace(mapeamento ).astype(int)
 df_all['edjefe'] = df_all['edjefe'].replace(mapeamento ).astype(int)
 df_all['dependency'] = df_all['dependency'].replace(mapeamento ).astype(float )
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_all['v2a1'].fillna(-1, inplace=True)
 df_all['v18q1'].fillna(0, inplace=True)
 df_all['SQBmeaned'].fillna(-1, inplace=True)
 df_all['meaneduc'].fillna(-1, inplace=True)
 df_all['rez_esc'].fillna(-1, inplace=True )
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], 25)
 Q3 = np.percentile(df[i], 75)
 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>
feats = [c for c in df_all.columns if c not in ['Id', 'idhogar', 'edjefe', 'edjefa','Target']]
total=pd.concat([train.drop('Survived',axis=1),test])
 target=train['Survived']
total.head()<data_type_conversions>
rf6 = RandomForestClassifier(n_jobs=-1, n_estimators=200, random_state=42, oob_score=True, class_weight='balanced')
 rf6.fit(train[feats], train['Target'])
 test['Target'] = rf6.predict(test[feats] ).astype(int)
 test[['Id', 'Target']].to_csv('submission.csv', index=False )
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>
rf7 = RandomForestClassifier(max_depth=None, random_state=42, n_jobs=4, n_estimators=700,
min_impurity_decrease=1e-3, min_samples_leaf=2,
verbose=0, class_weight='balanced')
 rf7.fit(train[feats], train['Target'])
 test['Target'] = rf7.predict(test[feats] ).astype(int)
 test[['Id', 'Target']].to_csv('submission.csv', index=False )
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>
rf8 = RandomForestClassifier(max_depth=None, max_features=None, random_state=42, n_jobs=4, n_estimators=700,
min_impurity_decrease=1e-3, min_samples_leaf=2,
verbose=0, class_weight='balanced')
 rf8.fit(train[feats], train['Target'])
 test['Target'] = rf8.predict(test[feats] ).astype(int)
 test[['Id', 'Target']].to_csv('submission.csv', index=False )
total['Fare_1_S']=total['Embarked_2']*total['Pclass_1']*total['Sex'] 
<categorify>
rf9 = RandomForestClassifier(max_depth=None, random_state=42, n_jobs=4, n_estimators=700,
min_impurity_decrease=1e-3, min_samples_leaf=2, criterion="entropy",
verbose=0, class_weight='balanced')
 rf9.fit(train[feats], train['Target'])
 test['Target'] = rf9.predict(test[feats] ).astype(int)
 test[['Id', 'Target']].to_csv('submission.csv', index=False )
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>
rf10 = RandomForestClassifier(max_depth=None, random_state=42, n_jobs=4, n_estimators=700, max_leaf_nodes=10,
min_impurity_decrease=1e-3, min_samples_leaf=2,
verbose=0, class_weight='balanced')
 rf10.fit(train[feats], train['Target'])
 test['Target'] = rf10.predict(test[feats] ).astype(int)
 test[['Id', 'Target']].to_csv('submission.csv', index=False )
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_1_over = resample(df_1,
replace=True,
n_samples=len(df_4),
random_state=42)
 df_2_over = resample(df_2,
replace=True,
n_samples=len(df_4),
random_state=42)
 df_3_over = resample(df_3,
replace=True,
n_samples=len(df_4),
random_state=42)
 df_over= pd.concat([df_1_over, df_2_over, df_3_over, df_4])
 df_over['Target'].value_counts()
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.25)
 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>
train, test = train_test_split(df_over, test_size=0.2, random_state=42)
 rf.fit(train[feats], train['Target'])
 preds_test = rf.predict(test[feats])
 accuracy_score(test['Target'], preds_test )
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_2_under = resample(df_2,
replace=False,
n_samples=len(df_1),
random_state=42)
 df_3_under = resample(df_3,
replace=False,
n_samples=len(df_1),
random_state=42)
 df_4_under = resample(df_4,
replace=False,
n_samples=len(df_1),
random_state=42)
 df_under= pd.concat([df_1, df_2_under, df_3_under, df_4_under])
 df_under['Target'].value_counts()
sns.set(style="darkgrid")
 warnings.filterwarnings('ignore')
 SEED = 69<load_from_csv>
train, test = train_test_split(df_under, test_size=0.2, random_state=42)
 rf.fit(train[feats], train['Target'])
 preds_test = rf.predict(test[feats])
 accuracy_score(test['Target'], preds_test )
df_train = pd.read_csv('.. /input/titanic/train.csv')
 df_test = pd.read_csv('.. /input/titanic/test.csv')
 df_all = pd.concat([df_train, df_test], sort = True ).reset_index(drop = True)
 df_train.name = 'Train DF'
df_test.name = 'Test DF'
df_all.name = 'All DF'
dfs = [df_train, df_test]
def print_df_info(train, test, resp_col):

 print('Number of Training Examples = {}'.format(train.shape[0]))
print('Number of Test Examples = {}
'.format(test.shape[0]))
print('Training X Shape = {}'.format(train.shape))
print('Training y Shape = {}
'.format(train[resp_col].shape[0]))
print('Test X Shape = {}'.format(test.shape))
print('Test y Shape = {}
'.format(test.shape[0]))
print(train.columns)
 print(test.columns)
 print_df_info(df_train, df_test, 'Survived' )<count_missing_values>
import imblearn
def print_missing_val_count(dfs):

 for df in dfs:
print('{}'.format(df.name))
for col in df.columns:
print('{} column missing values: {}'.format(col, df[col].isnull().sum()))
print('
')
 print_missing_val_count(dfs )<filter>
X, y = df_all[feats], df_all[['Target']]
df_corr_age = df_corr[df_corr['Feature 1'] == 'Age']
df_corr_age<groupby>
ros = RandomOverSampler(random_state=42)
 X_ros,y_ros= ros.fit_resample(X,y)
 y_ros['Target'].value_counts()
pclass_sex_med_group = df_all.groupby(['Sex', 'Pclass'] ).median().Age
print(pclass_sex_med_group)
 pclass_sex_mean_group = df_all.groupby(['Sex', 'Pclass'] ).mean().Age
print(pclass_sex_mean_group )<groupby>
df_over = pd.concat([X_ros, y_ros], axis=1)
 train, test = train_test_split(df_over, test_size=0.2, random_state=42)
 rf.fit(train[feats], train['Target'])
 preds_test = rf.predict(test[feats])
 accuracy_score(test['Target'], preds_test )
df_all.Age = df_all.groupby(['Sex', 'Pclass'] ).Age.apply(lambda x: x.fillna(x.median()))<filter>
tl = TomekLinks()
X_tl, y_tl = tl.fit_resample(X,y)
 y_tl['Target'].value_counts()
df_all[df_all.Embarked.isnull() ]<groupby>
df_under = pd.concat([X_tl, y_tl], axis=1)
 train, test = train_test_split(df_under, test_size=0.2, random_state=42)
 rf.fit(train[feats], train['Target'])
 preds_test = rf.predict(test[feats])
 accuracy_score(test['Target'], preds_test )
df_all.groupby(['Sex', 'Pclass'] ).agg(lambda x:x.value_counts().index[0] )<data_type_conversions>
sm = SMOTE()
X_sm, y_sm = sm.fit_resample(X,y)
 y_sm['Target'].value_counts()
df_all.Embarked = df_all.Embarked.fillna("C" )<filter>
df_over = pd.concat([X_sm, y_sm], axis=1)
 train, test = train_test_split(df_over, test_size=0.2, random_state=42)
 rf.fit(train[feats], train['Target'])
 preds_test = rf.predict(test[feats])
 accuracy_score(test['Target'], preds_test )