code: def least_squares_cg(
      Cui, X, Y, regularization, num_threads=0, cg_steps=3
  ):
      users, factors = X.shape
      YtY = Y.T.dot(Y) + regularization * np.eye(factors, dtype=Y.dtype)
  
      for u in range(users):
          # start from previous iteration
          x = X[u]
  
          # calculate residual error r = (YtCuPu - (YtCuY.dot(Xu)
          r = -YtY.dot(x)
          for i, confidence in nonzeros(Cui, u):
              if confidence > 0:
                  r += (
                      confidence - (confidence - 1) * Y[i].dot(x)
                  ) * Y[i]
              else:
                  confidence *= -1
                  r += -(confidence - 1) * Y[i].dot(x) * Y[i]
  
          p = r.copy()
          rsold = r.dot(r)
          if rsold < 1e-20:
              continue
  
          for _ in range(cg_steps):
              # calculate Ap = YtCuYp - without actually calculating YtCuY
              Ap = YtY.dot(p)
              for i, confidence in nonzeros(Cui, u):
                  if confidence < 0:
                      confidence *= -1
  
                  Ap += (confidence - 1) * Y[i].dot(p) * Y[i]
  
              # standard CG update
              alpha = rsold / p.dot(Ap)
              x += alpha * p
              r -= alpha * Ap
              rsnew = r.dot(r)
              if rsnew < 1e-20:
                  break
              p = r + (rsnew / rsold) * p
              rsold = rsnew
  
          X[u] = x
  
imports: import functools
  import heapq
  import logging
  import time
  import numpy
  import scipy
  import scipy.sparse
  from tqdm.auto import tqdm
  from ..utils import check_blas_config, check_csr, check_random_state, nonzeros
  from .None import _als
  from .matrix_factorization_base import MatrixFactorizationBase
label_desc: Use image node_id 5 for calling a global function with example usage: least_squares_cg(Cui, X, Y, regularization, num_threads, cg_steps) without return types