Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
181 views
in Technique[技术] by (71.8m points)

python - Why does my matrix with variable indices include only the first row?

So, I'm trying to make a graph with different colored squares where each one is colored according to the probability stored in my csc_matrix nprob, which is 7x7. The x and y in my graph are just supposed to be the place in the matrix.

    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)

When I print nprob[i,j] I'm just getting nprob[0,j] (first row of the matrix) and I don't know why. That's probably the crux of my issue (I can also choose any integer for i and get just that row of the matrix, but I'm wanting to get all the rows denoted by the list i). I also get my empty graph. So I think my question is, why are the dimensions of my 7x7 matrix just 1x7 when I call it with variables? Here's the error:

---------------------------------------------------------------------------
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)
question from:https://stackoverflow.com/questions/65865018/why-does-my-matrix-with-variable-indices-include-only-the-first-row

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
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)

Indexing a sparse matrix, or even a numpy array with identical index arrays like i returns the diagonal. That's what we call "advanced indexing".

Because sparse is modeled on the np.matrix subclass, this indexing produced a np.matrix object, which will be 2d, hence the (1,7) shape. The same indexing on a numpy array produces a 1d array:

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])

It isn't clear why you are indexing nprob with [i,j] or even [x,y]. You aren't trying to select a subset the nprob values, are you?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...