Kaggle competition: Jane Street Market Prediction
countries=train['Country_Region'].unique()
for country in countries:
country_df=train[train['Country_Region']==country]
provinces=country_df['Province_State'].unique()
for province in provinces:
train_df=country_df[country_df['Province_State']==province]
train_df.pop('Id')
 x=train_df[['Province_State','Country_Region','Day_of_Week','Month','Day','Day_of_Year','Week_of_Year','Quarter']]
x['Country_Region']=l1.transform(x['Country_Region'])
 x['Province_State']=l2.transform(x['Province_State'])
 y1=train_df[['ConfirmedCases']]
y2=train_df[['Fatalities']]
model_1=DecisionTreeClassifier()
model_2=DecisionTreeClassifier()
model_1.fit(x,y1)
 model_2.fit(x,y2)
 test_df=test.query('Province_State==@province & Country_Region==@country')
 test_id=test_df['ForecastId'].values.tolist()
test_df.pop('ForecastId')
 test_x=test_df[['Province_State','Country_Region','Day_of_Week','Month','Day','Day_of_Year','Week_of_Year','Quarter']]
test_x['Country_Region']=l1.transform(test_x['Country_Region'])
 test_x['Province_State']=l2.transform(test_x['Province_State'])
 test_y1=model_1.predict(test_x)
 test_y2=model_2.predict(test_x)
 test_res=pd.DataFrame(columns=submission.columns)
 test_res['ForecastId']=test_id
test_res['ConfirmedCases']=test_y1
test_res['Fatalities']=test_y2
submission=submission.append(test_res )<save_to_csv>
import pandas as pd
import gc
from sklearn.preprocessing import MinMaxScaler
from sklearn.model_selection import StratifiedKFold
lcc=submission["ConfirmedCases"]
lf=submission["Fatalities"]
buscc = output["ConfirmedCases"]
busf = output["Fatalities"]
sdfcc = sub_df["ConfirmedCases"]
sdff = sub_df["Fatalities"]
sub_df["ConfirmedCases"] = 0.1 * buscc.values + 0.9 * sdfcc.values
sub_df["Fatalities"] = 0.1 * busf.values + 0.9 * sdff.values
sub_df.to_csv('submission.csv',index=False )<set_options>
train = pd.read_csv('/kaggle/input/jane-street-market-prediction/train.csv')
 train = reduce_mem_usage(train)
 features = [c for c in train.columns if 'feature' in c]
train = train.astype({c: np.float32 for c in train.select_dtypes(include='float16' ).columns})
 train = train.fillna(train.mean())
 f_mean = np.mean(train[features[1:]].values,axis=0)
 train = train.query('date > 85' ).reset_index(drop = True)
 train = train[train.weight != 0]
warnings.filterwarnings('ignore')
<load_from_csv>
n_folds = 5
seed = 42
ScaleData = False
if(ScaleData):
scaler = MinMaxScaler(feature_range =(0, 1)).fit(train[features])
 train.loc[:,features] = scaler.transform(train[features])
 X = train[features]
y =(train['resp'].values > 0 ).astype(int)
 X_train = train[train['date'] < 400][features]
X_test = train[train['date'] >= 400][features]
train['action'] =(( train['weight'].values * train['resp'].values)> 0 ).astype('int')
 y_train = train[train['date'] < 400]['action']
y_test = train[train['date'] >= 400]['action']
X_train = train[features]
y_train = train['action']
test = pd.read_csv(".. /input/covid19-global-forecasting-week-4/test.csv")
 train = pd.read_csv(".. /input/covid19-global-forecasting-week-4/train.csv")
 df_pop = pd.read_csv('.. /input/population-by-country-2020/population_by_country_2020.csv')
 covid_pop = pd.read_csv('.. /input/covid19-global-forecasting-locations-population/locations_population.csv' )<count_unique_values>
unique, counts = np.unique(y_test, return_counts=True)
 dict(zip(unique, counts))
print("Max Date: ", max(train['Date']), ", Min Date: ", min(train['Date']))
print("Max Id: ", max(train['Id']), ", Unique Ids: ", train['Id'].nunique())
 print("Max Date: ", max(test['Date']), ", Min Date: ", min(test['Date']))<count_missing_values>
NUM_ROUND = 2000
params = {
'objective': 'cross_entropy',
"metric": "auc",
"random_state": seed,
"learning_rate": 0.05,
"num_leaves": 511,
"feature_fraction": 0.8,
"min_data_in_leaf": 20,
"min_data_per_group": 90,
"lambda_l1": 20,
"lambda_l2": 5,
"device": "gpu"
}
trainData = lgb.Dataset(X, label = y, free_raw_data = False)
 model = lgb.train(params, trainData, num_boost_round = NUM_ROUND, 
)
<count_missing_values><EOS> 
janestreet.competition.make_env.__called__ = False
env = janestreet.make_env()
iter_test = env.iter_test()
for(test_df, sample_prediction_df)in tqdm(iter_test):
if(test_df.iloc[0].weight > 0):
if(ScaleData):
test_df[features] = scaler.transform(test_df[features])
 test_np = test_df.loc[:, features].values
if np.isnan(test_np[:, 1:].sum()):
test_np[:, 1:] = np.nan_to_num(test_np[:, 1:])+ np.isnan(test_np[:, 1:])* f_mean
test_df[features] = test_np
action = model.predict(test_df[features])[0]
if(action > 0.5):
sample_prediction_df.action = 1
else:
sample_prediction_df.action = 0
else:
sample_prediction_df.action = 0
env.predict(sample_prediction_df)