Kaggle competition: 2019  ML competition with KISTI
<SOS> metric: accuracy Kaggle data source: 2019-ml-competition-with-kisti<train_model>
%matplotlib inline
warnings.filterwarnings('ignore')
RF_Model.fit(X_train, y_train )<predict_on_test>
train = pd.read_csv(".. /input/train.csv")
 test = pd.read_csv(".. /input/test.csv")
 train.describe(include="all" )
predictions = RF_Model.predict(X_test)
 predictions<compute_test_metric>
print('The null of train set')
 print(pd.isnull(train ).sum())
 print('*'*50)
 for col in train.columns:
msg = 'column: {:>10}\t Percent of NaN value: {:.2f}%'.format(col, 100 *(train[col].isnull().sum() / train[col].shape[0]))
print(msg )
print(f'Test : {RF_Model.score(X_test, y_test):.3f}')
 print(f'Train : {RF_Model.score(X_train, y_train):.3f}' )<drop_column>
print('The null of test set')
 print(pd.isnull(test ).sum())
 print('*'*50)
 for col in test.columns:
msg = 'column: {:>10}\t Percent of NaN value: {:.2f}%'.format(col, 100 *(train[col].isnull().sum() / train[col].shape[0]))
print(msg )
TestForPred = all_test.drop(['PassengerId', 'Survived'], axis = 1 )<predict_on_test>
train = train.drop(['Cabin'], axis = 1)
 test = test.drop(['Cabin'], axis = 1 )
t_pred = RF_Model.predict(TestForPred ).astype(int )<define_variables>
train = train.drop(['Ticket'], axis = 1)
 test = test.drop(['Ticket'], axis = 1 )
PassengerId = all_test['PassengerId']<prepare_output>
train = train.fillna({"Embarked": "S"} )
RF_Sub = pd.DataFrame({'PassengerId': PassengerId, 'Survived':t_pred })
 RF_Sub.head()<save_to_csv>
combine = [train, test]
for dataset in combine:
dataset['Title'] = dataset.Name.str.extract('([A-Za-z]+)\.', expand=False)
 pd.crosstab(train['Title'], train['Sex'] )
RF_Sub.to_csv("RF_Class_Submission.csv", index = False )<load_from_csv>
for dataset in combine:
dataset['Title'] = dataset['Title'].replace(['Lady', 'Capt', 'Col',
'Don', 'Dr', 'Major', 'Rev', 'Jonkheer', 'Dona'], 'Rare')
 dataset['Title'] = dataset['Title'].replace(['Countess', 'Lady', 'Sir'], 'Royal')
 dataset['Title'] = dataset['Title'].replace('Mlle', 'Miss')
 dataset['Title'] = dataset['Title'].replace('Ms', 'Miss')
 dataset['Title'] = dataset['Title'].replace('Mme', 'Mrs')
 train[['Title', 'Survived']].groupby(['Title'], as_index=False ).mean()
data_train = pd.read_csv('/kaggle/input/titanic/train.csv')
 data_test = pd.read_csv('/kaggle/input/titanic/test.csv')
 data = pd.concat([data_train, data_test], ignore_index=True, sort=False )<create_dataframe>
title_mapping = {"Mr": 1, "Miss": 2, "Mrs": 3, "Master": 4, "Royal": 5, "Rare": 6}
for dataset in combine:
dataset['Title'] = dataset['Title'].map(title_mapping)
 dataset['Title'] = dataset['Title'].fillna(0)
 train.head()
pClass_1 = round(
(data_train[data_train.Pclass == 1].Survived == 1 ).value_counts() [1] /
len(data_train[data_train.Pclass == 1])* 100, 2)
 pClass_2 = round(
(data_train[data_train.Pclass == 2].Survived == 1 ).value_counts() [1] /
len(data_train[data_train.Pclass == 2])* 100, 2)
 pClass_3 = round(
(data_train[data_train.Pclass == 3].Survived == 1 ).value_counts() [1] /
len(data_train[data_train.Pclass == 3])* 100, 2)
 pClassDf = pd.DataFrame(
 {"Survived": {"Class 1": pClass_1,
"Class 2": pClass_2,
"Class 3": pClass_3},
"Not survived": {"Class 1": 100 - pClass_1,
"Class 2": 100 - pClass_2,
"Class 3": 100 - pClass_3}})
 pClassDf.plot.bar().set_title("Survived ~ Class")
 plt.show()
data_train[['Pclass', 'Survived']].groupby(['Pclass'], as_index=False ).mean().sort_values(by='Survived', ascending=False )<feature_engineering>
mr_age = train[train["Title"] == 1]["AgeGroup"].mode()
miss_age = train[train["Title"] == 2]["AgeGroup"].mode()
mrs_age = train[train["Title"] == 3]["AgeGroup"].mode()
master_age = train[train["Title"] == 4]["AgeGroup"].mode()
royal_age = train[train["Title"] == 5]["AgeGroup"].mode()
rare_age = train[train["Title"] == 6]["AgeGroup"].mode()
age_title_mapping = {1: "Young Adult", 2: "Student", 3: "Adult", 4: "Baby", 5: "Adult", 6: "Adult"}
for x in range(len(train["AgeGroup"])) :
if train["AgeGroup"][x] == "Unknown":
train["AgeGroup"][x] = age_title_mapping[train["Title"][x]]
for x in range(len(test["AgeGroup"])) :
if test["AgeGroup"][x] == "Unknown":
test["AgeGroup"][x] = age_title_mapping[test["Title"][x]]
data['Family'] = data.Parch + data.SibSp
data['IsAlone'] = data.Family == 0
data['SmallFamily'] = data['Family'].map(lambda s: 1 if 1 <= s <= 3 else 0)
 data['BigFamily'] = data['Family'].map(lambda s: 1 if 4 <= s else 0 )<count_unique_values>
age_mapping = {'Baby': 1, 'Child': 2, 'Teenager': 3, 'Student': 4, 'Young Adult': 5, 'Adult': 6, 'Senior': 7}
train['AgeGroup'] = train['AgeGroup'].map(age_mapping)
 test['AgeGroup'] = test['AgeGroup'].map(age_mapping)
 train.head()
train = train.drop(['Age'], axis = 1)
 test = test.drop(['Age'], axis = 1 )
data['Salutation'] = data.Name.apply(lambda name: name.split(',')[1].split('.')[0].strip())
 print(data.Salutation.unique())
 data.Salutation.nunique()<feature_engineering>
train = train.drop(['Name'], axis = 1)
 test = test.drop(['Name'], axis = 1 )
dataset_title = [i.split(",")[1].split(".")[0].strip() for i in data["Name"]]
data['Title']=data.Name.apply(lambda x: x.split('.')[0].split(',')[1].strip())
 newtitles={
"Capt": "Officer",
"Col": "Officer",
"Major": "Officer",
"Jonkheer": "Royalty",
"Don": "Royalty",
"Sir" : "Royalty",
"Dr": "Officer",
"Rev": "Officer",
"the Countess":"Royalty",
"Dona": "Royalty",
"Mme": "Mrs",
"Mlle": "Miss",
"Ms": "Mrs",
"Mr" : "Mr",
"Mrs" : "Mrs",
"Miss" : "Miss",
"Master" : "Master",
"Lady" : "Royalty"}
data['Title']=data.Title.map(newtitles)
 data['Mother'] =(data['Title'] == 'Mrs')&(data['Parch'] > 0)
 data['Mother'] = data['Mother'].astype(int )<data_type_conversions>
sex_mapping = {"male": 0, "female": 1}
train['Sex'] = train['Sex'].map(sex_mapping)
 test['Sex'] = test['Sex'].map(sex_mapping)
 train.head()
grp = data.groupby(['Sex', 'Pclass', 'Title'])
 data.Age = grp.Age.apply(lambda x_: x_.fillna(x_.median()))
data.Age.fillna(data.Age.median, inplace=True)
 data['AgeRange'] = pd.cut(data['Age'].astype(int), 5 )<feature_engineering>
