Kaggle competition: Titanic - Machine Learning from Disaster
<SOS> metric: categorizationaccuracy Kaggle data source: titanic-machine-learning-from-disaster<feature_engineering>
!pip install seaborn==0.11.0
data_clas.add_test(test["text"] )<predict_on_test>
pd.options.display.max_rows=200
pd.set_option('mode.chained_assignment', None)
 simplefilter("ignore", category=ConvergenceWarning)
 simplefilter("ignore", category=RuntimeWarning)
 sns.__version__
test_preds, _ = learn_clas.get_preds(DatasetType.Test,
ordered=True )<create_dataframe>
train = pd.read_csv('/kaggle/input/titanic/train.csv', index_col='PassengerId')
 test = pd.read_csv('/kaggle/input/titanic/test.csv', index_col='PassengerId' )
test_pred_df = pd.DataFrame(test_preds.data.cpu().numpy() ,
columns=['clickbait', 'news', 'other'])
 ulmfit_preds = pd.Series(np.argmax(test_pred_df.values, axis=1),
name='label' ).map({0: 'clickbait', 1: 'news', 2: 'other'})
<save_to_csv>
train.isna().sum()
ulmfit_preds.to_csv('ulmfit_predictions.csv', index_label='id', header=True )<set_options>
test.isna().sum()
%matplotlib inline
font = {"family":"HGMaruGothicMPRO"}
matplotlib.rc("font",**font)
<load_from_csv>
def imputer(df):
age_impute_series = df.groupby(['Pclass', 'Sex'] ).Age.transform('mean')
 df.Age.fillna(age_impute_series, inplace=True)
 df.Cabin = df.Cabin.str.extract(pat='([A-Z])')
 df.Cabin.fillna('M', inplace=True)
 df['Deck'] = df.Cabin.replace({'A':'ABC', 'B':'ABC', 'C':'ABC', 'D':'DE', 'E':'DE', 'F':'FG',
'G':'FG', 'T':'ABC'})
 df.drop('Cabin', axis=1, inplace=True)
 for feature in df.columns:
df[feature].fillna(df[feature].mode() [0], inplace=True)
 return df
train = pd.read_csv(".. /input/exam-for-students20200129/train.csv")
 test = pd.read_csv(".. /input/exam-for-students20200129/test.csv")
 country = pd.read_csv(".. /input/exam-for-students20200129/country_info.csv" )<feature_engineering>
train_imputed = imputer(train.copy())
 test_imputed = imputer(test.copy() )
train["ConvertedSalary"] = np.log1p(train[["ConvertedSalary"]] )<concatenate>
def ticket_extractor(ticket):
alpha = re.sub('\d', '', ticket)
 if alpha:
return alpha
else:
num = re.search('\d{1,9}', ticket)
 return ticket
temp = train_imputed.copy()
temp['Ticket_extracted'] = temp.Ticket.apply(ticket_extractor)
 for i in range(len(temp.Ticket)) :
try:
int(temp.Ticket_extracted.iloc[i])
 temp.Ticket_extracted.iloc[i] = f'Num_{len(temp.Ticket_extracted.iloc[i])}'
except:
continue
for label, pattern in [('_ca', 'C[.]?A[.]?'),('_PC', 'PC'),('_SOTON', 'SOTON'),('_STON', 'STON'), 
('_WC', 'W[.]?[/]?C'),('_SC', 'S[.]?C[.]?'),('_A', 'A[.]?'),('_SOC', 'S[.]?O[.]?[/]?C'), 
('_PP', 'PP'),('_FC', '(F.C.|F.C.C.) '),('_LS_number', 'Num_(6|7)'),('_SS_number', 'Num_(3|4|5)'), 
('rare', '^[^_]')]:
temp.Ticket_extracted[temp.Ticket_extracted.str.contains(pattern)] = label
temp['Ticket_extracted'].value_counts(dropna=False)
 fig, ax = plt.subplots(2, 1, figsize=(15, 15))
sns.countplot(data=temp, x='Ticket_extracted', hue='Survived', ax=ax[0])
 ax[0].set_title('Extracted Ticket - Survival "count" plot', size=20, loc='Left', y=1.03)
 sns.barplot(data=temp, x='Ticket_extracted', y='Survived', ax=ax[1])
 ax[1].set_title('Extracted Ticket - Survival "chance" plot', size=20, loc='Left', y=1.03)
 plt.show()
train["hantei"] = "train"
test["hantei"] = "test"
full_data= pd.concat([train, test] )<merge>
temp = train_imputed.copy()
temp['Title'] = temp.Name.str.extract(pat='([a-zA-Z]+\.) ')
 temp.Title[~temp.Title.isin(['Mr.', 'Miss.', 'Mrs.', 'Master.'])] = 'rare'
full_data = full_data.merge(country, on='Country', how='left' ).set_index(full_data.index )<count_unique_values>
def feature_creator(df_train, df_test):
df_train['Fare_cat'] = pd.qcut(df_train['Fare'], 7)
 df_test['Fare_cat'] = pd.qcut(df_test['Fare'], 7)
 df_train['Fare_cat'] = LabelEncoder().fit_transform(df_train['Fare_cat'])
 df_test['Fare_cat'] = LabelEncoder().fit_transform(df_test['Fare_cat'])
 df_train['Age_cat'] = pd.cut(df_train.Age, bins=[0, 5, 24, 30, 36, np.inf])
 df_test['Age_cat'] = pd.cut(df_test.Age, bins=[0, 5, 24, 30, 36, np.inf])
 df_train['Age_cat'] = LabelEncoder().fit_transform(df_train['Age_cat'])
 df_test['Age_cat'] = LabelEncoder().fit_transform(df_test['Age_cat'])
 for df in [df_train, df_test]:
df['Title'] = df.Name.str.extract(pat='([a-zA-Z]+\.) ')
 df.Title[~df.Title.isin(['Mr.', 'Miss.', 'Mrs.', 'Master.'])] = 'rare'
df_full = pd.concat([df_train, df_test], axis=0)
 df_full['Ticket_frequency'] = df_full.Ticket.map(df_full.Ticket.value_counts())
 df_train = df_full.loc[df_train.index]
df_test = df_full.loc[df_test.index].loc[df_test.index].drop('Survived', axis=1)
 for df in [df_train, df_test]:
df['Ticket_extracted'] = df.Ticket.apply(ticket_extractor)
 for i in range(len(df.Ticket)) :
try:
int(df.Ticket_extracted.iloc[i])
 df.Ticket_extracted.iloc[i] = f'Num_{len(df.Ticket_extracted.iloc[i])}'
except:
continue
for label, pattern in [('_ca', 'C[.]?A[.]?'),('_PC', 'PC'),('_SOTON', 'SOTON'),('_STON', 'STON'), 
('_WC', 'W[.]?[/]?C'),('_SC', 'S[.]?C[.]?'),('_A', 'A[.]?'),('_SOC', 'S[.]?O[.]?[/]?C'), 
('_PP', 'PP'),('_FC', '(F.C.|F.C.C.) '),('_LS_number', 'Num_(6|7)'), 
('_SS_number', 'Num_(3|4|5)'),('rare', '^[^_]')]:
df.Ticket_extracted[df.Ticket_extracted.str.contains(pattern)] = label
df_train['Family_size'] = df_train['SibSp'] + df_train['Parch'] + 1
df_test['Family_size'] = df_test['SibSp'] + df_test['Parch'] + 1
df_train['Family_size_cat'] = df_train.Family_size.replace({1:'alone', 2:'small_family', 3:'small_family', 4:'small_family'
,5:'large_family', 6:'large_family', 7:'large_family'
,8:'large_family', 9:'large_family', 10:'large_family'
,11:'large_family'})
 df_test['Family_size_cat'] = df_test.Family_size.replace({1:'alone', 2:'small_family', 3:'small_family', 4:'small_family'
,5:'large_family', 6:'large_family', 7:'large_family'
,8:'large_family', 9:'large_family', 10:'large_family'
,11:'large_family'})
 df_train['Name_length'] = df_train.Name.str.replace(pat='[^a-zA-Z]', repl='' ).str.len()
