Kaggle competition: IEEE-CIS Fraud Detection
train_data['name_title'] = train_data.Name.apply(lambda x: x.split(',', 1)[1].split('.', 1)[0])
 train_data['name_title']<count_values>
train_identity = pd.read_csv('/kaggle/input/ieee-fraud-detection/train_identity.csv', index_col = 'TransactionID')
 print('Successfully loaded train_identity')
 train_transaction = pd.read_csv('/kaggle/input/ieee-fraud-detection/train_transaction.csv', index_col = 'TransactionID')
 print('Successfully loaded train_transaction')
 test_identity = pd.read_csv('/kaggle/input/ieee-fraud-detection/test_identity.csv', index_col = 'TransactionID')
 print('Successfully loaded test_identity')
 test_transaction = pd.read_csv('/kaggle/input/ieee-fraud-detection/test_transaction.csv', index_col = 'TransactionID')
 print('Successfully loaded test_transaction')
 sub = pd.read_csv('/kaggle/input/ieee-fraud-detection/sample_submission.csv')
 print('Successfully loaded sample_submisssion')
 print('Data was successfully loades!')
 print('Merging data.... ')
 train = train_transaction.merge(train_identity, how = 'left', left_index = True, right_index = True)
 test = test_transaction.merge(test_identity, how = 'left', left_index = True, right_index = True)
 print('Data was successfully merged!')
 del train_identity, train_transaction, test_identity, test_transaction
print('Train dataset has {} rows and {} columns'.format(train.shape[0], train.shape[1]))
print('Test dataset has {} rows and {} columns'.format(test.shape[0], test.shape[1]))
train_data['name_title'].value_counts()<feature_engineering>
def missing_values(df):
df1 = pd.DataFrame(df.isnull().sum() ).reset_index()
df1.columns = ['features', 'freq']
df1['percentage'] = df1['freq']/df.shape[0]
df1.sort_values('percentage', ascending = False, inplace = True)
 return df1
missing_train = missing_values(train)
 missing_train.columns = ['features', 'freq_tr', 'percentage_tr']
missing_train
def feature_engineer_df(df):

 print("adding column 'cabin_count'...")
 df['cabin_count'] = df.Cabin.apply(lambda x: 0 if pd.isna(x)else len(x.split()))
print("adding column 'cabin_adv'...")
 df['cabin_adv'] = df.Cabin.apply(lambda x: str(x)[0])
 print("adding column 'numeric_ticket'...")
 df['numeric_ticket'] = df.Ticket.apply(lambda x: 1 if x.isnumeric() else 0)
 print("adding column 'name_title'...")
 df['name_title'] = df.Name.apply(lambda x: x.split(',', 1)[1].split('.', 1)[0])
 print('imputing null values...')
 print("adding column 'age missing'...")
 df['age_missing'] = df.Age.apply(lambda x: 1 if pd.isnull(x)else 0)
 df.Age = df.Age.fillna(df.Age.mean())
 print("adding column 'fare_missing'...")
 df['fare_missing'] = df.Fare.apply(lambda x: 1 if pd.isnull(x)else 0)
 df.Fare = df.Fare.fillna(df.Fare.median())
 print("dropping rows with missing 'Embarked'...")
 print(f"{np.count_nonzero(df['Embarked'].isnull().values)} rows with missing 'Embarked' will be dropped")
 df.Embarked = df.dropna(subset=['Embarked'])
 print("normalizing 'sibsp'...")
 df['norm_sibsp'] = np.log(df.SibSp+1)
 print("normalizing 'fare'...")
 df['norm_fare'] = np.log(df.Fare+1 )<categorify>
missing_test = missing_values(test)
 missing_test.columns = ['features', 'freq_te', 'percentage_te']
missing_test
def solve_mismatch(df_train, df_test):
df_train['train_data'] = 1
df_test['train_data'] = 0
df_test['Survived'] = 0
combined_df = pd.concat([df_train, df_test])
 combined_df.Pclass = combined_df.Pclass.astype(str)
 combined_dummy = pd.get_dummies(combined_df[['PassengerId', 'Survived', 'Pclass', 'Sex', 'Age', 'SibSp', 'Parch', 'norm_fare', 'Embarked', 'cabin_adv',
'cabin_count', 'numeric_ticket', 'name_title', 'age_missing', 'fare_missing', 'train_data']])
 new_train = combined_dummy[combined_dummy['train_data']==1]
new_test = combined_dummy[combined_dummy['train_data']==0]
new_train.drop(['train_data'], axis=1, inplace=True)
 new_test.drop(['train_data', 'Survived'], axis=1, inplace=True)
 return(new_train, new_test )<split>
missing = missing_train.merge(missing_test, on = 'features')
 missing.head(10 )
def ft_splitted(df, drop=['Survived', 'PassengerId']):
X = df.drop(drop, axis=1)
 y = df[drop[0]]
return([X, y] )<feature_engineering>
train['id_24'].value_counts(normalize = True, dropna = False )
feature_engineer_df(train_data )<count_values>
test['id_24'].value_counts(normalize = True, dropna = False )
train_data['age_missing'].value_counts()<compute_test_metric>
drop_features = []
drop_features.append('id_24' )
feature_engineer_df(test_data )<count_values>
for i in ['id_24', 'id_25', 'id_08', 'id_07', 'id_21', 'id_26', 'id_27', 'id_23', 'id_22']:
drop_features.append(i)
 drop_features
test_data['age_missing'].value_counts()<compute_test_metric>
train['dist2'].value_counts(normalize = True, dropna = False ).head()
train_data, test_data = solve_mismatch(train_data, test_data )<split>
test['dist2'].value_counts(normalize = True, dropna = False ).head()
X_train, y_train = ft_splitted(train_data )<normalization>
check_features = ['dist2']
def scale_data(X):
scale = StandardScaler()
X_scaled = X.copy()
X_scaled[['Age', 'SibSp', 'Parch', 'norm_fare']] = scale.fit_transform(X_scaled[['Age', 'SibSp', 'Parch', 'norm_fare']])
 return(X_scaled )<normalization>
missing[missing['features']=='D7']
X_train_scaled = scale_data(X_train)
 X_train_scaled.head()<normalization>
check_features.append('D7' )
X_test_scaled = scale_data(test_data.drop(['PassengerId'], axis=1))
X_test_scaled.head()<compute_train_metric>
missing[missing['features']=='id_18']
def get_model_accuracy(model, cv=5):
cv_score = cross_val_score(model, X_train_scaled, y_train, cv=cv)
 print(cv_score)
 print(f"{model.__class__.__name__}({format(cv_score.mean() *100, '.2f')}%)")
 return(cv_score.mean() )<choose_model_class>
drop_features.append('id_18' )
GNB = GaussianNB()
get_model_accuracy(GNB )<choose_model_class>
missing[missing['features']=='D13']
LR = LogisticRegression()
get_model_accuracy(LR )<find_best_params>
missing[missing['features']=='D14']
SVC = svm.SVC(probability=True)
 get_model_accuracy(SVC )<choose_model_class>
