Kaggle competition: Coupon Purchase Prediction
train_features_CC = np.array(train_features_CC)
 train_features_F = np.array(train_features_F)
 test_features_CC = np.array(test_features_CC)
 test_features_F = np.array(test_features_F)
 train_target_CC = np.array(train_target_CC)
 train_target_F = np.array(train_target_F)
 test_target_CC = np.array(test_target_CC)
 test_target_F = np.array(test_target_F)
 features_CC = np.array(features_CC)
 features_F = np.array(features_F)
 target_CC = np.array(target_CC)
 target_F = np.array(target_F )<split>
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
from collections import deque
from scipy import sparse
import implicit
from IPython.display import display
import zipfile
from pathlib import Path
import time
import warnings; warnings.simplefilter('ignore' )
random_number = random.randint(0, 10000)
 model_CC = LinearRegression()
train_features_CC, test_features_CC, train_target_CC, test_target_CC = train_test_split(features_CC, target_CC,
test_size = 0.2,
random_state = random_number)
 regressor = model_CC.fit(train_features_CC, train_target_CC)
 prediction_CC = regressor.predict(features_CC )<prepare_output>
df_coupon_list_train = pd.read_csv('.. /input/coupon-purchase-prediction-translated/coupon_list_train_translated.csv')
 df_coupon_detail_train = pd.read_csv('.. /input/coupon-purchase-prediction-translated/coupon_detail_train_translated.csv')
 df_coupon_list_test = pd.read_csv('.. /input/coupon-purchase-prediction-translated/coupon_list_test_translated.csv')
 df_user_list = pd.read_csv('.. /input/coupon-purchase-prediction-translated/user_list_translated.csv')
for country_index in range(country_number):
for day_index in range(len(prediction_CC[country_index])) :
prediction_CC[country_index][day_index] += target_CC[country_index][-1]<split>
df_purchased_coupons_train = df_coupon_detail_train.merge(df_coupon_list_train, on='COUPON_ID_hash', how='inner' )
random_number = random.randint(0, 10000)
 model_F = LinearRegression()
train_features_F, test_features_F, train_target_F, test_target_F = train_test_split(features_F, target_F,
test_size = 0.2,
random_state = random_number)
 regressor_F = model_F.fit(train_features_F, train_target_F)
 prediction_F = regressor_F.predict(features_F )<prepare_output>
features = ['COUPON_ID_hash', 'USER_ID_hash', 'GENRE_NAME', 'DISCOUNT_PRICE', 'large_area_name', 'ken_name', 'small_area_name']
df_purchased_coupons_train = df_purchased_coupons_train[features]
df_purchased_coupons_train
for country_index in range(country_number):
for day_index in range(len(prediction_F[country_index])) :
prediction_F[country_index][day_index] += target_F[country_index][-1]<feature_engineering>
df_coupon_list_test['USER_ID_hash'] = 'dummyuser'
df_coupon_list_test = df_coupon_list_test[features]
df_coupon_list_test
for index in range(len(prediction_CC)) :
for inner_index in range(len(prediction_CC[index])) :
if prediction_CC[index][inner_index] >= 0:
prediction_CC[index][inner_index] = round(prediction_CC[index][inner_index])
 elif prediction_CC[index][inner_index] < 0:
prediction_CC[index][inner_index] = 0<feature_engineering>
df_combined = pd.concat([df_purchased_coupons_train, df_coupon_list_test], axis=0)
 df_combined['DISCOUNT_PRICE'] = 1 / np.log10(df_combined['DISCOUNT_PRICE'])
 features.extend(['DISCOUNT_PRICE'])
 df_combined
for index in range(len(prediction_F)) :
for inner_index in range(len(prediction_F[index])) :
if prediction_F[index][inner_index] >= 0:
prediction_F[index][inner_index] = round(prediction_F[index][inner_index])
 elif prediction_F[index][inner_index] < 0:
prediction_F[index][inner_index] = 0<feature_engineering>
categoricals = ['GENRE_NAME', 'large_area_name', 'ken_name', 'small_area_name']
df_combined_categoricals = df_combined[categoricals]
df_combined_categoricals = pd.get_dummies(df_combined_categoricals, dummy_na=False)
 df_combined_categoricals
for index in range(len(prediction_CC)) :
prediction_CC[index][0] = last_day_CC[index]
for inner_index in range(1, len(prediction_CC[index])) :
prediction_CC[index][inner_index] = prediction_CC[index][inner_index] + prediction_CC[index][inner_index-1]
prediction_CC<feature_engineering>
continuous = list(set(features)- set(categoricals))
df_combined = pd.concat([df_combined[continuous], df_combined_categoricals], axis=1)
 print(df_combined.isna().sum())
 NAN_SUBSTITUTION_VALUE = 1
df_combined = df_combined.fillna(NAN_SUBSTITUTION_VALUE)
 df_combined
for index in range(len(prediction_F)) :
prediction_F[index][0] = last_day_F[index]
for inner_index in range(1, len(prediction_F[index])) :
prediction_F[index][inner_index] = prediction_F[index][inner_index] + prediction_F[index][inner_index-1]
prediction_F<split>
df_train = df_combined[df_combined['USER_ID_hash'] != 'dummyuser']
df_test = df_combined[df_combined['USER_ID_hash'] == 'dummyuser']
df_test = df_test.drop('USER_ID_hash', axis=1)
 display(df_train)
 display(df_test )
target_CC = CC_dataset.iloc[:,-13:]
target_CC = np.array(target_CC)
 target_CC<prepare_x_and_y>
df_train_dropped_coupons = df_train.drop('COUPON_ID_hash', axis=1)
 df_user_profiles = df_train_dropped_coupons.groupby('USER_ID_hash' ).mean()
df_user_profiles
target_F = Fatal_dataset.iloc[:,-13:]
target_F = np.array(target_F)
 target_F<concatenate>
FEATURE_WEIGHTS = {
'GENRE_NAME': 2,
'DISCOUNT_PRICE': 2,
'large_area_name': 0.5,
'ken_name': 1.5,
'small_area_name': 5
}
confirmed_cases = np.concatenate(( target_CC, prediction_CC), axis = 1)
 fatalities = np.concatenate(( target_F, prediction_F), axis = 1 )<load_from_csv>
def find_appropriate_weight(weights_dict, colname):
for col, weight in weights_dict.items() :
if col in colname:
return weight
raise ValueError
submitted_data = pd.read_csv("/kaggle/input/covid19-global-forecasting-week-2/submission.csv" )<define_variables>
W_values = [find_appropriate_weight(FEATURE_WEIGHTS, colname)
 for colname in df_user_profiles.columns]
W = np.diag(W_values)
 W
dim_CC = confirmed_cases.shape[0]*confirmed_cases.shape[1]
dim_F = fatalities.shape[0]*fatalities.shape[1]<prepare_x_and_y>
df_test_only_features = df_test.drop('COUPON_ID_hash', axis=1)
 similarity_scores = np.dot(np.dot(df_user_profiles, W), df_test_only_features.T)
 similarity_scores
confirmed_cases = confirmed_cases.reshape(dim_CC,)
 fatalities = fatalities.reshape(dim_F, )<prepare_output>
s_coupons_ids = df_test['COUPON_ID_hash']
index = df_user_profiles.index
columns = pd.Series([s_coupons_ids[i] for i in range(0, similarity_scores.shape[1])], name='COUPON_ID_hash')
 df_results = pd.DataFrame(index=index, columns=columns, data=similarity_scores)
 df_results
submitted_data['Fatalities'] = fatalities
submitted_data['ConfirmedCases'] = confirmed_cases<save_to_csv>
def get_top10_coupon_hashes_string(row):
sorted_row = row.sort_values()
return ' '.join(sorted_row.index[-10:][::-1].tolist() )
submitted_data.to_csv('submission.csv', index = False, encoding = 'utf-8-sig' )<load_from_csv>
output = df_results.apply(get_top10_coupon_hashes_string, axis=1)
 output
df_train = pd.read_csv("/kaggle/input/covid19-global-forecasting-week-2/train.csv")
 df_test = pd.read_csv("/kaggle/input/covid19-global-forecasting-week-2/test.csv")
 df_sub = pd.read_csv("/kaggle/input/covid19-global-forecasting-week-2/submission.csv" )<data_type_conversions>
df_output = pd.DataFrame(data={'USER_ID_hash': output.index, 'PURCHASED_COUPONS': output.values})
 df_output
<something_strange><EOS> 
df_output_all = pd.merge(df_user_list, df_output, how='left', on='USER_ID_hash')
 df_output_all.to_csv('cosine_sim_python.csv', header=True, index=False, columns=['USER_ID_hash', 'PURCHASED_COUPONS'])
 df_output_all