<jupyter_start><jupyter_text>Prepare folding scheme (do i need it ?) and load the pre-trained metafeatures<jupyter_code># Get the K fold indexes
n_folds =6
kf = KFold(n_splits=n_folds, shuffle=False, random_state=156)

with open("../models/test_features_rougher.pkl", "rb") as f:
    test_rougher = pickle.load(f)
with open("../models/train_features_rougher.pkl", "rb") as f:
    train_rougher = pickle.load(f)
with open("../models/test_features_final.pkl", "rb") as f:
    test_final = pickle.load(f)
with open("../models/train_features_final.pkl", "rb") as f:
    train_final = pickle.load(f)<jupyter_output><empty_output><jupyter_text>Define the N features for which to pull the level0 features for; separately for rougher & final<jupyter_code>N_final = [50,150,250,350,450]
N_rougher = [50,150,250]
   
# Retrieve rougher predictions:
level0_rougher_train = np.hstack([train_rougher[Nr] for Nr in N_rougher])
level0_final_train = np.hstack([train_final[Nf] for Nf in N_final])

level0_rougher_test = np.hstack([test_rougher[Nr] for Nr in N_rougher])
level0_final_test = np.hstack([test_final[Nf] for Nf in N_final])
tgt = 'rougher.output.recovery'
predictions = level0_rougher_train
yTrue = y[tgt].values
print(f'Shape y : {yTrue.shape}, preds : {predictions.shape}' )

r= mase(level0_rougher_train.mean(axis = 1),y['rougher.output.recovery'].values)
f=mase(level0_final_train.mean(axis = 1),y['final.output.recovery'].values)
print(f'R {r} F {f} sum = {r*0.25+0.75*f}')
#predictions.mean(axis = 1)*r+<jupyter_output><empty_output><jupyter_text>Run an optimization with constraints on positive weights<jupyter_code>from scipy.optimize import minimize

def f(weights):
    valid_preds_r = np.average(predictions_r, axis=1, weights=weights).reshape((-1,1))
    valid_preds_f = np.average(predictions_f, axis=1, weights=weights).reshape((-1,1))

    return np.hstack([valid_preds_r,valid_preds_f])

def loss_function(weights,C = 0.7):
    y_valid_pred = f(weights)
    sc = 0.25*mase(y_valid_pred[:,0],y['rougher.output.recovery'].values) + 0.75*mase(y_valid_pred[:,1],y['final.output.recovery'].values) + C*(weights**2).sum()
    #print('loss', sc, 'current weights', weights)
    return sc

opt_weights = minimize(loss_function,
                                [1/predictions_r.shape[1]] * predictions_r.shape[1],
                                constraints=({'type': 'eq','fun': lambda w: 1-sum(w)}),
                                method= 'SLSQP',
                                bounds=[(0.0, 1.0)] * predictions_r.shape[1],
                                options = {'ftol':1e-10},
                            )['x']

print('Optimum weights = ', opt_weights, 'with loss', loss_function(opt_weights))

def acc_function(weights):
    y_valid_pred = f(weights)
    sc = 0.25*mase(y_valid_pred[:,0],y['rougher.output.recovery'].values) + 0.75*mase(y_valid_pred[:,1],y['final.output.recovery'].values)
    return sc

print('Ensembled Accuracy =', acc_function(opt_weights))

# 0.00000000e+00 1.53911667e-15 4.63643997e-02 0.00000000e+00
# 7.57377707e-02 8.77897830e-01]<jupyter_output>Optimum weights =  [0.09214325 0.08326996 0.07945414 0.0990743  0.08116201 0.06146224
 0.03220819 0.03520512 0.04069526 0.02343084 0.03200855 0.02826872
 0.0393319  0.0419227  0.03373259 0.03186965 0.04191969 0.03413302
 0.01055219 0.0226742  0.010269   0.01671587 0.01688005 0.01161656] with loss 1.7586020102753923
Ensembled Accuracy = 1.7179782225732336<jupyter_text>Make a submission:<jupyter_code>preds = pd.DataFrame(data = {'date':X_test.index,'rougher.output.recovery':level0_rougher_test.dot(opt_weights), 'final.output.recovery':level0_final_test.dot(opt_weights)})
stacked_preds_sub = preds
stacked_preds_sub['date'] = stacked_preds_sub['date'].dt.strftime('%Y-%m-%dT%H:%M:%SZ')
stacked_preds_sub.set_index('date',inplace=True)
#stacked_preds_sub.drop_duplicates(inplace=True)
stacked_preds_sub.to_csv('../results/stacked_sub_lgb_lasso_base_alldata_r_350_f450_C07_weighted.csv')
stacked_preds_sub.plot(style=['o','o'],figsize = (20,10),alpha=0.9)

preds_av = pd.DataFrame(data = {'date':X_test.index,'rougher.output.recovery':level0_testFeatures_rougher.mean(axis=1), 'final.output.recovery':level0_testFeatures_final.mean(axis=1)})

preds_av['date'] = preds_av['date'].dt.strftime('%Y-%m-%dT%H:%M:%SZ')
preds_av.set_index('date',inplace=True)
preds_av.plot(figsize = (20,10),style=['o','o'],alpha=0.9)

preds_av.to_csv('../results/stacked_sub_lgb_lasso_base_alldata_lagdiff5_ts_averaged.csv')
r = np.power(level0_testFeatures_rougher.prod(axis=1),1/level0_testFeatures_rougher.shape[1])
f = np.power(level0_testFeatures_final.prod(axis=1),1/level0_testFeatures_final.shape[1])

preds_av = pd.DataFrame(data = {'date':X_test.index,'rougher.output.recovery':r, 'final.output.recovery':f})
preds_av['date'] = preds_av['date'].dt.strftime('%Y-%m-%dT%H:%M:%SZ')
preds_av.set_index('date',inplace=True)
preds_av.plot(figsize = (20,10),style=['o','o'],alpha=0.9)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.15987963891675025, 'fraction_numerical': 0.08686058174523571, 'mean_word_length': 5.400513478818999, 'pattern_counts': {'":': 0, '<': 16, '<?xml version=': 0, '>': 16, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 2, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '422328', 'license': 'MIT', 'n_tokens_mistral': 2226, 'n_tokens_neox': 1994, 'n_words': 267, 'path': 'notebooks/stack_models_from_N_features-bestsub.ipynb', 'repo_name': 'AntonBiryukovUofC/unearthed-zinc', 'size': '4985'}