<jupyter_start><jupyter_text>In alternative squares, you are minimizing the squared error and the size of the entries of the matrices `U` and `V` at the same time. We are attempting to predict preferences over matrix `X` such that `X`=`U` \dot `V`^T<jupyter_code>import math, numpy as np
pu = [[(0,0,1),(0,0,22),(0,2,1),(0,3,1),(0,5,0)],
     [(1,0,1),(1,1,32),(1,2,0),(1,3,0),(1,4,1),(1,5,0)],
     [(2,0,0),(2,1,18),(2,2,1),(2,3,1),(2,4,0),(2,5,1)],
     [(3,0,1),(3,1,40),(3,2,1),(3,3,0),(3,4,0),(3,5,1)],
     [(4,0,0),(4,1,40),(4,2,40),(4,2,0),(4,4,1),(4,5,0)],
     [(5,0,0),(5,1,25),(5,2,1),(5,3,1),(5,4,1)]]

pv = [[(0,0,1),(0,1,1),(0,2,0),(0,3,1),(0,4,0),(0,5,0)],
     [(1,0,22),(1,1,32),(1,2,18),(1,3,40),(1,4,40),(1,5,25)],
     [(2,0,1),(2,1,0),(2,2,1),(2,3,1),(2,4,0),(2,5,1)],
     [(3,0,1),(3,1,0),(3,2,1),(3,3,0),(3,5,1)],
     [(4,1,1),(4,2,0),(4,3,0),(4,4,1),(4,5,1)],
     [(5,0,0),(5,1,0),(5,2,1),(5,3,1),(5,4,0)]]

V =np.mat([[0.15968384, 0.9441198, 0.83651085],
          [0.73573009, 0.24906915, 0.85338239],
          [0.25605814, 0.6990532, 0.50900407],
          [0.2405843, 0.318488888, 0.60233653],
          [0.24237479, 0.15293281, 0.22240255],
          [0.03943766, 0.19287528, 0.95094265]])

print(V)
U = np.mat(np.zeros([6,3]))
L = 0.03
for iter in range(5):
    print("\n ---- ITER %s -----" %(iter+1))
    print("U")
    urs = []
    for uset in pu:
        vo = []
        pvo = []
        for i,j,p in uset:
            vor = []
            for k in range(3):
                vor.append(V[j,k])
            vo.append(vor)
            pvo.append(p)
        vo = np.mat(vo)
        ur = np.linalg.inv(vo.T * vo + L*np.mat(np.eye(3))) * vo.T*np.mat(pvo).T
        urs.append(ur.T)
    U = np.vstack(urs)
    print(U)

    print("V")
    vrs = []
    for vset in pv:
        uo = []
        puo = []
        for j,i,p in vset:
            uor = []
            for k in range(3):
                uor.append(U[i,k])
            uo.append(uor)
            puo.append(p)
        uo = np.mat(uo)
        vr = np.linalg.inv(uo.T * uo + L*np.mat(np.eye(3))) * uo.T*np.mat(puo).T
        vrs.append(vr.T)
    V = np.vstack(vrs)
    print(V)

    err = 0
    n = 0
    for uset in pu:
        for i,j,p in uset:
            err += (p - (U[i]*V[j].T)[0,0])**2
            n += 1
    print(math.sqrt(err/n))
print(U*V.T)<jupyter_output>---- ITER 1 -----
U
[[-14.08349169  13.24220379  -0.70349893]
 [ 36.40898688 -10.96084492   2.19757684]
 [ 19.79884149  -6.62335708   2.23966928]
 [ 44.36457118 -13.91334038   3.61771677]
 [ 52.35523555   2.4980526   -2.86869512]
 [ 24.08497063 -11.99211162   7.23072391]]
V
[[0.01321315 0.04193223 0.10682645]
 [1.05176511 2.34321895 5.02102149]
 [0.01550272 0.08639102 0.28664038]
 [0.01772662 0.10962727 0.27417929]
 [0.02458558 0.08853457 0.18185938]
 [0.01057354 0.03557102 0.25530393]]
8.238323069225373

 ---- ITER 2 -----
U
[[  5.49407554  10.2119681    7.96218738]
 [  5.39614745   6.23460581   2.29300159]
 [  0.63971363   0.83104807   3.05621934]
 [  5.57584382   5.14273419   4.35177842]
 [-31.65376333 -21.5401682   24.90084154]
 [  1.54972779   2.34388318   3.54947045]]
V
[[ 7.69727734e-02 -4.99607169e-03  9.11168868e-02]
 [ 9.75896044e+00 -8.05038628e+00  7.01265400e+00]
 [ 2.60901050e-01 -1.90973011e-01  1.69706381e-01]
 [-1.36406392e-01 -4.58542380e-02  2.96251510e-01]
 [-5.59[...]
{'fraction_non_alphanumeric': 0.24561403508771928, 'fraction_numerical': 0.3095450490633363, 'mean_word_length': 2.4644696189495368, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '223490', 'license': 'Apache-2.0', 'n_tokens_mistral': 2248, 'n_tokens_neox': 1687, 'n_words': 246, 'path': 'Alternating Squares Approach.ipynb', 'repo_name': 'dalvarez83/Building-a-recommendation-system-with-python-course', 'size': '3363'}