Kaggle competition: 2019  ML competition with KISTI
<SOS> metric: accuracy Kaggle data source: 2019-ml-competition-with-kisti<data_type_conversions>
plt.style.use('seaborn')
 sns.set(font_scale=2.5)
 warnings.filterwarnings('ignore')
 %matplotlib inline
test["Date"] = test["Date"].apply(lambda x: x.replace("-",""))
test["Date"] = test["Date"].astype(int)
 test.isnull().sum() 
<prepare_x_and_y>
df_train = pd.read_csv('.. /input/train.csv')
 df_test = pd.read_csv('.. /input/test.csv' )
x = train[['Lat', 'Long', 'Date']]
y1 = train[['ConfirmedCases']]
y2 = train[['Fatalities']]
x_test = test[['Lat', 'Long', 'Date']]<choose_model_class>
for col in df_train.columns:
msg = 'column: {:>10}\t Percent of NaN value: {:.2f}%'.format(col, 100 *(df_train[col].isnull().sum() / df_train[col].shape[0]))
print(msg )
Tree_model = RandomForestClassifier(max_depth=200, random_state=0 )<predict_on_test>
for col in df_test.columns:
msg = 'column: {:>10}\t Percent of NaN value: {:.2f}%'.format(col, 100 *(df_test[col].isnull().sum() / df_test[col].shape[0]))
print(msg )
Tree_model.fit(x,y1)
 pred1 = Tree_model.predict(x_test)
 pred1 = pd.DataFrame(pred1)
 pred1.columns = ["ConfirmedCases_prediction"]<predict_on_test>
df_train['FamilySize'] = df_train['SibSp'] + df_train['Parch'] + 1
df_test['FamilySize'] = df_test['SibSp'] + df_test['Parch'] + 1
Tree_model.fit(x,y2)
 pred2 = Tree_model.predict(x_test)
 pred2 = pd.DataFrame(pred2)
 pred2.columns = ["Death_prediction"] 
<load_from_csv>
df_train['Initial']= df_train.Name.str.extract('([A-Za-z]+)\.')
 df_test['Initial']= df_test.Name.str.extract('([A-Za-z]+)\.' )
Sub = pd.read_csv(".. /input/covid19-global-forecasting-week-1/submission.csv")
 sub_new = Sub[["ForecastId"]]
sub_new<concatenate>
df_train['Initial'].replace(['Mlle','Mme','Ms','Dr','Major','Lady','Countess','Jonkheer','Col','Rev','Capt','Sir','Don', 'Dona'],
['Miss','Miss','Miss','Mr','Mr','Mrs','Mrs','Other','Other','Other','Mr','Mr','Mr', 'Mr'],inplace=True)
 df_test['Initial'].replace(['Mlle','Mme','Ms','Dr','Major','Lady','Countess','Jonkheer','Col','Rev','Capt','Sir','Don', 'Dona'],
['Miss','Miss','Miss','Mr','Mr','Mrs','Mrs','Other','Other','Other','Mr','Mr','Mr', 'Mr'],inplace=True )
submit = pd.concat([pred1,pred2,sub_new],axis=1)
 submit.head() 
<data_type_conversions>
df_train.loc[(df_train.Age.isnull())&(df_train.Initial=='Mr'),'Age'] = 33
df_train.loc[(df_train.Age.isnull())&(df_train.Initial=='Mrs'),'Age'] = 36
df_train.loc[(df_train.Age.isnull())&(df_train.Initial=='Master'),'Age'] = 5
df_train.loc[(df_train.Age.isnull())&(df_train.Initial=='Miss'),'Age'] = 22
df_train.loc[(df_train.Age.isnull())&(df_train.Initial=='Other'),'Age'] = 46
df_test.loc[(df_test.Age.isnull())&(df_test.Initial=='Mr'),'Age'] = 33
df_test.loc[(df_test.Age.isnull())&(df_test.Initial=='Mrs'),'Age'] = 36
df_test.loc[(df_test.Age.isnull())&(df_test.Initial=='Master'),'Age'] = 5
df_test.loc[(df_test.Age.isnull())&(df_test.Initial=='Miss'),'Age'] = 22
df_test.loc[(df_test.Age.isnull())&(df_test.Initial=='Other'),'Age'] = 46
submit.columns = ['ConfirmedCases', 'Fatalities', 'ForecastId']
submit = submit[['ForecastId','ConfirmedCases', 'Fatalities']]
submit["ConfirmedCases"] = submit["ConfirmedCases"].astype(int)
 submit["Fatalities"] = submit["Fatalities"].astype(int )<save_to_csv>
