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)
输出: