import matplotlib.pyplot as plt
import numpy as np
from LoadData import load_rating_data, spilt_rating_dat
from sklearn.model_selection import train_test_split
from ProbabilisticMatrixFactorization import PMF
from mf import MF
import re

if __name__ == "__main__":
    file_path = "data/ml-100k/u.data"
    matrix=[]
    for i in range(51):  #943
        matrix.append([0]*7)
    file=open("silabuz.txt","r")
    for z in file:
        w=z.split(',')
        matrix[int(w[0])][int(w[1])]=int(w[2])


    pmf = PMF()
    
    pmf.set_params({"num_feat": 2, "epsilon": 1, "_lambda": 0.1, "momentum": 0.8, "maxepoch": 10, "num_batches": 100,
                    "batch_size": 1000})
    ratings = load_rating_data(file_path)
    print(ratings[:,0])
    print(len(np.unique(ratings[:, 0])), len(np.unique(ratings[:, 1])), pmf.num_feat)
    train, test = train_test_split(ratings, test_size=0.1)  # spilt_rating_dat(ratings)

    pmf.fit(train, test)


    # Check performance by plotting train and test errors
    plt.plot(range(pmf.maxepoch), pmf.rmse_train, marker='o', label='Training Data')
    plt.plot(range(pmf.maxepoch), pmf.rmse_test, marker='v', label='Test Data')
    plt.title('The MovieLens Dataset Learning Curve')
    plt.xlabel('Number of Epochs')
    plt.ylabel('RMSE')
    plt.legend()
    plt.grid()
    plt.show()
    print("precision_acc,recall_acc:" + str(pmf.topK(test)))

    #print(pmf.predict(0))