Beispiel #1
0
 def __init__(self,
              X,
              XAxis,
              Y,
              R,
              outerIter=70,
              testSize=0.5,
              samples=10,
              seed=10):
     self.X = X
     self.axisInfo = np.array(XAxis[0], dtype="int")
     self.Y = Y
     self.R = R
     self.outerIter = outerIter
     self.samples = samples
     self.ttss = StratifiedShuffleSplit(Y,
                                        n_iter=samples,
                                        test_size=testSize,
                                        random_state=seed)
     self.rawFeatures = createRawFeatures(X)
     self.flatX = sptenmat.sptenmat(
         X, [0]).tocsrmat()  # matricize along the first mode
     self.pcaModel = RandomizedPCA(n_components=R)
     self.predModel = LogisticRegression(C=1.0, penalty='l1', tol=1e-6)
     self.nmfModel = NMF(n_components=R,
                         max_iter=self.outerIter,
                         nls_max_iter=self.innerIter)
Beispiel #2
0
def ctor(verbose):
    x = numpy.array([[[0, 0, 0.9052], [0.9121, 0, 0.7363]],
                     [[0.1757, 0.2089, 0], [0, 0.7455, 0]],
                     [[0, 0, 0.6754], [0, 0, 0]]])
    obj = sptenmat.sptenmat(x, [0], [1, 2], [10, 10, 10])
    print obj

    subs = numpy.array([[1, 3, 5], [1, 1, 0], [2, 2, 2], [3, 4, 4], [1, 1, 1],
                        [1, 1, 1]])
    vals = numpy.array([[0.5], [1.5], [100], [3.5], [4.5], [5.5]])
    siz = numpy.array([4, 5, 6])
    spt = sptensor.sptensor(subs, vals, siz)

    print spt

    obj = sptenmat.sptenmat(spt, [0, 1], [2])
    print obj
Beispiel #3
0
def ctor(verbose):
    x = numpy.array([
        [[0,0,0.9052],[0.9121,0,0.7363]],
        [[0.1757,0.2089,0],[0,0.7455,0]],
        [[0,0,0.6754],[0,0,0]]
        ])
    obj = sptenmat.sptenmat(x, [0], [1,2], [10,10,10]);
    print obj;
    
    subs = numpy.array([[1, 3, 5], [1, 1, 0], [2, 2, 2], [3, 4, 4], [1, 1, 1], [1, 1, 1]]);
    vals = numpy.array([[0.5], [1.5], [100], [3.5], [4.5], [5.5]]);
    siz = numpy.array([4, 5, 6]);
    spt = sptensor.sptensor(subs, vals, siz);
    
    print spt;
    
    obj = sptenmat.sptenmat(spt, [0,1], [2]);
    print obj;
Beispiel #4
0
def tosparsematTest(verbose):
    subs = numpy.array([[1, 3, 5], [1, 1, 0], [2, 2, 2], [3, 4, 4], [1, 1, 1], [1, 1, 1]]);
    vals = numpy.array([[0.5], [1.5], [100], [3.5], [4.5], [5.5]]);
    siz = numpy.array([4, 5, 6]);
    spt = sptensor.sptensor(subs, vals, siz);
    print spt;
    
    sptm = sptenmat.sptenmat(spt,[1]);
    print sptm;
    
    print sptm.tosparsemat();
Beispiel #5
0
def tosparsematTest(verbose):
    subs = numpy.array([[1, 3, 5], [1, 1, 0], [2, 2, 2], [3, 4, 4], [1, 1, 1],
                        [1, 1, 1]])
    vals = numpy.array([[0.5], [1.5], [100], [3.5], [4.5], [5.5]])
    siz = numpy.array([4, 5, 6])
    spt = sptensor.sptensor(subs, vals, siz)
    print spt

    sptm = sptenmat.sptenmat(spt, [1])
    print sptm

    print sptm.tosparsemat()
 def __init__(self, X, XAxis, Y, R, outerIter=70, testSize=0.5, samples=10, seed=10):
     self.X = X
     self.axisInfo = np.array(XAxis[0], dtype="int")
     self.Y = Y
     self.R = R
     self.outerIter = outerIter
     self.samples = samples
     self.ttss = StratifiedShuffleSplit(Y,n_iter=samples, test_size=testSize, random_state=seed)
     self.rawFeatures = createRawFeatures(X)
     self.flatX =  sptenmat.sptenmat(X, [0]).tocsrmat() # matricize along the first mode
     self.pcaModel = RandomizedPCA(n_components=R)
     self.predModel = LogisticRegression(C=1.0, penalty='l1', tol=1e-6)
     self.nmfModel = NMF(n_components=R, max_iter = self.outerIter, nls_max_iter = self.innerIter)
Beispiel #7
0
def DTA(Xnew, R, C=None, alpha=None):
    """DTA analysis"""

    # number of dimensions of the input tensor.
    N = Xnew.ndims()

    # If the co-variacne matrices are not given,
    # initialize all of them to be 0
    if (C == None):
        C = []
        dv = Xnew.shape
        for i in range(0, N):
            C.extend([sparse.coo_matrix(([], ([], [])), [dv[i], dv[i]])])

    # If the forgetting factor is not given, it is 1.
    if (alpha == None):
        alpha = 1

    U = []
    Cnew = []
    for i in range(0, N):
        if (Xnew.__class__ == tensor.tensor):
            XM = tenmat.tenmat(Xnew, [i]).tondarray()
        elif (Xnew.__class__ == sptensor.sptensor):
            XM = sptenmat.sptenmat(Xnew, [i]).tosparsemat()
        elif (Xnew.__class__ == ttensor.ttensor):
            raise TypeError("It is not supported yet.")
        else:
            raise TypeError(
                "1st argument must be tensor, sptensor, or ttensor")

        Cnew.extend(
            [numpy.array(alpha * C[i] + numpy.dot(XM, XM.transpose()))])

        (w, v) = eigwrapper(Cnew[i], R[i])

        U.extend([numpy.array(v)])

    core = Xnew.ttm(U, None, 't')
    T = ttensor.ttensor(core, U)
    return (T, Cnew)
Beispiel #8
0
def DTA(Xnew, R, C = None, alpha = None):
    """DTA analysis"""
    
    # number of dimensions of the input tensor.
    N = Xnew.ndims();
    
    # If the co-variacne matrices are not given,
    # initialize all of them to be 0
    if(C == None):
        C = [];
        dv = Xnew.shape;
        for i in range(0, N):
            C.extend([ sparse.coo_matrix(([],([],[])),[dv[i], dv[i]]) ]);
    
    # If the forgetting factor is not given, it is 1.
    if(alpha == None):
        alpha = 1;
    
    U = [];
    Cnew = [];
    for i in range (0,N):
        if(Xnew.__class__ == tensor.tensor):
            XM = tenmat.tenmat(Xnew,[i]).tondarray();
        elif(Xnew.__class__ == sptensor.sptensor):
            XM = sptenmat.sptenmat(Xnew,[i]).tosparsemat();
        elif(Xnew.__class__ == ttensor.ttensor):
            raise TypeError("It is not supported yet.");
        else:
            raise TypeError("1st argument must be tensor, sptensor, or ttensor");
        
        
        Cnew.extend([ numpy.array(alpha*C[i] + numpy.dot(XM, XM.transpose())) ]);
        
        (w,v) = eigwrapper(Cnew[i], R[i]);
        
        U.extend([ numpy.array(v) ]);

    core = Xnew.ttm(U, None, 't');
    T = ttensor.ttensor(core, U);
    return (T, Cnew);
Beispiel #9
0
from sklearn import preprocessing
import nimfa

import sys
sys.path.append("..")

import KLProjection
import predictionModel
import sptenmat
import tensorTools
import json

X, axisDict, classDict = tensorTools.loadSingleTensor("data/cms-tensor-{0}.dat")
Y = np.array(classDict.values(), dtype='int')

flatX =  sptenmat.sptenmat(X, [0]).tocsrmat() # matricize along the first mode
testSize = 0.5

seed = 400
R = 50
ttss = StratifiedShuffleSplit(Y, n_iter=1, test_size=testSize, random_state=seed)

