Kaggle competition: Titanic - Machine Learning from Disaster
<SOS> metric: categorizationaccuracy Kaggle data source: titanic-machine-learning-from-disaster<prepare_x_and_y>
mytrainset = pd.read_csv('.. /input/titanic/train.csv')
 mytrainset.head()
FEATURES = ['Date']
X_submission = pd.DataFrame(columns=['ForecastId', 'ConfirmedCases', 'Fatalities'])
 for i in tqdm(X_train.Country_Region.unique()):
z_train = X_train[X_train['Country_Region'] == i]
z_test = X_test[X_test['Country_Region'] == i]
for k in z_train.Province_State.unique() :
p_train = z_train[z_train['Province_State'] == k]
p_test = z_test[z_test['Province_State'] == k]
X_train_final = p_train[FEATURES]
y1 = p_train['ConfirmedCases']
y2 = p_train['Fatalities']
model = xgb.XGBRegressor(n_estimators=2000)
 model.fit(X_train_final, y1)
 ConfirmedCasesPreds = model.predict(p_test[FEATURES])
 model.fit(X_train_final, y2)
 FatalitiesPreds = model.predict(p_test[FEATURES])
 p_test['ConfirmedCases'] = ConfirmedCasesPreds
p_test['Fatalities'] = FatalitiesPreds
X_submission = pd.concat([X_submission, p_test[['ForecastId', 'ConfirmedCases', 'Fatalities']]], axis=0)
<save_to_csv>
mytestset = pd.read_csv('.. /input/titanic/test.csv')
 mytestset.head()
X_submission.to_csv('submission.csv', index=False )<import_modules>
ages_mean_train = mytrainset['Age'].mean()
ages_mean_train = round(ages_mean_train)
 ages_mean_test = mytestset['Age'].mean()
ages_mean_test = round(ages_mean_test)
import pandas as pd
import numpy as np
from sklearn.preprocessing import LabelEncoder
from sklearn.model_selection import train_test_split
from sklearn.ensemble import ExtraTreesRegressor
from sklearn.metrics import mean_squared_log_error<load_from_csv>
mytrainset['Age'] = mytrainset['Age'].replace(np.nan, ages_mean_train)
 mytrainset['Age'] = mytrainset['Age'].replace(np.nan, ages_mean_train)
 mytrainset = mytrainset.drop(["Cabin"], axis=1)
 mytrainset = mytrainset.drop(["Name"], axis=1)
 mytrainset = mytrainset.drop(["Ticket"], axis=1)
 mytrainset['Embarked'].replace({'C':1, 'Q':2, 'S':3},inplace=True)
 mytrainset['Embarked'] = mytrainset['Embarked'].fillna(0)
 mytrainset['Sex'].replace({'male':0, 'female':1},inplace=True)
 mytestset['Age'] = mytestset['Age'].replace(np.nan, ages_mean_test)
 mytestset = mytestset.drop(["Cabin"], axis=1)
 mytestset = mytestset.drop(["Name"], axis=1)
 mytestset = mytestset.drop(["Ticket"], axis=1)
 mytestset['Embarked'].replace({'C':1, 'Q':2, 'S':3},inplace=True)
 mytestset['Sex'].replace({'male':0, 'female':1},inplace=True)
 mytestset['Fare'] = mytestset['Fare'].fillna(mytestset['Fare'].mean())
df=pd.read_csv('/kaggle/input/covid19-global-forecasting-week-4/train.csv', index_col='Id')
 dtest=pd.read_csv('/kaggle/input/covid19-global-forecasting-week-4/test.csv', index_col='ForecastId' )<prepare_x_and_y>
y = mytrainset['Survived']
features = ["Pclass", "Age", "Sex", "SibSp", "Parch", "Embarked"]
X = mytrainset[features]
X_train, X_test, Y_train, Y_test = train_test_split(X, y, test_size=0.3, random_state=1 )
y1=df['ConfirmedCases']
y2=df['Fatalities']
df.drop('ConfirmedCases', axis=1, inplace=True)
 df.drop('Fatalities', axis=1, inplace=True )<feature_engineering>
model= RandomForestClassifier(n_estimators=200,max_depth=5, random_state=1)
 model = model.fit(X_train, Y_train)
 predictions = model.predict(X_test )
df['check']=1
dtest['check']=2
combo=pd.concat([df,dtest])
 def date_split(date):
d=date.str.split('-', n=1, expand=True)
 return d[1]
combo['MM_DD']= date_split(combo['Date'])
 combo['Province_State']=combo['Province_State'].fillna(0 )<categorify>
