Exemplo n.º 1
0
    def create(self,
               data,
               row_labels=None,
               col_labels=None,
               foldin=False,
               truncate=False):
        #is_row is what I'm originally folding in
        self._values = map(itemgetter(0), data)
        self._rows = map(itemgetter(1), data)
        self._cols = map(itemgetter(2), data)

        if foldin:  #new to make sure not folding in user and item at same time
            #idea: create matrix normally but keep track of the columns (items) or rows to be folded in before doing update
            if col_labels:  #if col_labels defined then I'm folding in a row
                self._additional_elements = [
                    x for x in self._cols if x not in col_labels
                ]
            else:  #else I am folding in a column
                self._additional_elements = [
                    x for x in self._rows if x not in row_labels
                ]
            if truncate:
                for item in self._additional_elements:
                    if col_labels:
                        index_remove = self._cols.index(item)
                    else:
                        index_remove = self._rows.index(item)
                    del self._values[index_remove]
                    del self._rows[index_remove]
                    del self._cols[index_remove]

        self._matrix = divisiSparseMatrix.from_named_lists(
            self._values, self._rows, self._cols, row_labels, col_labels)
Exemplo n.º 2
0
    def update(
        self,
        matrix,
        is_batch=False
    ):  #isbatch is for creating the final sparse matrix ,since you will want to collect all then construct final matrix at end
        #To update the stored data matrix with the new values and create a new divisi spare matrix with it to retain the zeroes
        self._values.extend(matrix._values)
        self._rows.extend(matrix._rows)
        self._cols.extend(matrix._cols)

        if not is_batch:
            self._matrix = divisiSparseMatrix.from_named_lists(
                self._values, self._rows, self._cols)
Exemplo n.º 3
0
def blend(mats, factors=None, symmetric=False, post_weights=None):
    """
    Combine multiple labeled matrices into one, with weighted data from
    all the matrices.

    mats: a list of matrices to blend.
    factors: List of scaling factor for each matrix.
      If None, the reciprocal of the first singular value is used.
    post_weights: List of weights to apply to each scaled matrix.
      You can use this to, for example, say that one matrix is twice as
      important as another. If None, no post-weighting is performed.
    symmetric: Use square_from_named_lists.
    """
    assert len(mats) > 0
    if len(mats) == 1:
        if factors is None: return mats[0]
        else: return mats[0] * factors[0]

    b_values = []
    b_row_labels = []
    b_col_labels = []

    if factors is None:
        factors = [blend_factor(mat) for mat in mats]

    if post_weights is not None:
        factors = [
            factor * post_weight
            for factor, post_weight in zip(factors, post_weights)
        ]

    for mat, factor in zip(mats, factors):
        # FIXME: using bare find(), multiplying in numpy form, and
        # translating the labels manually would be a bit faster
        values, row_labels, col_labels = mat.named_lists()
        b_values.extend([v * factor for v in values])
        b_row_labels.extend(row_labels)
        b_col_labels.extend(col_labels)

    if symmetric:
        return SparseMatrix.square_from_named_lists(b_values, b_row_labels,
                                                    b_col_labels)
    else:
        return SparseMatrix.from_named_lists(b_values, b_row_labels,
                                             b_col_labels)
Exemplo n.º 4
0
def blend(mats, factors=None, symmetric=False, post_weights=None):
    """
    Combine multiple labeled matrices into one, with weighted data from
    all the matrices.

    mats: a list of matrices to blend.
    factors: List of scaling factor for each matrix.
      If None, the reciprocal of the first singular value is used.
    post_weights: List of weights to apply to each scaled matrix.
      You can use this to, for example, say that one matrix is twice as
      important as another. If None, no post-weighting is performed.
    symmetric: Use square_from_named_lists.
    """
    assert len(mats) > 0
    if len(mats) == 1:
        if factors is None: return mats[0]
        else: return mats[0] * factors[0]
    
    b_values = []
    b_row_labels = []
    b_col_labels = []
    
    if factors is None:
        factors = [blend_factor(mat) for mat in mats]

    if post_weights is not None:
        factors = [factor*post_weight for factor, post_weight in zip(factors, post_weights)]
    
    for mat, factor in zip(mats, factors):
        # FIXME: using bare find(), multiplying in numpy form, and
        # translating the labels manually would be a bit faster
        values, row_labels, col_labels = mat.named_lists()
        b_values.extend([v*factor for v in values])
        b_row_labels.extend(row_labels)
        b_col_labels.extend(col_labels)
    
    if symmetric:
        return SparseMatrix.square_from_named_lists(b_values, b_row_labels, b_col_labels)
    else:
        return SparseMatrix.from_named_lists(b_values, b_row_labels, b_col_labels)
Exemplo n.º 5
0
 def index_sparseMatrix(
         self
 ):  #create the divisi2 sparse matrix from already existing values
     self._matrix = divisiSparseMatrix.from_named_lists(
         self._values, self._rows, self._cols)
Exemplo n.º 6
0
 def create(self, data):
     values = map(itemgetter(0), data)
     rows = map(itemgetter(1), data)
     cols = map(itemgetter(2), data)
     self._matrix = divisiSparseMatrix.from_named_lists(values, rows, cols)
Exemplo n.º 7
0
 def create(self, data):
     values = map(itemgetter(0), data)
     rows = map(itemgetter(1), data)
     cols = map(itemgetter(2), data)
     self._matrix = divisiSparseMatrix.from_named_lists(values, rows, cols)