human
I am working on the file "FDMS/TME5/TME4_5_FiltrageCollaboratif_tsne.ipynb".
The first task is:
We have not been very successful with incorporating the bias and L1 into that implementation...
We build a simpler model below, and then add the regularization and bias.
Can you write Python program for it?
 Write me a the Python program you requested?
assistant
#Here is the Python program you requested.

class mf():
    def __init__(self, k, eps=1e-3, nIter=2000, lambd=0.5):
        self.k = k
        self.lambd = lambd
        self.eps = eps
        self.nIter = nIter

    def fit(self, trainUsers, trainItems, rawArray):
        print "MF with %d latent features, %d iterations."%(self.k, self.nIter)

        printevery = np.round(self.nIter/10,decimals=-4)

        self.p = {}
        self.q = {}
        self.bu = {}
        self.bi = {}
        self.mu = np.random.random() * 2 - 1

        for j in range(len(rawArray)):
            u = rawArray[j][0]
            i = rawArray[j][1]
            if not u in self.p:
                self.p[u] = np.random.rand(1,self.k)
                self.bu[u] = np.random.rand() * 2 - 1
            if not i in self.q:
                self.q[i] = np.random.rand(self.k,1)
                self.bi[i] = np.random.rand() * 2 - 1
        loss = []
        for it in range(self.nIter):
            ind = np.random.randint(len(rawArray))
            u = rawArray[ind][0]
            i = rawArray[ind][1]

            # thx to A & A !
            tmp = trainUsers[u][i] - (self.mu + self.bi[i] + self.bu[u] +self.p[u].dot(self.q[i])[0][0])
            self.p[u] = (1 - self.lambd * self.eps) * self.p[u] + self.eps * 2 * tmp * self.q[i].transpose()
            self.bu[u] = (1 - self.lambd * self.eps) * self.bu[u] + self.eps * 2 * tmp
            self.q[i] = (1 - self.lambd * self.eps) * self.q[i] + self.eps * 2 * tmp * self.p[u].transpose()
            self.bi[i] = (1 - self.lambd * self.eps) * self.bi[i] + self.eps * 2 * tmp
            self.mu = (1 - self.lambd * self.eps) * self.mu + self.eps * 2 * tmp

            loss.append(tmp*tmp)

            if it % printevery == 0 or it == self.nIter-1:
                print "Iteration %s, Err %.05f, True %.05f"%( str(it).ljust(8), np.mean(loss) , model.score(arrayTest))

        return self.p, self.q

    def predict(self, rawArray_test):
        pred = np.zeros(len(rawArray_test))
        for ind,t in enumerate(rawArray_test):
           ...