df_test['Name_length'] = df_test.Name.str.replace(pat='[^a-zA-Z]', repl='' ).str.len()
df_full = pd.concat([df_train, df_test], axis=0 ).reset_index()
df_full['Family_name'] = df_full.Name.str.split(',', n=1, expand=True ).iloc[:, 0]
df_full['Family_Survival'] = 0.5
for grp, grp_df in df_full.groupby(['Family_name', 'Fare']):
if(len(grp_df)!= 1):
for ind, row in grp_df.iterrows() :
smax = grp_df.drop(ind)['Survived'].max()
smin = grp_df.drop(ind)['Survived'].min()
passID = row['PassengerId']
if(smax == 1.0):
df_full.loc[df_full['PassengerId'] == passID, 'Family_Survival'] = 1
elif(smin==0.0):
df_full.loc[df_full['PassengerId'] == passID, 'Family_Survival'] = 0
for _, grp_df in df_full.groupby('Ticket'):
if(len(grp_df)!= 1):
for ind, row in grp_df.iterrows() :
if(row['Family_Survival'] == 0)|(row['Family_Survival']== 0.5):
smax = grp_df.drop(ind)['Survived'].max()
smin = grp_df.drop(ind)['Survived'].min()
passID = row['PassengerId']
if(smax == 1.0):
df_full.loc[df_full['PassengerId'] == passID, 'Family_Survival'] = 1
elif(smin==0.0):
df_full.loc[df_full['PassengerId'] == passID, 'Family_Survival'] = 0
df_full.set_index('PassengerId', inplace=True)
 df_train = df_full[:891]
df_test = df_full[891:]
df_train.drop(['Family_name'], axis=1, inplace=True)
 df_test.drop(['Family_name', 'Survived'], axis=1, inplace=True)
 y_train =df_train.Survived
df_train.drop('Survived', axis=1, inplace=True)
 return df_train, y_train, df_test
print(train.nunique() )<count_unique_values>
X_train, y_train, X_test = feature_creator(train_imputed.copy() , test_imputed.copy())
 y_train = y_train.astype(int )
for i in retsumei:
print(i,train[i].nunique() )<count_unique_values>
class FeatureEngineering(BaseEstimator, TransformerMixin):
def __init__(self, bin_fare=False, bin_age=True, family_size=True, bin_family_size=True,
drop_Name_length=False, drop_Ticket_frequency=False, drop_all=True, drop_Family_Survival=True,
drop_Ticket_extracted=False, scaling='StandardScaler', target_encode_title=True, test=False):
self.bin_fare = bin_fare
self.bin_age = bin_age
self.family_size = family_size
self.bin_family_size = bin_family_size
self.drop_Name_length = drop_Name_length
self.drop_Ticket_frequency = drop_Ticket_frequency
self.drop_all = drop_all
self.drop_Ticket_extracted = drop_Ticket_extracted
self.scaling = scaling
self.drop_Family_Survival = drop_Family_Survival
self.target_encode_title = target_encode_title
self.transformer = None
self.drop_list = []
self.test = test
def fit(self, X, y=None):
return self
def transform(self, X, y=None):
X['Pclass'] = X['Pclass'].astype(str)
 dummies = pd.get_dummies(X.loc[:, ['Pclass', 'Sex']], drop_first=True)
 X = pd.concat([X, dummies], axis=1)
 X.drop(['Pclass', 'Sex'], axis=1, inplace=True)
 X.drop(['Name', 'Ticket'], axis=1, inplace=True)
 if self.bin_fare:
self.drop_list.append('Fare')
 else:
self.drop_list.append('Fare_cat')
 if self.bin_age:
self.drop_list.append('Age')
 else:
self.drop_list.append('Age_cat')
 if self.family_size:
self.drop_list.extend(['SibSp', 'Parch'])
 if self.bin_family_size:
self.drop_list.append('Family_size')
 else:
self.drop_list.append('Family_size_cat')
 else:
self.drop_list.extend(['Family_size', 'Family_size_cat'])
 if self.drop_Name_length:
X.drop('Name_length', axis=1, inplace=True)
 if self.drop_Ticket_frequency:
X.drop('Ticket_frequency', axis=1, inplace=True)
 if self.drop_Family_Survival:
X.drop('Family_Survival', axis=1, inplace=True)
 if self.drop_all:
X.drop(self.drop_list, axis=1, inplace=True)
 self.cols = ['Title', 'Ticket_extracted'] if self.target_encode_title else ['Ticket_extracted']
if not self.test and len(X)> 400:
self.target_encoder = TargetEncoder(cols=self.cols, smoothing=5)
 X.loc[:, self.cols] = self.target_encoder.fit_transform(X.loc[:, self.cols], y_train.loc[X.index])
 else:
X.loc[:, self.cols] = self.target_encoder.transform(X.loc[:, self.cols])
 if self.drop_Ticket_extracted:
X.drop('Ticket_extracted', axis=1, inplace=True)
 X = pd.get_dummies(X, drop_first=True)
 features_to_scale = [feature for feature in X.columns if X[feature].nunique() != 2]
