Kaggle competition: Titanic - Machine Learning from Disaster
<SOS> metric: categorizationaccuracy Kaggle data source: titanic-machine-learning-from-disaster<create_dataframe>
!pip install pycomp --upgrade --no-cache-dir
covid_data = dataframe[['Date', 'State', 'Country', 'Cumulative_cases', 'Cumulative_death',
'Daily_cases', 'Daily_death', 'Latitude', 'Longitude', 'Temperature',
'Min_temperature', 'Max_temperature', 'Wind_speed', 'Precipitation',
'Fog_Presence', 'Population', 'Population Density/km', 'Median_Age',
'Sex_Ratio', 'Age%_65+', 'Hospital Beds/1000', 'Available Beds/1000',
'Confirmed Cases/1000', 'Lung Patients(F)', 'Lung Patients(M)',
'Life Expectancy(M)', 'Life Expectancy(F)', 'Total_tests_conducted',
'Out_Travels(mill.) ', 'In_travels(mill.) ', 'Domestic_Travels(mill.) ']]<feature_engineering>
filterwarnings('ignore')
 DATA_PATH = '.. /input/titanic'
TRAIN_FILENAME = 'train.csv'
TEST_FILENAME = 'test.csv'
training_data['Country_Region'] = training_data['Country_Region'] + ' ' + training_data['Province_State']
testing_data['Country_Region'] = testing_data['Country_Region'] + ' ' + testing_data['Province_State']
del training_data['Province_State']
del testing_data['Province_State']
def split_date(date):
date = date.split('-')
 date[0] = int(date[0])
 if(date[1][0] == '0'):
date[1] = int(date[1][1])
 else:
date[1] = int(date[1])
 if(date[2][0] == '0'):
date[2] = int(date[2][1])
 else:
date[2] = int(date[2])
 return date
training_data.Date = training_data.Date.apply(split_date)
 testing_data.Date = testing_data.Date.apply(split_date )<feature_engineering>
df = pd.read_csv(os.path.join(DATA_PATH, TRAIN_FILENAME))
df.head()
year = []
month = []
day = []
for i in training_data.Date:
year.append(i[0])
 month.append(i[1])
 day.append(i[2])
 training_data['Year'] = year
training_data['Month'] = month
training_data['Day'] = day
del training_data['Date']
year = []
month = []
day = []
for i in testing_data.Date:
year.append(i[0])
 month.append(i[1])
 day.append(i[2])
 testing_data['Year'] = year
testing_data['Month'] = month
testing_data['Day'] = day
del testing_data['Date']
del training_data['Id']
del testing_data['ForecastId']
del testing_data['Year']
del training_data['Year']<drop_column>
feature_adder = CustomFeaturesTitanic(name_title=True, cabin_class=True, ticket_class=True)
 df_custom = feature_adder.fit_transform(df)
 df_custom.head()
latest_data = covid_data['Date'] == '30-03-2020'
country_data_detailed = covid_data[latest_data]
country_data_detailed.drop(['Daily_cases','Daily_death','Latitude','Longitude'],axis=1,inplace=True)
 country_data_detailed.head(3 )<data_type_conversions>
dup_dropper = EliminaDuplicatas()
df_nodup = dup_dropper.fit_transform(df_slct)
 print(f'Total of duplicates before: {df_slct.duplicated().sum() }')
 print(f'Total of duplicates after: {df_nodup.duplicated().sum() }' )
country_data_detailed['Lung Patients(F)'].replace('Not reported',np.nan,inplace=True)
 country_data_detailed['Lung Patients(F)'] = country_data_detailed['Lung Patients(F)'].astype("float" )<load_from_csv>
