np.reshape()
np.reshape()创建一个副本更改array的shape,但是不会更改原来的array。
# importing the module
import numpy as np
# creating an array
gfg = np.array([1, 2, 3, 4, 5, 6])
print("Original array:")
display(gfg)
# using reshape()
print("Changed array")
display(gfg.reshape(2, 3))
print("Original array:")
display(gfg)
输出:

np.resize()
np.resize()就地更改(in-place)原array。注意np.resize()只会return None。
# importing the module
import numpy as np
# creating an array
gfg = np.array([1, 2, 3, 4, 5, 6])
print("Original array:")
display(gfg)
# using resize()
print("Changed array")
# this will print nothing as None is returned
display(gfg.resize(2, 3))
print("Original array:")
display(gfg)
输出:

本文详细介绍了numpy库中reshape()和resize()两个函数的使用。reshape()函数创建数组副本并改变其形状,而不影响原始数组;而resize()函数则直接修改原数组的大小。在使用reshape()时,会返回重塑后的数组,而resize()操作后返回None。请注意,resize()在某些情况下可能会导致数据丢失,因为它会调整元素数量以适应新的尺寸。
748

被折叠的 条评论
为什么被折叠?



