<jupyter_start><jupyter_text>CV TRAIN<jupyter_code>hyper_params = {
    'task': 'train',
    'boosting_type': 'gbdt',
    'objective': 'regression',
    'metric': ['l1','l2'],
    'learning_rate': 0.005,
    'feature_fraction': 0.9,
    'bagging_fraction': 0.7,
    'bagging_freq': 10,
    'verbose': 0,
    "max_depth": 8,
    "num_leaves": 128,  
    "max_bin": 512,
    "num_iterations": 15000
}


# CROSS VALIDATION

name='lgbm'+version


score_df = pd.DataFrame()
oof = list()
predictions = list()

oof_x, oof_y = np.zeros(data.shape[0]), np.zeros(data.shape[0])
preds_x, preds_y = 0, 0

scores_per_fold=[]

all_preds=[]
all_targets=[]

np.random.seed(42)
sh=np.random.permutation(unpaths)


k=5
val_size=int(len(sh)/k)
for f in range(k):
    print('fold:', f)

    if f<(k-1):
        val_paths=sh[val_size*f:val_size*(f+1)]
    else:
        val_paths=sh[val_size*f:]

    train_paths=[x for x in unpaths if x not in val_paths]
    
    val_paths=np.array(val_paths)
    train_paths=np.array(train_paths)
    
    X_train = data.loc[data.path.isin(train_paths), TRAIN_FEATS ].reset_index(drop=True)

    y_trainXY =  data.loc[data.path.isin(train_paths), ['x', 'y'] ].reset_index(drop=True).values

    X_val = data.loc[data.path.isin(val_paths), TRAIN_FEATS ].reset_index(drop=True)
    y_valXY = data.loc[data.path.isin(val_paths), ['x', 'y'] ].reset_index(drop=True).values

    val_predsXY=[]
    for i in range(2):
        y_train = y_trainXY[:,i]
        y_val = y_valXY[:,i]

        gbm = lgb.LGBMRegressor(**hyper_params)
        model = gbm.fit(X_train, y_train,
            eval_set=[(X_train, y_train), (X_val, y_val)],
            eval_metric='l1',
            verbose=100,
            early_stopping_rounds=200)
        
        joblib.dump(model, weights_path+'fold_'+str(f)+['x','y'][i]+'_v2')
        
        val_predsXY.append(gbm.predict(X_val, num_iteration=gbm.best_iteration_) )
    val_predsXY=np.stack(val_predsXY).T
    
    print(euclidean_distance(val_predsXY[:,0], val_predsXY[:,1], y_valXY[:,0], y_valXY[:,1]))

    all_preds.append(val_predsXY)
    all_targets.append(y_valXY)
        

all_predsV=np.vstack(all_preds)
all_targetsV=np.vstack(all_targets)    

print('mean validation error, 5-fold', euclidean_distance(all_targetsV[:,0], all_targetsV[:,1], all_predsV[:,0], all_predsV[:,1]))
pd.DataFrame(all_predsV,columns=['x', 'y']).to_csv('/gdrive/My Drive/Colab Notebooks/lgbm_oof.csv.gz', index=False, compression='gzip')
pd.DataFrame(all_targetsV,columns=['x', 'y']).to_csv('/gdrive/My Drive/Colab Notebooks/all_targetsV.csv.gz', index=False, compression='gzip')<jupyter_output><empty_output><jupyter_text>EVALUATION - PLOTS<jupyter_code>import matplotlib.pyplot as plt
plt.plot(all_predsV[:,0],all_predsV[:,1],'.')
plt.show()<jupyter_output><empty_output><jupyter_text>**5 FOLD CV PREDICTIONS OVER TARGET GRID**  With blue dots are the true checkpoints  Orange dots for predictions<jupyter_code>plt.figure(figsize=(10,10))
plt.plot(all_targetsV[:,0],all_targetsV[:,1],'.')
plt.plot(all_predsV[:,0],all_predsV[:,1],'.')
plt.show()
print('mean validation error, 5-fold', euclidean_distance(all_targetsV[:,0], all_targetsV[:,1], all_predsV[:,0], all_predsV[:,1]))<jupyter_output><empty_output><jupyter_text>Full train<jupyter_code>hyper_params["num_iterations"]= 2000


name=version+'KNeighbors'

train_paths=unpaths[:]

train_paths=np.array(train_paths)

X_train = data.loc[data.path.isin(train_paths), TRAIN_FEATS ].reset_index(drop=True)

y_trainXY =  data.loc[data.path.isin(train_paths), ['x', 'y'] ].reset_index(drop=True).values

train_predsXY=[]
all_train_targets=[]
for i in range(2):
    y_train = y_trainXY[:,i]

    gbm = lgb.LGBMRegressor(**hyper_params)


    model = gbm.fit(X_train, y_train,
        eval_set=[(X_train, y_train)],
        eval_metric='l1',
        verbose=100
        )
    
    joblib.dump(model, weights_path+'FullTrain_'+['x','y'][i])
    
    train_predsXY.append(gbm.predict(X_train, num_iteration=gbm.best_iteration_) )

train_predsXY=np.stack(train_predsXY).T

print('train error',euclidean_distance(train_predsXY[:,0], train_predsXY[:,1], y_trainXY[:,0], y_trainXY[:,1]))

print('Save LightGBM model fully trained')
joblib.dump(model,'/gdrive/My Drive/Colab Notebooks/weights/lgbm_model')<jupyter_output><empty_output>
StarcoderdataJupyterStructuredCleanDedup