- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
有没有办法全局避免 matrix
出现在 numpy 计算的任何结果中?例如,当前如果您将 x
作为 numpy.ndarray
并将 y
作为 scipy.sparse.csc_matrix
,然后你说 x += y
,x
之后会变成一个 matrix
。有没有一种方法可以防止这种情况发生,即保持 x
为 ndarray
,更一般地说,在所有有 a 的地方继续使用 ndarray
矩阵
产生了吗?
最佳答案
我添加了scipy
标签,这是一个scipy.sparse
问题,不是np.matrix
问题。
In [250]: y=sparse.csr_matrix([[0,1],[1,0]])
In [251]: x=np.arange(2)
In [252]: y+x
Out[252]:
matrix([[0, 2],
[1, 1]])
稀疏 + 数组 => 矩阵
(作为旁注,np.matrix
是 np.ndarray
的子类。sparse.csr_matrix
不是子类。它有许多类似 numpy 的操作,但它在自己的代码中实现它们)。
In [255]: x += y
In [256]: x
Out[256]:
matrix([[0, 2],
[1, 1]])
从技术上讲,这不应该发生;实际上,它正在执行 x = x+y
为 x
分配一个新值,而不仅仅是修改 x
。
如果我首先将 y
转换为规则的密集 matrix
,我会得到一个错误。允许该操作会将一维数组更改为二维数组。
In [258]: x += y.todense()
...
ValueError: non-broadcastable output operand with shape (2,) doesn't match the broadcast shape (2,2)
将 x
更改为 2d 允许加法继续进行 - 无需将数组更改为矩阵:
In [259]: x=np.eye(2)
In [260]: x
Out[260]:
array([[ 1., 0.],
[ 0., 1.]])
In [261]: x += y.todense()
In [262]: x
Out[262]:
array([[ 1., 1.],
[ 1., 1.]])
一般来说,用稀疏矩阵执行加法/减法是很棘手的。它们是为矩阵乘法而设计的。乘法不像加法那样改变稀疏性。例如,y+1
使其变稠密。
如果不深入研究稀疏加法的编码方式,我会说 - 不要在未首先将 y
转换为之前尝试此 x+=...
操作一个密集的版本。
In [265]: x += y.A
In [266]: x
Out[266]:
array([[ 1., 2.],
[ 2., 1.]])
我想不出不这样做的好理由。
(我应该检查 scipy
github
是否有关于此的错误问题)。
scipy/sparse/compressed.py 有 csr
添加代码。 x+y
使用 x.__add__(y)
但有时会翻转为 y.__add__(x)
。 x+=y
使用 x.__iadd__(y)
。所以我可能还需要为 ndarray
检查 __iadd__
。
但是稀疏矩阵的基本加法是:
def __add__(self,other):
# First check if argument is a scalar
if isscalarlike(other):
if other == 0:
return self.copy()
else: # Now we would add this scalar to every element.
raise NotImplementedError('adding a nonzero scalar to a '
'sparse matrix is not supported')
elif isspmatrix(other):
if (other.shape != self.shape):
raise ValueError("inconsistent shapes")
return self._binopt(other,'_plus_')
elif isdense(other):
# Convert this matrix to a dense matrix and add them
return self.todense() + other
else:
return NotImplemented
所以 y+x
变成了 y.todense() + x
。 x+y
使用相同的东西。
不管 +=
的细节如何,很明显,将稀疏添加到密集(数组或 np.matrix)涉及将稀疏转换为密集。没有代码遍历稀疏值并将这些值有选择地添加到密集数组。
只有当数组都是稀疏的时,它才会执行特殊的稀疏加法。 y+y
有效,返回稀疏。 y+=y
失败并出现来自 sparse.base.__iadd__
的 NotImplmenentedError
。
这是我想出的最好的诊断序列,尝试了将 y
添加到 (2,2)
数组的各种方法。
In [348]: x=np.eye(2)
In [349]: x+y
Out[349]:
matrix([[ 1., 1.],
[ 1., 1.]])
In [350]: x+y.todense()
Out[350]:
matrix([[ 1., 1.],
[ 1., 1.]])
加法产生一个矩阵,但是可以在不改变x
类(或形状)的情况下将值写入x
In [351]: x[:] = x+y
In [352]: x
Out[352]:
array([[ 1., 1.],
[ 1., 1.]])
+=
与密集矩阵做同样的事情:
In [353]: x += y.todense()
In [354]: x
Out[354]:
array([[ 1., 2.],
[ 2., 1.]])
但是 +=sparse
中的一些东西改变了 x
In [355]: x += y
In [356]: x
Out[356]:
matrix([[ 1., 3.],
[ 3., 1.]])
进一步测试并查看 id(x)
和 x.__array_interface__
很明显 x += y
替换了 x
。即使 x
以 np.matrix
开头也是如此。所以稀疏 +=
不是就地操作。 x += y.todense()
是一个就地操作。
关于python - 避免在 numpy 操作中隐式转换为矩阵,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35546740/
我需要将文本放在 中在一个 Div 中,在另一个 Div 中,在另一个 Div 中。所以这是它的样子: #document Change PIN
奇怪的事情发生了。 我有一个基本的 html 代码。 html,头部, body 。(因为我收到了一些反对票,这里是完整的代码) 这是我的CSS: html { backgroun
我正在尝试将 Assets 中的一组图像加载到 UICollectionview 中存在的 ImageView 中,但每当我运行应用程序时它都会显示错误。而且也没有显示图像。 我在ViewDidLoa
我需要根据带参数的 perl 脚本的输出更改一些环境变量。在 tcsh 中,我可以使用别名命令来评估 perl 脚本的输出。 tcsh: alias setsdk 'eval `/localhome/
我使用 Windows 身份验证创建了一个新的 Blazor(服务器端)应用程序,并使用 IIS Express 运行它。它将显示一条消息“Hello Domain\User!”来自右上方的以下 Ra
这是我的方法 void login(Event event);我想知道 Kotlin 中应该如何 最佳答案 在 Kotlin 中通配符运算符是 * 。它指示编译器它是未知的,但一旦知道,就不会有其他类
看下面的代码 for story in book if story.title.length < 140 - var story
我正在尝试用 C 语言学习字符串处理。我写了一个程序,它存储了一些音乐轨道,并帮助用户检查他/她想到的歌曲是否存在于存储的轨道中。这是通过要求用户输入一串字符来完成的。然后程序使用 strstr()
我正在学习 sscanf 并遇到如下格式字符串: sscanf("%[^:]:%[^*=]%*[*=]%n",a,b,&c); 我理解 %[^:] 部分意味着扫描直到遇到 ':' 并将其分配给 a。:
def char_check(x,y): if (str(x) in y or x.find(y) > -1) or (str(y) in x or y.find(x) > -1):
我有一种情况,我想将文本文件中的现有行包含到一个新 block 中。 line 1 line 2 line in block line 3 line 4 应该变成 line 1 line 2 line
我有一个新项目,我正在尝试设置 Django 调试工具栏。首先,我尝试了快速设置,它只涉及将 'debug_toolbar' 添加到我的已安装应用程序列表中。有了这个,当我转到我的根 URL 时,调试
在 Matlab 中,如果我有一个函数 f,例如签名是 f(a,b,c),我可以创建一个只有一个变量 b 的函数,它将使用固定的 a=a1 和 c=c1 调用 f: g = @(b) f(a1, b,
我不明白为什么 ForEach 中的元素之间有多余的垂直间距在 VStack 里面在 ScrollView 里面使用 GeometryReader 时渲染自定义水平分隔线。 Scrol
我想知道,是否有关于何时使用 session 和 cookie 的指南或最佳实践? 什么应该和什么不应该存储在其中?谢谢! 最佳答案 这些文档很好地了解了 session cookie 的安全问题以及
我在 scipy/numpy 中有一个 Nx3 矩阵,我想用它制作一个 3 维条形图,其中 X 轴和 Y 轴由矩阵的第一列和第二列的值、高度确定每个条形的 是矩阵中的第三列,条形的数量由 N 确定。
假设我用两种不同的方式初始化信号量 sem_init(&randomsem,0,1) sem_init(&randomsem,0,0) 现在, sem_wait(&randomsem) 在这两种情况下
我怀疑该值如何存储在“WORD”中,因为 PStr 包含实际输出。? 既然Pstr中存储的是小写到大写的字母,那么在printf中如何将其给出为“WORD”。有人可以吗?解释一下? #include
我有一个 3x3 数组: var my_array = [[0,1,2], [3,4,5], [6,7,8]]; 并想获得它的第一个 2
我意识到您可以使用如下方式轻松检查焦点: var hasFocus = true; $(window).blur(function(){ hasFocus = false; }); $(win
我是一名优秀的程序员,十分优秀!