Kaggle competition: COVID19 Global Forecasting (Week 4)
<SOS> metric: MCRMSLE Kaggle data source: covid19-global-forecasting-(week-4)<save_to_csv>
import numpy as np
import pandas as pd
import seaborn as sns
from sklearn.model_selection import train_test_split
from xgboost import XGBRegressor
from sklearn.multioutput import MultiOutputRegressor
from sklearn.impute import SimpleImputer
sub_df.to_csv("Customer_Transaction.csv", index=False )<define_variables>
train_data = pd.read_csv('/kaggle/input/covid19-global-forecasting-week-4/train.csv')
 test_data = pd.read_csv('/kaggle/input/covid19-global-forecasting-week-4/test.csv')
 submission_csv = pd.read_csv('/kaggle/input/covid19-global-forecasting-week-4/submission.csv' )
root = Path(".. /input" )<load_from_csv>
train_data['Date'] = pd.to_datetime(train_data['Date'], infer_datetime_format=True)
 test_data['Date'] = pd.to_datetime(test_data['Date'], infer_datetime_format=True )
train = pd.read_csv(root.joinpath("train.csv"))
test = pd.read_csv(root.joinpath("test.csv"))<drop_column>
train_data.loc[:, 'Date'] = train_data.Date.dt.strftime('%y%m%d')
 train_data.loc[:, 'Date'] = train_data['Date'].astype(int)
 test_data.loc[:, 'Date'] = test_data.Date.dt.strftime('%y%m%d')
 test_data.loc[:, 'Date'] = test_data['Date'].astype(int )
train_df = train.copy()
test_df = test.copy()
train_df.drop(columns=["ID_code", "target"], inplace=True)
 test_df.drop(columns=["ID_code"], inplace=True)
 target = train.target<randomize_order>
train_data['Province_State'] = np.where(train_data['Province_State'] == 'nan',train_data['Country_Region'],train_data['Province_State'])
 test_data['Province_State'] = np.where(test_data['Province_State'] == 'nan',test_data['Country_Region'],test_data['Province_State'] )
def augment_train(df_train, y_train):
t0 = df_train[y_train == 0].copy()
t1 = df_train[y_train == 1].copy()
i = 0
N = 3
for I in range(2):
for col in df_train.columns:
i = i + 1000
np.random.seed(i)
 np.random.shuffle(t0[col].values)
 np.random.shuffle(t1[col].values)
 df_train = pd.concat([df_train, t0.copy() ])
 df_train = pd.concat([df_train, t1.copy() ])
 y_train = pd.concat([y_train, pd.Series([0] * t0.shape[0]), pd.Series([1] * t1.shape[0])])
 return df_train, y_train<choose_model_class>
convert_dict = {'Province_State': str}
train_data = train_data.astype(convert_dict)
 test_data = test_data.astype(convert_dict )
model = CatBoostClassifier(subsample=0.36,
custom_loss='Logloss',
random_strength = 0,
max_depth=3,
eval_metric="AUC",
learning_rate=0.02,
iterations=60000,
bootstrap_type='Bernoulli',
l2_leaf_reg=0.3,
task_type="GPU",
random_seed=432013,
od_type="Iter",
border_count=128
)<split>
s =(train_data.dtypes == 'object')
 object_cols = list(s[s].index )
def run_cat(model, trt, tst, tar,n_splits=5, plot=False):
kf = KFold(n_splits=n_splits, random_state=432013, shuffle=True)
 oof = np.zeros(len(trt))
feature_importance_df = pd.DataFrame()
y_valid_pred = 0 * tar
y_test_pred = 0
for n_fold,(train_index, valid_index)in enumerate(kf.split(trt, tar)) :
y_train, y_valid = tar.iloc[train_index], tar.iloc[valid_index]
X_train, X_valid = trt.iloc[train_index,:], trt.iloc[valid_index,:]
X_train, y_train = augment_train(X_train, y_train)
 _train = Pool(X_train, label=y_train)
 _valid = Pool(X_valid, label=y_valid)
 print("Fold ", n_fold)
 fit_model = model.fit(_train,
verbose_eval=1000,
early_stopping_rounds=1000,
eval_set=[_valid],
use_best_model=True,
plot=False, 
)
 pred = fit_model.predict_proba(X_valid)[:,1]