embarked_mapping = {"S": 1, "C": 2, "Q": 3}
train['Embarked'] = train['Embarked'].map(embarked_mapping)
 test['Embarked'] = test['Embarked'].map(embarked_mapping)
 train.head()
data['Ticket_Lett'] = data['Ticket'].apply(lambda x: str(x)[0])
 data['Ticket_Lett'] = data['Ticket_Lett'].apply(lambda x: str(x))
data['Ticket_Lett'] = np.where(( data['Ticket_Lett'] ).isin(['1', '2', '3', 'S', 'P', 'C', 'A']), data['Ticket_Lett'],
np.where(( data['Ticket_Lett'] ).isin(['W', '4', '7', '6', 'L', '5', '8']),
'Low_ticket', 'Other_ticket'))
data['Ticket_Len'] = data['Ticket'].apply(lambda x: len(x))<data_type_conversions>
predictors = train.drop(['Survived', 'PassengerId'], axis=1)
 target = train["Survived"]
x_train, x_val, y_train, y_val = train_test_split(predictors, target, test_size = 0.22, random_state = 0 )
data.Fare.fillna(data.Fare.mean() , inplace = True)
<correct_missing_values>
gaussian = GaussianNB()
gaussian.fit(x_train, y_train)
 y_pred = gaussian.predict(x_val)
 acc_gaussian = round(accuracy_score(y_pred, y_val)* 100, 2)
 print(acc_gaussian )
data.Embarked.fillna(data.Embarked.mode() [0], inplace = True )<feature_engineering>
logreg = LogisticRegression()
logreg.fit(x_train, y_train)
 y_pred = logreg.predict(x_val)
 acc_logreg = round(accuracy_score(y_pred, y_val)* 100, 2)
 print(acc_logreg )
data.Cabin.fillna('NA', inplace=True)
 data['Cabin'] = data['Cabin'].map(lambda s: s[0] )<categorify>
svc = SVC()
svc.fit(x_train, y_train)
 y_pred = svc.predict(x_val)
 acc_svc = round(accuracy_score(y_pred, y_val)* 100, 2)
 print(acc_svc )
data['Title'] = LabelEncoder().fit_transform(data['Title'])
 data = pd.concat([data,
pd.get_dummies(data.Cabin, prefix="Cabin"),
pd.get_dummies(data.AgeRange, prefix="AgeRange"),
pd.get_dummies(data.Embarked, prefix="Embarked", drop_first = True),
pd.get_dummies(data.Title, prefix="Title", drop_first = True),
pd.get_dummies(data.FareCategory, prefix="Fare", drop_first = True),
pd.get_dummies(data.Ticket_Lett, prefix="Ticket", drop_first = True),
pd.get_dummies(data.Pclass, prefix="Class", drop_first = True)], axis=1)
 data['IsAlone'] = LabelEncoder().fit_transform(data['IsAlone'])
 data['Sex'] = LabelEncoder().fit_transform(data['Sex'])
<drop_column>
linear_svc = LinearSVC()
linear_svc.fit(x_train, y_train)
 y_pred = linear_svc.predict(x_val)
 acc_linear_svc = round(accuracy_score(y_pred, y_val)* 100, 2)
 print(acc_linear_svc )
data.drop(['Pclass', 'Fare','Cabin', 'FareCategory','Name','Salutation', 'Ticket_Lett', 'Ticket','Embarked', 'AgeRange', 'SibSp', 'Parch', 'Age'], axis=1, inplace=True )<split>
perceptron = Perceptron()
perceptron.fit(x_train, y_train)
 y_pred = perceptron.predict(x_val)
 acc_perceptron = round(accuracy_score(y_pred, y_val)* 100, 2)
 print(acc_perceptron )
X_pred = data[data.Survived.isnull() ].drop(['Survived'], axis=1)
 train_data = data.dropna()
X = train_data.drop(['Survived'], axis=1)
 y = train_data['Survived']
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, stratify=y)
<compute_train_metric>
decisiontree = DecisionTreeClassifier()
decisiontree.fit(x_train, y_train)
 y_pred = decisiontree.predict(x_val)
 acc_decisiontree = round(accuracy_score(y_pred, y_val)* 100, 2)
 print(acc_decisiontree )
