Kaggle competition: Titanic - Machine Learning from Disaster
regressor = RandomForestRegressor(n_estimators=200, random_state=42, n_jobs=-1,
max_depth=7)
 regressor.fit(X_train, y_train)
 y_rfr = regressor.predict(X_test)
 print('RandomForestRegressor')
 print(y_rfr.min())
 print(y_rfr.max() )<train_model>
%matplotlib inline
regressor = CatBoostRegressor(silent=True)
 regressor.fit(X_train, y_train)
 y_cbr = regressor.predict(X_test)
 print('CatBoostRegressor')
 print(y_cbr.min())
 print(y_cbr.max() )<train_model>
train_data = pd.read_csv("/kaggle/input/titanic/train.csv")
 test_data = pd.read_csv("/kaggle/input/titanic/test.csv")
 y_sample = pd.read_csv("/kaggle/input/titanic/gender_submission.csv" )
regressor = AdaBoostRegressor()
regressor.fit(X_train, y_train)
 y_abr = regressor.predict(X_test)
 print('AdaBoostRegressor')
 print(y_abr.min())
 print(y_abr.max() )<train_model>
train_data.groupby('Survived' ).mean()
regressor = BaggingRegressor()
regressor.fit(X_train, y_train)
 y_br = regressor.predict(X_test)
 print('BaggingRegressor')
 print(y_br.min())
 print(y_br.max() )<train_model>
train_data.groupby(train_data['Age'].isnull() ).mean()
regressor = LinearRegression()
regressor.fit(X_train, y_train)
 y_lr = regressor.predict(X_test)
 print('LinearRegressor')
 print(y_lr.min())
 print(y_lr.max() )<train_model>
train_data.groupby('Pclass' ).mean()
regressor = BayesianRidge()
regressor.fit(X_train, y_train)
 y_bridge = regressor.predict(X_test)
 print('BayesianRidge')
 print(y_bridge.min())
 print(y_bridge.max() )<train_model>
train_data.groupby('Parch' ).mean()
regressor = GradientBoostingRegressor()
regressor.fit(X_train, y_train)
 y_gbr = regressor.predict(X_test)
 print('GradientBoostingRegressor')
 print(y_gbr.min())
 print(y_gbr.max() )<feature_engineering>
train_data['Age'] = train_data[['Age','Parch']].apply(age_aprox, axis = 1)
 test_data['Age'] = test_data[['Age','Parch']].apply(age_aprox, axis = 1 )
sample_submission['expected'] = y_test<save_to_csv>
train_data['Family_count'] = train_data['SibSp'] +train_data['Parch']
sample_submission.to_csv('submission.csv', index=False )<import_modules>
test_data['Family_count'] = test_data['SibSp'] +test_data['Parch']
import pandas as pd
import numpy as np
import sys
import os
import random
from pathlib import Path
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn.model_selection import train_test_split, StratifiedKFold
from sklearn import metrics
from sklearn.metrics import mean_squared_error
from sklearn.ensemble import RandomForestRegressor
import lightgbm as lgb<load_from_csv>
round(( train_data['Cabin'].isnull().sum() /len(train_data)) *100,2 )
train_df = pd.read_csv(PATH+"train.csv")
 test_df = pd.read_csv(PATH+"test.csv" )<train_model>
train_data.pivot_table('Survived',index = 'Sex',columns = 'Embarked',aggfunc= 'count' )
print("Train: rows:{} cols:{}".format(train_df.shape[0], train_df.shape[1]))
print("Test: rows:{} cols:{}".format(test_df.shape[0], test_df.shape[1]))<count_missing_values>
for name in train_data['Name']:
train_data['Title'] = train_data['Name'].str.extract('([A-Za-z]+)\.',expand=True)
 test_data['Title'] = test_data['Name'].str.extract('([A-Za-z]+)\.',expand=True )
def missing_data(data):
total = data.isnull().sum().sort_values(ascending = False)
 percent =(data.isnull().sum() /data.isnull().count() *100 ).sort_values(ascending = False)
 return np.transpose(pd.concat([total, percent], axis=1, keys=['Total', 'Percent']))<count_missing_values>
train_data.groupby('Title' ).count() ['PassengerId']
missing_data(train_df )<count_missing_values>
test_data.groupby('Title' ).count() ['PassengerId']
missing_data(test_df )<count_unique_values>
gender = {'male':1,'female': 0}
train_data['Sex'] = train_data['Sex'].map(gender)
 test_data['Sex'] = test_data['Sex'].map(gender )