missing[missing['features']=='D12']
KNN = KNeighborsClassifier()
get_model_accuracy(KNN )<choose_model_class>
missing[missing['features']=='id_04']
RFC = RandomForestClassifier(random_state=42)
 get_model_accuracy(RFC )<choose_model_class>
missing[missing['features']=='id_03']
GB = GradientBoostingClassifier(random_state=42)
 get_model_accuracy(GB )<choose_model_class>
missing[missing['features']=='D6']
voting_clf_all = VotingClassifier(
 estimators=[('GNB', GNB),('LR', LR),('SVC', SVC),('KNN', KNN),('RFC', RFC),('GB', GB)],
voting='soft')
 get_model_accuracy(voting_clf_all )<choose_model_class>
missing[missing['features']=='id_33']
voting_clf_best_3 = VotingClassifier(
 estimators=[('SVC', SVC),('RFC', RFC),('KNN', KNN)],
voting='soft')
 get_model_accuracy(voting_clf_best_3 )<find_best_params>
missing[missing['features']=='id_09']
def clf_performance(classifier):
print(classifier.__class__.__name__)
 print(f"Best Score: {classifier.best_score_}")
 print(f"Best Parameters: {classifier.best_params_}" )<train_on_grid>
missing[missing['features']=='id_10']
param_grid = {
'random_state': [42],
'max_iter': [100, 500, 2000],
'penalty': ['l1', 'l2'],
'C': np.logspace(-4, 4, 20),
'solver': ['liblinear', 'lbfgs']
}
clf_LR = GridSearchCV(LR, param_grid=param_grid, cv=5, verbose=True, n_jobs=-1)
 best_clf_LR = clf_LR.fit(X_train_scaled, y_train)
 clf_performance(best_clf_LR )<init_hyperparams>
check_features.append('id_10' )
print_valid_params("'C': 11.288378916846883, 'max_iter': 100, 'penalty': 'l1', 'random_state': 42, 'solver': 'liblinear'" )<find_best_params>
missing[missing['features']=='D9']
tuned_LR = LogisticRegression(C= 11.288378916846883, max_iter= 100, penalty= 'l1', random_state= 42, solver= 'liblinear')
 get_model_accuracy(tuned_LR )<train_on_grid>
missing[missing['features']=='D8']
param_grid = {
'random_state': [42],
'C': [.1,.3, 1, 3],
'kernel': ['rbf'],
'gamma': [.03,.1,.3, 1]
}
clf_SVC = GridSearchCV(SVC, param_grid=param_grid, cv=5, verbose=True, n_jobs=-1)
 best_clf_SVC = clf_SVC.fit(X_train_scaled, y_train)
 clf_performance(best_clf_SVC )<init_hyperparams>
missing[missing['features']=='id_30']
print_valid_params("'C': 3, 'gamma': 0.03, 'kernel': 'rbf', 'random_state': 42" )<find_best_params>
missing[missing['features']=='id_32']
tuned_SVC = svm.SVC(C= 3, gamma= 0.03, kernel= 'rbf', random_state= 42, probability=True)
 get_model_accuracy(tuned_SVC )<train_on_grid>
missing[missing['features']=='id_34']
param_grid = {
'n_neighbors': [3, 5, 7, 9],
'weights': ['uniform', 'distance'],
'algorithm': ['auto', 'ball_tree', 'kd_tree'],
'p': [1, 2]
}
clf_KNN = GridSearchCV(KNN, param_grid=param_grid, cv=5, verbose=True, n_jobs=-1)
 best_clf_KNN = clf_KNN.fit(X_train_scaled, y_train)
 clf_performance(best_clf_KNN )<set_options>
drop_features.append('id_34' )
print_valid_params("'algorithm': 'auto', 'n_neighbors': 7, 'p': 2, 'weights': 'uniform'" )<find_best_params>
missing[missing['features']=='id_14']
tuned_KNN = KNeighborsClassifier(algorithm= 'auto', n_neighbors= 7, p= 2, weights= 'uniform')
 get_model_accuracy(tuned_KNN )<train_on_grid>
missing[missing['features']=='V141']
param_grid = {
'random_state': [42],
'n_estimators': [10, 30, 100, 300, 1000],
'bootstrap': [True, False],
'max_depth': [1, 3, 10, 30, 100, None],
'max_features': ['auto', 'sqrt'],
'min_samples_leaf': [1, 3, 10, 30],
'min_samples_split': [2, 4, 7, 10]
}
clf_RFC = RandomizedSearchCV(RFC, param_distributions=param_grid, n_iter=100, cv=5, verbose=True, n_jobs=-1)
 best_clf_RFC = clf_RFC.fit(X_train_scaled, y_train)
 clf_performance(best_clf_RFC )<init_hyperparams>
drop_features.append('V141' )
print_valid_params("'random_state': 42, 'n_estimators': 30, 'min_samples_split': 7, 'min_samples_leaf': 1, 'max_features': 'sqrt', 'max_depth': None, 'bootstrap': False" )<find_best_params>
missing[missing['features']=='V157']
tuned_RFC = RandomForestClassifier(random_state= 42, n_estimators= 30, min_samples_split= 7, min_samples_leaf= 1, max_features= 'sqrt', max_depth= None, bootstrap= False)
 get_model_accuracy(tuned_RFC )<train_on_grid>
check_features.append('V157' )
param_grid = {
'random_state': [42],
'n_estimators': [100, 300],
'learning_rate': [.1,.3, 1],
'max_depth': [1, 2, 3, 10],
'min_samples_split': [.1,.3, 1, 3, 10],
'min_samples_leaf': [.1,.3, 1, 3]
}
clf_GB = GridSearchCV(GB, param_grid=param_grid, cv=5, verbose=True, n_jobs=-1)
 best_clf_GB = clf_GB.fit(X_train_scaled, y_train)
 clf_performance(best_clf_GB )<init_hyperparams>
missing[missing['features']=='V162']
print_valid_params("'learning_rate': 0.1, 'max_depth': 3, 'min_samples_leaf': 3, 'min_samples_split': 0.1, 'n_estimators': 300, 'random_state': 42" )<find_best_params>
missing[missing['features']=='V161']
tuned_GB = GradientBoostingClassifier(learning_rate= 0.1, max_depth= 3, min_samples_leaf= 3, min_samples_split= 0.1, n_estimators= 300, random_state= 42)
 get_model_accuracy(tuned_GB )<train_on_grid>
missing[missing['features']=='V158']
params = {
'weights': [[1, 1, 1], [1, 1, 2], [1, 2, 1], [2, 1, 1], [1, 2, 2], [2, 1, 2], [2, 2, 1]]
}
vote_weight = GridSearchCV(voting_clf_best_3, param_grid=params, cv=5, verbose=True, n_jobs=-1)
 best_clf_weight = vote_weight.fit(X_train_scaled, y_train)
 clf_performance(best_clf_weight )<define_search_space>
