Kaggle competition: COVID19 Global Forecasting (Week 1)
<SOS> metric: MCRMSLE Kaggle data source: covid19-global-forecasting-(week-1)<choose_model_class>
dt=DecisionTreeClassifier(random_state=42 )<import_modules>
df = 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' )
from sklearn.model_selection import GridSearchCV<import_modules>
ids = submission['ForecastId']
from sklearn.model_selection import GridSearchCV<define_search_space>
input_cols = ["Lat","Long","Date"]
output_cols = ["ConfirmedCases","Fatalities"]
params = {
'max_depth': [3,5,8,12,15],
'min_samples_leaf': [5,8,12,15,20],
'criterion': ["gini", "entropy"]
}<choose_model_class>
for i in range(df.shape[0]):
df["Date"][i] = df["Date"][i][:4] + df["Date"][i][5:7] + df["Date"][i][8:]
df["Date"][i] = int(df["Date"][i] )
grid_search = GridSearchCV(estimator=dt,
param_grid=params,
cv=4, n_jobs=-1, verbose=1, scoring = "accuracy" )<train_on_grid>
for i in range(test.shape[0]):
test["Date"][i] = test["Date"][i][:4] + test["Date"][i][5:7] + test["Date"][i][8:]
test["Date"][i] = int(test["Date"][i] )
grid_search.fit(X_train, y_train )<find_best_params>
X = df[input_cols]
Y1 = df[output_cols[0]]
Y2 = df[output_cols[1]]
grid_search.cv_results_<prepare_output>
x_test = test[input_cols]
score_df = pd.DataFrame(grid_search.cv_results_)
 score_df.head()<count_duplicates>
score_df.nlargest(5,"mean_test_score" )<find_best_params>
pca = PCA(n_components=3)
 pca.fit(X)
 X_pca_scaled = pca.transform(X)
 x_test_pca_scaled = pca.transform(x_test )
grid_search.best_estimator_<find_best_params>
X_trainval, X_test, y1_trainval, y1_test, y2_trainval, y2_test = train_test_split(X_pca_scaled, Y1, Y2, random_state = 0)
 X_train, X_valid, y1_train, y1_valid, y2_train, y2_valid = train_test_split(X_trainval, y1_trainval, y2_trainval, random_state = 1)
 print("훈련 세트의 크기: {}, 검증 세트의 크기: {}, 테스트 세트의 크기: {}".format(X_train.shape[0], X_valid.shape[0], X_test.shape[0]))
dt_best=grid_search.best_estimator_<import_modules>
kfold = KFold(n_splits=5, shuffle = True, random_state = 0)
 best_score = 0
for max_features in [0.5, 0.6, 0.7, 0.8, 0.9, 1]:
rf = RandomForestClassifier(n_estimators = 200, criterion = 'entropy', max_features = max_features)
 scores = cross_val_score(rf, X_trainval, y1_trainval, cv=kfold)
 score = np.mean(scores)
 if score > best_score:
best_score = score
best_parameters = {'max_features': max_features}
rf = RandomForestClassifier(**best_parameters, criterion = 'entropy')
 rf.fit(X_trainval, y1_trainval)
 test_score = rf.score(X_test, y1_test)
 print("검증 세트에서 최고 점수: {:.2f}".format(best_score))
print("최적 매개변수: ", best_parameters)
 print("최적 매개변수에서 테스트 세트 점수: {:.2f}".format(test_score))
from sklearn.metrics import confusion_matrix, accuracy_score<compute_test_metric>
rf = RandomForestClassifier(**best_parameters, criterion = 'entropy')
 rf.fit(X_trainval, y1_trainval )
accuracy_score(y_test,dt_best.predict(X_test))<predict_on_test>
print("훈련 세트 정확도: {:.3f}".format(rf.score(X_trainval, y1_trainval)))
 print("테스트 세트 정확도: {:.3f}".format(rf.score(X_test, y1_test)) )
predictions=dt_best.predict(df_test )<save_to_csv>
pred1 = rf.predict(x_test_pca_scaled )
titanic_3=pd.DataFrame({'PassengerId':PassengerId,'Survived':predictions})
 titanic_3.to_csv('My_3rd_submission',index=False )<load_from_csv>
kfold = KFold(n_splits=5, shuffle = True, random_state = 0)
 best_score = 0
for max_features in [0.5, 0.6, 0.7, 0.8, 0.9, 1]:
rf = RandomForestClassifier(n_estimators = 200, criterion = 'entropy', max_features = max_features)
 scores = cross_val_score(rf, X_trainval, y2_trainval, cv=kfold)
 score = np.mean(scores)
 if score > best_score:
best_score = score
best_parameters = {'max_features': max_features}
rf = RandomForestClassifier(**best_parameters, criterion = 'entropy')
 rf.fit(X_trainval, y2_trainval)
 test_score = rf.score(X_test, y2_test)
 print("검증 세트에서 최고 점수: {:.2f}".format(best_score))
print("최적 매개변수: ", best_parameters)
 print("최적 매개변수에서 테스트 세트 점수: {:.2f}".format(test_score))
pd.read_csv('My_3rd_submission' )<import_modules>
rf = RandomForestClassifier(**best_parameters, criterion = 'entropy')
 rf.fit(X_trainval, y2_trainval )
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import GridSearchCV
from sklearn.ensemble import RandomForestClassifier
from sklearn.svm import SVC
import keras<load_from_csv>
print("훈련 세트 정확도: {:.3f}".format(rf.score(X_trainval, y2_trainval)))
 print("테스트 세트 정확도: {:.3f}".format(rf.score(X_test, y2_test)) )
train = pd.read_csv("/kaggle/input/titanic/train.csv")
 train.head()<load_from_csv>
pred2 = rf.predict(x_test_pca_scaled )
<create_dataframe><EOS> 
output = pd.DataFrame({ 'ForecastId' : ids, 'ConfirmedCases': pred1,'Fatalities':pred2 })
 output
output.to_csv('submission.csv', index=False )