def factorTensor(X): # set the seed for the same initialization np.random.seed(seed) Y, iterStats, mstats = CP_APR.cp_apr(X, R, tol=tol, maxiters=outerIters, maxinner=innerIters) Y.normalize_sort(1) Y = decompTools.zeroSmallFactors(Y, zeroThr=zeroThr) return Y
def findFactors(X, R=100, outerIter=70, innerIter=10, zeroThr=1e-4): """ Find the factor basis for this tensor """ M, cpstats, mstats = CP_APR.cp_apr(X, R=R, maxiters=outerIter, maxinner=innerIter) M.normalize_sort(1) M = decompTools.zeroSmallFactors(M, zeroThr) return KLProjection.KLProjection(M.U, R), M, mstats
tol = 1e-2 zeroThr = 1e-5 # input file and output file inputFile = args.inputFile.format("data") yaxis = decompTools.loadAxisInfo(args.inputFile.format("info")) print "Starting Tensor Factorization with ID:{0}".format(exptID) X = sptensor.loadTensor(inputFile) np.random.seed(seed) Y, ls = CP_ALS.cp_als(X, R, tol=tol, maxiters=iter) ll = CP_APR.loglikelihood(X, Y) # normalize the factors using the 1 norm and then sort in descending order Y.normalize_sort(1) Y = decompTools.zeroSmallFactors(Y, zeroThr=zeroThr) Y.writeRawFile("results/als-raw-{0}.dat".format(exptID)) Youtfile = "results/als-db-{0}-{1}.csv".format(exptID, iter) Ysqlfile = "results/als-sql-{0}.sql".format(exptID) # save the decomposition into the format Yout = decompTools.getDBOutput(Y, yaxis) Yout = np.column_stack((np.repeat(exptID, Yout.shape[0]), Yout)) np.savetxt(Youtfile, Yout, fmt="%s", delimiter="|") sqlOut = file(Ysqlfile, "w") sqlOut.write( "load client from /home/joyceho/workspace/tensor/{0} of del modified by coldel| insert into joyceho.tensor_factors;\n" .format(Youtfile)) sqlOut.write( "insert into joyceho.tensor_models values({0}, {1}, \'{2}\',\'{3}\', {4}, {5}, {6}, {7}, {8});\n"
tol = 1e-2 zeroThr = 1e-5 # input file and output file inputFile = args.inputFile.format("data") yaxis = decompTools.loadAxisInfo(args.inputFile.format("info")) print "Starting Tensor Factorization with ID:{0}".format(exptID) X = sptensor.loadTensor(inputFile) np.random.seed(seed) Y, ls = CP_ALS.cp_als(X, R, tol=tol, maxiters=iter) ll = CP_APR.loglikelihood(X, Y) # normalize the factors using the 1 norm and then sort in descending order Y.normalize_sort(1) Y = decompTools.zeroSmallFactors(Y, zeroThr=zeroThr) Y.writeRawFile("results/als-raw-{0}.dat".format(exptID)) Youtfile = "results/als-db-{0}-{1}.csv".format(exptID, iter) Ysqlfile = "results/als-sql-{0}.sql".format(exptID) # save the decomposition into the format Yout = decompTools.getDBOutput(Y, yaxis) Yout = np.column_stack((np.repeat(exptID, Yout.shape[0]), Yout)) np.savetxt(Youtfile, Yout, fmt="%s", delimiter="|") sqlOut = file(Ysqlfile, "w") sqlOut.write("load client from /home/joyceho/workspace/tensor/{0} of del modified by coldel| insert into joyceho.tensor_factors;\n".format(Youtfile)) sqlOut.write("insert into joyceho.tensor_models values({0}, {1}, \'{2}\',\'{3}\', {4}, {5}, {6}, {7}, {8});\n".format(exptID, labelID, patientSet, exptDesc, iter, innerIter, ls, ll, 0)) sqlOut.close() print "Completed Tensor Factorization with ID:{0}".format(exptID)
import ktensor import numpy import decompTools caseX = ktensor.loadTensor("results/apr-raw-1.dat") controlX = ktensor.loadTensor("results/apr-raw-2.dat") allX = ktensor.loadTensor("results/apr-raw-200.dat") ## since they don't share the same axis, remove one of the factors caseX.U = [caseX.U[1], caseX.U[2]] caseX = decompTools.zeroSmallFactors(caseX, 1e-2) controlX.U = [controlX.U[1], controlX.U[2]] controlX = decompTools.zeroSmallFactors(controlX, 1e-2) allX.U = [allX.U[1], allX.U[2]] allX = decompTools.zeroSmallFactors(allX, 1e-2) fms = caseX.greedy_fms(controlX) numpy.savetxt("plots/case-control.csv", fms, delimiter=",") allFMS = caseX.greedy_fms(allX) numpy.savetxt("plots/case-all.csv", allFMS, delimiter=",")
import ktensor import numpy; import decompTools caseX = ktensor.loadTensor("results/apr-raw-1.dat") controlX = ktensor.loadTensor("results/apr-raw-2.dat") allX = ktensor.loadTensor("results/apr-raw-200.dat") ## since they don't share the same axis, remove one of the factors caseX.U = [caseX.U[1], caseX.U[2]] caseX = decompTools.zeroSmallFactors(caseX, 1e-2) controlX.U = [controlX.U[1], controlX.U[2]] controlX = decompTools.zeroSmallFactors(controlX, 1e-2) allX.U = [allX.U[1], allX.U[2]] allX = decompTools.zeroSmallFactors(allX, 1e-2) fms = caseX.greedy_fms(controlX) numpy.savetxt("plots/case-control.csv", fms, delimiter=",") allFMS = caseX.greedy_fms(allX) numpy.savetxt("plots/case-all.csv", allFMS, delimiter=",")