check_features.append('V158' )
print_valid_params("'weights': [1, 1, 2]" )<choose_model_class>
missing[missing['features']=='V156']
weighted_voting_clf_best_3 = VotingClassifier(
 estimators=[('SVC', SVC),('RFC', RFC),('KNN', KNN)],
voting='soft',
weights= [1, 1, 2] 
)
 get_model_accuracy(weighted_voting_clf_best_3 )<save_to_csv>
check_features.append('V156' )
def submission_to_csv(y_preds, filename='submission.csv'):
submission = {'PassengerId': test_data.PassengerId, 'survived': y_preds}
submission_df = pd.DataFrame(data=submission)
 submission_csv = submission_df.to_csv(filename, index=False)
 return(submission_csv )<save_to_csv>
V = ['V142', 'V155', 'V154', 'V140', 'V149', 'V148', 'V147', 'V146', 'V153', 'V163', 'V139', 'V138', 'V151', 'V152',
'V145','V144', 'V143', 'V160', 'V159', 'V164', 'V165', 'V166', 'V150', 'V337', 'V333', 'V336', 'V335', 'V334', 'V338',
'V339', 'V325', 'V332', 'V324', 'V330', 'V329', 'V328', 'V327', 'V326', 'V322', 'V323', 'V331', 'V278', 'V277', 'V252',
'V253', 'V254', 'V257', 'V258', 'V260', 'V243', 'V262', 'V263', 'V264', 'V249', 'V266', 'V267', 'V268', 'V269', 'V273',
'V274', 'V275', 'V276', 'V265', 'V261', 'V247', 'V246', 'V241', 'V240', 'V237', 'V236', 'V235', 'V233', 'V232',
'V231', 'V230', 'V229', 'V228', 'V226', 'V225', 'V224', 'V223', 'V219', 'V218', 'V217', 'V244', 'V248', 'V242', 'V211',
'V214', 'V213', 'V212', 'V196', 'V205', 'V183', 'V216', 'V206', 'V186', 'V187', 'V192', 'V207', 'V215', 'V182', 'V191',
'V181', 'V167', 'V168', 'V199', 'V193', 'V172', 'V173', 'V202', 'V203', 'V176', 'V177', 'V178', 'V179', 'V204', 'V190',
'V194', 'V201', 'V189', 'V188', 'V185', 'V184', 'V180', 'V175', 'V174', 'V171', 'V170', 'V169', 'V195', 'V200', 'V197',
'V198', 'V208', 'V210', 'V209', 'V272', 'V234', 'V222', 'V238', 'V239', 'V227', 'V251', 'V250', 'V271', 'V270', 'V221',
'V220', 'V255', 'V256', 'V259', 'V245', 'V3', 'V1', 'V2', 'V4', 'V5', 'V6', 'V7', 'V8', 'V9', 'V10', 'V11', 'V46', 'V42',
'V44', 'V43', 'V47', 'V41', 'V40', 'V39', 'V38', 'V37', 'V36', 'V35', 'V52', 'V51', 'V50', 'V49', 'V48', 'V45', 'V88',
'V93', 'V85', 'V87', 'V84', 'V83', 'V82', 'V81', 'V90', 'V91', 'V92', 'V94', 'V80', 'V79', 'V78', 'V77', 'V76',
'V75', 'V86', 'V53', 'V74', 'V73', 'V72', 'V66', 'V54', 'V67', 'V64', 'V63', 'V62', 'V61', 'V71', 'V69', 'V55',
'V60', 'V59', 'V58', 'V57', 'V65', 'V56', 'V70', 'V22', 'V23', 'V24', 'V34', 'V33', 'V32', 'V31', 'V30', 'V29',
'V26', 'V25', 'V15', 'V21', 'V14', 'V16', 'V17', 'V18', 'V19', 'V12', 'V20', 'V13', 'V282', 'V301', 'V300', 'V296',
'V289', 'V288', 'V283', 'V281', 'V315', 'V314', 'V313', 'V114', 'V110', 'V105', 'V104', 'V103', 'V102', 'V101', 'V100',
'V95', 'V99', 'V98', 'V97', 'V107', 'V111', 'V96', 'V112', 'V106', 'V113', 'V137', 'V136', 'V108', 'V135', 'V134', 'V133',
'V132', 'V131', 'V130', 'V129', 'V128', 'V127', 'V126', 'V125', 'V124', 'V123', 'V122', 'V121', 'V120', 'V119', 'V118',
'V117', 'V116', 'V115', 'V109', 'V312', 'V321', 'V294', 'V306', 'V305', 'V304', 'V303', 'V302', 'V299', 'V298', 'V297',
'V295', 'V293', 'V308', 'V292', 'V291', 'V290', 'V287', 'V286', 'V285', 'V284', 'V280', 'V279', 'V320', 'V307', 'V309',
'V316', 'V310', 'V318', 'V317', 'V319', 'V311']
tuned_RFC.fit(X_train_scaled, y_train)
 tuned_RFC_preds = tuned_RFC.predict(X_test_scaled)
 submission_to_csv(tuned_RFC_preds, 'RFC_submission.csv' )<save_to_csv>
V1drop = ['V142', 'V146', 'V138', 'V151', 'V152', 'V333', 'V338', 'V339', 'V325', 'V332', 'V324', 'V330', 'V329', 'V322',
'V323', 'V278', 'V277', 'V252', 'V253', 'V254', 'V260']
for i in V1drop:
drop_features.append(i )
tuned_GB.fit(X_train_scaled, y_train)
 tuned_GB_preds = tuned_GB.predict(X_test_scaled)
 submission_to_csv(tuned_GB_preds, 'GB_clf_submission.csv' )<save_to_csv>
V2drop = ['V263', 'V249', 'V266', 'V267', 'V268', 'V273', 'V276', 'V275', 'V247', 'V241', 'V240', 'V237', 'V235', 'V225',
'V224', 'V224', 'V248', 'V211', 'V213', 'V196', 'V205', 'V183', 'V206', 'V192']
for i in V2drop:
drop_features.append(i )
weighted_voting_clf_best_3.fit(X_train_scaled, y_train)
 weighted_voting_clf_best_3_preds = weighted_voting_clf_best_3.predict(X_test_scaled)
 submission_to_csv(weighted_voting_clf_best_3_preds, 'soft_voting_clf_submission.csv' )<import_modules>
V3drop = ['V191', 'V181', 'V193', 'V172', 'V173', 'V202', 'V203', 'V177', 'V179', 'V194', 'V185', 'V184', 'V175', 'V174',
'V195', 'V197', 'V198', 'V208', 'V210', 'V227', 'V251', 'V250', 'V271', 'V270', 'V225']
for i in V3drop:
drop_features.append(i )
import pandas as pd
import plotly.express as px
import matplotlib.pyplot as plt
import plotly.graph_objects as go
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
import numpy as np
from sklearn.metrics import mean_squared_log_error
from sklearn.tree import DecisionTreeRegressor<load_from_csv>
V4drop = ['V89', 'V256', 'V3', 'V1', 'V2', 'V4', 'V5', 'V6', 'V7', 'V8', 'V9', 'V10', 'V11', 'V46', 'V42', 'V43', 'V47',
'V41', 'V39', 'V36', 'V35', 'V51', 'V50', 'V49', 'V48', 'V88', 'V93', 'V85', 'V84', 'V83', 'V81', 'V90', 'V91',
'V92', 'V94', 'V80', 'V79', 'V75', 'V75']
for i in V4drop:
drop_features.append(i )
train_df=pd.read_csv(".. /input/covid19-global-forecasting-week-1/train.csv")
 test_df=pd.read_csv(".. /input/covid19-global-forecasting-week-1/test.csv" )<filter>
V5drop = ['V68', 'V27', 'V28', 'V53', 'V74', 'V73', 'V72', 'V66', 'V54', 'V67', 'V64', 'V63', 'V62', 'V61', 'V71', 'V69',
'V55', 'V60', 'V59', 'V58', 'V57', 'V65', 'V56', 'V70', 'V22', 'V23', 'V24', 'V34', 'V33', 'V32', 'V31', 'V30',
'V29', 'V26', 'V25', 'V15', 'V21', 'V14', 'V16', 'V17', 'V18', 'V19', 'V12', 'V20', 'V13', 'V301', 'V300', 'V296',
'V289', 'V288']
for i in V5drop:
drop_features.append(i )
train_df[train_df["Country/Region"]=="China"] 
<groupby>
V6drop = ['V114', 'V110', 'V105', 'V104', 'V103', 'V102', 'V101', 'V100', 'V95', 'V99', 'V98', 'V107', 'V111', 'V112', 'V106',
'V113', 'V108', 'V134', 'V133', 'V135', 'V132', 'V131', 'V130', 'V129', 'V126', 'V125', 'V124', 'V123', 'V122',
'V121', 'V120', 'V119', 'V118', 'V117', 'V116', 'V115', 'V109', 'V294']
for i in V6drop:
drop_features.append(i )
temp_Confirmed = train_df.loc[train_df["Date"]==train_df["Date"][len(train_df)-1]].groupby(['Country/Region'])["ConfirmedCases"].sum().reset_index()
temp_Confirmed.head()<prepare_output>
V7drop = ['V305', 'V304', 'V303', 'V302', 'V299', 'V298', 'V297', 'V295', 'V293', 'V292', 'V291', 'V290', 'V287', 'V286',
'V285', 'V289', 'V279', 'V309', 'V316', 'V318', 'V319']
for i in V7drop:
drop_features.append(i )
Country_Confirmed=pd.DataFrame()
Country_Confirmed["Name"]=temp_Confirmed["Country/Region"]
Country_Confirmed["Confirmed"]=temp_Confirmed["ConfirmedCases"]
Country_Confirmed.head()<groupby>
D_done = ['D7', 'D13', 'D14', 'D12', 'D6', 'D9', 'D8']
D_not_done = missing['features'].apply(lambda x: x if x[0]=='D' else 0)
 D_not_done = pd.DataFrame(D_not_done)
 D_not_done = D_not_done[D_not_done['features']!=0]
D_not_done = D_not_done[~D_not_done['features'].isin(D_done)]
D_not_done = D_not_done[~D_not_done['features'].isin(['DeviceInfo', 'DeviceType'])]
D = list(D_not_done['features'])
 D
temp_Fatalities = train_df.loc[train_df["Date"]==train_df["Date"][len(train_df)-1]].groupby(['Country/Region'])["Fatalities"].sum().reset_index()
temp_Fatalities.head()<prepare_output>
C_not_done = missing['features'].apply(lambda x: x if x[0]=='C' else 0)
 C_not_done = pd.DataFrame(C_not_done)
 C_not_done = C_not_done[C_not_done['features']!=0]
C_not_done = list(C_not_done['features'])
 C_not_done
Country_Fatalities=pd.DataFrame()
Country_Fatalities["Name"]=temp_Fatalities["Country/Region"]
Country_Fatalities["Fatalities"]=temp_Fatalities["Fatalities"]
Country_Fatalities.head()<data_type_conversions>
drop_features.append('C3' )
China=train_df[train_df["Country/Region"]=="China"].groupby(["Date"])["ConfirmedCases"].sum().reset_index()
China['Date']=pd.to_datetime(China['Date'] )<data_type_conversions>
id_done = ['id_24', 'id_25', 'id_08', 'id_07', 'id_21', 'id_26', 'id_27', 'id_23', 'id_22', 'id_18', 'id_04', 'id_03',
'id_33', 'id_09', 'id_30', 'id_32', 'id_32', 'id_34', 'id_14']
id_not_done = missing['features'].apply(lambda x: x if x[0]=='i' else 0)
 id_not_done = pd.DataFrame(id_not_done)
 id_not_done = id_not_done[id_not_done['features']!=0]
id_not_done = id_not_done[~id_not_done['features'].isin(id_done)]
id_not_done
China_Fatalities=train_df[train_df["Country/Region"]=="China"].groupby(["Date"])["Fatalities"].sum().reset_index()
China_Fatalities['Date']=pd.to_datetime(China_Fatalities['Date'] )<data_type_conversions>
check_features.append('id_10' )
Italy=train_df[train_df["Country/Region"]=="Italy"].groupby(["Date"])["ConfirmedCases"].sum().reset_index()
Italy['Date']=pd.to_datetime(Italy['Date'] )<data_type_conversions>
check_features.append('id_16' )
Italy_Fatalities=train_df[train_df["Country/Region"]=="Italy"].groupby(["Date"])["Fatalities"].sum().reset_index()
Italy_Fatalities['Date']=pd.to_datetime(Italy_Fatalities['Date'] )<data_type_conversions>
check_features.append('id_28' )
Other_countries=train_df[~train_df["Country/Region"].isin(['China','Italy'])]
Other_countries['Country/Region']= 'Others'
Other_countries=Other_countries.groupby(["Date"])["ConfirmedCases"].sum().reset_index()
Other_countries['Date']=pd.to_datetime(Other_countries['Date'] )<data_type_conversions>
check_features.append('id_29' )
Other_Fatalities=train_df[~train_df["Country/Region"].isin(["China","Italy"])]
Other_Fatalities['Country/Region']= 'Others'
Other_Fatalities=Other_Fatalities.groupby(["Date"])["Fatalities"].sum().reset_index()
Other_Fatalities['Date']=pd.to_datetime(Other_Fatalities['Date'] )<count_missing_values>
check_features.append('id_38' )
train_df.isnull().sum()<data_type_conversions>
check_features.append('id_37' )
X=train_df[["Date","Lat","Long"]]
X["Date"]=X["Date"].apply(lambda x:x.replace("-",""))
X["Date"] = X["Date"].astype(int )<prepare_x_and_y>
check_features.append('id_36' )
y1=train_df["ConfirmedCases"]
y2=train_df["Fatalities"]<split>
check_features.append('id_35' )
X_train_confirmed, X_test_confirmed, y_train_confirmed, y_test_confirmed = train_test_split(X, y1, test_size =.20, random_state = 42 )<find_best_model_class>
c_not_done = missing['features'].apply(lambda x: x if x[0]=='c' else 0)
 c_not_done = pd.DataFrame(c_not_done)
 c_not_done = c_not_done[c_not_done['features']!=0]