if not self.test and len(X)> 400:
if self.scaling == 'StandardScaler':
self.transformer = StandardScaler()
X.loc[:, features_to_scale] = self.transformer.fit_transform(X.loc[:, features_to_scale])
 elif self.scaling == 'RobustScaler':
self.transformer = RobustScaler()
X.loc[:, features_to_scale] = self.transformer.fit_transform(X.loc[:, features_to_scale])
 elif self.scaling == 'MinMaxScaler':
self.transformer = MinMaxScaler()
X.loc[:, features_to_scale] = self.transformer.fit_transform(X.loc[:, features_to_scale])
 else:
X.loc[:, features_to_scale] = self.transformer.transform(X.loc[:, features_to_scale])
 return X
full_retsumei = []
full_retsumei = full_data.columns
for j in full_retsumei:
print(j,full_data[j].nunique() ,full_data[j].dtype )<count_unique_values>
param_grid_pipeline = {'feature_engineering__bin_fare':[True, False],
'feature_engineering__bin_age':[True, False],
'feature_engineering__family_size':[True, False],
'feature_engineering__bin_family_size':[True, False],
'feature_engineering__drop_Name_length':[False],
'feature_engineering__drop_Ticket_frequency':[False],
'feature_engineering__drop_all':[True],
'feature_engineering__drop_Ticket_extracted':[True, False],
'feature_engineering__target_encode_title':[True, False],
'feature_engineering__scaling':['StandardScaler'],
'feature_engineering__drop_Family_Survival':[True]}
pipeline_ = Pipeline([('feature_engineering', FeatureEngineering()), 
('model', LogisticRegression())])
 cv = StratifiedShuffleSplit(n_splits=20, test_size=0.15, random_state=101)
 grid = GridSearchCV(pipeline_, param_grid_pipeline,
cv=cv.split(X_train, X_train.Deck),
scoring='accuracy', verbose=2, n_jobs=-1)
 grid.fit(X_train.copy() , y_train )
full_retsumei = []
full_retsumei = full_data.columns
for j in full_retsumei:
if full_data[j].nunique() >50:
if full_data[j].dtype =="object":
print(j,full_data[j].nunique() ,full_data[j].dtype )<import_modules>
pd.DataFrame(grid.cv_results_)['mean_test_score'].isna().sum()
<feature_engineering>
X_train_fe = fe.fit_transform(X_train.copy() )
henkan = ["Pop.Density(per sq.mi.) ","Coastline(coast/area ratio)","Net migration","Infant mortality(per 1000 births)"
,"Literacy(%)","Phones(per 1000)","Arable(%)","Crops(%)","Other(%)"
,"Birthrate","Deathrate","Agriculture","Industry","Service"]
for k in henkan:
print(k)
 full_data[k] = full_data[k].str.replace(',', '.' )<data_type_conversions>
fe.test = True
print(full_data['Service'].astype("float64"))<data_type_conversions>
X_test_fe = fe.transform(X_test.copy() )
for l in henkan:
print(full_data[l].astype("float64"))<data_type_conversions>
def learning_curve_plotter(Model, X, y, params_1, params_2, step=50):
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.1, random_state=42)
 plt.figure(figsize=(16, 7))
for i,(name, params)in enumerate([params_1, params_2]):
train_score = []
val_score = []
plt.subplot(1, 2, i+1)
 for j in range(100, len(X_train), step):
model = Model(**params ).fit(X_train[:j], y_train[:j])
 train_score.append(model.score(X_train[:j], y_train[:j]))
val_score.append(model.score(X_test, y_test))
plt.plot(train_score, 'r-', label='Training accuracy')
 plt.plot(val_score, 'b-', label='Validation accuracy')
 plt.title(f'{name}')
 plt.xlabel('Training set size')
 plt.ylabel('Accuracy')
 plt.legend()
