def test_similarityMatrixTopK_sparseToSparse(self):

        numRows = 20

        TopK = 5

        dense_input = np.random.random((numRows, numRows))
        sparse_input = sps.csr_matrix(dense_input)

        dense_output = similarityMatrixTopK(dense_input,
                                            k=TopK,
                                            forceSparseOutput=False,
                                            inplace=False)
        sparse_output = similarityMatrixTopK(sparse_input,
                                             k=TopK,
                                             forceSparseOutput=True)

        self.assertTrue(np.allclose(dense_output, sparse_output.todense()),
                        "sparseToSparse CSR incorrect")

        sparse_input = sps.csc_matrix(dense_input)
        sparse_output = similarityMatrixTopK(sparse_input,
                                             k=TopK,
                                             forceSparseOutput=True)
        self.assertTrue(np.allclose(dense_output, sparse_output.todense()),
                        "sparseToSparse CSC incorrect")
    def test_similarityMatrixTopK_denseToSparse(self):

        numRows = 100

        TopK = 20

        dense = np.random.random((numRows, numRows))

        sparse = similarityMatrixTopK(dense, k=TopK, forceSparseOutput=True)
        dense = similarityMatrixTopK(dense, k=TopK, forceSparseOutput=False)

        self.assertTrue(
            np.equal(dense, sparse.todense()).all(), "denseToSparse incorrect")
    def fit(self, topK=100, alpha=0.5):

        self.topK = topK
        self.alpha = alpha

        W = self.Similarity_1 * self.alpha + self.Similarity_2 * (1 -
                                                                  self.alpha)

        self.W_sparse = similarityMatrixTopK(W, k=self.topK).tocsr()
예제 #4
0
    def fit(self, W_sparse, selectTopK=False, topK=100):

        assert W_sparse.shape[0] == W_sparse.shape[1],\
            "ItemKNNCustomSimilarityRecommender: W_sparse matrice is not square. Current shape is {}".format(W_sparse.shape)

        assert self.URM_train.shape[1] == W_sparse.shape[0],\
            "ItemKNNCustomSimilarityRecommender: URM_train and W_sparse matrices are not consistent. " \
            "The number of columns in URM_train must be equal to the rows in W_sparse. " \
            "Current shapes are: URM_train {}, W_sparse {}".format(self.URM_train.shape, W_sparse.shape)

        if selectTopK:
            W_sparse = similarityMatrixTopK(W_sparse, k=topK)

        self.W_sparse = check_matrix(W_sparse, format='csr')
    def test_similarityMatrixTopK_denseToDense(self):

        numRows = 100

        TopK = 20

        dense_input = np.random.random((numRows, numRows))
        dense_output = similarityMatrixTopK(dense_input,
                                            k=TopK,
                                            forceSparseOutput=False)

        numExpectedNonZeroCells = TopK * numRows

        numNonZeroCells = np.sum(dense_output != 0)

        self.assertEqual(numExpectedNonZeroCells, numNonZeroCells,
                         "DenseToDense incorrect")