list(c_not_done['features'] )
dt1=DecisionTreeRegressor()
dt1.fit(X_train_confirmed, y_train_confirmed)
 y_pred_dt_confirmed=dt1.predict(X_test_confirmed )<compute_test_metric>
check_features.append('card4' )
np.sqrt(mean_squared_log_error(y_test_confirmed, y_pred_dt_confirmed))<train_model>
check_features.append('card6' )
rfc1=RandomForestRegressor(n_estimators=5, min_samples_split=3, random_state=42)
 rfc1.fit(X_train_confirmed, y_train_confirmed )<predict_on_test>
M_not_done = missing['features'].apply(lambda x: x if x[0]=='M' else 0)
 M_not_done = pd.DataFrame(M_not_done)
 M_not_done = M_not_done[M_not_done['features']!=0]
list(M_not_done['features'] )
y_pred_rfc_confirmed=rfc1.predict(X_test_confirmed )<compute_test_metric>
drop_features.append('M1' )
np.sqrt(mean_squared_log_error(y_test_confirmed, y_pred_rfc_confirmed))<split>
check_features.append('M2' )
X_train_fatal, X_test_fatal, y_train_fatal, y_test_fatal = train_test_split(X, y2, test_size =.20, random_state = 42 )<choose_model_class>
check_features.append('M3' )
dt2=DecisionTreeRegressor()<train_model>
check_features.append('M5' )
dt2.fit(X_train_fatal, y_train_fatal )<predict_on_test>
check_features.append('M6' )
y_pred_dt_fatal=dt2.predict(X_test_fatal )<compute_test_metric>
train_copy = train.copy()
test_copy = test.copy()
np.sqrt(mean_squared_log_error(y_test_fatal, y_pred_dt_fatal))<train_model>
def id_split(dataframe):
dataframe['device_name'] = dataframe['DeviceInfo'].str.split('/', expand=True)[0]
dataframe['device_version'] = dataframe['DeviceInfo'].str.split('/', expand=True)[1]
dataframe['OS_id_30'] = dataframe['id_30'].str.split(' ', expand=True)[0]
dataframe['version_id_30'] = dataframe['id_30'].str.split(' ', expand=True)[1]
dataframe['browser_id_31'] = dataframe['id_31'].str.split(' ', expand=True)[0]
dataframe['version_id_31'] = dataframe['id_31'].str.split(' ', expand=True)[1]
dataframe['screen_width'] = dataframe['id_33'].str.split('x', expand=True)[0]
dataframe['screen_height'] = dataframe['id_33'].str.split('x', expand=True)[1]
dataframe['id_34'] = dataframe['id_34'].str.split(':', expand=True)[1]
dataframe['id_23'] = dataframe['id_23'].str.split(':', expand=True)[1]
dataframe.loc[dataframe['device_name'].str.contains('SM', na=False), 'device_name'] = 'Samsung'
dataframe.loc[dataframe['device_name'].str.contains('SAMSUNG', na=False), 'device_name'] = 'Samsung'
dataframe.loc[dataframe['device_name'].str.contains('GT-', na=False), 'device_name'] = 'Samsung'
dataframe.loc[dataframe['device_name'].str.contains('Moto G', na=False), 'device_name'] = 'Motorola'
dataframe.loc[dataframe['device_name'].str.contains('Moto', na=False), 'device_name'] = 'Motorola'
dataframe.loc[dataframe['device_name'].str.contains('moto', na=False), 'device_name'] = 'Motorola'
dataframe.loc[dataframe['device_name'].str.contains('LG-', na=False), 'device_name'] = 'LG'
dataframe.loc[dataframe['device_name'].str.contains('rv:', na=False), 'device_name'] = 'RV'
dataframe.loc[dataframe['device_name'].str.contains('HUAWEI', na=False), 'device_name'] = 'Huawei'
dataframe.loc[dataframe['device_name'].str.contains('ALE-', na=False), 'device_name'] = 'Huawei'
dataframe.loc[dataframe['device_name'].str.contains('-L', na=False), 'device_name'] = 'Huawei'
dataframe.loc[dataframe['device_name'].str.contains('Blade', na=False), 'device_name'] = 'ZTE'
dataframe.loc[dataframe['device_name'].str.contains('BLADE', na=False), 'device_name'] = 'ZTE'
dataframe.loc[dataframe['device_name'].str.contains('Linux', na=False), 'device_name'] = 'Linux'
dataframe.loc[dataframe['device_name'].str.contains('XT', na=False), 'device_name'] = 'Sony'
dataframe.loc[dataframe['device_name'].str.contains('HTC', na=False), 'device_name'] = 'HTC'
dataframe.loc[dataframe['device_name'].str.contains('ASUS', na=False), 'device_name'] = 'Asus'
dataframe.loc[dataframe.device_name.isin(dataframe.device_name.value_counts() [dataframe.device_name.value_counts() < 200].index), 'device_name'] = 'Others'
gc.collect()
return dataframe
rfc2=RandomForestRegressor(n_estimators=5, min_samples_split=3, random_state=42)
 rfc2.fit(X_train_fatal, y_train_fatal )<predict_on_test>
train = id_split(train)
 test = id_split(test )
y_pred_rfc_fatal=rfc2.predict(X_test_fatal )<compute_test_metric>
usefull_features = [col for col in train.columns if col not in drop_features]
train = train[usefull_features]
usefull_features.remove('isFraud')
 test = test[usefull_features]
np.sqrt(mean_squared_log_error(y_test_fatal, y_pred_rfc_fatal))<count_missing_values>
train['TransactionAmt_Log'] = np.log(train['TransactionAmt'])
 test['TransactionAmt_Log'] = np.log(test['TransactionAmt'])
 train['TransactionAmt_decimal'] =(( train['TransactionAmt'] - train['TransactionAmt'].astype(int)) * 1000 ).astype(int)
 test['TransactionAmt_decimal'] =(( test['TransactionAmt'] - test['TransactionAmt'].astype(int)) * 1000 ).astype(int)
 for feature in ['id_02__id_20', 'id_02__D8', 'D11__DeviceInfo', 'DeviceInfo__P_emaildomain', 'P_emaildomain__C2',
'card2__dist1', 'card1__card5', 'card2__id_20', 'card5__P_emaildomain', 'addr1__card1']:
f1, f2 = feature.split('__')
 train[feature] = train[f1].astype(str)+ '_' + train[f2].astype(str)
 test[feature] = test[f1].astype(str)+ '_' + test[f2].astype(str)
 le = LabelEncoder()
