Kaggle competition: San Francisco Crime Classification
i = 0
k = 30
for date in dates:
k = k-1
i = i+1
test.loc[(test['Country_Region']=='China')&(test['Province_State']!='Hubei')&(test['Date']==date),
'Fatalities_x']= last_fat.values
test.loc[(test['Country_Region']=='China')&(test['Province_State']!='Hubei')&(test['Date']==date),
'ConfirmedCases_x']= last_amount.values + i<filter>
train = pd.read_csv('.. /input/train.csv', parse_dates=['Dates'])
 test = pd.read_csv('.. /input/test.csv', parse_dates=['Dates'], index_col='Id' )
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>
train.isnull().sum()
k=30
i=0
for date in dates:
k = k-1
i = i+1
test.loc[(test['Country_Region']=='China')&(test['Province_State']=='Hubei')&(test['Date']==date),'ConfirmedCases_x']= last_amount.values[0]
test.loc[(test['Country_Region']=='China')&(test['Province_State']=='Hubei')&(test['Date']==date),'Fatalities_x']= last_fat.values[0] + i<prepare_output>
train.isnull().sum()
sub = test[['ForecastId','ConfirmedCases_x','Fatalities_x']]<rename_columns>
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 )
sub.columns=['ForecastId','ConfirmedCases','Fatalities']<feature_engineering>
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 )
sub.loc[sub['ConfirmedCases']<0,'ConfirmedCases']=0<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'] )
<save_to_csv><EOS> 
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': 21,
'learning_rate': 0.4,
'max_bin': 465,
'num_leaves': 41,
'verbose' : 1}
bst = lgb.train(params, train_data, 120)
 predictions = bst.predict(test)
 submission = pd.DataFrame(predictions, columns=le2.inverse_transform(np.linspace(0, 38, 39, dtype='int16')) , index=test.index)
 submission.to_csv('LGBM_final.csv', index_label='Id' )