print("There are {} unique zipcodes.".format(train_df['zipcode'].nunique()))<feature_engineering>
title = {'Capt':'Others','Col':'Others','Countess':'Others','Don':'Others', 'Dr':'Others','Jonkheer':'Others', 'Lady':'Others', 'Major':'Others',
'Mlle':'Others', 'Mme':'Others', 'Ms':'Miss','Rev': 'Others','Sir':'Others','Dona': 'Others'}
for df in [train_df, test_df]:
df['date'] = pd.to_datetime(df['date'])
 df['dayofweek'] = df['date'].dt.dayofweek
df['weekofyear'] = df['date'].dt.weekofyear
df['dayofyear'] = df['date'].dt.dayofyear
df['quarter'] = df['date'].dt.quarter
df['is_month_start'] = pd.to_numeric(df['date'].dt.is_month_start)
 df['month'] = df['date'].dt.month
df['year'] = df['date'].dt.year
df['is_weekend'] = pd.to_numeric(df['dayofweek']>=5 )<feature_engineering>
train_data.replace({'Title':title},inplace=True)
 test_data.replace({'Title':title},inplace=True )
for df in [train_df, test_df]:
df['med_lat'] = np.round(df['lat'],1)
 df['med_long'] = np.round(df['long'],1)
 df['build_old'] = 2019 - df['yr_built']
df['sqft_living_diff'] = df['sqft_living'] - df['sqft_living15']
df['sqft_lot_diff'] = df['sqft_lot'] - df['sqft_lot15']
df['bedroom_bathroom_ratio'] = df['bedrooms'] / df['bathrooms']<feature_engineering>
train_data.groupby('Title' ).count() ['PassengerId']
features = ['bedrooms','bathrooms','floors',
'waterfront','view','condition','grade',
'sqft_living','sqft_lot','sqft_above','sqft_basement','sqft_living15','sqft_lot15',
'yr_built','yr_renovated',
'lat', 'long','zipcode',
'date', 'dayofweek', 'weekofyear', 'dayofyear', 'quarter',
'is_month_start', 'month', 'year', 'is_weekend',
'price']
mask = np.zeros_like(train_df[features].corr() , dtype=np.bool)
 mask[np.triu_indices_from(mask)] = True
f, ax = plt.subplots(figsize=(18,18))
plt.title('Pearson Correlation Matrix',fontsize=25)
 sns.heatmap(train_df[features].corr() ,linewidths=0.25,vmax=1.0,square=True,cmap="Blues",
linecolor='w',annot=True,mask=mask,cbar_kws={"shrink":.75}); 
<split>
features = ["Pclass", "Sex", "SibSp","Parch","Age","Embarked"]
y = train_data["Survived"]
X = pd.get_dummies(train_data[features])
 titanic_test = pd.get_dummies(test_data[features])
 print(X.shape)
 print(y.shape)
VALID_SIZE = 0.2
RANDOM_STATE = 2019
train, valid = train_test_split(train_df, test_size=VALID_SIZE, random_state=RANDOM_STATE, shuffle=True )<define_variables>
from sklearn.model_selection import train_test_split, cross_val_score
predictors = ['sqft_living', 'grade']
target = 'price'<prepare_x_and_y>
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.4, random_state=42)
 X_test, X_val, y_test, y_val = train_test_split(X_test, y_test, test_size=0.5, random_state=42 )
train_X = train[predictors]
train_Y = train[target].values
valid_X = valid[predictors]
valid_Y = valid[target].values<init_hyperparams>
for dataset in [y_train,y_val,y_test]:
print(round(len(dataset)/len(y),2))
RFC_METRIC = 'mse'
NUM_ESTIMATORS = 100
NO_JOBS = 4<choose_model_class>
from sklearn.model_selection import cross_val_score
model = RandomForestRegressor(n_jobs=NO_JOBS,
random_state=RANDOM_STATE,
criterion=RFC_METRIC,
n_estimators=NUM_ESTIMATORS,
verbose=False )<train_model>
rf_basic = RandomForestClassifier()
scores = cross_val_score(rf_basic,X_train,y_train, cv = 5)
 scores
