首页 > 解决方案 > 为什么我的变量索引矩阵只包含第一行?

问题描述

因此,我正在尝试制作一个具有不同颜色方块的图表,其中每个方块都根据存储在我的 csc_matrix nprob 中的概率着色,即 7x7。我图中的 x 和 y 应该是矩阵中的位置。

    nprob = prob/sum
    print(nprob.todense())
    i=[0,1,2,3,4,5,6]
    j=[0,1,2,3,4,5,6]

    print(nprob[i,j])
    x,y = np.meshgrid(np.arange(0,7,1),np.arange(0,7,1))  
    z,zx,zy=nprob[i,j],i,j
    fig, dens = plt.subplots()
    dens.set_title('probability density for...')
    dens.set_xlabel('i')
    dens.set_ylabel('t')     
    m = dens.pcolormesh(i, j, z, cmap = 'Blues', shading='auto')
    cbar=plt.colorbar(m)

当我打印 nprob[i,j] 我只是得到 nprob[0,j] (矩阵的第一行),我不知道为什么。这可能是我的问题的症结所在(我也可以为 i 选择任何整数并只获取矩阵的那一行,但我想要获取列表 i 表示的所有行)。我也得到了我的空图。所以我想我的问题是,当我用变量调用它时,为什么我的 7x7 矩阵的尺寸只有 1x7?这是错误:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-87-f3876b8770bc> in <module>
     13 dens.set_xlabel('i')
     14 dens.set_ylabel('t')
---> 15 m = dens.pcolormesh(i, j, z, cmap = 'Blues', shading='auto')
     16 cbar=plt.colorbar(m)

/opt/miniconda3/lib/python3.8/site-packages/matplotlib/__init__.py in inner(ax, data, *args, **kwargs)
   1445     def inner(ax, *args, data=None, **kwargs):
   1446         if data is None:
-> 1447             return func(ax, *map(sanitize_sequence, args), **kwargs)
   1448 
   1449         bound = new_sig.bind(ax, *args, **kwargs)

/opt/miniconda3/lib/python3.8/site-packages/matplotlib/axes/_axes.py in pcolormesh(self, alpha, norm, cmap, vmin, vmax, shading, antialiased, *args, **kwargs)
   6090         kwargs.setdefault('edgecolors', 'None')
   6091 
-> 6092         X, Y, C, shading = self._pcolorargs('pcolormesh', *args,
   6093                                             shading=shading, kwargs=kwargs)
   6094         Ny, Nx = X.shape

/opt/miniconda3/lib/python3.8/site-packages/matplotlib/axes/_axes.py in _pcolorargs(self, funcname, shading, *args, **kwargs)
   5609         if shading == 'flat':
   5610             if not (ncols in (Nx, Nx - 1) and nrows in (Ny, Ny - 1)):
-> 5611                 raise TypeError('Dimensions of C %s are incompatible with'
   5612                                 ' X (%d) and/or Y (%d); see help(%s)' % (
   5613                                     C.shape, Nx, Ny, funcname))

TypeError: Dimensions of C (1, 7) are incompatible with X (7) and/or Y (7); see help(pcolormesh)

标签: pythonsparse-matrix

解决方案


In [43]:     i=[0,1,2,3,4,5,6]
    ...:     j=[0,1,2,3,4,5,6]
    ...: 
In [44]: nprob[i,j]
Out[44]: matrix([[1., 1., 1., 1., 1., 1., 1.]])
In [45]: _.shape
Out[45]: (1, 7)

索引稀疏矩阵,甚至是具有相同索引数组的 numpy 数组,例如i返回对角线。这就是我们所说的“高级索引”。

因为sparse是在子类上建模的np.matrix,所以这个索引产生了一个np.matrix对象,它将是 2d,因此是 (1,7) 形状。numpy 数组上的相同索引会产生一个 1d 数组:

In [48]: x = np.arange(9).reshape(3,3)
In [49]: x
Out[49]: 
array([[0, 1, 2],
       [3, 4, 5],
       [6, 7, 8]])
In [50]: x[np.arange(3),np.arange(3)]
Out[50]: array([0, 4, 8])

目前尚不清楚您为什么要nprob使用[i,j]or 甚至[x,y]. 您不是在尝试选择nprob值的子集,是吗?


推荐阅读