Kaggle competition: Titanic - Machine Learning from Disaster
treeDepth = 30
mdl = tree.DecisionTreeRegressor(max_depth=treeDepth)
 param_grid = {
'n_estimators': [100],
'learning_rate': [0.0002],
'loss' : ["exponential"]
}
regrMdl = ensemble.AdaBoostRegressor(base_estimator=mdl)
 model_Confirmed["Adaboost"] = model_selection.RandomizedSearchCV(estimator = regrMdl, param_distributions = param_grid, n_iter = 100,
cv = 3, verbose=0, random_state=42, n_jobs = -1 ).fit(train_data, train_labels)
 model_Death["Adaboost"] = model_selection.RandomizedSearchCV(estimator = regrMdl, param_distributions = param_grid, n_iter = 100,
cv = 3, verbose=0, random_state=42, n_jobs = -1 ).fit(train_data, train_death )<train_model>
df = pd.read_csv('.. /input/train.csv')
 df.head(2 )
model_Confirmed["RandomForest"] = ensemble.RandomForestRegressor(n_estimators=200, max_depth=30, random_state=42,
n_jobs = -1 ).fit(train_data, train_labels)
 model_Death["RandomForest"] = ensemble.RandomForestRegressor(n_estimators=200, max_depth=30, random_state=42,
n_jobs = -1 ).fit(train_data, train_death)
<train_model>
df[df['Embarked'].isna() ]
model_Confirmed["Xgboost"] = xgb.XGBRegressor(objective ='reg:squarederror',n_estimators=200, max_depth=20, random_state=42,
n_jobs = -1 ).fit(train_data, train_labels)
 model_Death["Xgboost"] = xgb.XGBRegressor(objective ='reg:squarederror',n_estimators=200, max_depth=20, random_state=42,
n_jobs = -1 ).fit(train_data, train_death )<compute_test_metric>
titles = df['Name'].str.extract('([A-za-z]+)\.', expand=False)
 titles_df = pd.DataFrame()
titles_df['Title'] = titles
titles_df['Survived'] = df['Survived']
pred_score(model, data_pred, death_bool, plot_type )<define_variables>
titles_df['Title'].value_counts().head(6 )
days_prog = 30
Matrix_confirmed,Matrix_Death ={},{}
for model_name in model_Confirmed:
new_data_list = new_data[new_data.days_mart == days_x]
labels = new_data_list.countries
predictions_confirmed = []
predictions_Death = []
new_data_list = new_data_list.drop(['Province_State', 'ForecastId','confirmed','countries','deaths'], axis='columns')
 for _ in range(days_prog):
prediction_confirmed = model_Confirmed[model_name].predict(new_data_list)
 prediction_Death = model_Death[model_name].predict(new_data_list)
 prediction_confirmed = prediction_confirmed*(0.95**(new_data_list.days_after_Quarantine.values/10 - 1.4))
prediction_Death = prediction_Death*(0.95**(new_data_list.days_after_Quarantine.values/10 - 1.4))
prediction_confirmed = prediction_confirmed + new_data_list.pred_conf.values
prediction_Death = prediction_Death + new_data_list.pred_deaths.values

 new_data_list["delta_conf2"] = new_data_list["delta_conf1"]
