Python numpy.rank() Examples

The following are 28 code examples of numpy.rank(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module numpy , or try the search function .
Example #1
Source File: gaze_data.py    From simulated-unsupervised-tensorflow with Apache License 2.0 6 votes vote down vote up
def __init__(self, config, rng=None):
    self.rng = np.random.RandomState(1) if rng is None else rng

    self.data_path = os.path.join(config.data_dir, 'gaze')
    self.sample_path = os.path.join(self.data_path, config.sample_dir)
    self.batch_size = config.batch_size
    self.debug = config.debug

    self.real_data, synthetic_image_path = load(config, self.data_path, self.sample_path, rng)

    self.synthetic_data_paths = np.array(glob(os.path.join(synthetic_image_path, '*_cropped.png')))
    self.synthetic_data_dims = list(imread(self.synthetic_data_paths[0]).shape) + [1]

    self.synthetic_data_paths.sort()

    if np.rank(self.real_data) == 3:
      self.real_data = np.expand_dims(self.real_data, -1)
    
    self.real_p = 0 
Example #2
Source File: test_deprecations.py    From pySINDy with MIT License 5 votes vote down vote up
def test(self):
        a = np.arange(10)
        assert_warns(np.VisibleDeprecationWarning, np.rank, a) 
Example #3
Source File: test_deprecations.py    From keras-lambda with MIT License 5 votes vote down vote up
def test(self):
        a = np.arange(10)
        assert_warns(np.VisibleDeprecationWarning, np.rank, a) 
Example #4
Source File: test_deprecations.py    From twitter-stock-recommendation with MIT License 5 votes vote down vote up
def test(self):
        a = np.arange(10)
        assert_warns(np.VisibleDeprecationWarning, np.rank, a) 
Example #5
Source File: test_deprecations.py    From Serverless-Deep-Learning-with-TensorFlow-and-AWS-Lambda with MIT License 5 votes vote down vote up
def test(self):
        a = np.arange(10)
        assert_warns(np.VisibleDeprecationWarning, np.rank, a) 
Example #6
Source File: test_quantity_non_ufuncs.py    From Carnets with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_lstsq(self):
        b = np.array([1., 2., 4.]) * u.m / u.s
        x, residuals, rank, s = np.linalg.lstsq(self.q, b, rcond=None)
        xx, residualsx, rankx, sx = np.linalg.lstsq(self.q.value, b.value,
                                                    rcond=None)
        xx <<= b.unit / self.q.unit
        residualsx <<= b.unit ** 2
        sx <<= self.q.unit

        assert_array_equal(x, xx)
        assert_array_equal(residuals, residualsx)
        assert_array_equal(s, sx)
        assert rank == rankx
        assert u.allclose(self.q @ x, b)

        # Also do one where we can check the answer...
        m = np.eye(3)
        b = np.arange(3) * u.m
        x, residuals, rank, s = np.linalg.lstsq(m, b, rcond=1.*u.percent)
        assert_array_equal(x, b)
        assert np.all(residuals == 0 * u.m**2)
        assert rank == 3
        assert_array_equal(s, np.array([1., 1., 1.]) << u.one)

        with pytest.raises(u.UnitsError):
            np.linalg.lstsq(m, b, rcond=1.*u.s) 
Example #7
Source File: test_deprecations.py    From coffeegrindsize with MIT License 5 votes vote down vote up
def test(self):
        a = np.arange(10)
        assert_warns(np.VisibleDeprecationWarning, np.rank, a) 
Example #8
Source File: test_deprecations.py    From elasticintel with GNU General Public License v3.0 5 votes vote down vote up
def test(self):
        a = np.arange(10)
        assert_warns(np.VisibleDeprecationWarning, np.rank, a) 
Example #9
Source File: test_deprecations.py    From ImageFusion with MIT License 5 votes vote down vote up
def test(self):
        a = np.arange(10)
        assert_warns(np.VisibleDeprecationWarning, np.rank, a) 
Example #10
Source File: test_deprecations.py    From mxnet-lambda with Apache License 2.0 5 votes vote down vote up
def test(self):
        a = np.arange(10)
        assert_warns(np.VisibleDeprecationWarning, np.rank, a) 
Example #11
Source File: test_deprecations.py    From recruit with Apache License 2.0 5 votes vote down vote up
def test(self):
        a = np.arange(10)
        assert_warns(np.VisibleDeprecationWarning, np.rank, a) 
Example #12
Source File: test_deprecations.py    From predictive-maintenance-using-machine-learning with Apache License 2.0 5 votes vote down vote up
def test(self):
        a = np.arange(10)
        assert_warns(np.VisibleDeprecationWarning, np.rank, a) 
Example #13
Source File: test_deprecations.py    From GraphicDesignPatternByPython with MIT License 5 votes vote down vote up
def test(self):
        a = np.arange(10)
        assert_warns(np.VisibleDeprecationWarning, np.rank, a) 
Example #14
Source File: test_deprecations.py    From Mastering-Elasticsearch-7.0 with MIT License 5 votes vote down vote up
def test(self):
        a = np.arange(10)
        assert_warns(np.VisibleDeprecationWarning, np.rank, a) 
Example #15
Source File: var_regression_matrix.py    From Quijote-simulations with MIT License 5 votes vote down vote up
def var_regression_matrix(H, x, model, sigma=1):
    """
    Compute the variance of the 'regression error'.
    
    Parameters
    ----------
    H : 2d-array
        The regression matrix
    x : 2d-array
        The coordinates to calculate the regression error variance at.
    model : str
        A string of tokens that define the regression model (e.g. 
        '1 x1 x2 x1*x2')
    sigma : scalar
        An estimate of the variance (default: 1).
    
    Returns
    -------
    var : scalar
        The variance of the regression error, evaluated at ``x``.
        
    """
    x = np.atleast_2d(x)
    H = np.atleast_2d(H)
    
    if x.shape[0]==1:
        x = x.T
    
    if np.rank(H)<(np.dot(H.T, H)).shape[0]:
        raise ValueError("model and DOE don't suit together")
    
    x_mod = build_regression_matrix(x, model)
    var = sigma**2*np.dot(np.dot(x_mod.T, np.linalg.inv(np.dot(H.T, H))), x_mod)
    return var 
Example #16
Source File: sputils.py    From Computable with MIT License 5 votes vote down vote up
def isshape(x):
    """Is x a valid 2-tuple of dimensions?
    """
    try:
        # Assume it's a tuple of matrix dimensions (M, N)
        (M, N) = x
    except:
        return False
    else:
        if isintlike(M) and isintlike(N):
            if np.rank(M) == 0 and np.rank(N) == 0:
                return True
        return False 
Example #17
Source File: coo.py    From Computable with MIT License 5 votes vote down vote up
def getnnz(self):
        nnz = len(self.data)
        if nnz != len(self.row) or nnz != len(self.col):
            raise ValueError('row, column, and data array must all be the same length')

        if np.rank(self.data) != 1 or np.rank(self.row) != 1 or np.rank(self.col) != 1:
            raise ValueError('row, column, and data arrays must have rank 1')

        return int(nnz) 
Example #18
Source File: test_deprecations.py    From vnpy_crypto with MIT License 5 votes vote down vote up
def test(self):
        a = np.arange(10)
        assert_warns(np.VisibleDeprecationWarning, np.rank, a) 
Example #19
Source File: utils.py    From calfem-python with MIT License 5 votes vote down vote up
def scalfact2(ex,ey,ed,rat=0.2):
    """
    Determine scale factor for drawing computational results, such as 
    displacements, section forces or flux.
    
    Parameters:
    
        ex, ey      element node coordinates
                       
        ed          element displacement matrix or section force matrix
    
        rat         relation between illustrated quantity and element size. 
                    If not specified, 0.2 is used.
        
    """

    nen = -1
    if ex.shape != ey.shape:
        print("ex and ey shapes do not match.")
        return 1.0
    
    dlmax = 0.
    edmax = 1.
    
    if np.rank(ex)==1:
        nen = ex.shape[0]
        nel = 1
        dxmax=ex.T.max()-ex.T.min()
        dymax=ey.T.max()-ey.T.min()
        dlmax=max(dxmax,dymax);
        edmax=abs(ed).max();
    else:
        nen = ex.shape[1]
        nel = ex.shape[0]
        dxmax=ex.T.max()-ex.T.min()
        dymax=ey.T.max()-ey.T.min()
        dlmax=max(dxmax,dymax);
        edmax=abs(ed).max();
        
    k = rat
    return k*dlmax/edmax 
Example #20
Source File: test_deprecations.py    From auto-alt-text-lambda-api with MIT License 5 votes vote down vote up
def test(self):
        a = np.arange(10)
        assert_warns(np.VisibleDeprecationWarning, np.rank, a) 
Example #21
Source File: statTools.py    From hoggorm with BSD 2-Clause "Simplified" License 5 votes vote down vote up
def matrixRank(arr, tol=1e-8):
    """
    Computes the rank of an array/matrix, i.e. number of linearly independent
    variables. This is not the same as numpy.rank() which only returns the
    number of ways (2-way, 3-way, etc) an array/matrix has.

    PARAMETERS
    ----------
    arrX : numpy array
        A numpy array containing the data

    RETURNS
    -------
    scalar
        Rank of matrix.

    Examples
    --------
    >>> import hoggorm as ho
    >>>
    >>> # Get the rank of the data
    >>> ho.matrixRank(myData)
    >>> 8

    """
    if len(arr.shape) != 2:
        raise ValueError('Input must be a 2-d array or Matrix object')

    s = numpy.linalg.svd(arr, compute_uv=0)
    return numpy.sum(numpy.where(s > tol, 1, 0)) 
Example #22
Source File: fromnumeric.py    From mxnet-lambda with Apache License 2.0 4 votes vote down vote up
def rank(a):
    """
    Return the number of dimensions of an array.

    If `a` is not already an array, a conversion is attempted.
    Scalars are zero dimensional.

    .. note::
        This function is deprecated in NumPy 1.9 to avoid confusion with
        `numpy.linalg.matrix_rank`. The ``ndim`` attribute or function
        should be used instead.

    Parameters
    ----------
    a : array_like
        Array whose number of dimensions is desired. If `a` is not an array,
        a conversion is attempted.

    Returns
    -------
    number_of_dimensions : int
        The number of dimensions in the array.

    See Also
    --------
    ndim : equivalent function
    ndarray.ndim : equivalent property
    shape : dimensions of array
    ndarray.shape : dimensions of array

    Notes
    -----
    In the old Numeric package, `rank` was the term used for the number of
    dimensions, but in NumPy `ndim` is used instead.

    Examples
    --------
    >>> np.rank([1,2,3])
    1
    >>> np.rank(np.array([[1,2,3],[4,5,6]]))
    2
    >>> np.rank(1)
    0

    """
    # 2014-04-12, 1.9
    warnings.warn(
        "`rank` is deprecated; use the `ndim` attribute or function instead. "
        "To find the rank of a matrix see `numpy.linalg.matrix_rank`.",
        VisibleDeprecationWarning, stacklevel=2)
    try:
        return a.ndim
    except AttributeError:
        return asarray(a).ndim 
Example #23
Source File: bsr.py    From Computable with MIT License 4 votes vote down vote up
def check_format(self, full_check=True):
        """check whether the matrix format is valid

            *Parameters*:
                full_check:
                    True  - rigorous check, O(N) operations : default
                    False - basic check, O(1) operations

        """
        M,N = self.shape
        R,C = self.blocksize

        # index arrays should have integer data types
        if self.indptr.dtype.kind != 'i':
            warn("indptr array has non-integer dtype (%s)"
                    % self.indptr.dtype.name)
        if self.indices.dtype.kind != 'i':
            warn("indices array has non-integer dtype (%s)"
                    % self.indices.dtype.name)

        # only support 32-bit ints for now
        self.indptr = np.asarray(self.indptr, np.intc)
        self.indices = np.asarray(self.indices, np.intc)
        self.data = to_native(self.data)

        # check array shapes
        if np.rank(self.indices) != 1 or np.rank(self.indptr) != 1:
            raise ValueError("indices, and indptr should be rank 1")
        if np.rank(self.data) != 3:
            raise ValueError("data should be rank 3")

        # check index pointer
        if (len(self.indptr) != M//R + 1):
            raise ValueError("index pointer size (%d) should be (%d)" %
                                (len(self.indptr), M//R + 1))
        if (self.indptr[0] != 0):
            raise ValueError("index pointer should start with 0")

        # check index and data arrays
        if (len(self.indices) != len(self.data)):
            raise ValueError("indices and data should have the same size")
        if (self.indptr[-1] > len(self.indices)):
            raise ValueError("Last value of index pointer should be less than "
                                "the size of index and data arrays")

        self.prune()

        if full_check:
            # check format validity (more expensive)
            if self.nnz > 0:
                if self.indices.max() >= N//C:
                    raise ValueError("column index values must be < %d (now max %d)" % (N//C, self.indices.max()))
                if self.indices.min() < 0:
                    raise ValueError("column index values must be >= 0")
                if np.diff(self.indptr).min() < 0:
                    raise ValueError("index pointer values must form a "
                                        "non-decreasing sequence")

        # if not self.has_sorted_indices():
        #    warn('Indices were not in sorted order. Sorting indices.')
        #    self.sort_indices(check_first=False) 
Example #24
Source File: fromnumeric.py    From Splunking-Crime with GNU Affero General Public License v3.0 4 votes vote down vote up
def rank(a):
    """
    Return the number of dimensions of an array.

    If `a` is not already an array, a conversion is attempted.
    Scalars are zero dimensional.

    .. note::
        This function is deprecated in NumPy 1.9 to avoid confusion with
        `numpy.linalg.matrix_rank`. The ``ndim`` attribute or function
        should be used instead.

    Parameters
    ----------
    a : array_like
        Array whose number of dimensions is desired. If `a` is not an array,
        a conversion is attempted.

    Returns
    -------
    number_of_dimensions : int
        The number of dimensions in the array.

    See Also
    --------
    ndim : equivalent function
    ndarray.ndim : equivalent property
    shape : dimensions of array
    ndarray.shape : dimensions of array

    Notes
    -----
    In the old Numeric package, `rank` was the term used for the number of
    dimensions, but in NumPy `ndim` is used instead.

    Examples
    --------
    >>> np.rank([1,2,3])
    1
    >>> np.rank(np.array([[1,2,3],[4,5,6]]))
    2
    >>> np.rank(1)
    0

    """
    # 2014-04-12, 1.9
    warnings.warn(
        "`rank` is deprecated; use the `ndim` attribute or function instead. "
        "To find the rank of a matrix see `numpy.linalg.matrix_rank`.",
        VisibleDeprecationWarning, stacklevel=2)
    try:
        return a.ndim
    except AttributeError:
        return asarray(a).ndim 
Example #25
Source File: fromnumeric.py    From elasticintel with GNU General Public License v3.0 4 votes vote down vote up
def rank(a):
    """
    Return the number of dimensions of an array.

    If `a` is not already an array, a conversion is attempted.
    Scalars are zero dimensional.

    .. note::
        This function is deprecated in NumPy 1.9 to avoid confusion with
        `numpy.linalg.matrix_rank`. The ``ndim`` attribute or function
        should be used instead.

    Parameters
    ----------
    a : array_like
        Array whose number of dimensions is desired. If `a` is not an array,
        a conversion is attempted.

    Returns
    -------
    number_of_dimensions : int
        The number of dimensions in the array.

    See Also
    --------
    ndim : equivalent function
    ndarray.ndim : equivalent property
    shape : dimensions of array
    ndarray.shape : dimensions of array

    Notes
    -----
    In the old Numeric package, `rank` was the term used for the number of
    dimensions, but in NumPy `ndim` is used instead.

    Examples
    --------
    >>> np.rank([1,2,3])
    1
    >>> np.rank(np.array([[1,2,3],[4,5,6]]))
    2
    >>> np.rank(1)
    0

    """
    # 2014-04-12, 1.9
    warnings.warn(
        "`rank` is deprecated; use the `ndim` attribute or function instead. "
        "To find the rank of a matrix see `numpy.linalg.matrix_rank`.",
        VisibleDeprecationWarning, stacklevel=2)
    try:
        return a.ndim
    except AttributeError:
        return asarray(a).ndim 
Example #26
Source File: fromnumeric.py    From vnpy_crypto with MIT License 4 votes vote down vote up
def rank(a):
    """
    Return the number of dimensions of an array.

    If `a` is not already an array, a conversion is attempted.
    Scalars are zero dimensional.

    .. note::
        This function is deprecated in NumPy 1.9 to avoid confusion with
        `numpy.linalg.matrix_rank`. The ``ndim`` attribute or function
        should be used instead.

    Parameters
    ----------
    a : array_like
        Array whose number of dimensions is desired. If `a` is not an array,
        a conversion is attempted.

    Returns
    -------
    number_of_dimensions : int
        The number of dimensions in the array.

    See Also
    --------
    ndim : equivalent function
    ndarray.ndim : equivalent property
    shape : dimensions of array
    ndarray.shape : dimensions of array

    Notes
    -----
    In the old Numeric package, `rank` was the term used for the number of
    dimensions, but in NumPy `ndim` is used instead.

    Examples
    --------
    >>> np.rank([1,2,3])
    1
    >>> np.rank(np.array([[1,2,3],[4,5,6]]))
    2
    >>> np.rank(1)
    0

    """
    # 2014-04-12, 1.9
    warnings.warn(
        "`rank` is deprecated; use the `ndim` attribute or function instead. "
        "To find the rank of a matrix see `numpy.linalg.matrix_rank`.",
        VisibleDeprecationWarning, stacklevel=2)
    try:
        return a.ndim
    except AttributeError:
        return asarray(a).ndim 
Example #27
Source File: fromnumeric.py    From auto-alt-text-lambda-api with MIT License 4 votes vote down vote up
def rank(a):
    """
    Return the number of dimensions of an array.

    If `a` is not already an array, a conversion is attempted.
    Scalars are zero dimensional.

    .. note::
        This function is deprecated in NumPy 1.9 to avoid confusion with
        `numpy.linalg.matrix_rank`. The ``ndim`` attribute or function
        should be used instead.

    Parameters
    ----------
    a : array_like
        Array whose number of dimensions is desired. If `a` is not an array,
        a conversion is attempted.

    Returns
    -------
    number_of_dimensions : int
        The number of dimensions in the array.

    See Also
    --------
    ndim : equivalent function
    ndarray.ndim : equivalent property
    shape : dimensions of array
    ndarray.shape : dimensions of array

    Notes
    -----
    In the old Numeric package, `rank` was the term used for the number of
    dimensions, but in Numpy `ndim` is used instead.

    Examples
    --------
    >>> np.rank([1,2,3])
    1
    >>> np.rank(np.array([[1,2,3],[4,5,6]]))
    2
    >>> np.rank(1)
    0

    """
    # 2014-04-12, 1.9
    warnings.warn(
        "`rank` is deprecated; use the `ndim` attribute or function instead. "
        "To find the rank of a matrix see `numpy.linalg.matrix_rank`.",
        VisibleDeprecationWarning)
    try:
        return a.ndim
    except AttributeError:
        return asarray(a).ndim 
Example #28
Source File: fromnumeric.py    From keras-lambda with MIT License 4 votes vote down vote up
def rank(a):
    """
    Return the number of dimensions of an array.

    If `a` is not already an array, a conversion is attempted.
    Scalars are zero dimensional.

    .. note::
        This function is deprecated in NumPy 1.9 to avoid confusion with
        `numpy.linalg.matrix_rank`. The ``ndim`` attribute or function
        should be used instead.

    Parameters
    ----------
    a : array_like
        Array whose number of dimensions is desired. If `a` is not an array,
        a conversion is attempted.

    Returns
    -------
    number_of_dimensions : int
        The number of dimensions in the array.

    See Also
    --------
    ndim : equivalent function
    ndarray.ndim : equivalent property
    shape : dimensions of array
    ndarray.shape : dimensions of array

    Notes
    -----
    In the old Numeric package, `rank` was the term used for the number of
    dimensions, but in Numpy `ndim` is used instead.

    Examples
    --------
    >>> np.rank([1,2,3])
    1
    >>> np.rank(np.array([[1,2,3],[4,5,6]]))
    2
    >>> np.rank(1)
    0

    """
    # 2014-04-12, 1.9
    warnings.warn(
        "`rank` is deprecated; use the `ndim` attribute or function instead. "
        "To find the rank of a matrix see `numpy.linalg.matrix_rank`.",
        VisibleDeprecationWarning)
    try:
        return a.ndim
    except AttributeError:
        return asarray(a).ndim