b = sps.load_npz(dir + matrix_dict["cbf_item_artist"] + "-cat" + str(cat) +
                     ".npz")
    c = sps.load_npz(dir + matrix_dict["nlp"] + "-cat" + str(cat) + ".npz")
    d = sps.load_npz(dir + matrix_dict["rp3beta"] + "-cat" + str(cat) + ".npz")
    e = sps.load_npz(dir + matrix_dict["cf_user"] + "-cat" + str(cat) + ".npz")
    f = sps.load_npz(dir + matrix_dict["slim"] + "-cat" + str(cat) + ".npz")
    g = sps.load_npz(dir + matrix_dict["cbf_user_artist"] + "-cat" + str(cat) +
                     ".npz")
    h = sps.load_npz(dir + matrix_dict["cbf_user_album"] + "-cat" + str(cat) +
                     ".npz")
    matrix.append([a, b, c, d, e, f, g, h])

    rprec = []
    for i in range(0, 10):
        print("[ Ensembling cat", i + 1, "]")
        rprec.append(ensembler(matrix[i], w[i], normalization_type="max"))
    res = sps.vstack(rprec).tocsr()

    import time
    name = "ensemble-" + mode + "-data-" + time.strftime(
        "%x") + "-" + time.strftime("%X")
    name = name.replace("/", "_")
    sps.save_npz("results/" + name + ".npz", res)

    print("[ Initizalizing Datereader ]")
    dr = Datareader(verbose=False, mode=mode, only_load="False")

    res = eurm_to_recommendation_list(res, datareader=dr)

    if mode == "offline":
        print("[ Initizalizing Evaluator ]")
e = sps.load_npz("../offline/offline-cfuser-cat" + str(cat) + ".npz")
f = sps.load_npz("../offline/slim_bpr_completo_test1-cat" + str(cat) + ".npz")
g = sps.load_npz("../offline/eurm_cbfu_artists_offline-cat" + str(cat) +
                 ".npz")

matrix = [a, b, c, d, e, f, g]

a = float(sys.argv[1])
b = float(sys.argv[2])
c = float(sys.argv[3])
d = float(sys.argv[4])
e = float(sys.argv[5])
f = float(sys.argv[6])
g = float(sys.argv[7])

res = ensembler(matrix, [a, b, c, d, e, f, g], normalization_type="max")

ev = Evaluator(dr)
ret = [
    -ev.evaluate_single_metric(eurm_to_recommendation_list(res, cat=cat),
                               cat=cat,
                               name="ens" + str(cat),
                               metric='prec',
                               level='track')
]

if os.path.isfile("best.npy"):
    best = np.load("best.npy")
    if ret[0] < best[-1].astype(np.float):
        b = sys.argv[1:]
        b.append(ret[0])
        print(arg)
        best = list(arg[1:].astype(np.float))
        w.append(best)

    for i in tqdm(range(1,11)):
        if mode == "offline":

            CBF_ALBUM = sps.load_npz(mode+"/offline-cbf_item_album-cat"+str(i)+".npz")
            CBF_ARTISTA = sps.load_npz(mode+"/offline-cbf_item_artist-cat"+str(i)+".npz")
            NLP = norm_max_row(sps.load_npz(mode + "/nlp_eurm_offline_bm25-cat" + str(1) + ".npz"))
            RP3BETA = sps.load_npz(mode+"/offline-rp3beta-cat"+str(i)+".npz")
            CF_USER = sps.load_npz(mode + "/cfu_eurm-cat"+str(i)+".npz")
            SLIM = sps.load_npz(mode +"/slim_bpr_completo_test1-cat"+str(i)+".npz")
            CBF_USER_ARTIST = sps.load_npz(mode +"/eurm_cbfu_artists_offline-cat"+str(i)+".npz")


        matrix = [CBF_ALBUM, CBF_ARTISTA, NLP, RP3BETA, CF_USER, SLIM, CBF_USER_ARTIST]

        we = w[i-1]

        res.append(ensembler(matrix, we, normalization_type="lele"))

    ret = sps.vstack(res).tocsr()
    if mode == "offline":
        ev.evaluate(eurm_to_recommendation_list(ret), "best_test", verbose=True)

#    sps.save_npz("ensemble_per_cat_"+mode+"_new_data_28_maggio.npz", ret)
    if mode == "online":
        sb = Submitter(dr)
        sb.submit(recommendation_list=eurm_to_recommendation_list_submission(ret), name="best_test", track="main", verify=True, gzipped=False)