numpy.any(axis=i) 用于 scipy.sparse [英] numpy.any(axis=i) for scipy.sparse

查看:70
本文介绍了numpy.any(axis=i) 用于 scipy.sparse的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

import numpya = numpy.array([[0, 1, 0, 0],[1, 0, 0, 0],[0, 0, 1, 0],[0, 0, 0, 0],[0, 0, 0, 0],])numpy.any(a,axis=0)numpy.any(a,axis=1)

生产

array([真、真、真、假])数组([真,真,真,假,假])

不过之后

from scipy import sparsea = sparse.csr_matrix(a)

相同的 numpy.any(a, axis) 调用产生

<5x4 稀疏矩阵,类型为 ''具有压缩稀疏行格式的 3 个存储元素>

回溯(最近一次调用最后一次):文件<stdin>",第 1 行,在 <module> 中.文件<__array_function__ internals>",第 5 行,在任何文件C:\Users\user\.conda\envs\py385\lib\site-packages\numpy\core\fromnumeric.py",第 2330 行,在任何return _wrapreduction(a, np.logical_or, 'any', axis, None, out, keepdims=keepdims)文件C:\Users\user\.conda\envs\py385\lib\site-packages\numpy\core\fromnumeric.py",第 87 行,在 _wrapreductionreturn ufunc.reduce(obj,axis,dtype,out,**passkwargs)numpy.AxisError:轴 1 超出维度 0 数组的范围

当然,a 实际上是一个非常大的稀疏矩阵,转换为普通的 numpy 数组不是一种选择.如何为 csr_matrix 和其他 scipy.sparse 矩阵获取相同(或等效)的结果?

添加:

根据官方scipy文档中的使用信息,

<块引用>

尽管它们与 NumPy 数组相似,强烈不鼓励直接在这些矩阵上使用 NumPy 函数,因为 NumPy 可能无法正确转换它们以进行计算,从而导致意外(和不正确)的结果.如果您确实想将 NumPy 函数应用于这些矩阵,请首先检查 SciPy 是否对给定的稀疏矩阵类有自己的实现,或将稀疏矩阵转换为 NumPy 数组(例如,使用 toarray() 类的方法),然后再应用该方法.

我正在寻找它自己的实现";或同等学历.

解决方案

你可以在 bool 数组上使用 sum 而不是 any

导入numpya = numpy.array([[0, 1, 0, 0],[1, 0, 0, 0],[0, 0, 1, 0],[0, 0, 0, 0],[0, 0, 0, 0],])从 scipy 导入稀疏a = sparse.csr_matrix(a.astype(bool))# 在 bool 数组上使用 sum 而不是 any打印(a.sum(axis=0).astype(bool))打印(a.sum(axis=1).flatten().astype(bool))

输出:

[[真真真假]][[真真真假假]]

如果你想做所有",那会有点棘手,因为 scipy 似乎没有prod"的实现.但是这篇文章对这种情况给出了答案.>

import numpy
a = numpy.array([
    [0, 1, 0, 0],
    [1, 0, 0, 0],
    [0, 0, 1, 0],
    [0, 0, 0, 0],
    [0, 0, 0, 0],
])
numpy.any(a, axis=0)
numpy.any(a, axis=1)

produces

array([ True,  True,  True, False])
array([ True,  True,  True, False, False])

However, after

from scipy import sparse
a = sparse.csr_matrix(a)

the same numpy.any(a, axis) calls produces

<5x4 sparse matrix of type '<class 'numpy.intc'>'
        with 3 stored elements in Compressed Sparse Row format>

and

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<__array_function__ internals>", line 5, in any
  File "C:\Users\user\.conda\envs\py385\lib\site-packages\numpy\core\fromnumeric.py", line 2330, in any
    return _wrapreduction(a, np.logical_or, 'any', axis, None, out, keepdims=keepdims)
  File "C:\Users\user\.conda\envs\py385\lib\site-packages\numpy\core\fromnumeric.py", line 87, in _wrapreduction
    return ufunc.reduce(obj, axis, dtype, out, **passkwargs)
numpy.AxisError: axis 1 is out of bounds for array of dimension 0

Of course, a is actually so large a sparse matrix that converting to normal numpy array is not an option. How can I acquire the same (or equivalent) result for a csr_matrix and other scipy.sparse matrices?

ADDED:

According to Usage information in official scipy documentation,

Despite their similarity to NumPy arrays, it is strongly discouraged to use NumPy functions directly on these matrices because NumPy may not properly convert them for computations, leading to unexpected (and incorrect) results. If you do want to apply a NumPy function to these matrices, first check if SciPy has its own implementation for the given sparse matrix class, or convert the sparse matrix to a NumPy array (e.g., using the toarray() method of the class) first before applying the method.

I'm looking for "its own implementation" or equivalent.

解决方案

you can use sum instead of any on bool arrays

import numpy
a = numpy.array([
    [0, 1, 0, 0],
    [1, 0, 0, 0],
    [0, 0, 1, 0],
    [0, 0, 0, 0],
    [0, 0, 0, 0],
])

from scipy import sparse
a = sparse.csr_matrix(a.astype(bool))
# Use sum instead of any on a bool array
print(a.sum(axis=0).astype(bool))
print(a.sum(axis=1).flatten().astype(bool))

output:

[[ True  True  True False]]
[[ True  True  True False False]]

If you want to do 'all' that would be a little tricky since scipy doesn't appear to have an implementation for 'prod'. But this post has an answer for that case.

这篇关于numpy.any(axis=i) 用于 scipy.sparse的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