clf = GradientBoostingClassifier(learning_rate=0.1,
n_estimators=700,
max_depth=2)
 clf.fit(X_train, np.ravel(y_train))
print("RF Accuracy: " + repr(round(clf.score(X_test, y_test)* 100, 2)) + "%")
 result_rf = cross_val_score(clf, X_train, y_train, cv=5, scoring='accuracy')
 print('The cross validated score for Random forest is:', round(result_rf.mean() * 100, 2))
y_pred = cross_val_predict(clf, X_train, y_train, cv=5 )<choose_model_class>
randomforest = RandomForestClassifier()
randomforest.fit(x_train, y_train)
 y_pred = randomforest.predict(x_val)
 acc_randomforest = round(accuracy_score(y_pred, y_val)* 100, 2)
 print(acc_randomforest )
clf = RandomForestClassifier(criterion='entropy',
n_estimators=700,
min_samples_split=5,
min_samples_leaf=1,
max_features = "auto",
oob_score=True,
random_state=0,
n_jobs=-1)
 clf.fit(X_train, np.ravel(y_train))
print("RF Accuracy: " + repr(round(clf.score(X_test, y_test)* 100, 2)) + "%")
 result_rf = cross_val_score(clf, X_train, y_train, cv=5, scoring='accuracy')
 print('The cross validated score for Random forest is:', round(result_rf.mean() * 100, 2))
y_pred = cross_val_predict(clf, X_train, y_train, cv=5)
<save_to_csv>
knn = KNeighborsClassifier()
knn.fit(x_train, y_train)
 y_pred = knn.predict(x_val)
 acc_knn = round(accuracy_score(y_pred, y_val)* 100, 2)
 print(acc_knn )
result = clf.predict(X_pred)
 submission = pd.DataFrame({'PassengerId':X_pred.PassengerId,'Survived':result})
 submission.Survived = submission.Survived.astype(int)
 print(submission.shape)
 filename = 'TitanicPredictions4.csv'
submission.to_csv(filename,index=False)
 print('Saved file: ' + filename )<load_from_csv>
sgd = SGDClassifier()
sgd.fit(x_train, y_train)
 y_pred = sgd.predict(x_val)
 acc_sgd = round(accuracy_score(y_pred, y_val)* 100, 2)
 print(acc_sgd )
train = pd.read_csv("/kaggle/input/covid19-global-forecasting-week-1/train.csv")
 test = pd.read_csv("/kaggle/input/covid19-global-forecasting-week-1/test.csv")
 submission = pd.read_csv("/kaggle/input/covid19-global-forecasting-week-1/submission.csv")
 train.head() 
<define_variables>
gbk = GradientBoostingClassifier()
gbk.fit(x_train, y_train)
 y_pred = gbk.predict(x_val)
 acc_gbk = round(accuracy_score(y_pred, y_val)* 100, 2)
 print(acc_gbk )
print(f'Total reported cases are {len(train)}.')
 print(f'Total confirmed cases are {int(train["ConfirmedCases"].sum())}.')
 print(f'Total fatality cases are {int(train["Fatalities"].sum())}.')
 print(f'Total countries are {len(train["Country/Region"].unique())}.' )<data_type_conversions>
models = pd.DataFrame({
'Model': ['Support Vector Machines', 'KNN', 'Logistic Regression',
'Random Forest', 'Naive Bayes', 'Perceptron', 'Linear SVC',
'Decision Tree', 'Stochastic Gradient Descent', 'Gradient Boosting Classifier','XGBClassifier'],
'Score': [acc_svc, acc_knn, acc_logreg,
acc_randomforest, acc_gaussian, acc_perceptron,acc_linear_svc, acc_decisiontree,
acc_sgd, acc_gbk,acc_xgb]})
 models.sort_values(by='Score', ascending=False )
<count_missing_values><EOS> 
ids = test['PassengerId']
predictions = gbk.predict(test.drop('PassengerId', axis=1))
output = pd.DataFrame({ 'PassengerId' : ids, 'Survived': predictions })
 output.to_csv('./submission.csv', index=False )