Kaggle competition: Titanic - Machine Learning from Disaster
def col_indicies(names):

 return np.isin(missing_columns_output, names)
 ordinal = ['ExterQual', 'ExterCond', 'BsmtQual', 'BsmtCond', 'HeatingQC', 'KitchenQual', 'GarageQual', 'GarageCond']
non_ordinal_categorical = np.setdiff1d(categorical, ordinal)
 housing_encoder = ColumnTransformer([ 
('quality', OrdinalEncoder(categories=[['NA', 'Po', 'Fa', 'TA', 'Gd', 'Ex']]*len(ordinal)) , col_indicies(ordinal)) , 
('LandSlope', OrdinalEncoder(categories=[['Gtl', 'Mod', 'Sev']]), col_indicies(['LandSlope'])) , 
('BsmtExposure', OrdinalEncoder(categories=[['NA', 'No', 'Mn', 'Av', 'Gd']]), col_indicies(['BsmtExposure'])) , 
('Functional', OrdinalEncoder(categories=[['Sal', 'Sev', 'Maj2', 'Maj1', 'Mod', 'Min2', 'Min1', 'Typ']]), col_indicies(['Functional'])) , 
('GarageFinish', OrdinalEncoder(categories=[['NA', 'Unf', 'RFn', 'Fin']]), col_indicies(['GarageFinish'])) , 
('Fence', OrdinalEncoder(categories=[['NA', 'MnWw', 'GdWo', 'MnPrv', 'GdPrv']]), col_indicies(['Fence'])) , 
('BsmtFinType', OrdinalEncoder(categories=[['NA', 'Unf', 'LwQ', 'Rec', 'BLQ', 'ALQ', 'GLQ']]*2), col_indicies(['BsmtFinType1', 'BsmtFinType2'])) , 
('PavedDrive', OrdinalEncoder(categories=[['N', 'P', 'Y']]), col_indicies(['PavedDrive'])) , 
('categorical', OneHotEncoder(handle_unknown='ignore'), col_indicies(non_ordinal_categorical))
], remainder='passthrough')
 housing_all_hot_encoder = ColumnTransformer([ 
('categorical', OneHotEncoder(handle_unknown='ignore'), col_indicies(categorical))
], remainder='passthrough' )<choose_model_class>
%matplotlib inline
ridge = Pipeline([ 
('fill_missing', missing_preprocessor), 
('encoding', housing_encoder), 
('scale', RobustScaler(with_centering=False)) , 
('ridge', Ridge(alpha=11.905772393787833))
])
 ridge = TransformedTargetRegressor(ridge, func=np.log1p, inverse_func=np.expm1)
 hot_ridge = Pipeline([ 
('fill_missing', missing_preprocessor), 
('encoding', housing_all_hot_encoder), 
('scale', RobustScaler(with_centering=False)) , 
('ridge', Ridge(alpha=11.905772393787833))
])
 hot_ridge = TransformedTargetRegressor(hot_ridge, func=np.log1p, inverse_func=np.expm1)
<train_on_grid>
training_data = pd.read_csv('/kaggle/input/titanic/train.csv')
 testing_data = pd.read_csv('/kaggle/input/titanic/test.csv')
 combined_data1 = [training_data, testing_data]
