NumPy 数组操作 numpy.resize
此函数返回一个指定大小的新数组。如果新尺寸大于原始尺寸,则原始数组的条目将被重复复制。该函数接受以下参数。
numpy.resize(arr, shape)
在此,
序号 | 参数和说明 |
---|---|
1 | arr 要调整大小的输入数组 |
2 | shape 结果数组的新形状 |
示例
import numpy as np
a = np.array([[1,2,3],[4,5,6]])
print 'First array:'
print a
print '\n'
print 'The shape of first array:'
print a.shape
print '\n'
b = np.resize(a, (3,2))
print 'Second array:'
print b
print '\n'
print 'The shape of second array:'
print b.shape
print '\n'
# Observe that first row of a is repeated in b since size is bigger
print 'Resize the second array:'
b = np.resize(a,(3,3))
print b
上述程序将产生以下输出 –
First array:
[[1 2 3]
[4 5 6]]
The shape of first array:
(2, 3)
Second array:
[[1 2]
[3 4]
[5 6]]
The shape of second array:
(3, 2)
Resize the second array:
[[1 2 3]
[4 5 6]
[1 2 3]]