Kaggle competition: San Francisco Crime Classification
test[test['Country_Region']=='Italy']<load_from_csv>
train_sub = pd.read_csv(".. /input/covid19-global-forecasting-week-2/train.csv" )<merge>
pd.options.display.max_columns=100
train = pd.read_csv('.. /input/train.csv', parse_dates=['Dates'])
 test = pd.read_csv('.. /input/test.csv', parse_dates=['Dates'], index_col='Id')
 def feature_engineering(data):
data['Date'] = pd.to_datetime(data['Dates'].dt.date)
 data['n_days'] =(data['Date'] - data['Date'].min() ).apply(lambda x: x.days)
 data['Day'] = data['Dates'].dt.day
data['DayOfWeek'] = data['Dates'].dt.weekday
data['Month'] = data['Dates'].dt.month
data['Year'] = data['Dates'].dt.year
data['Hour'] = data['Dates'].dt.hour
data['Minute'] = data['Dates'].dt.minute
data['Block'] = data['Address'].str.contains('block', case=False ).apply(lambda x: 1 if x == True else 0)
 data["X_Y"] = data["X"] - data["Y"]
data["XY"] = data["X"] + data["Y"]
data.drop(columns=['Dates','Date','Address'], inplace=True)
 return data
train = feature_engineering(train)
 test = feature_engineering(test)
 train.drop(columns=['Descript','Resolution'], inplace=True )
test = pd.merge(test,train_sub[['Province_State','Country_Region',
'Date','ConfirmedCases',
'Fatalities']],
on=['Province_State','Country_Region',
'Date'], how='left' )<filter>
for i in train,test:
data = i
coord = data[['X','Y']]
pca = PCA(n_components=2)
 pca.fit(coord)
 coord_pca = pca.transform(coord)
 data['coord_pca1'] = coord_pca[:, 0]
data['coord_pca2'] = coord_pca[:, 1]
test.loc[test['ConfirmedCases_x'].isnull() ==True]<feature_engineering>
test.loc[test['ConfirmedCases_x'].isnull() ==True, 'ConfirmedCases_x'] =test.loc[test['ConfirmedCases_x'].isnull() ==True, 'ConfirmedCases_y']<feature_engineering>
le1 = LabelEncoder()
train['PdDistrict'] = le1.fit_transform(train['PdDistrict'])
 test['PdDistrict'] = le1.transform(test['PdDistrict'])
 le2 = LabelEncoder()
X = train.drop(columns=['Category'])
 y= le2.fit_transform(train['Category'] )
test.loc[test['Fatalities_x'].isnull() ==True, 'Fatalities_x'] = test.loc[test['Fatalities_x'].isnull() ==True, 'Fatalities_y']<filter>
last_amount = test.loc[(test['Country_Region']=='Italy')&(test['Date']=='2020-03-31'),'ConfirmedCases_x']<filter>
last_fat = test.loc[(test['Country_Region']=='Italy')&(test['Date']=='2020-03-31'),'Fatalities_x']<define_variables>
train_data = lgb.Dataset(X, label=y, categorical_feature=['PdDistrict', ])
 params = {'boosting':'gbdt',
'objective':'multiclass',
'num_class':39,
'max_delta_step':0.9,
'min_data_in_leaf': 20,
'learning_rate': 0.29,
'max_bin': 501,
'num_leaves': 41,
'verbose' : 1}
bst = lgb.train(params, train_data, 120)
 predictions_lgb = bst.predict(test)
i = 0
k = 30<filter>
train_data = lgb.Dataset(X, label=y, categorical_feature=['PdDistrict', ])
 params = {'boosting':'gbdt',
'objective':'multiclass',
'num_class':39,
'max_delta_step':0.9,
'min_data_in_leaf': 4,
'learning_rate': 0.29,
'max_bin': 501,
'num_leaves': 41,
'verbose' : 1}
bst = lgb.train(params, train_data, 120)
 predictions_lgb1 = bst.predict(test)
test.loc[(test['Country_Region']=='Italy')]<feature_engineering>
con2 =(predictions_lgb + predictions_lgb1)/2
con2
for date in dates:
k = k-1
i = i+1
test.loc[(test['Country_Region']=='Italy')&(test['Date']==date),
'ConfirmedCases_x']=last_amount.values[0] + i*(5000-(100*i))
test.loc[(test['Country_Region']=='Italy')&(test['Date']==date),
'Fatalities_x'] = last_fat.values[0]+i*(800-(10*i))<filter>
test.loc[(test['Country_Region']=='Italy')]<define_variables>
last_amount = test.loc[(test['Country_Region']=='China')&(test['Province_State']!='Hubei')&(test['Date']=='2020-03-31'),'ConfirmedCases_x']
last_fat = test.loc[(test['Country_Region']=='China')&(test['Province_State']!='Hubei')&(test['Date']=='2020-03-31'),'Fatalities_x']<feature_engineering>
submission = pd.DataFrame(con2, columns=le2.inverse_transform(np.linspace(0, 38, 39, dtype='int16')) ,
index=test.index)
 submission.to_csv('submission.csv', index='Id' )