Kaggle competition: Titanic - Machine Learning from Disaster
test_df_predict.groupby(['Date','Country_Region'])['Confirmedcase','Fatalities'].max().reset_index().head(10)
<sort_values>
sns.set()
test_df_predict.groupby('Country_Region')['Confirmedcase', 'Fatalities'].sum().reset_index().sort_values(by='Confirmedcase',ascending=False ).head(15)
<save_to_csv>
data_train = pd.read_csv('/kaggle/input/titanic/train.csv')
 data_test = pd.read_csv('/kaggle/input/titanic/test.csv')
 data_total = data_train.append(data_test, ignore_index=True)
 data_train_length = len(data_train.index)
 for c in ['Pclass', 'Sex', 'Embarked', 'SibSp', 'Parch', 'Survived']:
sns.catplot(x=c, kind='count', data=data_train)
 plt.show()
for c in ['Age', 'Fare']:
data_train[c].hist().set(xlabel=c)
 plt.show()
for c in ['Pclass', 'Sex', 'Embarked', 'SibSp', 'Parch']:
sns.catplot(x=c, y='Survived', kind='bar', data=data_train)
 plt.show()
for c in ['Age', 'Fare']:
g = sns.FacetGrid(data_train, col='Survived')
 g.map(plt.hist, c)
 plt.show()
corr = data_train.corr()
sns.boxplot(x='Pclass', y='Age', hue='Survived', data=data_train)
 plt.show()
sns.boxplot(x='Pclass', y='Fare', hue='Survived', data=data_train[data_train['Fare'] < 200])
 plt.show()
cases_pred = [round(value)for value in cases_pred ]
fatalities_pred = [round(value)for value in fatalities_pred ]
submission['ConfirmedCases'] = cases_pred
submission['Fatalities'] = fatalities_pred
submission.to_csv('submission.csv',index=False)
<set_options>
age_mean_by_pclass = data_total[['Pclass', 'Age']].groupby('Pclass' ).mean()
data_total['Age'] = data_total.apply(
 lambda row: age_mean_by_pclass.loc[row['Pclass'], 'Age'] if np.isnan(row['Age'])else row['Age'], axis=1)
 fare_median_by_pclass = data_total[['Pclass', 'Fare']].groupby('Pclass' ).median()
data_total['Fare'] = data_total.apply(
 lambda row: fare_median_by_pclass.loc[row['Pclass'], 'Fare'] if np.isnan(row['Fare'])else row['Fare'], axis=1)
 data_total['Embarked'].fillna(data_total['Embarked'].mode() [0], inplace=True)
 data_total.info()
%matplotlib inline 
<define_variables>
data_total['isAlone'] =(( data_total['SibSp'] + data_total['Parch'])== 0)* 1
data_total['bigGroup'] =(( data_total['SibSp'] + data_total['Parch'])> 3)* 1
data_total['Title'] = data_total['Name'].str.extract('([A-Za-z]+)\.', expand=False)
 data_total['Title'] = data_total['Title'].replace(['Lady', 'Countess','Capt', 'Col', 'Don', 'Dr', 'Major', 'Rev', 'Sir', 'Jonkheer', 'Dona'], 'Other')
 data_total['Title'] = data_total['Title'].replace(['Mlle', 'Ms', 'Mme'], 'Miss')
 age_bin_count = 10
data_total['Age_Bin'] = pd.cut(data_total['Age'], age_bin_count, labels=list(range(1,age_bin_count+1)))
 fare_bin_count = 5
data_total['Fare_Bin'] = pd.qcut(data_total['Fare'], fare_bin_count, labels=list(range(1,fare_bin_count+1)))
 enc = OneHotEncoder().fit(data_total[['Sex', 'Embarked', 'Title']])
 enc_features_arr = enc.transform(data_total[['Sex', 'Embarked', 'Title']] ).toarray()
enc_features_labels = [label for cat in enc.categories_ for label in cat]
enc_features_df = pd.DataFrame(enc_features_arr, columns=enc_features_labels ).drop(columns=['male', 'S', 'Other'])
 data_total = data_total.join(enc_features_df)
 data_total = data_total.drop(columns=['PassengerId', 'Name', 'Cabin', 'Ticket', 'SibSp', 'Parch', 'Age', 'Fare', 'Sex', 'Embarked', 'Title'])
 data_total.head()
PATH ='/kaggle/input/covid19-global-forecasting-week-4'<load_from_csv>
data_train = data_total[:data_train_length]
X = data_train.drop(columns=['Survived'], axis=1)
 y = data_train['Survived']
X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=0)
 scaler = StandardScaler().fit(X_train)
 X_train = scaler.transform(X_train)
 X_test = scaler.transform(X_test )
train = pd.read_csv(f'{PATH}/train.csv')
 train.head()<groupby>

 svc_params = [{'C': [0.2, 0.4, 0.5, 1, 2, 3, 4, 5], 'kernel': ['rbf'], 'gamma': [.10,.15,.20,.25,.30]}]
knn_params = [{'n_neighbors': [7, 8, 9, 10], 'weights': ['uniform'], 'algorithm': ['brute'], 'p': [2]}]
rfc_params = [{'n_estimators': [80, 90, 100, 110, 120, 130, 140], 'criterion': ['entropy']}]
classifier_names = ['SVC', 'KNN', 'RFC']
params = [svc_params, knn_params, rfc_params]
classifier_objs = [SVC() , KNeighborsClassifier() , RandomForestClassifier() ]
best_params = {}
for i in range(len(classifier_names)) :
classifier_objs[i].fit(X_train, y_train)
 grid_search = GridSearchCV(estimator=classifier_objs[i], param_grid=params[i], scoring='accuracy', cv=10 ).fit(X_train, y_train)
 best_params[classifier_names[i]] = grid_search.best_params_
total_countries_covid = train.groupby(['Country_Region'])['ConfirmedCases'].sum() 
<sort_values>
classifier_tuned = [SVC(C=best_params['SVC']['C'], gamma=best_params['SVC']['gamma'], kernel=best_params['SVC']['kernel']),
KNeighborsClassifier(algorithm=best_params['KNN']['algorithm'], n_neighbors=best_params['KNN']['n_neighbors'],
p=best_params['KNN']['p'], weights=best_params['KNN']['weights']),
LogisticRegression() ,
RandomForestClassifier(criterion=best_params['RFC']['criterion'], n_estimators=best_params['RFC']['n_estimators']),
GaussianNB() ]
predictions_train = []
for clf in classifier_tuned:
clf.fit(X_train, y_train)
 y_pred = clf.predict(X_test)
 predictions_train.append(y_pred)
 print(clf)
 print(confusion_matrix(y_test, y_pred))
print('accuracy score: ' + str(accuracy_score(y_test, y_pred)))
 print('cross val score: ' + str(cross_val_score(clf, X_train, y_train ).mean()))
print('–––––––––')
 predictions_train = pd.DataFrame(predictions_train ).transpose()
predictions_train['y_pred_voted'] = predictions_train.mean(axis=1 ).round()
predictions_train_voted = predictions_train['y_pred_voted'].values
print('Result after voting:')
 print(confusion_matrix(y_test, predictions_train_voted))
print(accuracy_score(y_test, predictions_train_voted))