Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

delete csr python

import numpy as np
from scipy.sparse import csr_matrix

def delete_from_csr(mat, row_indices=[], col_indices=[]):
    """
    Remove the rows (denoted by ``row_indices``) and columns (denoted by ``col_indices``) from the CSR sparse matrix ``mat``.
    WARNING: Indices of altered axes are reset in the returned matrix
    """
    if not isinstance(mat, csr_matrix):
        raise ValueError("works only for CSR format -- use .tocsr() first")

    rows = []
    cols = []
    if row_indices:
        rows = list(row_indices)
    if col_indices:
        cols = list(col_indices)

    if len(rows) > 0 and len(cols) > 0:
        row_mask = np.ones(mat.shape[0], dtype=bool)
        row_mask[rows] = False
        col_mask = np.ones(mat.shape[1], dtype=bool)
        col_mask[cols] = False
        return mat[row_mask][:,col_mask]
    elif len(rows) > 0:
        mask = np.ones(mat.shape[0], dtype=bool)
        mask[rows] = False
        return mat[mask]
    elif len(cols) > 0:
        mask = np.ones(mat.shape[1], dtype=bool)
        mask[cols] = False
        return mat[:,mask]
    else:
        return mat
Comment

delete csr python

def delete_row_csr(mat, i):
    if not isinstance(mat, scipy.sparse.csr_matrix):
        raise ValueError("works only for CSR format -- use .tocsr() first")
    n = mat.indptr[i+1] - mat.indptr[i]
    if n > 0:
        mat.data[mat.indptr[i]:-n] = mat.data[mat.indptr[i+1]:]
        mat.data = mat.data[:-n]
        mat.indices[mat.indptr[i]:-n] = mat.indices[mat.indptr[i+1]:]
        mat.indices = mat.indices[:-n]
    mat.indptr[i:-1] = mat.indptr[i+1:]
    mat.indptr[i:] -= n
    mat.indptr = mat.indptr[:-1]
    mat._shape = (mat._shape[0]-1, mat._shape[1])
Comment

delete csr python

def delete_rows_csr(mat, indices):
    """
    Remove the rows denoted by ``indices`` form the CSR sparse matrix ``mat``.
    """
    if not isinstance(mat, scipy.sparse.csr_matrix):
        raise ValueError("works only for CSR format -- use .tocsr() first")
    indices = list(indices)
    mask = numpy.ones(mat.shape[0], dtype=bool)
    mask[indices] = False
    return mat[mask]
Comment

delete csr python

scipy.sparse.vstack([X[:i, :], X[i:, :]])
Comment

delete csr python

A0 = J.T * B
Comment

PREVIOUS NEXT
Code Example
Python :: geopandas plot fullscreen 
Python :: pandas return indices that match 
Python :: same quotes in a quotes 
Python :: recover dict from 0-d numpy array 
Python :: installing django on windows 
Python :: write a python program which accepts the user 
Python :: max sum slice python 5 - autopilot 
Python :: Using a generic exception block 
Python :: python file write all the bounding box coordinates using opencv 
Python :: count wit for loop pthoon 
Python :: install wget in anaconda 
Python :: Get the count of each categorical value (0 and 1) in labels 
Python :: pandas perform action on column 
Python :: .lowertkinter 
Python :: pandas df where 
Python :: c to python translator 
Python :: python remove warnings 
Python :: python remove list from nested list 
Python :: python hide terminal 
Python :: render() django 
Python :: join python 
Python :: mysql_python 
Python :: function in function python 
Python :: what is readline() in python 
Python :: python find if part of list is in list 
Python :: dataframe-name python 
Python :: Getting the data type 
Python :: reading a dataset in python 
Python :: django or flask 
Python :: how to load pretrained model in pytorch 
ADD CONTENT
Topic
Content
Source link
Name
2+2 =