accuracy_score(predictions, Y_test)
le=LabelEncoder()
combo['MM_DD']=le.fit_transform(combo['MM_DD'])
 combo=pd.get_dummies(combo)
 df1=combo[combo['check']==1]
dtest1=combo[combo['check']==2]<drop_column>
gnb = GaussianNB()
NB_model_predictions = gnb.fit(X_train, Y_train ).predict(X_test)
 accuracy_score(NB_model_predictions, Y_test)
df1.drop('check', axis=1, inplace=True)
 dtest1.drop('check', axis=1, inplace=True )<split>
decisiontreeModel = DecisionTreeClassifier()
decisiontreeModel = decisiontreeModel.fit(X_train, Y_train)
 decisiontreePredicition = decisiontreeModel.predict(X_test)
 accuracy_score(decisiontreePredicition, Y_test)
X_train1, X_valid1, y_train1, y_valid1 = train_test_split(df1, y1, train_size=0.8, test_size=0.2, random_state=0)
 X_train2, X_valid2, y_train2, y_valid2 = train_test_split(df1, y2, train_size=0.8, test_size=0.2, random_state=0 )<compute_train_metric>
knn = KNeighborsClassifier(n_neighbors=5, metric='euclidean')
 knn.fit(X_train, Y_train)
 knnPredictions = knn.predict(X_test)
 accuracy_score(Y_test, knnPredictions)
ef= ExtraTreesRegressor(n_estimators=15, random_state=3)
 p2=ef.fit(X_train1, y_train1 ).predict(X_valid1)
 rmsle2=np.sqrt(mean_squared_log_error(y_valid1 , p2))
print(rmsle2 )<compute_train_metric>
LRModel = LogisticRegression(max_iter = 200)
 LRModel.fit(X_train, Y_train)
 LRModel_Prediction = LRModel.predict(X_test)
 accuracy_score(Y_test, LRModel_Prediction)
ef2= ExtraTreesRegressor(n_estimators=29,criterion='friedman_mse', random_state=7)
 p3=ef2.fit(X_train2, y_train2 ).predict(X_valid2)
 rmsle3=np.sqrt(mean_squared_log_error(y_valid2 , p3))
print(rmsle3 )<predict_on_test>
cv = KFold(n_splits=10, random_state=1, shuffle=True)
 scores = cross_val_score(model, X, y, scoring='accuracy', cv=cv, n_jobs=-1)
 print("%0.2f accuracy" %(scores.mean()))
pre1=ef.fit(df1,y1 ).predict(dtest1)
 pre2=ef2.fit(df1,y2 ).predict(dtest1 )<save_to_csv>
scores = cross_val_score(gnb, X, y, scoring='accuracy', cv=cv, n_jobs=-1)
 scores
print("%0.2f accuracy" %(scores.mean()))
output=pd.DataFrame({'ForecastId': dtest.index, 'ConfirmedCases':pre1, 'Fatalities':pre2})
 output.to_csv('submission.csv', index=False )<import_modules>
scores = cross_val_score(decisiontreeModel, X, y, scoring='accuracy', cv=cv, n_jobs=-1)
 scores
print("%0.2f accuracy" %(scores.mean()))
import pandas as pd
from pathlib import Path
from pandas_profiling import ProfileReport
from sklearn.tree import DecisionTreeClassifier
from sklearn.preprocessing import LabelEncoder
import datetime
from sklearn.model_selection import GridSearchCV
from sklearn import preprocessing
from sklearn.model_selection import cross_val_score<load_from_csv>
scores = cross_val_score(knn, X, y, scoring='accuracy', cv=cv, n_jobs=-1)
 scores
print("%0.2f accuracy" %(scores.mean()))
dataset_path = Path('/kaggle/input/covid19-global-forecasting-week-4')
 train = pd.read_csv(dataset_path/'train.csv')
 test = pd.read_csv(dataset_path/'test.csv')
 submission = pd.read_csv(dataset_path/'submission.csv' )<categorify>
scores = cross_val_score(LRModel, X, y, scoring='accuracy', cv=cv, n_jobs=-1)
 scores
print("%0.2f accuracy" %(scores.mean()))
def fill_state(state,country):
if pd.isna(state): return country
return state<feature_engineering>
testing = mytestset[features]
testPredicitons = model.predict(testing )
<feature_engineering><EOS> 
testPredicitons = {'PassengerId':mytestset["PassengerId"], "Survived": testPredicitons}
pd.DataFrame(testPredicitons ).to_csv("predictions.csv", index = False )