plt.show()
for l in henkan:
full_data[l] = full_data[l].astype("float64")
<data_type_conversions>
param_grid_logreg = {'penalty':['elasticnet'],
'C':0.01 * np.arange(100),
'l1_ratio':0.1 * np.arange(10),
'solver':['saga']}
<count_unique_values>
grid_logreg = GridSearchCV(LogisticRegression() , param_grid_logreg,
cv=RepeatedStratifiedKFold(n_splits=10, n_repeats=2, random_state=42),
scoring='accuracy', verbose=2, n_jobs=-1 )
for j in full_retsumei:
if full_data[j].nunique() >50:
if full_data[j].dtype =="object":
print(j,full_data[j].nunique() ,full_data[j].dtype )<count_unique_values>
grid_logreg.fit(X_train_fe, y_train )
for j in full_retsumei:
if full_data[j].nunique() <30 and full_data[j].nunique() > 2:
if full_data[j].dtype =="object":
print(j,full_data[j].nunique() ,full_data[j].dtype )<import_modules>
params_logreg = {'C': 0.28, 'l1_ratio': 0.9, 'penalty': 'elasticnet', 'solver': 'saga'}
<import_modules>
param_grid_knn = {'n_neighbors':np.arange(50),
'weights':['uniform'],
'algorithm':['ball_tree'],
'leaf_size':np.arange(1, 40, 2)}
<count_values>
grid_knn = GridSearchCV(KNeighborsClassifier() , param_grid_knn,
cv=RepeatedStratifiedKFold(n_splits=10, n_repeats=2, random_state=42),
scoring='accuracy', verbose=2, n_jobs=-1 )
full_data["Age"].value_counts() 
<feature_engineering>
grid_knn.fit(X_train_fe, y_train )
full_data["Age"] = full_data["Age"].str.replace('Under 18 years old', '1')
 full_data["Age"] = full_data["Age"].str.replace('18 - 24 years old', '2')
 full_data["Age"] = full_data["Age"].str.replace('25 - 34 years old', '3')
 full_data["Age"] = full_data["Age"].str.replace('35 - 44 years old', '4')
 full_data["Age"] = full_data["Age"].str.replace('45 - 54 years old', '5')
 full_data["Age"] = full_data["Age"].str.replace('55 - 64 years old', '6')
 full_data["Age"] = full_data["Age"].str.replace('65 years or older', '7' )<data_type_conversions>
params_knn = {'algorithm': 'ball_tree', 'leaf_size': 1, 'n_neighbors': 7, 'weights': 'uniform'}
full_data['Age'].astype("float64" )<count_values>
param_grid_svc = {'C':[0.001, 0.01, 0.1, 1, 5],
'kernel':['rbf'],
'gamma':0.01 * np.arange(100),
'probability':[True]}
full_data["Age"].value_counts()<count_unique_values>
grid_svc = GridSearchCV(SVC() , param_grid_svc, cv=RepeatedStratifiedKFold(n_splits=10, n_repeats=2, random_state=42),
scoring='accuracy', verbose=2, n_jobs=-1 )
full_retsumei = []
full_retsumei = full_data.columns
ordinal = []
for j in full_retsumei:
if full_data[j].dtype =="object":
print(j,full_data[j].nunique() ,full_data[j].dtype)
 ordinal.append(j )<categorify>
grid_svc.fit(X_train_fe, y_train )
oe = ce.OrdinalEncoder(cols=ordinal, return_df=False)
 full_data[ordinal] = oe.fit_transform(full_data[ordinal] )<train_model>
params_svc = {'C': 1, 'gamma': 0.09, 'kernel': 'rbf', 'probability': True}
full_data.fillna(-1, inplace=True )<drop_column>
param_grid_random = {'n_estimators':[300, 500, 1000],
'max_depth':[5, 9],
'max_samples':[0.5, 0.7, 0.9],
'max_features':[0.5, 0.7, 0.9],
'min_samples_split':[2, 5, 8]
}
<count_values>
grid_random = GridSearchCV(RandomForestClassifier() , param_grid_random,
cv=RepeatedStratifiedKFold(n_splits=10, n_repeats=2, random_state=42),
scoring='accuracy', verbose=2, n_jobs=-1 )
full_data["hantei"].value_counts()<filter>
grid_random.fit(X_train_fe, y_train )
len(full_data[full_data['hantei'] == 1] )<prepare_x_and_y>
params_random = {'max_depth': 5, 'max_features': 0.5, 'max_samples': 0.9,
'min_samples_split': 8, 'n_estimators': 300}
X_train = full_data[full_data['hantei'] == 1]
X_test = full_data[full_data['hantei'] == 2]
X_train.drop(['hantei'], axis=1, inplace=True)
 X_test.drop(['hantei'], axis=1, inplace=True )<prepare_x_and_y>
param_grid_gradient = {'max_depth':[3, 4],
'n_estimators':[300, 400, 500],
'learning_rate':[0.01, 0.03, 0.05],
'subsample':[0.5, 0.7],
'max_features':[0.5, 0.7],
}
y_train = X_train["ConvertedSalary"]
X_train2 = X_train.drop(["ConvertedSalary"], axis=1)
 X_test = X_test.drop(["ConvertedSalary"], axis=1)
 X_train2.sort_index(axis=1, ascending=False)
 X_test.sort_index(axis=1, ascending=False )<split>