cat_custom_features = ['Pclass']
mod_dict = {col: str for col in cat_custom_features}
print(f'Selected columns dtype before transformation:
')
 print(df_nodup.dtypes[cat_custom_features])
 dtype_mod = ModificaTipoPrimitivo(mod_dict=mod_dict)
 df_mod = dtype_mod.fit_transform(df_nodup)
 print(f'Selected columns dtype after transformation:
')
 print(df_mod.dtypes[cat_custom_features] )
temperature_data = pd.read_csv('.. /input/covcsd-covid19-countries-statistical-dataset/temperature_data.csv')
 temperature_data.head()<compute_train_metric>
imputer = SimpleImputer(strategy='median')
 X_train_num_imp = imputer.fit_transform(X_train_num)
 X_train_num_imp = pd.DataFrame(X_train_num_imp, columns=num_features)
 print(f'Null data before imputer: {X_train_num.isnull().sum().sum() }')
 print(f'Null data after imputer: {X_train_num_imp.isnull().sum().sum() }' )
sample = temperature_dataset['Temperature'].sample(n=250)
 test = temperature_dataset['Temperature']
stat, p = ttest_ind(sample, test)
 print('Statistics=%.3f, p=%.3f' %(stat, p))<normalization>
tmp_ov = data_overview(df=X_train_num_imp)
 tmp_ov['skew'] = tmp_ov.query('feature in @num_features')['feature'].apply(lambda x: skew(X_train_num_imp[x]))
tmp_ov['kurtosis'] = tmp_ov.query('feature in @num_features')['feature'].apply(lambda x: kurtosis(X_train_num_imp[x]))
tmp_ov[~tmp_ov['skew'].isnull() ].sort_values(by='skew', ascending=False ).loc[:, ['feature', 'skew', 'kurtosis']]
training_data['ConfirmedCases'] = training_data['ConfirmedCases'].apply(int)
 training_data['Fatalities'] = training_data['Fatalities'].apply(int)
 cases = training_data.ConfirmedCases
fatalities = training_data.Fatalities
del training_data['ConfirmedCases']
del training_data['Fatalities']
lb = LabelEncoder()
training_data['Country_Region'] = lb.fit_transform(training_data['Country_Region'])
 testing_data['Country_Region'] = lb.transform(testing_data['Country_Region'])
 scaler = MinMaxScaler()
x_train = scaler.fit_transform(training_data.values)
 x_test = scaler.transform(testing_data.values )<train_model>
scaler = DynamicScaler(scaler_type='Standard')
 X_train_num_scaled = scaler.fit_transform(X_train_num_log)
 X_train_num_scaled = pd.DataFrame(X_train_num_scaled, columns=num_features)
 X_train_num_scaled.head()
rf = XGBRegressor(n_estimators = 1500 , max_depth = 15, learning_rate=0.1)
 rf.fit(x_train,cases)
 cases_pred = rf.predict(x_test)
 rf = XGBRegressor(n_estimators = 1500 , max_depth = 15, learning_rate=0.1)
 rf.fit(x_train,fatalities)
 fatalities_pred = rf.predict(x_test )<feature_engineering>
encoder = DummiesEncoding(cat_features_ori=cat_features, dummy_na=True)
 X_train_cat_enc = encoder.fit_transform(X_train_cat)
 print(f'Shape before encoding: {X_train_cat.shape}')
 print(f'Shape after encoding: {X_train_cat_enc.shape}')
 X_train_cat_enc.head()
cases_pred = np.around(cases_pred)
 fatalities_pred = np.around(fatalities_pred)
 cases_pred[cases_pred < 0] = 0
fatalities_pred[fatalities_pred < 0] = 0<load_from_csv>
TARGET = 'Survived'
INITIAL_FEATURES = ['Survived', 'Pclass', 'Sex', 'Age', 'SibSp', 'Parch', 'Fare', 'Embarked', 'name_title',
'ticket_class', 'cabin_class', 'name_length', 'age_cat', 'fare_cat', 'family_size']
INITIAL_PRED_FEATURES = [col for col in INITIAL_FEATURES if col not in TARGET]
DTYPE_MODIFICATION_DICT = {'Pclass': str}
NUM_FEATURES = ['Age', 'SibSp', 'Parch', 'Fare', 'name_length', 'family_size']
CAT_FEATURES = ['Pclass', 'Sex', 'Embarked', 'name_title', 'ticket_class', 'cabin_class', 'age_cat', 'fare_cat']
CAT_FEATURES_FINAL = ['Pclass_1', 'Pclass_2', 'Pclass_3', 'Pclass_nan', 'Sex_female', 'Sex_male', 'Sex_nan',
'Embarked_C', 'Embarked_Q', 'Embarked_S', 'Embarked_nan', 'name_title_Master.',
'name_title_Miss.', 'name_title_Mr.', 'name_title_Mrs.', 'name_title_OTHER',
'name_title_nan', 'ticket_class_A', 'ticket_class_OTHER', 'ticket_class_PC',
'ticket_class_SC', 'ticket_class_STON_SOTON', 'ticket_class_nan', 'cabin_class_A',
'cabin_class_B', 'cabin_class_C', 'cabin_class_D', 'cabin_class_E', 'cabin_class_FGT',
'cabin_class_nan', 'age_cat_0_10', 'age_cat_10_20', 'age_cat_20_40', 'age_cat_40_60',
'age_cat_greater_60', 'age_cat_nan', 'fare_cat_0_8', 'fare_cat_15_25',
'fare_cat_25_50', 'fare_cat_8_15', 'fare_cat_greater_50', 'fare_cat_nan']
MODEL_FEATURES = NUM_FEATURES + CAT_FEATURES_FINAL
NUM_STRATEGY_IMPUTER = 'median'
SCALER_TYPE = None
LOG_APPLICATION = False
COLS_TO_LOG = ['Fare', 'Age']
ENCODER_DUMMY_NA = True
NAME_TITLE = True
CABIN_CLASS = True
TICKET_CLASS = True
NAME_LENGTH = True
AGE_CAT = True
FARE_CAT = True
FAMILY_SIZE = True
initial_train_pipeline = Pipeline([ 
('feature_adder', CustomFeaturesTitanic(name_title=NAME_TITLE, cabin_class=CABIN_CLASS,
ticket_class=TICKET_CLASS, name_length=NAME_LENGTH,
age_cat=AGE_CAT, fare_cat=FARE_CAT, family_size=FAMILY_SIZE)) , 
('col_filter', FiltraColunas(features=INITIAL_FEATURES)) , 
('dtype_modifier', ModificaTipoPrimitivo(mod_dict=DTYPE_MODIFICATION_DICT)) , 
('dup_dropper', EliminaDuplicatas())
 ])
 initial_pred_pipeline = Pipeline([ 
('feature_adder', CustomFeaturesTitanic(name_title=NAME_TITLE, cabin_class=CABIN_CLASS,
ticket_class=TICKET_CLASS, name_length=NAME_LENGTH,
age_cat=AGE_CAT, fare_cat=FARE_CAT, family_size=FAMILY_SIZE)) , 
('col_filter', FiltraColunas(features=INITIAL_PRED_FEATURES)) , 
('dtype_modifier', ModificaTipoPrimitivo(mod_dict=DTYPE_MODIFICATION_DICT))
])
 num_pipeline = Pipeline([ 
('imputer', SimpleImputer(strategy=NUM_STRATEGY_IMPUTER)) , 
('log_transformer', DynamicLogTransformation(application=LOG_APPLICATION, num_features=NUM_FEATURES,
cols_to_log=COLS_TO_LOG)) , 
('scaler', DynamicScaler(scaler_type=SCALER_TYPE))
])
 cat_pipeline = Pipeline([ 
('encoder', DummiesEncoding(dummy_na=ENCODER_DUMMY_NA, cat_features_final=CAT_FEATURES_FINAL))
])
 prep_pipeline = ColumnTransformer([ 
('num', num_pipeline, NUM_FEATURES), 
('cat', cat_pipeline, CAT_FEATURES)
 ] )
submission_dataset = pd.read_csv(".. /input/covid19-global-forecasting-week-4/submission.csv")
 submission_dataset['ConfirmedCases'] = cases_pred
submission_dataset['Fatalities'] = fatalities_pred
submission_dataset.head()<save_to_csv>
df = pd.read_csv(os.path.join(DATA_PATH, TRAIN_FILENAME))
df_prep = initial_train_pipeline.fit_transform(df)
 X_train, X_val, y_train, y_val = train_test_split(df_prep.drop(TARGET, axis=1), df_prep[TARGET].values,
test_size=.20, random_state=42)
 X_train_prep = prep_pipeline.fit_transform(X_train)
 X_val_prep = prep_pipeline.fit_transform(X_val)
 print(f'Shape of X_train_prep: {X_train_prep.shape}')
 print(f'Shape of X_val_prep: {X_val_prep.shape}')
 print(f'
Total features considered: {len(MODEL_FEATURES)}' )
submission_dataset.to_csv("submission.csv" , index = False )<load_from_csv>
df_prep = pd.DataFrame(X_train_prep, columns=MODEL_FEATURES)
 df_prep['Survived'] = y_train
plot_corr_matrix(df=df_prep, corr_col='Survived', figsize=(12, 12), cbar=False, n_vars=15 )
test = pd.read_csv(".. /input/covid19-global-forecasting-week-4/test.csv")
 train = pd.read_csv(".. /input/covid19-global-forecasting-week-4/train.csv" )<sort_values>
dtree = DecisionTreeClassifier()
forest = RandomForestClassifier()
lgbm = LGBMClassifier()
xgb = XGBClassifier()
adaboost = AdaBoostClassifier()
gradboost = GradientBoostingClassifier()
model_obj = [dtree, forest, lgbm, xgb, adaboost, gradboost]
model_names = [type(model ).__name__ for model in model_obj]
set_classifiers = {name: {'model': obj, 'params': {}} for(name, obj)in zip(model_names, model_obj)}
print(f'Classifiers that will be trained on next steps:
{model_names}' )
train[train['Country_Region'] == 'US'].sort_values('ConfirmedCases',ascending = False )<groupby>
trainer = ClassificadorBinario()
trainer.fit(set_classifiers, X_train_prep, y_train )
train[train['Country_Region'] == 'US'].groupby(['Date'] ).sum()<data_type_conversions>
metrics = trainer.evaluate_performance(X_train_prep, y_train, X_val_prep, y_val)
 metrics
train['Province_State'].fillna('', inplace=True)
 test['Province_State'].fillna('', inplace=True)
 train['Date'] = pd.to_datetime(train['Date'])
 test['Date'] = pd.to_datetime(test['Date'])
 train = train.sort_values(['Country_Region','Province_State','Date'])
 test = test.sort_values(['Country_Region','Province_State','Date'])
 train[['ConfirmedCases', 'Fatalities']] = train.groupby(['Country_Region', 'Province_State'])[['ConfirmedCases', 'Fatalities']].transform('cummax' )<compute_test_metric>
full_trainer = ClassificadorBinario()
full_trainer.training_flow(set_classifiers, X_train_prep, y_train, X_val_prep, y_val,
features=MODEL_FEATURES, random_search=True)
 full_trainer.visual_analysis(features=MODEL_FEATURES, model_shap='LGBMClassifier' )
def RMSLE(pred,actual):
return np.sqrt(np.mean(np.power(( np.log(pred+1)-np.log(actual+1)) ,2)))
 feature_day = [1,20,50,100,200,500,1000,5000,10000,15000,20000,50000,100000,200000, 500000]
def CreateInput(data):
feature = []
for day in feature_day:
data.loc[:,'Number day from ' + str(day)+ ' case'] = 0
if(train[(train['Country_Region'] == country)&(train['Province_State'] == province)&(train['ConfirmedCases'] < day)]['Date'].count() > 0):
fromday = train[(train['Country_Region'] == country)&(train['Province_State'] == province)&(train['ConfirmedCases'] < day)]['Date'].max()
else:
fromday = train[(train['Country_Region'] == country)&(train['Province_State'] == province)]['Date'].min()
for i in range(0, len(data)) :
if(data['Date'].iloc[i] > fromday):
day_denta = data['Date'].iloc[i] - fromday
data['Number day from ' + str(day)+ ' case'].iloc[i] = day_denta.days
feature = feature + ['Number day from ' + str(day)+ ' case']
return data[feature]<install_modules>
metrics = pd.read_csv('output/metrics/metrics.csv')
 metrics
!pip install pmdarima<save_to_csv>
forest_tunning_grid = {
'bootstrap': [True, False],
'class_weight': [None, 'balanced'],
'criterion': ['gini', 'entropy'],
'max_depth': [5, 6, 7, 9, 10],
'n_estimators': np.arange(300, 600, 50),
'random_state': [42]
}
lgbm_tunning_grid = {
'boosting_type': ['gbdt'],
'class_weight': [None, 'balanced'],
'learning_rate': [0.003, 0.01, 0.03, 0.1, 0.3, 1, 3],
'max_depth': np.arange(-1, 100, 2),
'n_estimators': np.arange(300, 700, 50),
'num_leaves': [5, 10, 15, 20],
'objective': ['binary'],
'random_state': [42],
'reg_alpha': np.linspace (.0, 25, 15)
 }
xgboost_tunning_grid = {
'booster': ['gbtree'],
'max_depth': [3, 4, 6, 7],
'learning_rate': [0.003, 0.01, 0.03, 0.1, 0.3, 1, 3],
'n_estimators': np.arange(300, 700, 50),
'objective': ['binary:logistic'],
'seed': [42],
'reg_alpha': np.linspace (.0, 25, 15),
'reg_lambda': np.linspace (.0, 25, 15),
'colsample_bylevel': [0.5, 0.7, 0.9]
}
adaboost_tunning_grid = {
'base_estimator': [DecisionTreeClassifier(max_depth=7)],
'n_estimators': np.arange(50, 700, 50),
'learning_rate': [0.003, 0.01, 0.03, 0.1, 0.3, 1, 3],
'algorithm': ['SAMME', 'SAMME.R'],
'random_state': [42]
}
gradboost_tunning_grid = {
'n_estimators': np.arange(50, 700, 50),
'max_depth': [3, 6, 7, 8, 9, 10, 15],
'max_features': [None, 'auto', 'sqrt', 'log2'],
'max_leaf_nodes': np.arange(3, 50, 2),
'random_state': [42]
}
df_val = df_val_2
submission = df_val[['ForecastId','ConfirmedCases_hat','Fatalities_hat']]
submission.columns = ['ForecastId','ConfirmedCases','Fatalities']
submission = submission.round({'ConfirmedCases': 0, 'Fatalities': 0})
 submission.to_csv('submission.csv', index=False)
 submission<import_modules>
tunning_models_keys = ['RandomForestClassifier', 'LGBMClassifier', 'XGBClassifier', 'AdaBoostClassifier',
'GradientBoostingClassifier']
tunning_param_grids = [forest_tunning_grid, lgbm_tunning_grid, xgboost_tunning_grid, adaboost_tunning_grid,
gradboost_tunning_grid]
tunned_pipelines = {}
general_metrics = pd.DataFrame({})
 pipe_model_key = 'model'
X = X_train.append(X_val)
 y = np.concatenate(( y_train, y_val))
for model_name, param_grid in zip(tunning_models_keys, tunning_param_grids):
baseline_model = trainer._get_estimator(model_name)
 feature_importance = baseline_model.feature_importances_
general_metrics = general_metrics.append(trainer._get_metrics(model_name))
tunning_pipeline = Pipeline([ 
('prep', prep_pipeline), 
('selector', SeletorTopFeatures(feature_importance, k=len(MODEL_FEATURES))), 
(pipe_model_key, baseline_model)
 ])
 model_param_grid = {pipe_model_key + '__' + k: v for k, v in param_grid.items() }
tunning_param_grid = {
'prep__num__imputer__strategy': ['mean', 'median', 'most_frequent'],
'prep__num__log_transformer__application': [True, False],
'prep__num__scaler__scaler_type': [None, 'Standard', 'MinMax'],
'selector__k': np.arange(5, len(MODEL_FEATURES)+ 1, 2)
 }
tunning_param_grid.update(model_param_grid)
 tunning_search = RandomizedSearchCV(tunning_pipeline, tunning_param_grid, scoring='accuracy', cv=5,
n_jobs=-1, verbose=-1, random_state=42)
 tunning_search.fit(X, y)
 print(f'
Best hyperparameters for {model_name} found by RandomizedSearch:
')
 for k, v in tunning_search.best_params_.items() :
print(f'{k}: {v}')
 final_pipeline = tunning_search.best_estimator_
tunned_pipelines[model_name] = final_pipeline
final_metrics = clf_cv_performance(final_pipeline, X, y, model_name=model_name)
 model_metrics = trainer._get_metrics(model_name=model_name)
 metrics_cols = general_metrics.columns
final_metrics = final_metrics.loc[:, metrics_cols]
general_metrics = general_metrics.append(final_metrics)
 general_metrics
warnings.filterwarnings('ignore')
 for dirname, _, filenames in os.walk('/kaggle/input'):
for filename in filenames:
print(os.path.join(dirname, filename)) 
<load_from_csv>
FINAL_MODEL = 'RandomForestClassifier'
final_pipeline = tunned_pipelines[FINAL_MODEL]
covid_cases = pd.read_csv('/kaggle/input/novel-corona-virus-2019-dataset/covid_19_data.csv')
 covid_cases.head()<load_from_csv>
df_test = pd.read_csv(os.path.join(DATA_PATH, TEST_FILENAME))
print(f'Shape of test dataset: {df_test.shape}')
 df_test.head()
training_data = pd.read_csv("/kaggle/input/covid19-global-forecasting-week-4/train.csv")
 testing_data = pd.read_csv("/kaggle/input/covid19-global-forecasting-week-4/test.csv" )<data_type_conversions>
model_consumer = ConsumoModelo(model=final_pipeline, features=INITIAL_PRED_FEATURES)
 prediction_pipeline = Pipeline([ 
('initial', initial_pred_pipeline), 
('prediction', model_consumer)
 ])
 df_pred = prediction_pipeline.fit_transform(df_test)
 df_pred.head()
<concatenate><EOS> 
df_sub = df_test.merge(df_pred, how='left', left_index=True, right_index=True)
 df_sub = df_sub.loc[:, ['PassengerId', 'y_pred']]
df_sub.columns = ['PassengerId', 'Survived']
df_sub.to_csv('output/submission.csv', index=False)
 df_sub.head()