model.fit(train_X, train_Y )<predict_on_test>
print('Parameters currently in use:
')
 pprint(rf_basic.get_params() )
preds = model.predict(valid_X )<compute_test_metric>
from sklearn.model_selection import GridSearchCV
print("RF Model score: ", model.score(train_X, train_Y))<compute_test_metric>
from sklearn.model_selection import GridSearchCV
def rmse(preds, y):
return np.sqrt(mean_squared_error(preds, y))<compute_test_metric>
def print_results(results):
print('BEST PARAMS: {}
'.format(results.best_params_))
means = results.cv_results_['mean_test_score']
stds = results.cv_results_['std_test_score']
for mean, std, params in zip(means, stds, results.cv_results_['params']):
print('{}(+/-{})for {}'.format(round(mean, 3), round(std * 2, 3), params))
print("Root mean squared error(valid set):",round(rmse(preds, valid_Y),2))<train_model>
rf_grid = RandomForestClassifier()
parameters = {
'n_estimators': [5, 50, 100, 200],
'max_depth': [2, 10, 20, None],
'max_features': ['auto', 'sqrt'],
'min_samples_leaf': [1, 2, 4],
'min_samples_split': [2, 5, 10],
}
cv = GridSearchCV(rf_grid, parameters, cv=5,verbose = 2)
predictors = ['sqft_living', 'grade', 'sqft_above']
target = 'price'
train_X = train[predictors]
train_Y = train[target].values
valid_X = valid[predictors]
valid_Y = valid[target].values
model = RandomForestRegressor(n_jobs=NO_JOBS,
random_state=RANDOM_STATE,
criterion=RFC_METRIC,
n_estimators=NUM_ESTIMATORS,
verbose=False)
 model.fit(train_X, train_Y)
 preds = model.predict(valid_X )<compute_test_metric>
from sklearn.metrics import accuracy_score, precision_score, recall_score
print("RF Model score: ", model.score(train_X, train_Y))
print("Root mean squared error(valid set):",round(rmse(preds, valid_Y),2))<prepare_x_and_y>
rf_best = RandomForestClassifier(n_estimators= 50, max_depth = 10, max_features = 'auto', min_samples_leaf =4, min_samples_split = 10)
 rf_best.fit(X_train,y_train )
predictors = ['sqft_living', 'sqft_lot',
'sqft_above', 'sqft_living15',
'waterfront', 'view', 'condition', 'grade',
'bedrooms', 'bathrooms', 'floors',
'zipcode',
'month', 'dayofweek',
'med_lat', 'med_long',
'build_old', 'sqft_living_diff', 'sqft_lot_diff',
]
target = 'price'
train_X = train[predictors]
train_Y = train[target].values
valid_X = valid[predictors]
valid_Y = valid[target].values
model = RandomForestRegressor(n_jobs=NO_JOBS,
random_state=RANDOM_STATE,
criterion=RFC_METRIC,
n_estimators=NUM_ESTIMATORS,
verbose=False)
 model.fit(train_X, train_Y)
 preds = model.predict(valid_X)
 plot_feature_importance()
print("RF Model score: ", model.score(train_X, train_Y))
print("Root mean squared error(valid set):",round(rmse(preds, valid_Y),2))<save_to_csv>
mdl = rf_best
y_pred = mdl.predict(X_val)
 accuracy = round(accuracy_score(y_val, y_pred), 3)
 precision = round(precision_score(y_val, y_pred), 3)
 recall = round(recall_score(y_val, y_pred), 3)
 print('MAX DEPTH: {} /
mdl.n_estimators,
accuracy,
precision,
recall))
test_X = test_df[predictors]
predictions_RF = model.predict(test_X)
 submission = pd.read_csv(PATH+"sample_submission.csv")
 submission['price'] = predictions_RF
submission.to_csv('submission.csv', index=False )<init_hyperparams>
y_pred = rf_best.predict(X_test)
 accuracy = round(accuracy_score(y_test, y_pred), 3)
 precision = round(precision_score(y_test, y_pred), 3)
 recall = round(recall_score(y_test, y_pred), 3)
 print('MAX DEPTH: {} /
rf_best.n_estimators,
accuracy,
precision,
recall))
param = {'num_leaves': 51,
'min_data_in_leaf': 30,
'objective':'regression',
'max_depth': -1,
'learning_rate': 0.01,
"boosting": "gbdt",
"metric": 'rmse',
"verbosity": -1,
"nthread": 4,
"random_state": 42}<define_variables>
rf_best.fit(X, y )
predictors = ['sqft_living', 'sqft_lot',
'sqft_above', 'sqft_living15',
'waterfront', 'view', 'condition', 'grade',
'bedrooms', 'bathrooms', 'floors',
'zipcode',
'month', 'dayofweek',
'med_lat', 'med_long',
'build_old', 'sqft_living_diff', 'sqft_lot_diff',
]
target = 'price'<create_dataframe>
predictions = rf_best.predict(titanic_test)
 output = pd.DataFrame({'PassengerId': test_data.PassengerId, 'Survived': predictions})
 output.to_csv('my_submission.csv', index=False)
 print("Your submission was successfully saved!" )