3 回答

TA贡献1843条经验 获得超7个赞
1.NumPy中形状的含义
>>> a = numpy.arange(12)>>> a array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11])
a
┌────┬────┬────┬────┬────┬────┬────┬────┬────┬────┬────┬────┐
│ 0 │ 1 │ 2 │ 3 │ 4 │ 5 │ 6 │ 7 │ 8 │ 9 │ 10 │ 11 │
└────┴────┴────┴────┴────┴────┴────┴────┴────┴────┴────┴────┘
>>> a.flags C_CONTIGUOUS : True F_CONTIGUOUS : True OWNDATA : True WRITEABLE : True ALIGNED : True UPDATEIFCOPY : False>>> a.dtype dtype('int64')>>> a.itemsize8>>> a.strides(8,)>>> a.shape(12,)
(12,)
i
a
i= 0 1 2 3 4 5 6 7 8 9 10 11
┌────┬────┬────┬────┬────┬────┬────┬────┬────┬────┬────┬────┐
│ 0 │ 1 │ 2 │ 3 │ 4 │ 5 │ 6 │ 7 │ 8 │ 9 │ 10 │ 11 │
└────┴────┴────┴────┴────┴────┴────┴────┴────┴────┴────┴────┘
>>> b = a.reshape((3, 4))
b
a
i
j
b
i= 0 0 0 0 1 1 1 1 2 2 2 2
j= 0 1 2 3 0 1 2 3 0 1 2 3
┌────┬────┬────┬────┬────┬────┬────┬────┬────┬────┬────┬────┐
│ 0 │ 1 │ 2 │ 3 │ 4 │ 5 │ 6 │ 7 │ 8 │ 9 │ 10 │ 11 │
└────┴────┴────┴────┴────┴────┴────┴────┴────┴────┴────┴────┘
>>> b[2,1]9
order
>>> c = a.reshape((3, 4), order='F')
i= 0 1 2 0 1 2 0 1 2 0 1 2
j= 0 0 0 1 1 1 2 2 2 3 3 3
┌────┬────┬────┬────┬────┬────┬────┬────┬────┬────┬────┬────┐
│ 0 │ 1 │ 2 │ 3 │ 4 │ 5 │ 6 │ 7 │ 8 │ 9 │ 10 │ 11 │
└────┴────┴────┴────┴────┴────┴────┴────┴────┴────┴────┴────┘
>>> c[2,1]5
>>> d = a.reshape((12, 1))
d
i= 0 1 2 3 4 5 6 7 8 9 10 11
j= 0 0 0 0 0 0 0 0 0 0 0 0
┌────┬────┬────┬────┬────┬────┬────┬────┬────┬────┬────┬────┐
│ 0 │ 1 │ 2 │ 3 │ 4 │ 5 │ 6 │ 7 │ 8 │ 9 │ 10 │ 11 │
└────┴────┴────┴────┴────┴────┴────┴────┴────┴────┴────┴────┘
>>> d[10,0]10
>>> e = a.reshape((1, 2, 1, 6, 1))
i= 0 0 0 0 0 0 0 0 0 0 0 0
j= 0 0 0 0 0 0 1 1 1 1 1 1
k= 0 0 0 0 0 0 0 0 0 0 0 0
l= 0 1 2 3 4 5 0 1 2 3 4 5
m= 0 0 0 0 0 0 0 0 0 0 0 0
┌────┬────┬────┬────┬────┬────┬────┬────┬────┬────┬────┬────┐
│ 0 │ 1 │ 2 │ 3 │ 4 │ 5 │ 6 │ 7 │ 8 │ 9 │ 10 │ 11 │
└────┴────┴────┴────┴────┴────┴────┴────┴────┴────┴────┴────┘
>>> e[0,1,0,0,0]6
2.怎么办?
numpy.reshape
numpy.dot(M[:,0], numpy.ones((1, R)))
M[:,0].sum()
M.sum(axis=0)

TA贡献1735条经验 获得超5个赞
(R,)
(1,R)
ones((1,R))
ones(R)
np.dot(M[:,0], np.ones(R))
matrix
ndarray
*

TA贡献1790条经验 获得超9个赞
# 1 dimension with 2 elements, shape = (2,). # Note there's nothing after the comma.z=np.array([ # start dimension 10, # not a dimension 20 # not a dimension]) # end dimensionprint(z.shape)
(2,)
# 2 dimensions, each with 1 element, shape = (2,1)w=np.array([ # start outer dimension [10], # element is in an inner dimension [20] # element is in an inner dimension]) # end outer dimensionprint(w.shape)
(2,1)
添加回答
举报