for train, test in ttss:
	nmfModel = nimfa.mf(flatX[train,:], method="nmf", max_iter=200, rank=R)
	nmfResult = nimfa.mf_run(nmfModel)
	nmfBasis = nmfResult.coef().transpose()
	nmfBasis = preprocessing.normalize(nmfBasis, norm="l1", axis=0)
	nmfBasisA = nmfBasis.toarray()
	outFile = file("results/nmf-404.dat", "wb")
	np.save(outFile, nmfBasisA)
	outFile.close()
Beispiel #10
0
        compOut.append({
            "expt": exptID,
            "R": R,
            "Outer": outerIter,
            "Model": "Limestone",
            "Comp": elapsed
        })

        klp = KLProjection.KLProjection(M.U, M.R)
        ptfFeat = klp.projectSlice(X, 0)
        ptfMatrix = khatrirao.khatrirao(M.U[1], M.U[2])
        np.save("results/pred-metric-limestone-{0}.dat".format(exptID),
                ptfMatrix)

        ## now we want to do PCA and NMF as well
        flatX = sptenmat.sptenmat(
            X, [0]).tocsrmat()  # matricize along the first mode
        startTime = time.time()
        pcaModel = RandomizedPCA(n_components=R)
        pcaModel.fit(flatX[train, :])
        elapsed = time.time() - startTime
        compOut.append({
            "expt": exptID,
            "R": R,
            "Outer": outerIter,
            "Model": "PCA",
            "Comp": elapsed
        })
        pcaFeat = pcaModel.transform(flatX)
        pcaBasis = pcaModel.components_
        np.save("results/pred-metric-pca-{0}.dat".format(exptID), pcaBasis)
Beispiel #11
0
    def ttm(self, mat, dims = None, option = None):
        """ computes the sptensor times the given matrix.
        arrs is a single 2-D matrix/array or a list of those matrices/arrays."""
        
        if(dims == None):
            dims = range(0,self.ndims());
        
        #Handle when arrs is a list of arrays
        if(mat.__class__ == list):
            if(len(mat) == 0):
                raise ValueError("the given list of arrays is empty!");
            
            (dims,vidx) = tools.tt_dimscehck(dims, self.ndims(), len(mat));
            
            Y = self.ttm(mat[vidx[0]],dims[0],option);
            for i in range(1, len(dims)):
                Y = Y.ttm(mat[vidx[i]],dims[i],option);
                
            return Y;                
        
        if(mat.ndim != 2):
            raise ValueError ("matrix in 2nd armuent must be a matrix!");

        if(option != None):
            if (option == 't'):
                mat = mat.transpose();
            else:
                raise ValueError ("unknown option.");          
        
        
        if(dims.__class__ == list):
            if(len(dims) != 1):
                raise ValueError("Error in number of elements in dims");
            else:
                dims = dims[0];
        
        if(dims < 0 or dims > self.ndims()):
            raise ValueError ("Dimension N must be between 1 and num of dimensions");
        
        #Check that sizes match
        if(self.shape[dims] != mat.shape[1]):
            raise ValueError ("size mismatch on V");
        
        #Compute the new size
        newsiz = list(self.shape);
        newsiz[dims] = mat.shape[0];
        
        #Compute Xn
        Xnt = sptenmat.sptenmat(self,None,[dims],None,'t');
        rdims = Xnt.rdims;
        cdims = Xnt.cdims;
        
        I = [];
        J = [];
        for i in range(0, len(Xnt.subs)):
            I.extend([Xnt.subs[i][0]]);
            J.extend([Xnt.subs[i][1]]);
        
        
        Z = (sparse.coo_matrix((Xnt.vals.flatten(),(I,J)),
            shape = (tools.getelts(Xnt.tsize, Xnt.rdims).prod(),
                     tools.getelts(Xnt.tsize, Xnt.cdims).prod()))
             * mat.transpose());
        
        Z = tensor.tensor(Z,newsiz).tosptensor();
        
        
        if(Z.nnz() <= 0.5 * numpy.array(newsiz).prod()):
            Ynt = sptenmat.sptenmat(Z, rdims, cdims);
            return Ynt.tosptensor();
        else:
            Ynt = tenmat.tenmat(Z.totensor(), rdims, cdims);
            return Ynt.totensor();