le.fit(list(train[feature].astype(str ).values)+ list(test[feature].astype(str ).values))
train[feature] = le.transform(list(train[feature].astype(str ).values))
test[feature] = le.transform(list(test[feature].astype(str ).values))
test_df.isnull().sum()<create_dataframe>
emails = {'gmail': 'google', 'att.net': 'att', 'twc.com': 'spectrum', 'scranton.edu': 'other',
'optonline.net': 'other', 'hotmail.co.uk': 'microsoft', 'comcast.net': 'other',
'yahoo.com.mx': 'yahoo', 'yahoo.fr': 'yahoo', 'yahoo.es': 'yahoo',
'charter.net': 'spectrum', 'live.com': 'microsoft', 'aim.com': 'aol',
'hotmail.de': 'microsoft', 'centurylink.net': 'centurylink', 'gmail.com': 'google',
'me.com': 'apple', 'earthlink.net': 'other', 'gmx.de': 'other', 'web.de': 'other',
'cfl.rr.com': 'other', 'hotmail.com': 'microsoft', 'protonmail.com': 'other',
'hotmail.fr': 'microsoft', 'windstream.net': 'other', 'outlook.es': 'microsoft',
'yahoo.co.jp': 'yahoo', 'yahoo.de': 'yahoo', 'servicios-ta.com': 'other',
'netzero.net': 'other', 'suddenlink.net': 'other', 'roadrunner.com': 'other',
'sc.rr.com': 'other', 'live.fr': 'microsoft', 'verizon.net': 'yahoo',
'msn.com': 'microsoft', 'q.com': 'centurylink', 'prodigy.net.mx': 'att',
'frontier.com': 'yahoo', 'anonymous.com': 'other', 'rocketmail.com': 'yahoo',
'sbcglobal.net': 'att', 'frontiernet.net': 'yahoo', 'ymail.com': 'yahoo',
'outlook.com': 'microsoft', 'mail.com': 'other', 'bellsouth.net': 'other',
'embarqmail.com': 'centurylink', 'cableone.net': 'other', 'hotmail.es': 'microsoft',
'mac.com': 'apple', 'yahoo.co.uk': 'yahoo', 'netzero.com': 'other',
'yahoo.com': 'yahoo', 'live.com.mx': 'microsoft', 'ptd.net': 'other',
'cox.net': 'other', 'aol.com': 'aol', 'juno.com': 'other', 'icloud.com': 'apple'}
us_emails = ['gmail', 'net', 'edu']
test_data=test_df[["Date","Lat","Long"]]<data_type_conversions>
for c in ['P_emaildomain', 'R_emaildomain']:
train[c + '_bin'] = train[c].map(emails)
 test[c + '_bin'] = test[c].map(emails)
 train[c + '_suffix'] = train[c].map(lambda x: str(x ).split('.')[-1])
 test[c + '_suffix'] = test[c].map(lambda x: str(x ).split('.')[-1])
 train[c + '_suffix'] = train[c + '_suffix'].map(lambda x: x if str(x)not in us_emails else 'us')
 test[c + '_suffix'] = test[c + '_suffix'].map(lambda x: x if str(x)not in us_emails else 'us' )
test_data["Date"]=test_data["Date"].apply(lambda x:x.replace("-",""))
test_data["Date"] = test_data["Date"].astype(int )<predict_on_test>
START_DATE = datetime.datetime.strptime('2017-11-30', '%Y-%m-%d')
 def setTime(df):
df['TransactionDT'] = df['TransactionDT'].fillna(df['TransactionDT'].median())
 df['DT'] = df['TransactionDT'].apply(lambda x:(START_DATE + datetime.timedelta(seconds = x)))
 df['DT_M'] =(df['DT'].dt.year-2017)*12 + df['DT'].dt.month
df['DT_W'] =(df['DT'].dt.year-2017)*52 + df['DT'].dt.weekofyear
df['DT_D'] =(df['DT'].dt.year-2017)*365 + df['DT'].dt.dayofyear
df['DT_hour'] = df['DT'].dt.hour
df['DT_day_week'] = df['DT'].dt.dayofweek
df['DT_day'] = df['DT'].dt.day
df['D9_not_na'] = np.where(df['D9'].isna() ,0,1)
 df['D8_not_same_day'] = np.where(df['D8']>=1,1,0)
 df['D8_D9_decimal_dist'] = df['D8'].fillna(0)-df['D8'].fillna(0 ).astype(int)
 df['D8_D9_decimal_dist'] =(( df['D8_D9_decimal_dist']-df['D9'])**2)**0.5
df['D8'] = df['D8'].fillna(-1 ).astype(int)
 return df
train=setTime(train)
 test=setTime(test )
y_confirmed=rfc1.predict(test_data )<predict_on_test>
def addNewFeatures(data):
data['uid'] = data['card1'].astype(str)+'_'+data['card2'].astype(str)
 data['uid2'] = data['uid'].astype(str)+'_'+data['card3'].astype(str)+'_'+data['card5'].astype(str)
 data['uid3'] = data['uid2'].astype(str)+'_'+data['addr1'].astype(str)+'_'+data['addr2'].astype(str)
 return data
y_fatal=rfc2.predict(test_data )<create_dataframe>
train = addNewFeatures(train)
 test = addNewFeatures(test )
submission=pd.DataFrame({'ForecastId': test_df["ForecastId"], 'ConfirmedCases': y_confirmed, 'Fatalities': y_fatal} )<data_type_conversions>
i_cols = ['card2','card3','card5','uid','uid2','uid3']
for col in i_cols:
for agg_type in ['mean','std']:
new_col_name = col+'_TransactionAmt_'+agg_type
temp_df = pd.concat([train[[col, 'TransactionAmt']], test[[col,'TransactionAmt']]])
 temp_df = temp_df.groupby([col])['TransactionAmt'].agg([agg_type] ).reset_index().rename(
 columns={agg_type: new_col_name})
 temp_df.index = list(temp_df[col])
 temp_df = temp_df[new_col_name].to_dict()
train[new_col_name] = train[col].map(temp_df)
 test[new_col_name] = test[col].map(temp_df )
submission["ConfirmedCases"]=submission["ConfirmedCases"].astype(int)
 submission["Fatalities"]=submission["Fatalities"].astype(int )<save_to_csv>
train = train.replace(np.inf,999)
 test = test.replace(np.inf,999 )
submission.to_csv('submission.csv', index=False )<save_to_csv>
i_cols = ['card1','card2','card3','card5',
'C1','C2','C4','C5','C6','C7','C8','C9','C10','C11','C12','C13','C14',
'D1','D2','D3','D4','D5','D6','D7','D8',
'addr1','addr2',
'dist1','dist2',
'P_emaildomain', 'R_emaildomain',
'DeviceInfo','device_name',
'id_30','id_33',
'uid','uid2','uid3',
]
for col in i_cols:
temp_df = pd.concat([train[[col]], test[[col]]])
 fq_encode = temp_df[col].value_counts(dropna=False ).to_dict()
train[col+'_fq_enc'] = train[col].map(fq_encode)
 test[col+'_fq_enc'] = test[col].map(fq_encode)
 for col in ['DT_M','DT_W','DT_D']:
temp_df = pd.concat([train[[col]], test[[col]]])
 fq_encode = temp_df[col].value_counts().to_dict()
train[col+'_total'] = train[col].map(fq_encode)
 test[col+'_total'] = test[col].map(fq_encode)
 periods = ['DT_M','DT_W','DT_D']
i_cols = ['uid']
for period in periods:
for col in i_cols:
new_column = col + '_' + period
temp_df = pd.concat([train[[col,period]], test[[col,period]]])
 temp_df[new_column] = temp_df[col].astype(str)+ '_' +(temp_df[period] ).astype(str)
 fq_encode = temp_df[new_column].value_counts().to_dict()
train[new_column] =(train[col].astype(str)+ '_' + train[period].astype(str)).map(fq_encode)
 test[new_column] =(test[col].astype(str)+ '_' + test[period].astype(str)).map(fq_encode)
 train[new_column] /= train[period+'_total']
test[new_column] /= test[period+'_total']
submission.to_csv('submission.csv', index=False )<load_from_csv>
train.drop(['TransactionDT', 'uid','uid2','uid3', 'DT','DT_M','DT_W','DT_D', 'DT_hour','DT_day_week','DT_day',
'DT_D_total','DT_W_total','DT_M_total', 'id_30','id_31','id_33', 'D1', 'D2', 'D9'], axis = 1, inplace = True)
 test.drop(['TransactionDT', 'uid','uid2','uid3', 'DT','DT_M','DT_W','DT_D', 'DT_hour','DT_day_week','DT_day',
'DT_D_total','DT_W_total','DT_M_total', 'id_30','id_31','id_33', 'D1', 'D2', 'D9'], axis = 1, inplace = True )
test = pd.read_csv(".. /input/covid19-global-forecasting-week-1/test.csv")
 train = pd.read_csv(".. /input/covid19-global-forecasting-week-1/train.csv")
 train['Province/State'].fillna('', inplace=True)
 test['Province/State'].fillna('', inplace=True)
 train['Date'] = pd.to_datetime(train['Date'])
 test['Date'] = pd.to_datetime(test['Date'])
 train = train.sort_values(['Country/Region','Province/State','Date'])
 test = test.sort_values(['Country/Region','Province/State','Date'] )<sort_values>
for col in train.columns:
if train[col].dtype == 'object':
le = LabelEncoder()
le.fit(list(train[col].astype(str ).values)+ list(test[col].astype(str ).values))
train[col] = le.transform(list(train[col].astype(str ).values))
test[col] = le.transform(list(test[col].astype(str ).values))
train.groupby(['Country/Region'] ).max().sort_values(['ConfirmedCases'],ascending = False ).head(10 )<install_modules>
def agg_features(df):
columns_a = ['TransactionAmt', 'id_02', 'D15']
columns_b = ['card1', 'card4', 'addr1']
for col_a in columns_a:
for col_b in columns_b:
df[f'{col_a}_to_mean_{col_b}'] = df[col_a] / df.groupby([col_b])[col_a].transform('mean')
 df[f'{col_a}_to_std_{col_b}'] = df[col_a] / df.groupby([col_b])[col_a].transform('std')
 return df
test = agg_features(test)
 train = agg_features(train )
!pip install pmdarima<merge>
train = reduce_mem_usage(train)
 test = reduce_mem_usage(test )
%matplotlib inline
register_matplotlib_converters()
country_plot = 'Vietnam'
def RMSLE(pred,actual):
return np.sqrt(np.mean(np.power(( np.log(pred+1)-np.log(actual+1)) ,2)))
 def RMSLE_Cal(pred_data):
df = pd.DataFrame(pred_data)
 df.columns = ['Date','Country/Region','Province/State','ForecastId','ConfirmedCases','Fatalities']
df_val = pd.merge(df,train[['Date','Country/Region','Province/State','ConfirmedCases','Fatalities']],on=['Date','Country/Region','Province/State'], how='left')
 df_val = df_val.sort_values('Date')
 val_size = df_val[df_val['ConfirmedCases_y'].notnull() == True].shape[0]
return RMSLE(df_val['ConfirmedCases_y'][0:val_size-1].values, df_val['ConfirmedCases_x'][0:val_size-1].values)
 def plotPrediction(pred_data):
df = pd.DataFrame(pred_data)
 df.columns = ['Date','Country/Region','Province/State','ForecastId','ConfirmedCases','Fatalities']
df = df[df['Country/Region'] == country_plot]
df_val = pd.merge(df,train[['Date','Country/Region','Province/State','ConfirmedCases','Fatalities']],on=['Date','Country/Region','Province/State'], how='left')
 df_val = df_val.sort_values('Date')
 fig, axes = plt.subplots(nrows=1,ncols=2,figsize=(20, 7))
axes[0].plot(df_val['Date'], df_val['ConfirmedCases_y'], label = 'Confirmed Cases')
 axes[0].plot(df_val['Date'], df_val['ConfirmedCases_x'], label = 'Confirmed Cases Forecast')
 axes[1].plot(df_val['Date'], df_val['Fatalities_y'], label = 'Fatalities')
 axes[1].plot(df_val['Date'], df_val['Fatalities_x'], label = 'Fatalities Forecast')
 axes[0].legend()
axes[1].legend()
fig.autofmt_xdate()
return RMSLE(df_val['ConfirmedCases_y'][0:val_size-1].values, df_val['ConfirmedCases_x'][0:val_size-1].values)
<save_to_csv>
X = train.drop(['isFraud'], axis = 1)
 y = train['isFraud']
print('Our train set have {} columns'.format(train.shape[1]))
print('Our test set have {} columns'.format(test.shape[1]))
gc.collect()
df_past = pd.merge(test,train,on = ['Province/State','Date','Country/Region','Lat','Long'], how = 'inner' ).drop(['Id','Lat','Long'],axis=1)
 df_future = pd.DataFrame(pred_data)
 df_future.columns = ['Date','Country/Region','Province/State','ForecastId','ConfirmedCases','Fatalities']
df_future = df_future[(df_future['Date'] > df_past['Date'].max())]
submission = df_past.append(df_future, sort = True)
 submission[['ForecastId','ConfirmedCases','Fatalities']].to_csv('submission.csv',index=False )<choose_model_class>
params = {
'objective':'binary',
'boosting_type':'gbdt',
'metric':'auc',
'n_jobs':-1,
'learning_rate':0.005,
'num_leaves': 2**8,
'max_depth':-1,
'tree_learner':'serial',
'colsample_bytree': 0.7,
'subsample_freq':1,
'subsample':0.7,
'n_estimators':100000,
'max_bin':255,
'verbose':-1,
'random_state': 47,
'early_stopping_rounds':100,
}
df_train = train.copy()
df_test = test.copy()
train_period = 10
feature_Confirmed = ['ConfirmedCases'] + ['ConfirmedCases_B' + str(i)for i in range(1,train_period)]
feature_Fatalities = ['Fatalities'] + ['Fatalities_B' + str(i)for i in range(1,train_period)]
pred_data = []
model = DecisionTreeRegressor(max_depth=5)
 for country in [country_plot,'Vietnam']:
for province in df_train[(df_train['Country/Region'] == country)]['Province/State'].unique() :
max_train_date = df_train[(df_train['Country/Region'] == country)&(df_train['Province/State'] == province)]['Date'].max()
for forcast in df_test[(df_test['Country/Region'] == country)&(df_test['Province/State'] == province)]['ForecastId'].unique() :
testdate = df_test[(df_test['Country/Region'] == country)&(df_test['Province/State'] == province)&(df_test['ForecastId'] == forcast)]['Date'].max()
data = df_train[(df_train['Country/Region'] == country)&(df_train['Province/State'] == province)&(df_train['ConfirmedCases'] > 0)&(df_train['Date']<testdate)][['Date','ConfirmedCases','Fatalities']]
for i in range(1,train_period):
data['ConfirmedCases_B' + str(i)] = data['ConfirmedCases'].shift(i)
 data['Fatalities_B' + str(i)] = data['Fatalities'].shift(i)
 data = data[(data.ConfirmedCases_B1 != data.ConfirmedCases)&(data.ConfirmedCases_B2 != data.ConfirmedCases)]
X_train_confirmed = data[feature_Confirmed][train_period-1:-1]
y_train_confirmed = data['ConfirmedCases'][train_period-1:-1]
feature_Confirmed = ['ConfirmedCases'] + ['ConfirmedCases_B' + str(i)for i in range(1,train_period)]
if(len(X_train_confirmed)< 2):
ConfirmedCases_hat = 0
elif(len(X_train_confirmed)< train_period):
ConfirmedCases_hat = X_train_confirmed['ConfirmedCases'].values[-1:][0]
else:
model.fit(X_train_confirmed,y_train_confirmed)
 X_pred_confirmed = data[feature_Confirmed][-1:]
ConfirmedCases_hat = model.predict(X_pred_confirmed)[-1]
data = data[(data.Fatalities_B1 != data.Fatalities)&(data.Fatalities_B2 != data.Fatalities)]
X_train_fatalities = data[feature_Fatalities][train_period-1:-1]
y_train_fatalities = data['Fatalities'][train_period-1:-1]
feature_Fatalities = ['Fatalities'] + ['Fatalities_B' + str(i)for i in range(1,train_period)]
if(len(X_train_fatalities)< 1):
Fatalities_hat = 0
elif(len(X_train_fatalities)< train_period):
Fatalities_hat = X_train_fatalities['Fatalities'].values[-1:][0]
else:
model.fit(X_train_fatalities,y_train_fatalities)
 X_pred_fatalities = data[feature_Fatalities][-1:]
Fatalities_hat = model.predict(X_pred_fatalities)[-1]
pred_data.append([testdate,country,province,forcast,ConfirmedCases_hat,Fatalities_hat])
 if(testdate > max_train_date):
lat = df_test[(df_test['Country/Region'] == country)&(df_test['Province/State'] == province)&(df_test['ForecastId'] == forcast)]['Lat'].max()
long = df_test[(df_test['Country/Region'] == country)&(df_test['Province/State'] == province)&(df_test['ForecastId'] == forcast)]['Long'].max()
df_train.loc[len(df_train)] = [int(forcast),province,country,lat,long,testdate,float(ConfirmedCases_hat),float(Fatalities_hat)] 
<feature_engineering>
NFOLDS = 10
folds = KFold(n_splits=NFOLDS)
 splits = folds.split(X, y)
 y_preds = np.zeros(test.shape[0])
 y_oof = np.zeros(X.shape[0])
 score = 0
for fold_n,(train_index, valid_index)in enumerate(splits):
X_train, X_valid = X.iloc[train_index], X.iloc[valid_index]
y_train, y_valid = y.iloc[train_index], y.iloc[valid_index]
dtrain = lgb.Dataset(X_train, label = y_train)
 dvalid = lgb.Dataset(X_valid, label = y_valid)
 clf = lgb.train(params, dtrain, 10000, valid_sets = [dtrain, dvalid],
verbose_eval = 200, early_stopping_rounds = 500)
 y_pred_valid = clf.predict(X_valid)
 y_oof[valid_index] = y_pred_valid
print(f"Fold {fold_n + 1} | AUC: {roc_auc_score(y_valid, y_pred_valid)}")
 score += roc_auc_score(y_valid, y_pred_valid)/ NFOLDS
y_preds += clf.predict(test)/ NFOLDS
del X_train, X_valid, y_train, y_valid
gc.collect()
print("Mean AUC: ", score)
 print("Out of folds AUC: ", roc_auc_score(y, y_oof))
def CreateInput(data):
feature = []
for day in [1,100,200,500,1000]:
data['Number day from ' + str(day)+ ' case'] = 0
if(train[(train['Country/Region'] == country)&(train['Province/State'] == province)&(train['ConfirmedCases'] > day)]['Date'].count() > 0):
fromday = train[(train['Country/Region'] == country)&(train['Province/State'] == province)&(train['ConfirmedCases'] < day)]['Date'].max()
else:
fromday = pd.Timestamp('2020-12-31')
 for i in range(0, len(data)) :
if(data['Date'].iloc[i] > fromday):
day_denta = data['Date'].iloc[i] - fromday
data['Number day from ' + str(day)+ ' case'].iloc[i] = day_denta.days
feature = feature + ['Number day from ' + str(day)+ ' case']
return data[feature]
model = xgb.XGBRegressor()
pred_data = []
for country in ['Vietnam']:
for province in train[(train['Country/Region'] == country)]['Province/State'].unique() :
df_train = train[(train['Country/Region'] == country)&(train['Province/State'] == province)]
df_test = test[(test['Country/Region'] == country)&(test['Province/State'] == province)]
X_train = CreateInput(df_train)
 y_train_confirmed = df_train['ConfirmedCases']
y_train_fatalities = df_train['Fatalities']
X_pred = CreateInput(df_test)
 if(y_train_confirmed[-5:].mean() == y_train_confirmed.iloc[-1]):
y_hat_confirmed = np.repeat(y_train_confirmed.iloc[-1], len(X_pred))
else:
model.fit(X_train,y_train_confirmed)
 y_hat_confirmed = model.predict(X_pred)
 if(y_train_fatalities[-5:].mean() == y_train_fatalities.iloc[-1]):
y_hat_fatalities = np.repeat(y_train_fatalities.iloc[-1], len(X_pred))
else:
model.fit(X_train,y_train_fatalities)
 y_hat_fatalities = model.predict(X_pred)
 for i in range(0,len(y_hat_confirmed)) :
pred_data.append([df_test['Date'].iloc[i],country,province,df_test['ForecastId'].iloc[i],y_hat_confirmed[i],y_hat_fatalities[i]] )<choose_model_class>
sub['isFraud'] = y_preds
sub.to_csv('submission_v1.csv', index = False )