Kaggle competition: COVID19 Global Forecasting (Week 4)
mean_match_features = test_data_df.groupby(['matchId'])[train_data_df.columns[3:-1]].agg('mean' ).reset_index().loc[:, 'assists':'winPoints']
size_match_features = pd.DataFrame(test_data_df.groupby(['matchId'])[train_data_df.columns[3]].agg('size' ).reset_index() [train_data_df.columns[3]])
 size_match_features.columns = ['match_size'] 
<groupby>
df=pd.read_csv("/kaggle/input/covid19-global-forecasting-week-4/train.csv")
 test=pd.read_csv("/kaggle/input/covid19-global-forecasting-week-4/test.csv")
 print(df.shape,"
",df.head() )
mean_group_features = test_data_df.groupby(['matchId','groupId'])[train_data_df.columns[3:-1]].agg('mean' ).reset_index().loc[:, 'assists':'winPoints']
mean_group_rank_features = test_data_df.groupby(['matchId','groupId'])[train_data_df.columns[3:-1]].agg('mean' ).groupby(['matchId'])[train_data_df.columns[3:-1]].rank(pct=True ).reset_index()
mean_group_features = mean_group_features.join(mean_group_rank_features, rsuffix='_rank')
 max_group_features = test_data_df.groupby(['matchId','groupId'])[train_data_df.columns[3:-1]].agg('max' ).reset_index().loc[:, 'assists':'winPoints']
max_group_rank_features = test_data_df.groupby(['matchId','groupId'])[train_data_df.columns[3:-1]].agg('max' ).groupby(['matchId'])[train_data_df.columns[3:-1]].rank(pct=True ).reset_index()
max_group_features = max_group_features.join(max_group_rank_features, rsuffix='_rank')
 min_group_features = test_data_df.groupby(['matchId','groupId'])[train_data_df.columns[3:-1]].agg('min' ).reset_index().loc[:, 'assists':'winPoints']
min_group_rank_features = test_data_df.groupby(['matchId','groupId'])[train_data_df.columns[3:-1]].agg('min' ).groupby(['matchId'])[train_data_df.columns[3:-1]].rank(pct=True ).reset_index()
min_group_features = min_group_features.join(min_group_rank_features, rsuffix='_rank')
 size_group_features = pd.DataFrame(test_data_df.groupby(['matchId','groupId'])[train_data_df.columns[3]].agg('size' ).reset_index() [train_data_df.columns[3]])
 size_group_features.columns = ['group_size']<merge>
df["Province_State"].fillna("state", inplace = True)
 df["Country_Region"] = [country_name.replace("'","")for country_name in df["Country_Region"]]
print(df.shape,"
",df.head() )
features_three = mean_group_features.join(max_group_features, lsuffix='_group_mean', rsuffix='_group_max')
 features_four = min_group_features.join(size_group_features, lsuffix='_group_min', rsuffix='_group_size')
 features_2 = features_three.join(features_four)
 features_2['matchId'] = test_data_df.groupby(['matchId', 'groupId'])['matchId'].agg('mean' ).values
features_2 = features_2.fillna(0.0)
 features_2 = features_2.replace([np.inf, -np.inf], 0.0 )<merge>
X_y=shuffle(new_data)
 y_cases=X_y['target_infection']
y_fatal=X_y['target_fatal']
X=X_y.drop(['target_infection','target_fatal'],axis=1)
 X_train_cases, X_test_cases, y_train_cases, y_test_cases = train_test_split(X, y_cases, test_size=0.33)
 X_train_fatal, X_test_fatal, y_train_fatal, y_test_fatal = train_test_split(X, y_fatal, test_size=0.33)
 print("Shape of infection train dataset:",(X_train_cases.shape,y_train_cases.shape))
print("Shape of infection test dataset:",(X_test_cases.shape,y_test_cases.shape))
print("Shape of fatal train dataset:",(X_train_fatal.shape,y_train_fatal.shape))
print("Shape of fatal test dataset:",(X_test_fatal.shape,y_test_fatal.shape))
features = pd.merge(features_2, features_1, on='matchId', how='right' )<groupby>
reg_case=ElasticNet(random_state=42,l1_ratio=0.1,max_iter=2700)
 params = [{'alpha': [10**-4,10**-3, 10**-2,10**-1, 10**0,10**1, 10**2,10**3,10**4]}]
clf = RandomizedSearchCV(reg_case, params, cv=4, scoring='neg_root_mean_squared_error',return_train_score=True)
 search=clf.fit(X_train_cases, y_train_cases)
 results = pd.DataFrame.from_dict(clf.cv_results_ )
matches = test_data_df.groupby(['matchId', 'groupId'])['matchId'].agg('mean' ).values<groupby>
best_alpha=1000
best_itr=2700
final_reg_case=ElasticNet(random_state=42,alpha=best_alpha,l1_ratio=0.1,max_iter=best_itr)
 final_reg_case.fit(X_train_cases,y_train_cases )
groups = test_data_df.groupby(['matchId', 'groupId'])['groupId'].agg('mean' ).values<drop_column>
pred=final_reg_case.predict(X_test_cases)
 print("The RMSE value",(mean_squared_error(y_test_cases,pred)) **0.5 )
features = features.drop(['matchId'], axis=1 )<normalization>
reg_fatal=ElasticNet(random_state=42,l1_ratio=0.1,max_iter=3800)
 params = [{'alpha': [10**-4,10**-3, 10**-2,10**-1, 10**0,10**1, 10**2,10**3,10**4]}]
clf = RandomizedSearchCV(reg_fatal, params, cv=4, scoring='neg_root_mean_squared_error',return_train_score=True)
 search=clf.fit(X_train_fatal, y_train_fatal)
 results = pd.DataFrame.from_dict(clf.cv_results_ )
test_features = features.values
test_features = scaler.transform(test_features )<predict_on_test>
best_alpha=100
best_iter=3800
final_reg_fatal = ElasticNet(random_state=42,alpha=best_alpha,l1_ratio=0.1,max_iter=best_iter)
 final_reg_fatal.fit(X_train_fatal, y_train_fatal )
predictions = model.predict(test_features )<groupby>
pred=final_reg_fatal.predict(X_test_fatal)
 print("The RMSE value",(mean_squared_error(y_test_fatal,pred)) **0.5 )
features['winPlacePercPred'] = predictions
features['matchId'] = matches
features['groupId'] = groups
group_preds = features.groupby(['matchId', 'groupId'])['winPlacePercPred'].agg('mean' ).groupby(['matchId'] ).rank(pct=True )<sort_values>
featured=pd.DataFrame(data)
 X_y_f=shuffle(featured)
 y_cases_f=X_y_f['target_infection']
y_fatal_f=X_y_f['target_fatal']
X_f=X_y_f.drop(['target_infection','target_fatal'],axis=1)
 X_train_cases_f, X_test_cases_f, y_train_cases_f, y_test_cases_f = train_test_split(X_f, y_cases_f, test_size=0.33)
 X_train_fatal_f, X_test_fatal_f, y_train_fatal_f, y_test_fatal_f = train_test_split(X_f, y_fatal_f, test_size=0.33)
 print("Shape of featurized infection train dataset:",(X_train_cases_f.shape,y_train_cases_f.shape))
print("Shape of featurized infection test dataset:",(X_test_cases_f.shape,y_test_cases_f.shape))
print("Shape of featurized fatal train dataset:",(X_train_fatal_f.shape,y_train_fatal_f.shape))
print("Shape of featurized fatal test dataset:",(X_test_fatal_f.shape,y_test_fatal_f.shape))
test_data_df = test_data_df.sort_values(['matchId', 'groupId'] )<define_variables>
reg_case_f=ElasticNet(random_state=42,l1_ratio=0.1,max_iter=4500)
 params = [{'alpha': [10**-4,10**-3, 10**-2,10**-1, 10**0,10**1, 10**2,10**3,10**4]}]
clf_f= RandomizedSearchCV(reg_case_f, params, cv=4, scoring='neg_root_mean_squared_error',return_train_score=True)
 search_f=clf_f.fit(X_train_cases_f, y_train_cases_f)
 results_f = pd.DataFrame.from_dict(clf_f.cv_results_ )
dictionary = dict(zip(features['groupId'].values, group_preds.values))<prepare_output>
best_alpha=10000
best_itr=4500
final_reg_case_f=ElasticNet(random_state=42,alpha=best_alpha,l1_ratio=0.1,max_iter=best_itr)
 final_reg_case_f.fit(X_train_cases_f,y_train_cases_f )
new_ranking_preds = []
for i in test_data_df['groupId'].values:
new_ranking_preds.append(dictionary[i])
 test_data_df['winPlacePercPred'] = new_ranking_preds<prepare_output>
pred_f=final_reg_case_f.predict(X_test_cases_f)
 print("RMSE is:",(mean_squared_error(y_test_cases_f,pred_f)) **0.5 )
predictions = pd.DataFrame(np.transpose(np.array([test_data_df.loc[:, 'Id'], test_data_df['winPlacePercPred']])))
 predictions.columns = ['Id', 'winPlacePerc']
predictions['Id'] = np.int32(predictions['Id'])
 predictions = predictions.sort_values(by=['Id'] )<sort_values>
reg_fatal_f=ElasticNet(random_state=42,alpha=best_alpha,l1_ratio=0.1,max_iter=2500)
 params = [{'alpha': [10**-4,10**-3, 10**-2,10**-1, 10**0,10**1, 10**2,10**3,10**4]}]
clf_f= RandomizedSearchCV(reg_fatal_f, params, cv=4, scoring='neg_root_mean_squared_error',return_train_score=True)
 search_f=clf_f.fit(X_train_fatal_f, y_train_fatal_f)
 results_f = pd.DataFrame.from_dict(clf_f.cv_results_ )
maxPlaces = test_data_df.sort_values(by=['Id'])['maxPlace'].values
numGroups = test_data_df.sort_values(by=['Id'])['numGroups'].values
new_predictions = predictions['winPlacePerc'].values
for i in range(0, len(test_data_df)) :
if maxPlaces[i] == 0:
new_predictions[i] = 0.0
if maxPlaces[i] == 1:
new_predictions[i] = 1.0
else:
gap = 1.0 /(maxPlaces[i] - 1.0)
 new_predictions[i] = round(new_predictions[i]/gap)*gap<prepare_output>
best_alpha=100
best_itr=2700
final_reg_fatal_f=ElasticNet(random_state=42,alpha=best_alpha,l1_ratio=0.1,max_iter=best_itr)
 final_reg_fatal_f.fit(X_train_fatal_f,y_train_fatal_f )
predictions['winPlacePerc'] = new_predictions<save_to_csv>
pred_f=final_reg_fatal_f.predict(X_test_fatal_f)
 print("RMSE is:",(mean_squared_error(y_test_fatal_f,pred_f)) **0.5 )
predictions.to_csv('PUBG_preds.csv', index=False )<count_unique_values>
test["Province_State"].fillna("state", inplace = True)
 test["Country_Region"] = [country_name.replace("'","")for country_name in test["Country_Region"]]
matchcount = train.matchId.nunique()
print("Number of unique matches: {}".format(train.matchId.nunique()))
print("Train Shape Before: {} Rows, {} Cols".format(*train.shape))
train = train.loc[train.matchId.isin(sorted(train.matchId.unique())[int(matchcount* 0.50):]),:]
print("Train Shape After: {} Rows, {} Cols".format(*train.shape))<categorify>
test['ConfirmedCases']=list(map(int,predicted_case))
test['Fatalities']=list(map(int,predicted_fatal))
lbl = preprocessing.LabelEncoder()
for col in ['matchType']:
lbl.fit(train[col])
 train[col] = lbl.transform(train[col])
 test[col] = lbl.transform(test[col] )<count_unique_values>
submission=test[['ForecastId','ConfirmedCases','Fatalities']]
submission=shuffle(submission)
 submission.to_csv("submission.csv",index=False )