df_train.Cabin.fillna('0', inplace=True)
 df_train.loc[df_train.Cabin.str[0] == 'A', 'Cabin'] = 1
df_train.loc[df_train.Cabin.str[0] == 'B', 'Cabin'] = 2
df_train.loc[df_train.Cabin.str[0] == 'C', 'Cabin'] = 3
df_train.loc[df_train.Cabin.str[0] == 'D', 'Cabin'] = 4
df_train.loc[df_train.Cabin.str[0] == 'E', 'Cabin'] = 5
df_train.loc[df_train.Cabin.str[0] == 'F', 'Cabin'] = 6
df_train.loc[df_train.Cabin.str[0] == 'G', 'Cabin'] = 7
df_train.loc[df_train.Cabin.str[0] == 'T', 'Cabin'] = 8
df_test.Cabin.fillna('0', inplace=True)
 df_test.loc[df_test.Cabin.str[0] == 'A', 'Cabin'] = 1
df_test.loc[df_test.Cabin.str[0] == 'B', 'Cabin'] = 2
df_test.loc[df_test.Cabin.str[0] == 'C', 'Cabin'] = 3
df_test.loc[df_test.Cabin.str[0] == 'D', 'Cabin'] = 4
df_test.loc[df_test.Cabin.str[0] == 'E', 'Cabin'] = 5
df_test.loc[df_test.Cabin.str[0] == 'F', 'Cabin'] = 6
df_test.loc[df_test.Cabin.str[0] == 'G', 'Cabin'] = 7
df_test.loc[df_test.Cabin.str[0] == 'T', 'Cabin'] = 8
submit.to_csv('submission.csv', index=False )<load_from_csv>
df_train['Age_cat'] = 0
df_train.loc[df_train['Age'] < 10, 'Age_cat'] = 0
df_train.loc[(10 <= df_train['Age'])&(df_train['Age'] < 20), 'Age_cat'] = 1
df_train.loc[(20 <= df_train['Age'])&(df_train['Age'] < 30), 'Age_cat'] = 2
df_train.loc[(30 <= df_train['Age'])&(df_train['Age'] < 40), 'Age_cat'] = 3
df_train.loc[(40 <= df_train['Age'])&(df_train['Age'] < 50), 'Age_cat'] = 4
df_train.loc[(50 <= df_train['Age'])&(df_train['Age'] < 60), 'Age_cat'] = 5
df_train.loc[(60 <= df_train['Age'])&(df_train['Age'] < 70), 'Age_cat'] = 6
df_train.loc[70 <= df_train['Age'], 'Age_cat'] = 7
df_test['Age_cat'] = 0
df_test.loc[df_test['Age'] < 10, 'Age_cat'] = 0
df_test.loc[(10 <= df_test['Age'])&(df_test['Age'] < 20), 'Age_cat'] = 1
df_test.loc[(20 <= df_test['Age'])&(df_test['Age'] < 30), 'Age_cat'] = 2
df_test.loc[(30 <= df_test['Age'])&(df_test['Age'] < 40), 'Age_cat'] = 3
df_test.loc[(40 <= df_test['Age'])&(df_test['Age'] < 50), 'Age_cat'] = 4
df_test.loc[(50 <= df_test['Age'])&(df_test['Age'] < 60), 'Age_cat'] = 5
df_test.loc[(60 <= df_test['Age'])&(df_test['Age'] < 70), 'Age_cat'] = 6
df_test.loc[70 <= df_test['Age'], 'Age_cat'] = 7
train_data = pd.read_csv("/kaggle/input/titanic/train.csv")
 test_data = pd.read_csv("/kaggle/input/titanic/test.csv" )<count_missing_values>
df_train['Initial'] = df_train['Initial'].map({'Master': 0, 'Miss': 1, 'Mr': 2, 'Mrs': 3, 'Other': 4})
 df_test['Initial'] = df_test['Initial'].map({'Master': 0, 'Miss': 1, 'Mr': 2, 'Mrs': 3, 'Other': 4} )
Col_with_missing = [col for col in train_data.columns if train_data[col].isnull().any() ]
print(Col_with_missing )<count_missing_values>
df_train['Embarked'] = df_train['Embarked'].map({'C': 0, 'Q': 1, 'S': 2})
 df_test['Embarked'] = df_test['Embarked'].map({'C': 0, 'Q': 1, 'S': 2} )
Col_with_missing = [col for col in test_data.columns if test_data[col].isnull().any() ]
print(Col_with_missing )<define_variables>
df_train['Sex'] = df_train['Sex'].map({'female': 0, 'male': 1})
 df_test['Sex'] = df_test['Sex'].map({'female': 0, 'male': 1} )
s =(train_data.dtypes == 'object')
 object_cols = list(s[s].index)
 print("Categorical variables:")
 print(object_cols )<prepare_x_and_y>
df_train = pd.get_dummies(df_train, columns=['Initial'], prefix='Initial')
 df_test = pd.get_dummies(df_test, columns=['Initial'], prefix='Initial' )
feature_name=['Pclass','Sex','Age','SibSp','Parch','Fare','Embarked']
X=train_data[feature_name]
y=train_data["Survived"]
X_test=test_data[feature_name]<split>
df_train = pd.get_dummies(df_train, columns=['Embarked'], prefix='Embarked')
 df_test = pd.get_dummies(df_test, columns=['Embarked'], prefix='Embarked' )
X_train, X_valid, y_train, y_valid = train_test_split(X, y, test_size=0.2 )<rename_columns>
df_train.drop(['PassengerId', 'Name', 'SibSp', 'Parch', 'Ticket','Age'], axis=1, inplace=True)
 df_test.drop(['PassengerId', 'Name', 'SibSp', 'Parch', 'Ticket','Age'], axis=1, inplace=True )
my_imputer=SimpleImputer(strategy="most_frequent")
 imputed_X_train= pd.DataFrame(my_imputer.fit_transform(X_train))
imputed_X_test=pd.DataFrame(my_imputer.transform(X_test))
imputed_X_valid=pd.DataFrame(my_imputer.transform(X_valid))
imputed_X_train.index = X_train.index
imputed_X_valid.index = X_valid.index
imputed_X_test.index = X_test.index
imputed_X_train.columns=X_train.columns
imputed_X_valid.columns=X_valid.columns
imputed_X_test.columns=X_test.columns<count_missing_values>
X_train = df_train.drop('Survived', axis=1 ).values
target_label = df_train['Survived'].values
X_test = df_test.values
Col_with_missing_2 = [col for col in imputed_X_test.columns if imputed_X_test[col].isnull().any() ]
print(Col_with_missing_2 )<define_variables>
X_tr, X_vld, y_tr, y_vld = train_test_split(X_train, target_label, test_size=0.3, random_state=2018 )
New_feature_train = imputed_X_train['Sex'] + "_" + imputed_X_train['Embarked']
New_feature_valid = imputed_X_valid['Sex'] + "_" + imputed_X_valid['Embarked']
New_feature_test = imputed_X_test['Sex'] + "_" + imputed_X_test['Embarked']<prepare_output>
gaussian = GaussianNB()
gaussian.fit(X_tr, y_tr)
 prediction = gaussian.predict(X_vld)
 acc_gaussian = round(accuracy_score(prediction,y_vld)* 100, 2)
 print(acc_gaussian )
imputed_X_train["Sex_Embarked"]=New_feature_train
imputed_X_valid["Sex_Embarked"]=New_feature_valid
imputed_X_test["Sex_Embarked"]=New_feature_test<define_variables>
logreg = LogisticRegression()
logreg.fit(X_tr, y_tr)
 prediction2 = logreg.predict(X_vld)
 acc_logreg = round(accuracy_score(prediction2,y_vld)* 100, 2)
 print(acc_logreg )
Cat_cols=['Sex','Embarked','Sex_Embarked']<categorify>
svc = SVC()
svc.fit(X_tr, y_tr)
 prediction3 = svc.predict(X_vld)
 acc_svc = round(accuracy_score(prediction3,y_vld)* 100, 2 )
OH_encoder = OneHotEncoder(handle_unknown='ignore', sparse=False)
 OH_cols_train = pd.DataFrame(OH_encoder.fit_transform(imputed_X_train[Cat_cols]))
OH_cols_valid = pd.DataFrame(OH_encoder.transform(imputed_X_valid[Cat_cols]))
OH_cols_test = pd.DataFrame(OH_encoder.transform(imputed_X_test[Cat_cols]))
OH_cols_train.index = imputed_X_train.index
OH_cols_valid.index = imputed_X_valid.index
OH_cols_test.index = imputed_X_test.index
num_X_train = imputed_X_train.drop(Cat_cols, axis =1)
 num_X_valid = imputed_X_valid.drop(Cat_cols, axis =1)
 num_X_test = imputed_X_test.drop(Cat_cols, axis =1)
 OH_X_train = pd.concat([num_X_train, OH_cols_train], axis=1)
 OH_X_valid = pd.concat([num_X_valid, OH_cols_valid], axis=1)
 OH_X_test = pd.concat([num_X_test, OH_cols_test], axis=1 )<data_type_conversions>
linear_svc = LinearSVC()
linear_svc.fit(X_tr, y_tr)
 prediction4 = svc.predict(X_vld)
 acc_linear_svc = round(accuracy_score(prediction4,y_vld)* 100, 2 )
OH_X_train = OH_X_train.apply(pd.to_numeric)
 OH_X_valid = OH_X_valid.apply(pd.to_numeric)
 OH_X_test = OH_X_test.apply(pd.to_numeric)
 OH_X_train=OH_X_train.rename(columns={0:"Sex1", 1:"Sex2"})
 OH_X_train=OH_X_train.rename(columns={2:"C", 3:"Q",4:"S"})
 OH_X_valid=OH_X_valid.rename(columns={0:"Sex1", 1:"Sex2"})
 OH_X_valid=OH_X_valid.rename(columns={2:"C", 3:"Q",4:"S"})
 OH_X_test=OH_X_test.rename(columns={0:"Sex1", 1:"Sex2"})
 OH_X_test=OH_X_test.rename(columns={2:"C", 3:"Q",4:"S"} )<train_model>
perceptron = Perceptron()
perceptron.fit(X_tr, y_tr)
 prediction5 = perceptron.predict(X_vld)
 acc_perceptron = round(accuracy_score(prediction5,y_vld)* 100, 2 )
my_model = XGBClassifier(n_estimators=1000, learning_rate=0.001)
 my_model.fit(OH_X_train, y_train, early_stopping_rounds=50,
eval_set=[(OH_X_valid, y_valid)], verbose=False)
 my_model.fit(OH_X_train, y_train)
 y_pred5 = my_model.predict(OH_X_valid)
 print("Accuracy:",metrics.accuracy_score(y_valid, y_pred5))<save_to_csv>