grid_gradient = GridSearchCV(GradientBoostingClassifier() , param_grid_gradient,
cv=RepeatedStratifiedKFold(n_splits=10, n_repeats=2, random_state=42),
scoring='accuracy', verbose=2, n_jobs=-1 )
X_train1,X_test1,y_train,y_test = train_test_split(X_train2,y_train )<train_model>
grid_gradient.fit(X_train_fe, y_train )
model = lgb.LGBMRegressor()
model.fit(X_train1, y_train )<predict_on_test>
params_gradient = {'learning_rate': 0.01, 'max_depth': 3, 'max_features': 0.5, 'n_estimators': 500, 'subsample': 0.5}
pred = model.predict(X_test1 )<prepare_output>
param_grid_xgb = {'n_estimators':[400, 600],
'learning_rate':[0.01, 0.03, 0.05],
'max_depth':[3, 4],
'subsample':[0.5, 0.7],
'colsample_bylevel':[0.5, 0.7],
'reg_lambda':[15, None],
}
pred1 = np.expm1(pred )<predict_on_test>
grid_xgb = GridSearchCV(XGBClassifier() , param_grid_xgb,
cv=RepeatedStratifiedKFold(n_splits=10, n_repeats=2, random_state=42),
scoring='accuracy', verbose=2, n_jobs=-1 )
pred = model.predict(X_test )<prepare_output>
grid_xgb.fit(X_train_fe, y_train )
pred1 = np.expm1(pred )<save_to_csv>
params_xgb = {'colsample_bylevel': 0.7, 'learning_rate': 0.03, 'max_depth': 3,
'n_estimators': 400, 'reg_lambda': 15, 'subsample': 0.5}
submission = pd.read_csv('.. /input/exam-for-students20200129/sample_submission.csv', index_col=0)
 submission['ConvertedSalary'] = pred1
submission.to_csv('submission.csv' )<set_options>
logreg = LogisticRegression(**params_logreg)
 svc = SVC(**params_svc)
 knn = KNeighborsClassifier(**params_knn)
 rfc = RandomForestClassifier(**params_random)
 gradient = GradientBoostingClassifier(**params_gradient)
 xgb = XGBClassifier(**params_xgb)
 estimators = [('logreg', logreg),('knn', knn),('svc', svc),('rfc', rfc),('gradient', gradient), 
('xgb', xgb)]
stack = StackingClassifier(estimators=estimators,
cv=10, n_jobs=-1 )
plt.style.use('ggplot')
 %matplotlib inline
pd.set_option('display.max_columns', 500 )<load_from_csv>
y_preds = logreg.fit(X_train_fe, y_train ).predict(X_test_fe )
df_train = pd.read_csv('.. /input/exam-for-students20200129/train.csv', index_col=0)
 df_test = pd.read_csv('.. /input/exam-for-students20200129/test.csv', index_col=0)
 df_train.ConvertedSalary = np.log1p(df_train.ConvertedSalary )<count_missing_values>
y_preds = svc.fit(X_train_fe, y_train ).predict(X_test_fe )
df_train.isnull().sum()<count_missing_values>
y_preds = knn.fit(X_train_fe, y_train ).predict(X_test_fe )
df_test.isnull().sum()<count_unique_values>
y_preds = rfc.fit(X_train_fe, y_train ).predict(X_test_fe )
cats = []
for col in df_train.columns:
if df_train[col].dtype == 'object':
cats.append(col)
 print(col, df_train[col].nunique() )<groupby>
y_preds = gradient.fit(X_train_fe, y_train ).predict(X_test_fe )
df_train.groupby(u ).size()<groupby>
y_preds = xgb.fit(X_train_fe, y_train ).predict(X_test_fe )
df_train.groupby(u ).size()<sort_values>
submission = pd.DataFrame({'PassengerId':test.index,
'Survived':y_preds} )
df_train.groupby(u ).size().sort_values() 
<sort_values>
submission.to_csv('submission.csv', index=False )
df_test.groupby(u ).size().sort_values()<drop_column>
submission.to_csv('submission.csv', index=False )
df_train.drop('Country',axis=1,inplace=True)
 df_test.drop('Country',axis=1,inplace=True )<groupby>
pd.read_csv('submission.csv' )