def testDiag(self): a = numpy.array([1, 2, 3]) X = sppy.diag(a) self.assertEquals(X.getnnz(), 3) self.assertEquals(X[0, 0], 1) self.assertEquals(X[1, 1], 2) self.assertEquals(X[2, 2], 3)
def writeAuthorAuthorMatrix(authorXFileName, authorAuthorFileName, sigma=0.05): if not os.path.isfile(authorAuthorFileName): Y = sppy.io.mmread(authorXFileName, storagetype="row") logging.debug("Read file: " + authorXFileName) logging.debug("Size of input: " + str(Y.shape) + " with " + str(Y.nnz) + " nonzeros") Y = sppy.csarray(Y, dtype=numpy.float, storagetype="row") #Y = Y[0:1000, :] invNorms = 1/numpy.sqrt((Y.power(2).sum(1))) Z = sppy.diag(invNorms, storagetype="row") Y = Z.dot(Y) blocksize = 500 numBlocks = int(ceil(Y.shape[0]/float(blocksize))) logging.debug("Number of blocks " + str(numBlocks)) allRowInds = numpy.array([], numpy.int32) allColInds = numpy.array([], numpy.int32) allValues = numpy.array([], numpy.float) for i in range(numBlocks): logging.debug("Iteration: " + str(i)) endInd = min(Y.shape[0], (i+1)*blocksize) tempY = Y.submatrix(i*blocksize, 0, endInd-i*blocksize, Y.shape[1]) tempC = tempY.dot(Y.T) tempC = tempC.clip(sigma, 1.0) rowInds, colInds = tempC.nonzero() rowInds += i*blocksize values = tempC.values() allRowInds = numpy.r_[allRowInds, rowInds] allColInds = numpy.r_[allColInds, colInds] allValues = numpy.r_[allValues, values] logging.debug(allValues.shape) coords = numpy.c_[allRowInds+1, allColInds+1, allValues] comment = "%%MatrixMarket matrix coordinate real general\n" comment += "%\n" comment += str(Y.shape[0]) + " " + str(Y.shape[0]) + " " + str(allRowInds.shape[0]) numpy.savetxt(authorAuthorFileName, coords, delimiter=" ", header=comment, comments="", fmt="%d %d %f") logging.debug("Saved matrix to " + authorAuthorFileName) logging.debug("Final size of C " + str(Y.shape[0]) + " with " + str(allRowInds.shape[0]) + " nonzeros") else: logging.debug("File exists: " + authorAuthorFileName)
def testDiag(self): n = 5 a = numpy.random.rand(n) A = sppy.diag(a) b = A.diag() for i in range(n): self.assertEquals(A[i,i], a[i]) self.assertEquals(A.nnz, n) nptst.assert_array_almost_equal(a, b)