Beispiel #12
0
    def ttm(self, mat, dims = None, option = None):
        """ computes the sptensor times the given matrix.
        arrs is a single 2-D matrix/array or a list of those matrices/arrays."""
        
        if(dims == None):
            dims = range(0,self.ndims());
        
        #Handle when arrs is a list of arrays
        if(mat.__class__ == list):
            if(len(mat) == 0):
                raise ValueError("the given list of arrays is empty!");
            
            (dims,vidx) = tools.tt_dimscehck(dims, self.ndims(), len(mat));
            
            Y = self.ttm(mat[vidx[0]],dims[0],option);
            for i in range(1, len(dims)):
                Y = Y.ttm(mat[vidx[i]],dims[i],option);
                
            return Y;                
        
        if(mat.ndim != 2):
            raise ValueError ("matrix in 2nd armuent must be a matrix!");

        if(option != None):
            if (option == 't'):
                mat = mat.transpose();
            else:
                raise ValueError ("unknown option.");          
        
        
        if(dims.__class__ == list):
            if(len(dims) != 1):
                raise ValueError("Error in number of elements in dims");
            else:
                dims = dims[0];
        
        if(dims < 0 or dims > self.ndims()):
            raise ValueError ("Dimension N must be between 1 and num of dimensions");
        
        #Check that sizes match
        if(self.shape[dims] != mat.shape[1]):
            raise ValueError ("size mismatch on V");
        
        #Compute the new size
        newsiz = list(self.shape);
        newsiz[dims] = mat.shape[0];
        
        #Compute Xn
        Xnt = sptenmat.sptenmat(self,None,[dims],None,'t');
        rdims = Xnt.rdims;
        cdims = Xnt.cdims;
        
        I = [];
        J = [];
        for i in range(0, len(Xnt.subs)):
            I.extend([Xnt.subs[i][0]]);
            J.extend([Xnt.subs[i][1]]);
        
        
        Z = (sparse.coo_matrix((Xnt.vals.flatten(),(I,J)),
            shape = (tools.getelts(Xnt.tsize, Xnt.rdims).prod(),
                     tools.getelts(Xnt.tsize, Xnt.cdims).prod()))
             * mat.transpose());
        
        Z = tensor.tensor(Z,newsiz).tosptensor();
        
        
        if(Z.nnz() <= 0.5 * numpy.array(newsiz).prod()):
            Ynt = sptenmat.sptenmat(Z, rdims, cdims);
            return Ynt.tosptensor();
        else:
            Ynt = tenmat.tenmat(Z.totensor(), rdims, cdims);
            return Ynt.totensor();
Beispiel #13
0
import numpy as np;
import CP_APR
import ktensor

""" 
Test file associated with the CP decomposition using APR
"""

""" Test factorization of sparse matrix """
subs = np.array([[0,3,1], [1,0,1], [1,2,1], [1,3,1], [3,0,0]]);
vals = np.array([[1],[1],[1],[1],[3]]);
siz = np.array([5,5,2]) # 5x5x2 tensor
X = sptensor.sptensor(subs, vals, siz)
U0 = np.array([[0.7689, 0.8843, 0.7487, 0.0900], [0.1673, 0.5880, 0.8256, 0.1117], [0.8620, 0.1548, 0.7900, 0.1363], [0.9899, 0.1999, 0.3185, 0.6787], [0.5144, 0.4070, 0.5341, 0.4952]])
U1 = np.array([[0.1897, 0.5606, 0.8790, 0.9900], [0.4950, 0.9296, 0.9889, 0.5277], [0.1476, 0.6967, 0.0006, 0.4795], [0.0550, 0.5828, 0.8654, 0.8013], [0.8507, 0.8154, 0.6126, 0.2278]])
U2 = np.array([[0.4981, 0.5747, 0.7386, 0.2467], [0.9009, 0.8452, 0.5860, 0.6664]])
Minit = ktensor.ktensor(np.ones(4), [U0, U1, U2])
fms = Minit.fms(Minit)

Y, cpstats, modelStats = CP_APR.cp_apr(X,4, Minit=Minit, maxiters=100);
Y.normalize_sort(1)

""" Test factorization of 2-mode sparse matrix """
matX = sptenmat.sptenmat(X, [0]).tosptensor()
CP_APR.cp_apr(matX, 4)

""" Test factorization of regular tensor """
XT = tensor.tensor(range(1,25), [3, 4, 2])
CP_APR.cp_apr(XT,4);