Numpy 中 reshape() 和 resize() 方法的区别
这俩 numpy.reshape()和 numpy.resize() 方法用于更改 NumPy 数组的大小。它们的区别在于 reshape() 不改变原始数组,只返回改变后的数组,而 resize() 方法不返回任何内容,直接改变原始数组。
示例 1:使用 reshape()
Python3
# 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)
Python3
# 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)
输出:
示例 2:使用 resize()
Python3
# 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)
输出: