numpy.resize(a, new_shape)

  • a:要被resize的数组
  • new_shape:被resize的大小,int或者tuple类型

返回一个特定形状的数组
Return a new array with the specified shape.
If the new array is larger than the original array, then the new array is filled with repeated copies of a. Note that this behavior is different from a.resize(new_shape) which fills with zeros instead of repeated copies of a.

注意:numpy.resize(a, new_shape)和a.resize(new_shape)方法不同

  • 如果new_shape与a相同:二者操作一致,不同处为a.resize为in_place操作
  • 如果new_shape与a不同:二者操作不一致,numpy.resize会取a中的元素进行填充,而a.resize会用0进行填充
a = np.array([[0,1], [2,3]])
b = np.resize(a, (3, 3))
print(a)
print(b)
'''
取a中的元素进行填充
[[0 1]
 [2 3]]
[[0 1 2]
 [3 0 1]
 [2 3 0]]
'''

a.resize(3, 3)
print(a)
'''
用0进行填充,并且为in-place操作
[[0 1 2]
 [3 0 0]
 [0 0 0]]
'''
Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