for data in combined_data1:
print('
',data.info() )
<choose_model_class>
training_data.corr() ['Survived'].sort_values(ascending = False )
lasso = Pipeline([ 
('fill_missing', missing_preprocessor), 
('encoding', housing_encoder), 
('scale', RobustScaler(with_centering=False)) , 
('lasso', Lasso(alpha=0.000565620644760902))
])
 lasso = TransformedTargetRegressor(lasso, func=np.log1p, inverse_func=np.expm1)
 hot_lasso = Pipeline([ 
('fill_missing', missing_preprocessor), 
('encoding', housing_all_hot_encoder), 
('scale', RobustScaler(with_centering=False)) , 
('lasso', Lasso(alpha=0.000565620644760902))
])
 hot_lasso = TransformedTargetRegressor(hot_lasso, func=np.log1p, inverse_func=np.expm1 )<train_on_grid>
for data in combined_data1:
data['title'] = data['Name'].apply(lambda x: x[x.find(',')+2:x.find('.')])
 print(data['title'].value_counts())
 print('
','='*50 )
<choose_model_class>
def converttitle(x):
if x not in ['Mr','Miss','Mrs','Master']:
return 'other'
else:
return x
for data in combined_data1:
data['title'] = data['title'].apply(converttitle)
 print(data['title'].value_counts())
 print('
','='*50)
kernel_ridge = Pipeline([ 
('fill_missing', missing_preprocessor), 
('encoding', housing_encoder), 
('scale', RobustScaler(with_centering=False)) , 
('kridge', KernelRidge(kernel='poly', degree=2, alpha=48.93900918477494))
])
 kernel_ridge = TransformedTargetRegressor(kernel_ridge, func=np.log1p, inverse_func=np.expm1 )<train_on_grid>
for data in combined_data1:
data['family_members'] = data['SibSp'] + data['Parch']
data['aboard_alone'] = data['family_members'].apply(lambda x: 'yes' if x == 0 else 'no')
 print(data.aboard_alone.value_counts() )
<choose_model_class>
def pclass(x):
if x==1:
return 'Upper'
elif x==2:
return 'Middle'
else:
return 'Lower'
for data in combined_data1:
data['Pclass'] = data['Pclass'].apply(pclass)
 print(data.Pclass.value_counts() ,'
' )
knn = Pipeline([ 
('fill_missing', missing_preprocessor), 
('encoding', housing_encoder), 
('scale', RobustScaler(with_centering=False)) , 
('knn', KNeighborsRegressor(n_neighbors=9, weights='distance', p=1))
])
 knn = TransformedTargetRegressor(knn, func=np.log1p, inverse_func=np.expm1)
<train_on_grid>
train_data = training_data[['Survived','Pclass','Sex','Age','Fare','title','Embarked','aboard_alone']]
test_data = testing_data[['Pclass','Sex','Age','Fare','title','Embarked','aboard_alone']]
combined_data = [train_data, test_data]
for data in combined_data:
print(data.isnull().sum() ,'
' )
<train_model>
def fare(x):
if x>300:
return mean
else:
return x
for data in combined_data:
mean = data.drop(data[data['Fare']>300].index)['Fare'].mean()
data['Fare'] = data['Fare'].apply(fare )
rf = Pipeline([ 
('fill_missing', missing_preprocessor), 
('encoding', housing_encoder), 
('scale', RobustScaler(with_centering=False)) , 
('rf', RandomForestRegressor(n_jobs=-1))
])
 rf = TransformedTargetRegressor(rf, func=np.log1p, inverse_func=np.expm1)
<train_on_grid>
for data in combined_data:
data['Fare'] = pd.cut(data['Fare'], bins = [0,20,50,100,300], labels = ['Eco','business','prime','Deluxe'])
 print(data.Fare.value_counts() )
param_grid = {
'regressor__rf__max_depth': [10, 20, 30, 40, 50, 60, None],
'regressor__rf__max_features': ['auto', 'sqrt'],
'regressor__rf__min_samples_leaf': [1, 2, 4],
'regressor__rf__n_estimators': [200, 400, 600, 800, 1000, 1200]
}
tuned_rf = RandomizedSearchCV(rf, param_grid, n_jobs=-1, cv=3, n_iter=10)
<init_hyperparams>
final_train = pd.get_dummies(train_data, drop_first= True)
 display('Train',final_train)
 final_test = pd.get_dummies(test_data, drop_first= True)
 display('Test',final_test )
param_init = {
"max_depth": 5,
"n_estimators": 3000,
"learning_rate": 0.01,
"subsample": 0.5,
"colsample_bytree": 0.7,
"min_child_weight": 1.5,
"reg_alpha": 0.75,
"reg_lambda": 0.4,
"seed": 42,
}
param_fit = {
"xgb__eval_metric": "rmse",
"xgb__verbose": 200
}
xgb_model = Pipeline([ 
('fill_missing', missing_preprocessor), 
('encoding', housing_encoder), 
('scale', RobustScaler(with_centering=False)) , 
('xgb', xgboost.XGBRegressor(**param_init))
])
 xgb_model = TransformedTargetRegressor(xgb_model, func=np.log1p, inverse_func=np.expm1 )<choose_model_class>
final_train.corr() ['Survived'].sort_values(ascending = False )
param_init = {
'objective': 'regression',
'num_leaves': 5,
'learning_rate': 0.05,
'n_estimators': 720,
'max_bin': 55,
'bagging_fraction': 0.8,
'bagging_freq': 5,
'feature_fraction': 0.2319,
'feature_fraction_seed': 9,
'bagging_seed': 9,
'min_data_in_leaf': 6,
'min_sum_hessian_in_leaf': 11
}
lgbm_model = Pipeline([ 
('fill_missing', missing_preprocessor), 
('encoding', housing_encoder), 
('scale', RobustScaler(with_centering=False)) , 
('lgbm', lgb.LGBMRegressor(**param_init))
])
 lgbm_model = TransformedTargetRegressor(lgbm_model, func=np.log1p, inverse_func=np.expm1 )<choose_model_class>
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.linear_model import LogisticRegression
from sklearn.tree import DecisionTreeClassifier
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import cross_val_score, GridSearchCV
import sklearn.metrics as sm
models = {
'ridge': ridge,
'lasso': lasso,
'kernel ridge': kernel_ridge,
'knn': knn,
'random forest': rf,
'xgboost': xgb_model,
'lgbm': lgbm_model
}<compute_train_metric>
x = final_train.drop('Survived', axis = 1)
 y = final_train['Survived']
for name, model in models.items() :
print(f'{name}:')
 print(np.sqrt(np.mean(cross_val_score(model, X_train, y_train, cv=4, scoring=make_scorer(mean_squared_log_error), n_jobs=-1))))<compute_train_metric>
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.2, random_state=0, stratify = y )
estimators = [('lasso', lasso), 
('ridge', ridge), 
('xgb', xgb_model), 
('lgbm', lgbm_model)]
stacking_regressor = StackingRegressor(estimators=estimators, final_estimator=RidgeCV())
 print(np.sqrt(np.mean(cross_val_score(stacking_regressor, X_train, y_train, cv=3, scoring=make_scorer(mean_squared_log_error), n_jobs=-1))))<train_model>
lr_model = LogisticRegression()
lr_model.fit(x_train,y_train)
 lr_predict = lr_model.predict(x_test)
 print(lr_predict[:5])
 print(y_test.head())
best_model = stacking_regressor
_ = best_model.fit(train.drop('SalePrice', axis=1), train['SalePrice'] )<save_to_csv>
sm.r2_score(y_test,lr_predict )
sub = pd.DataFrame()
sub['Id'] = test_id
sub['SalePrice'] = best_model.predict(test)
 sub.to_csv('submission.csv', index=False )<compute_train_metric>
print(sm.classification_report(y_test,lr_predict))
def rmse_cv(model):
rmse= np.sqrt(-cross_val_score(model, X_train, y, scoring="neg_mean_squared_error", cv = 5))
return(rmse)
<set_options>
dt_model = DecisionTreeClassifier(random_state = 0)
 clf = GridSearchCV(dt_model, param_grid = {'criterion':('gini', 'entropy'),
'max_depth':[2,3,4,5,6]},
cv=5)
 clf.fit(x_train,y_train)
 print(clf.best_params_)
 print(clf.best_score_)
 dt_predict = clf.predict(x_test)
pd.set_option('display.max_columns', 500 )<load_from_csv>
sm.r2_score(y_test,dt_predict )
train = pd.read_csv('/kaggle/input/house-prices-advanced-regression-techniques/train.csv')
 test = pd.read_csv('/kaggle/input/house-prices-advanced-regression-techniques/test.csv')
 sample = pd.read_csv('/kaggle/input/house-prices-advanced-regression-techniques/sample_submission.csv' )<concatenate>
print(sm.classification_report(y_test,dt_predict))
all_data = pd.concat(( train.loc[:,'MSSubClass':'SaleCondition'],test.loc[:,'MSSubClass':'SaleCondition']))<feature_engineering>
rf_model = RandomForestClassifier(random_state=0)
 rf_clf = GridSearchCV(rf_model, param_grid = {'n_estimators': [200, 300,400,500],
'max_depth' : [3,4,5,6,7],
'criterion' :['gini', 'entropy']},
cv=3)
 rf_clf.fit(x_train,y_train)
 print(rf_clf.best_params_)
 print(rf_clf.best_score_)
 rf_predict = rf_clf.predict(x_test)
train["SalePrice"] = np.log1p(train["SalePrice"])
 numeric_feats = all_data.dtypes[all_data.dtypes != "object"].index<feature_engineering>
print(sm.classification_report(y_test,rf_predict))
skewed_feats = train[numeric_feats].apply(lambda x: skew(x.dropna()))
skewed_feats = skewed_feats[skewed_feats > 0.75]
skewed_feats = skewed_feats.index
all_data[skewed_feats] = np.log1p(all_data[skewed_feats] )<categorify>
sm.r2_score(y_test,rf_predict )
all_data = pd.get_dummies(all_data)
 all_data = all_data.fillna(all_data.mean() )<prepare_x_and_y>
model = RandomForestClassifier(random_state=0,
max_depth = 6,
n_estimators = 200)
 model.fit(x,y)
 pred =(model.predict(final_test))
X_train = all_data[:train.shape[0]]
X_test = all_data[train.shape[0]:]
y = train.SalePrice<find_best_params>
submission = pd.DataFrame({
'PassengerId': testing_data['PassengerId'],
'Survived': pred
} )
<train_on_grid><EOS> 
submission.to_csv('Submission.csv', index=False )