Kaggle competition: Jane Street Market Prediction
def create_transformation_matrix_to_voxel_space(shape, voxel_size, offset):

 shape, voxel_size, offset = np.array(shape), np.array(voxel_size), np.array(offset)
 tm = np.eye(4, dtype=np.float32)
 translation = shape/2 + offset/voxel_size
tm = tm * np.array(np.hstack(( 1/voxel_size, [1])))
 tm[:3, 3] = np.transpose(translation)
 return tm
def transform_points(points, transf_matrix):

 if points.shape[0] not in [3,4]:
raise Exception("Points input should be(3,N)or(4,N)shape, received {}".format(points.shape))
return transf_matrix.dot(np.vstack(( points[:3, :], np.ones(points.shape[1])))) [:3, :]
def car_to_voxel_coords(points, shape, voxel_size, z_offset=0):
if len(shape)!= 3:
raise Exception("Voxel volume shape should be 3 dimensions(x,y,z)")
 if len(points.shape)!= 2 or points.shape[0] not in [3, 4]:
raise Exception("Input points should be(3,N)or(4,N)in shape, found {}".format(points.shape))
tm = create_transformation_matrix_to_voxel_space(shape, voxel_size,(0, 0, z_offset))
p = transform_points(points, tm)
 return p
def create_voxel_pointcloud(points, shape, voxel_size=(0.5,0.5,1), z_offset=0):
points_voxel_coords = car_to_voxel_coords(points.copy() , shape, voxel_size, z_offset)
 points_voxel_coords = points_voxel_coords[:3].transpose(1,0)
 points_voxel_coords = np.int0(points_voxel_coords)
 bev = np.zeros(shape, dtype=np.float32)
 bev_shape = np.array(shape)
 within_bounds =(np.all(points_voxel_coords >= 0, axis=1)* np.all(points_voxel_coords < bev_shape, axis=1))
points_voxel_coords = points_voxel_coords[within_bounds]
coord, count = np.unique(points_voxel_coords, axis=0, return_counts=True)
 bev[coord[:,1], coord[:,0], coord[:,2]] = count
return bev
def normalize_voxel_intensities(bev, max_intensity=16):
return(bev/max_intensity ).clip(0,1)
<prepare_output>
def concat_respx_pred(X,models):
y_multi_pre = np.stack([ models[f'{respx}'].predict_proba(X)[:,1] for respx in models if respx != 'lr'] ).T
return y_multi_pre
def model_predict_slow(X,models):
y_multi_pre = concat_respx_pred(X,models)
 y = models['lr'].predict(y_multi_pre)
 return y.astype(int)
 def model_predict_fast(X,models):
y = models['resp'].predict(X)
 return y.astype(int )
sub = pd.DataFrame(list(sub.items()))
sub.columns = sample_sub.columns
sub.head()<save_to_csv>
with open('.. /input/models/lgb_model_ensemble_0208_01.pkl', 'rb')as f:
models = pickle.load(f)
<install_modules><EOS> 
env = janestreet.make_env()
iter_test = env.iter_test()
model_predict = model_predict_slow
features = [f'feature_{x}' for x in range(130)]
for(test_df, sample_prediction_df)in tqdm(iter_test):
if test_df['weight'].values[0] < 0.001:
y_preds = 0
else:
X_test = test_df.loc[:, features].fillna(NANFILL ).values
y_preds = model_predict(X_test,models)
 sample_prediction_df.action = y_preds
env.predict(sample_prediction_df )