Kaggle competition: San Francisco Crime Classification
dft_hot = pd.concat([dft_hot, pd.DataFrame(encoded, columns = ['Emb'])], axis=1)
 dft_hot = dft_hot.drop(['Embarked'], axis=1 )<categorify>
%matplotlib inline
dft_hot = dft_hot[['SibSp', 'Parch', 'Fare', 'Emb', 'Cabin', 'Age',
'female', 'male', 'Pclass1', 'Pclass2', 'Pclass3']]<drop_column>
train = pd.read_csv('/kaggle/input/sf-crime/train.csv.zip', parse_dates= ['Dates'])
 test = pd.read_csv('/kaggle/input/sf-crime/test.csv.zip', parse_dates= ['Dates'], index_col = 'Id' )
dft_hot = dft_hot.drop(['Cabin'], axis=1 )<define_variables>
x = list(test)
 t_data= train[x]
join = pd.concat([t_data, test])
 merge = join.copy()
train_cols = list(dft_hot)
<create_dataframe>
t= train['Category']
merge.isnull().sum()
dft_i = pd.DataFrame(KNN(k=5 ).fit_transform(dft_hot))
dft_i.columns = train_cols<prepare_x_and_y>
LB = LabelEncoder()
tar = LB.fit_transform(t)
 print(LB.classes_ )
X_test = np.asarray(dft_i )<normalization>
date = pd.to_datetime(join['Dates'])
 merge['Date'] = date.dt.date
merge['Year'] = date.dt.year
merge['Month'] = date.dt.month
merge['Day'] = date.dt.day
merge['Hour'] = date.dt.hour
merge.drop('Dates', axis = 1, inplace = True )
X_test = preprocessing.StandardScaler().fit(X_test ).transform(X_test )<predict_on_test>
date = merge.groupby('Date' ).count().iloc[:, 0]
y_pred = cls.predict(X_test )<load_from_csv>
lb= LabelEncoder()
merge['PdDis'] = lb.fit_transform(merge['PdDistrict'])
 sns.countplot(merge['PdDis'] )
dft2 = pd.read_csv(file3 )<concatenate>
merge.drop('PdDistrict', axis = 1, inplace = True )
dft2 = pd.concat([dft2['PassengerId'], pd.DataFrame(y_pred, columns = ['Survived'] ).astype(int)], axis=1 )<save_to_csv>
merge['DayWeek'] = lb.fit_transform(merge['DayOfWeek'])
 merge[['DayOfWeek', 'DayWeek']].head(10 )
dft2.to_csv('titanic_results.csv', index=False )<import_modules>
merge.drop('DayOfWeek', axis = 1, inplace = True )
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from scipy.optimize import curve_fit
from statsmodels.tsa.statespace.sarimax import SARIMAX
from statsmodels.tsa.arima_model import ARIMA
from random import random 
<load_from_csv>
merge['Block'] = merge['Address'].str.contains('block', case = False)
 merge['ST'] = merge['Address'].str.contains('ST', case = False)
 merge.drop('Address', axis = 1, inplace = True )
PATH_WEEK2='/kaggle/input/covid19-global-forecasting-week-3'
df_train = pd.read_csv(f'{PATH_WEEK2}/train.csv')
 df_test = pd.read_csv(f'{PATH_WEEK2}/test.csv')
 df_train.head()
df_test.head()
df_train.rename(columns={'Country_Region':'Country'}, inplace=True)
 df_test.rename(columns={'Country_Region':'Country'}, inplace=True)
 df_train.rename(columns={'Province_State':'State'}, inplace=True)
 df_test.rename(columns={'Province_State':'State'}, inplace=True)
 df_train['Date'] = pd.to_datetime(df_train['Date'], infer_datetime_format=True)
 df_test['Date'] = pd.to_datetime(df_test['Date'], infer_datetime_format=True)
 df_train.info()
df_test.info()
y1_Train = df_train.iloc[:, -2]
y1_Train.head()
y2_Train = df_train.iloc[:, -1]
y2_Train.head()
EMPTY_VAL = "EMPTY_VAL"
def fillState(state, country):
if state == EMPTY_VAL: return country
return state<data_type_conversions>
medX= merge[merge['X'] < -120.5]['X'].median()
medY = merge[merge['Y'] < 90]['Y'].median()
merge.loc[merge['X'] >= -120.5, 'X'] = medX
merge.loc[merge['Y'] >= 90, 'Y'] = medY
X_Train = df_train.copy()
X_Train['State'].fillna(EMPTY_VAL, inplace=True)
 X_Train['State'] = X_Train.loc[:, ['State', 'Country']].apply(lambda x : fillState(x['State'], x['Country']), axis=1)
 X_Train.loc[:, 'Date'] = X_Train.Date.dt.strftime("%m%d")
 X_Train["Date"] = X_Train["Date"].astype(int)
 X_Train.head()