oof[valid_index] = pred
print("auc = ", roc_auc_score(y_valid, pred))
y_valid_pred.iloc[valid_index] = pred
y_test_pred += fit_model.predict_proba(tst)[:,1]
y_test_pred /= n_splits
print("average auc:", roc_auc_score(tar, oof))
return y_test_pred, oof<concatenate>
from sklearn.preprocessing import LabelEncoder
y_test_pred, oof = run_cat(model,train_df, test_df, target )<save_to_csv>
label_encoder1 = LabelEncoder()
label_encoder2 = LabelEncoder()
train_data['Province_State'] = label_encoder1.fit_transform(train_data['Province_State'])
 test_data['Province_State'] = label_encoder1.transform(test_data['Province_State'])
 train_data['Country_Region'] = label_encoder2.fit_transform(train_data['Country_Region'])
 test_data['Country_Region'] = label_encoder2.transform(test_data['Country_Region'])
submission = pd.read_csv(root.joinpath("sample_submission.csv"))
submission['target'] = y_test_pred
pd.Series(oof ).to_csv("Cat_oof.csv", index = False)
 submission.to_csv('submission_cb_light_0.8999.csv', index=False )<import_modules>
Test_id = test_data.ForecastId
warnings.filterwarnings('ignore')
 for dirname, _, filenames in os.walk('/kaggle/input'):
for filename in filenames:
print(os.path.join(dirname, filename))<load_from_csv>
train_data.drop(['Id'], axis=1, inplace=True)
 test_data.drop('ForecastId', axis=1, inplace=True )
train = pd.read_csv('.. /input/house-prices-advanced-regression-techniques/train.csv')
 train.shape<load_from_csv>
missing_val_count_by_column =(train_data.isnull().sum())
 print(missing_val_count_by_column[missing_val_count_by_column>0] )
test = pd.read_csv('.. /input/house-prices-advanced-regression-techniques/test.csv')
 test.shape<load_from_csv>
from xgboost import XGBRegressor
submission = pd.read_csv('.. /input/house-prices-advanced-regression-techniques/sample_submission.csv')
 submission.shape<drop_column>
X_train = train_data[['Province_State','Country_Region','Date']]
y_train = train_data[['ConfirmedCases', 'Fatalities']]
train_ID = train['Id']
test_ID = test['Id']
train.drop(['Id'], axis=1, inplace=True)
 test.drop(['Id'], axis=1, inplace=True )<drop_column>
y_train_confirm = y_train.ConfirmedCases
y_train_fatality = y_train.Fatalities
train = train[train.GrLivArea < 4500]
train.reset_index(drop=True, inplace=True )<prepare_x_and_y>
x_train = X_train.iloc[:,:].values
x_test = X_train.iloc[:,:].values
train["SalePrice"] = np.log1p(train["SalePrice"])
 y = train.SalePrice.reset_index(drop=True)
 train_features = train.drop(['SalePrice'], axis=1)
 test_features = test<data_type_conversions>
model1 = XGBRegressor(n_estimators=40000)
 model1.fit(X_train, y_train_confirm)
 y_pred_confirm = model1.predict(test_data )
features = pd.concat([train_features, test_features] ).reset_index(drop=True)
 print(features.shape)
 features['MSSubClass'] = features['MSSubClass'].apply(str)
 features['YrSold'] = features['YrSold'].astype(str)
 features['MoSold'] = features['MoSold'].astype(str )<feature_engineering>
model2 = XGBRegressor(n_estimators=20000)
 model2.fit(X_train,y_train_fatality)
 y_pred_fat = model2.predict(test_data )
features['Functional'] = features['Functional'].fillna('Typ')
 features['Electrical'] = features['Electrical'].fillna("SBrkr")
 features['KitchenQual'] = features['KitchenQual'].fillna("TA")
 features['Exterior1st'] = features['Exterior1st'].fillna(features['Exterior1st'].mode() [0])
 features['Exterior2nd'] = features['Exterior2nd'].fillna(features['Exterior2nd'].mode() [0])
 features['SaleType'] = features['SaleType'].fillna(features['SaleType'].mode() [0])
 features["PoolQC"] = features["PoolQC"].fillna("None" )<data_type_conversions>
df_sub = pd.DataFrame()
df_sub['ForecastId'] = Test_id
df_sub['ConfirmedCases'] = y_pred_confirm
df_sub['Fatalities'] = y_pred_fat
df_sub.to_csv('submission.csv', index=False )