decisiontree = DecisionTreeClassifier(criterion='gini', max_depth=5)
 decisiontree.fit(X_tr, y_tr)
 prediction6 = decisiontree.predict(X_vld)
 acc_decisiontree = round(accuracy_score(prediction6,y_vld)* 100, 2)
predictions2 = my_model.predict(OH_X_test)
 output = pd.DataFrame({'PassengerId': test_data.PassengerId, 'Survived': predictions2})
 output.to_csv('my_submission_02_06.csv', index=False)
 print("Your submission was successfully saved!" )<load_from_csv>
rf = RandomForestClassifier()
rf.fit(X_tr, y_tr)
 prediction7 = rf.predict(X_vld)
 acc_rf = round(accuracy_score(prediction7,y_vld)* 100, 2 )
data= pd.read_csv("/kaggle/input/covid19-global-forecasting-week-2/train.csv")
 test = pd.read_csv("/kaggle/input/covid19-global-forecasting-week-2/test.csv" )<feature_engineering>
knn = KNeighborsClassifier()
knn.fit(X_tr, y_tr)
 prediction8 = knn.predict(X_vld)
 acc_knn = round(accuracy_score(prediction8,y_vld)* 100, 2 )
data['Province_State']=data['Province_State'].fillna('')
 test['Province_State']=test['Province_State'].fillna('' )<import_modules>
sgd = SGDClassifier()
sgd.fit(X_tr, y_tr)
 prediction9 = knn.predict(X_vld)
 acc_sgd = round(accuracy_score(prediction9,y_vld)* 100, 2)
import matplotlib.pyplot as plt<data_type_conversions>
gbk = GradientBoostingClassifier()
gbk.fit(X_tr,y_tr)
 prediction10 = gbk.predict(X_vld)
 acc_gbk = round(accuracy_score(prediction10,y_vld)* 100, 2 )
datetime_str = '01/22/20 00:00:00'
datetime_object = datetime.strptime(datetime_str, '%m/%d/%y %H:%M:%S')
 data['days']=pd.to_datetime(data['Date'] ).sub(datetime_object)/np.timedelta64(1, 'D')
 test['days']=pd.to_datetime(test['Date'] ).sub(datetime_object)/np.timedelta64(1, 'D' )<sort_values>
xg_boost = xgb.XGBClassifier(base_score=0.5, booster='gbtree', colsample_bylevel=1,
colsample_bytree=0.65, gamma=2, learning_rate=0.3, max_delta_step=1,
max_depth=4, min_child_weight=2, missing=None, n_estimators=280,
n_jobs=1, nthread=None, objective='binary:logistic', random_state=0,
reg_alpha=0, reg_lambda=1, scale_pos_weight=1, seed=None,
silent=True, subsample=1)
 xg_boost.fit(X_tr, y_tr)
 prediction11 = gbk.predict(X_vld)
 acc_xg_boost = round(accuracy_score(prediction11,y_vld)* 100, 2 )
data.loc[(data['Province_State']=='')&(data['Country_Region']=='India'),:].sort_values(by="Date" )<import_modules>
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', 'XGboost'],
'Score': [acc_svc, acc_knn, acc_logreg,
acc_rf, acc_gaussian, acc_perceptron,acc_linear_svc, acc_decisiontree,
acc_sgd, acc_gbk, acc_xg_boost]})
 models.sort_values(by='Score', ascending=False )
from statsmodels.tsa.arima_model import ARIMA 
<data_type_conversions>
submission = pd.read_csv('.. /input/sample_submission.csv' )
data['Date']=pd.to_datetime(data['Date'])
 test['Date']=pd.to_datetime(test['Date'] )<create_dataframe>
prediction = linear_svc.predict(X_test)
 submission['Survived'] = prediction
pd.DataFrame(data.loc[data['Country_Region']=='Afghanistan',['ConfirmedCases']] ).reset_index(drop=True )<count_missing_values>
submission.to_csv('./submission7.csv', index=False )