X_Test = df_test.copy()
X_Test['State'].fillna(EMPTY_VAL, inplace=True)
 X_Test['State'] = X_Test.loc[:, ['State', 'Country']].apply(lambda x : fillState(x['State'], x['Country']), axis=1)
 X_Test.loc[:, 'Date'] = X_Test.Date.dt.strftime("%m%d")
 X_Test["Date"] = X_Test["Date"].astype(int)
 X_Test.head()<categorify>
merge['X+Y'] = merge['X'] + merge['Y']
merge['X-Y'] = merge['X'] - merge['Y']
le = preprocessing.LabelEncoder()
X_Train.Country = le.fit_transform(X_Train.Country)
 X_Train['State'] = le.fit_transform(X_Train['State'])
 X_Train.head()
X_Test.Country = le.fit_transform(X_Test.Country)
 X_Test['State'] = le.fit_transform(X_Test['State'])
 X_Test.head()
df_train.head()
df_train.loc[df_train.Country == 'Afghanistan', :]
df_test.tail()<categorify>
merge.drop('Date', axis = 1, inplace = True )
filterwarnings('ignore')
 le = preprocessing.LabelEncoder()
countries = X_Train.Country.unique()
df_out = pd.DataFrame({'ForecastId': [], 'ConfirmedCases': [], 'Fatalities': []})
 for country in countries:
states = X_Train.loc[X_Train.Country == country, :].State.unique()
for state in states:
X_Train_CS = X_Train.loc[(X_Train.Country == country)&(X_Train.State == state), ['State', 'Country', 'Date', 'ConfirmedCases', 'Fatalities']]
y1_Train_CS = X_Train_CS.loc[:, 'ConfirmedCases']
y2_Train_CS = X_Train_CS.loc[:, 'Fatalities']
X_Train_CS = X_Train_CS.loc[:, ['State', 'Country', 'Date']]
X_Train_CS.Country = le.fit_transform(X_Train_CS.Country)
 X_Train_CS['State'] = le.fit_transform(X_Train_CS['State'])
 X_Test_CS = X_Test.loc[(X_Test.Country == country)&(X_Test.State == state), ['State', 'Country', 'Date', 'ForecastId']]
X_Test_CS_Id = X_Test_CS.loc[:, 'ForecastId']
X_Test_CS = X_Test_CS.loc[:, ['State', 'Country', 'Date']]
X_Test_CS.Country = le.fit_transform(X_Test_CS.Country)
 X_Test_CS['State'] = le.fit_transform(X_Test_CS['State'])
 model1 = XGBRegressor(n_estimators=1000)
 model1.fit(X_Train_CS, y1_Train_CS)
 y1_pred = model1.predict(X_Test_CS)
 model2 = XGBRegressor(n_estimators=1000)
 model2.fit(X_Train_CS, y2_Train_CS)
 y2_pred = model2.predict(X_Test_CS)
 df = pd.DataFrame({'ForecastId': X_Test_CS_Id, 'ConfirmedCases': y1_pred, 'Fatalities': y2_pred})
 df_out = pd.concat([df_out, df], axis=0)
<save_to_csv>
data_train = merge[:train.shape[0]]
data_test = merge[train.shape[0]:]
df_out.ForecastId = df_out.ForecastId.astype('int')
 df_out.tail()
df_out.to_csv('submission.csv', index=False )<set_options>
trains = lg.Dataset(data_train, label = tar, categorical_feature=['PdDis', 'DayWeek'])
 params = {
'boosting':'gbdt',
'objective':'multiclass',
'num_class':39,
'max_delta_step':0.9,
'min_data_in_leaf': 20,
'learning_rate': 0.4,
'max_bin': 480,
'num_leaves': 45,
'verbose' : 1
}
bst = lg.train(params, trains, 120 )
start_time = time.time()
%matplotlib inline 
<install_modules>
prediction = bst.predict(data_test )
<import_modules><EOS> 
result1 = pd.DataFrame(prediction, columns = LB.inverse_transform(np.linspace(0, 38, 39, dtype='int16')) ,index=data_test.index)
 result1.head()
result1.to_csv('final_submission', index_label = 'Id' )