我用Python编写了以下代码: import numpy as np
arr = numpy.array([[[1,2,3],[4,5,6]],[[7,8,9],[10,11,12]]])
print(arr[::-1]) # reverses the array
print(arr[:,::-1] # reverses the array in the second dimension
print(arr[:,:,::-1] # reverses the array in the third dimension,i.e. all elements
print(arr[...,::-1]#
假设我有一个包含10个元素的列表a
a = [1,2,3,4,5,6,7,8,9,0]
我只想取N元素的平均值,比如4,也就是说,average of [1,2,3,4]如何使用Numpy来实现这一点?
通常,我可以迭代一个列表,但这将花费我不想使用invest的额外列表。
文档没有给我提供任何东西。
我有一个代码片段,如下所示:
def slice_table(table, index_vector)
to_index_product = []
array_indices = []
for i, index in enumerate(index_vector):
if isinstance(index, list):
to_index_product.append(index)
array_indices.append(i)
index_product = np.ix_(*to_index_p
我已经学会了如何在一维上分割张量。
我已经学习了如何切片一个2D张量,给出一个特定值的一维张量。
两者都使用tf.gather(),但我很确定我需要tf.gather_nd(),尽管我使用它显然是错误的。
在numpy中,我有一个5x52D数组,我可以使用带有行和列索引的np.ix_()对2x2数组进行切片(对于行和列,我总是需要相同的索引,从而得到一个平方矩阵):
import numpy as np
a = np.array([[1,2,3,4,5],[2,1,6,7,8],[3,6,1,9,10],[4,7,9,1,11],[5,8,10,11,1]])
a
阵列([ 1,2,3,4