new_data_list["delta_conf1"] = prediction_confirmed - new_data_list["pred_conf"]
new_data_list["pred_conf"] = prediction_confirmed
new_data_list["delta_deaths2"] = new_data_list["delta_deaths1"]
new_data_list["delta_deaths1"] = prediction_Death - new_data_list["pred_deaths"]
new_data_list["pred_deaths"] = prediction_Death
prediction_Death = 10**prediction_Death - 1
prediction_confirmed = 10**prediction_confirmed - 1
new_data_list[["days_mart", "days"]] += 1
predictions_Death.append(np.round(prediction_Death))
predictions_confirmed.append(np.round(prediction_confirmed))
data_list = pd.date_range('2020-04-'+str(days_x-30), periods = days_prog, freq ='d')
 data_list = data_list.strftime('%
Matrix_confirmed[model_name] = pd.DataFrame(predictions_confirmed,columns = labels,index = data_list)
 Matrix_Death[model_name] = pd.DataFrame(predictions_Death,columns = labels,index = data_list)
 Matrix_confirmed["Среднее"] =(Matrix_confirmed["Xgboost"] + Matrix_confirmed["RandomForest"] + Matrix_confirmed["Adaboost"])//3
Matrix_Death["Среднее"] =(Matrix_Death["Xgboost"] + Matrix_Death["RandomForest"] + Matrix_Death["Adaboost"])//3<feature_engineering>
age_eda = df.groupby(['Survived', 'Age'] ).size().rename('count' ).reset_index()
ans = Matrix_confirmed["Adaboost"].stack().reset_index()
ans_Death = Matrix_Death["Adaboost"].stack().reset_index()
ans["prediction_deaths"] = ans_Death[0]
ans["Province_State"] = new_data.groupby(["countries", "Province_State"] ).max().reset_index() ["Province_State"].tolist() *30
ans.columns = ['Date','Country_Region','ConfirmedCases', 'Fatalities',"Province_State"]
ans['Date'] = '2020-'+ans['Date']
train2 = train[(train.Date >= "2020-04-02")]
train2['Province_State'] = train2['Province_State'].fillna("zzz")
 train2.drop(['Id'], axis='columns',inplace=True)
 ans = pd.concat([train2, ans],ignore_index=True)
 ans['ConfirmedCases'] = ans['ConfirmedCases']
ans['Fatalities'] = ans['Fatalities']
ans = ans.sort_values(by=['Country_Region','Province_State','Date'])
 ans['ForecastId'] = np.arange(1,len(ans)+1)
 ans = ans[['ForecastId','ConfirmedCases','Fatalities']]
ans.to_csv('submission.csv', index=False )<load_from_csv>
del age_eda
age_eda = df.copy()
age_eda['Age'].dropna(inplace=True)
 age_eda['Age'] = age_eda['Age'].astype(int)
 age_eda.loc[ age_eda['Age'] <= 11, 'Age'] = 0
age_eda.loc[(age_eda['Age'] > 11)&(age_eda['Age'] <= 18), 'Age'] = 1
age_eda.loc[(age_eda['Age'] > 18)&(age_eda['Age'] <= 22), 'Age'] = 2
age_eda.loc[(age_eda['Age'] > 22)&(age_eda['Age'] <= 27), 'Age'] = 3
age_eda.loc[(age_eda['Age'] > 27)&(age_eda['Age'] <= 32), 'Age'] = 4
age_eda.loc[(age_eda['Age'] > 32)&(age_eda['Age'] <= 40), 'Age'] = 5
age_eda.loc[(age_eda['Age'] > 40), 'Age'] = 6
train_df = pd.read_csv(sorted(paths)[-1])
 test_df = pd.read_csv(sorted(paths)[-2])
 submission = pd.read_csv(sorted(paths)[-3])
 sub_08_04 = pd.read_csv(sorted(paths)[-5] )<import_modules>
from sklearn import linear_model
from sklearn.svm import SVC
from sklearn.tree import DecisionTreeClassifier
from sklearn.ensemble import GradientBoostingClassifier
from sklearn.neighbors import KNeighborsClassifier
from sklearn.ensemble import RandomForestClassifier, AdaBoostClassifier
from sklearn.naive_bayes import GaussianNB
from xgboost import XGBClassifier
from sklearn.model_selection import train_test_split
from sklearn.model_selection import cross_val_score
from statsmodels.tsa.statespace.sarimax import SARIMAX<define_variables>
train = pd.read_csv('.. /input/train.csv')
 test = pd.read_csv('.. /input/test.csv')
 train_test = train.append(test, sort=False)
 train_test.reset_index(inplace=True)
 train_test.drop(['index'], inplace=True, axis=1)
 train_test.head(2 )
count_len = len(train_df[train_df['Country_Region'] == 'Russia'])
 train_cc = []
train_f = []
count = 0
for i in range(int(len(train_df)/ count_len)) :
train_cc.append(train_df.ConfirmedCases[count:count+count_len].values.tolist())
 train_f.append(train_df.Fatalities[count:count+count_len].values.tolist())
 count += count_len<define_variables>
def add_titles(data):
data['Title'] = data['Name'].str.extract('([A-za-z]+)\.', expand=False)
 title_mapping = {"Mr": 'Mr',
"Miss": 'Miss',
"Mrs": 'Mrs',
"Master": 'Master',
"Rev": 'Rev',
"Dr": 'Other',
"Col": 'Other',
"Major": 'Other',
"Mlle": 'Other',
"Countess": 'Other',
"Ms": 'Other',
"Lady": 'Other',
"Jonkheer": 'Other',
"Don": 'Other',
"Dona" : 'Other',
"Mme": 'Other',
"Capt": 'Other',
"Sir": 'Other'}
data['Title'] = data['Title'].map(title_mapping )
delta =(datetime.today().date() - date(2020, 4, 2)).days<train_on_grid>
add_titles(train_test )
test_count = len(test_df[test_df['Country_Region'] == 'Russia'])- delta - 1
predicted_cc = []
for i in range(len(train_cc)) :
try:
data1 = train_cc[i]
model1 = SARIMAX(data1, order=(1,1,0), seasonal_order=(1,1,0,12), measurement_error=True)
 model1_fit = model1.fit(disp=False)
 predicted1 = model1_fit.predict(len(data1), len(data1)+test_count)
 predicted_cc.append(predicted1.tolist())
 except:
data1 = train_cc[i]
model1 = SARIMAX(data1,order=(1,1,0),seasonal_order=(1,1,0,12),measurement_error=True,enforce_stationarity=False)
 model1_fit = model1.fit(disp=False)
 predicted1 = model1_fit.predict(len(data1), len(data1)+test_count)
 predicted_cc.append(predicted1.tolist() )<train_on_grid>
def clean_fare(data):
data['Fare'] = data['Fare'].fillna(data['Fare'].dropna().median() )
predicted_f = []
for i in range(len(train_f)) :
try:
data2 = train_f[i]
model2 = SARIMAX(data2,order=(1,1,0), seasonal_order=(1,1,0,12), measurement_error=True)
 model2_fit = model2.fit(disp=False)
 predicted2 = model2_fit.predict(len(data2), len(data2)+test_count)
 predicted_f.append(predicted2.tolist())
 except:
data2 = train_f[i]
model2 = SARIMAX(data2,order=(1,1,0),seasonal_order=(1,1,0,12),measurement_error=True,enforce_stationarity=False)
 model2_fit = model2.fit(disp=False)
 predicted2 = model2_fit.predict(len(data2), len(data2)+test_count)
 predicted_f.append(predicted2.tolist() )<train_model>
clean_fare(train_test )
check_lenght = len(train_cc[0][-delta:])+ len(predicted_cc[0])
 if check_lenght == 43:
print('Check OK')
 else:
print('Check failed' )<define_variables>
def clean_age(data):
data["Age"] = data.groupby(['Sex','Pclass','Title'])['Age'].transform(lambda x: x.fillna(x.median()))
new_countries_idxs = [121, 166, 183, 210, 221, 299, 310]<define_variables>
clean_age(train_test )
days_from =(datetime.today().date() - date(2020, 4, 9)).days<filter>
train_test.loc[train_test['Age'].isnull() ]
sub_08_04[sub_08_04['Country_Region'] == 'Russia'].iloc[14:14+days_from]<filter>
train_test.loc[979, 'Age'] = train_test['Age'].median()
train_df[train_df['Country_Region'] == 'Russia'].iloc[78:78+days_from]<define_variables>
def bin_age(dataset):
dataset['Age'] = dataset['Age'].astype(int)
 dataset.loc[ dataset['Age'] <= 11, 'Age'] = 0
dataset.loc[(dataset['Age'] > 11)&(dataset['Age'] <= 18), 'Age'] = 1
dataset.loc[(dataset['Age'] > 18)&(dataset['Age'] <= 22), 'Age'] = 2
dataset.loc[(dataset['Age'] > 22)&(dataset['Age'] <= 27), 'Age'] = 3
dataset.loc[(dataset['Age'] > 27)&(dataset['Age'] <= 32), 'Age'] = 4
dataset.loc[(dataset['Age'] > 32)&(dataset['Age'] <= 40), 'Age'] = 5
dataset.loc[(dataset['Age'] > 40)&(dataset['Age'] <= 66), 'Age'] = 6
dataset.loc[ dataset['Age'] > 66, 'Age'] = 6
sub_count_len = len(sub_08_04[sub_08_04['Country_Region'] == 'Russia'])
 sub_0804_cc_preds = []
sub_0804_f_preds = []
sub_count = 0
for i in range(int(len(sub_08_04)/ sub_count_len)) :
sub_0804_cc_preds.append(sub_08_04.ConfirmedCases[sub_count+14:sub_count+14+days_from].values.tolist())
 sub_0804_f_preds.append(sub_08_04.Fatalities[sub_count+14:sub_count+14+days_from].values.tolist())
 sub_count += sub_count_len<define_variables>
def clean_embarked(data):
data['Embarked'] = data['Embarked'].fillna('S' )
train_count_len = len(train_df[train_df['Country_Region'] == 'Russia'])
 train_cc_act = []
train_f_act = []
train_count = 0
for i in range(int(len(train_df)/ train_count_len)) :
train_cc_act.append(train_df.ConfirmedCases[train_count+78:train_count+78+days_from].values.tolist())
 train_f_act.append(train_df.Fatalities[train_count+78:train_count+78+days_from].values.tolist())
 train_count += train_count_len<concatenate>
def clean_cabin(data):
data['Cabin'].fillna('U', inplace=True)
 data['Cabin'] = data['Cabin'].map(lambda x: x[0] )
train_0804_cc_act = []
train_0804_f_act = []
for i in range(len(train_cc_act)) :
if i not in new_countries_idxs:
train_0804_cc_act.append(train_cc_act[i])
 train_0804_f_act.append(train_f_act[i] )<categorify>
def clean_family(data):
data['FamilySize'] = data['SibSp'] + data['Parch'] + 1
data.drop(['SibSp', 'Parch'], axis = 1, inplace = True )
div_act_sub_cc = []
div_act_sub_f = []
for i in range(len(train_0804_cc_act)) :
div_act_sub_cc.append([train_0804_cc_act[i][0] / sub_0804_cc_preds[i][0],
train_0804_cc_act[i][1] / sub_0804_cc_preds[i][1]])
 div_act_sub_f.append([(train_0804_f_act[i][0]+1)/(sub_0804_f_preds[i][0]+1), 
(train_0804_f_act[i][1]+1)/(sub_0804_f_preds[i][1]+1)] )<feature_engineering>
clean_family(train_test )
cc_becs = []
f_becs = []
for i in range(len(div_act_sub_cc)) :
cc_becs.append(np.mean(div_act_sub_cc[i]))
f_becs.append(np.mean(div_act_sub_f[i]))<prepare_output>
def encode(data, labels):
for label in labels:
data = data.join(pd.get_dummies(data[label], prefix = label))
data.drop(label, axis=1, inplace=True)
 return data
for idx in new_countries_idxs:
cc_becs.insert(idx, 1)
 f_becs.insert(idx, 1 )<sort_values>
store = train_test.copy()
predicted_ConfirmedCases = []
predicted_Fatalities = []
for i in range(int(len(train_df)/ count_len)) :
predicted_ConfirmedCases.append(train_cc[i][-delta:])
 predicted_ConfirmedCases.append(predicted_cc[i])
 predicted_Fatalities.append(train_f[i][-delta:])
 predicted_Fatalities.append(predicted_f[i])
 predicted_ConfirmedCases = list(itertools.chain.from_iterable(predicted_ConfirmedCases))
predicted_Fatalities = list(itertools.chain.from_iterable(predicted_Fatalities))<categorify>
train_test = encode(train_test, ['Pclass', 'Sex', 'Embarked', 'Title', 'Cabin'])
 train_test.head(1 ).T
mod_predicted_cc = []
mod_predicted_f = []
part = 1
for i in range(len(predicted_cc)) :
mod_predicted_cc.append([x*(cc_becs[i]*part)for x in predicted_cc[i]])
 mod_predicted_f.append([x*(f_becs[i]*part)for x in predicted_f[i]] )<define_variables>
train = train_test.loc[:890, :]
test = train_test.loc[891:, :]
mean_ConfirmedCases_preds = []
mean_Fatalities_preds = []
for i in range(int(len(predicted_ConfirmedCases))):
mean_ConfirmedCases_preds.append(predicted_ConfirmedCases[i] * 0.15 + m_predicted_ConfirmedCases[i] * 0.85)
 mean_Fatalities_preds.append(predicted_Fatalities[i] * 0.15 + m_predicted_Fatalities[i] * 0.85 )<prepare_output>
def model(classifier, train, test):
target = train['Survived'].values
features = train.drop(['PassengerId', 'Survived', 'Name', 'Ticket'], axis = 1 ).values
scores = cross_val_score(classifier, features, target, cv=5)
 print(f'Scores for 5 fold CV: {round(np.mean(scores*100)) }')
 classifier_ = classifier.fit(features, target)
 test = test.drop(['PassengerId', 'Survived', 'Name', 'Ticket'], axis = 1)
 predictions = classifier_.predict(test ).astype(int)
 return predictions
submission['ConfirmedCases'] = predicted_ConfirmedCases
submission['Fatalities'] = predicted_Fatalities<create_dataframe>
def submit(predictions):
submission = pd.read_csv('.. /input/gender_submission.csv')
 submission['Survived'] = predictions
submission.to_csv('submission.csv', index=False)
 return submission
submission1 = pd.DataFrame(data=submission.ForecastId, columns=['ForecastId'])
 submission1['ConfirmedCases'] = m_predicted_ConfirmedCases
submission1['Fatalities'] = m_predicted_Fatalities<create_dataframe>
classifier = linear_model.LogisticRegression(solver='liblinear')
 predictions = model(classifier, train, test )
submission2 = pd.DataFrame(data=submission.ForecastId, columns=['ForecastId'])
 submission2['ConfirmedCases'] = mean_ConfirmedCases_preds
submission2['Fatalities'] = mean_Fatalities_preds<load_from_csv>
classifier = SVC(gamma='auto', kernel='linear')
 predictions = model(classifier, train, test )
concl_df = pd.read_csv(sorted(paths)[-2])
 concl_feats = ['ForecastId', 'Country_Region', 'Date']
conclusion = pd.concat([concl_df[concl_feats], submission[['ConfirmedCases', 'Fatalities']]], axis=1)
 concl1_df = pd.read_csv(sorted(paths)[-2])
 conclusion1 = pd.concat([concl1_df[concl_feats], submission1[['ConfirmedCases', 'Fatalities']]], axis=1)
 concl2_df = pd.read_csv(sorted(paths)[-2])
 conclusion2 = pd.concat([concl2_df[concl_feats], submission2[['ConfirmedCases', 'Fatalities']]], axis=1 )<filter>
classifier = DecisionTreeClassifier(random_state = 1, max_depth = 3)
 predictions = model(classifier, train, test )
region = 'Russia'
conclusion2[conclusion2['Country_Region'] == region]<feature_engineering>
classifier = GradientBoostingClassifier()
predictions = model(classifier, train, test )
original_cc = conclusion[conclusion['Country_Region'] == region]['ConfirmedCases'].values
corrected_cc = conclusion1[conclusion1['Country_Region'] == region]['ConfirmedCases'].values
mean_cc = conclusion2[conclusion2['Country_Region'] == region]['ConfirmedCases'].values
original_f = conclusion[conclusion['Country_Region'] == region]['Fatalities'].values
corrected_f = conclusion1[conclusion1['Country_Region'] == region]['Fatalities'].values
mean_f = conclusion2[conclusion2['Country_Region'] == region]['Fatalities'].values
date = conclusion[conclusion['Country_Region'] == region]['Date'].values
for i in range(len(date)) :
date[i] = date[i][5:]
fig,(ax1, ax2)= plt.subplots(2,1, figsize=(14,14), dpi=120)
 ax1.plot(date, original_cc, ':g', label='original SARIMAX')
 ax1.plot(date, corrected_cc, ':b', label='corrected SARIMAX')
 ax1.plot(date, mean_cc, 'r', label='weighted SARIMAX')
 ax2.plot(date, original_f, ':g', label='original SARIMAX')
 ax2.plot(date, corrected_f, ':b', label='corrected SARIMAX')
 ax2.plot(date, mean_f, 'r', label='weighted SARIMAX')
 ax1.set_title(f'ConfirmedCases differences {region}')
 ax1.set_xlabel('Date')
 ax1.set_xticklabels(date, rotation=40)
 ax1.set_ylabel('ConfirmedCases')
 ax1.legend(loc='best')
 ax1.grid()
ax2.set_title(f'Fatalities differences {region}')
 ax2.set_xlabel('Date')
 ax2.set_xticklabels(date, rotation=40)
 ax2.set_ylabel('Fatalities')
 ax2.legend(loc='best')
 ax2.grid()
plt.show() ;<save_to_csv>
classifier = KNeighborsClassifier(3)
 predictions = model(classifier, train, test )
submission2.to_csv('submission.csv', index=False )<import_modules>
classifier = GaussianNB()
predictions = model(classifier, train, test )
import numpy as np
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
import nltk
from sklearn.preprocessing import LabelBinarizer,LabelEncoder,StandardScaler,MinMaxScaler
from sklearn.linear_model import LogisticRegression,SGDClassifier,LinearRegression
from sklearn.naive_bayes import MultinomialNB
from sklearn.svm import SVC
from sklearn.ensemble import RandomForestRegressor
from sklearn.metrics import classification_report,confusion_matrix,accuracy_score
from sklearn.model_selection import train_test_split
import keras
from keras.wrappers.scikit_learn import KerasRegressor
from sklearn.model_selection import cross_val_score
from sklearn.model_selection import KFold
from keras.models import Sequential
from keras.layers import Dense,LSTM
import tensorflow as tf<load_from_csv>
classifier = AdaBoostClassifier()
predictions = model(classifier, train, test )
train_df = pd.read_csv(".. /input/covid19-global-forecasting-week-4/train.csv")
 test_df = pd.read_csv(".. /input/covid19-global-forecasting-week-4/test.csv")
 submission = pd.read_csv(".. /input/covid19-global-forecasting-week-4/submission.csv" )<count_missing_values>
classifier = RandomForestClassifier(n_estimators=100, max_depth = 7)
 predictions = model(classifier, train, test )
train_df.isna().sum()<count_missing_values>
xg_test = test.drop(['PassengerId', 'Survived', 'Name', 'Ticket'], axis = 1 ).as_matrix()
xg_train = train.drop(['PassengerId', 'Survived', 'Name', 'Ticket'], axis = 1 ).as_matrix()
target = train['Survived'].values
classifier = XGBClassifier()
classifier_ = classifier.fit(xg_train, target)
 classifier_.score(xg_train, target )
test_df.isna().sum()<drop_column>
predictions = classifier_.predict(xg_test ).astype(int )
train_df['Country_Region'] = train_df['Country_Region'] + ' ' + train_df['Province_State']
test_df['Country_Region'] = test_df['Country_Region'] + ' ' + test_df['Province_State']
del train_df['Province_State']
del test_df['Province_State']<feature_engineering>
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
train_df.Date = train_df.Date.apply(split_date)
 test_df.Date = test_df.Date.apply(split_date )<feature_engineering>
submission = submit(predictions)
 submission['Survived'].value_counts()