Numpy中reshape()和resize()方法的区别
numpy.reshape()和numpy.resize()方法都是用来改变一个NumPy数组的大小。它们之间的区别是,reshape()不改变原始数组,只返回改变后的数组,而resize()方法不返回任何东西,直接改变原始数组。
例子1:使用reshape()。
# 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)
输出:
例子2:使用resize()。
# 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)
输出: