| by YoungTimes | No comments

Python- numpy数组初始化为相同的值

有时我们需要将numpy数组初始化为相同的值,numpy提供了一些方法帮助我们实现这个目的。

1. np.zeros

np.zeros返回来一个给定形状和类型的用0填充的数组。

numpy.zeros(shape, dtype=float, order='C')
np.zeros(5)
array([ 0.,  0.,  0.,  0.,  0.])

np.zeros((5,), dtype=int)
array([0, 0, 0, 0, 0])

np.zeros((2, 1))
array([[ 0.],
       [ 0.]])

np.zeros((2, 2))
array([[ 0.,  0.],
       [ 0.,  0.]])

2. np.ones

np.ones返回来一个给定形状和类型的用1填充的数组。

numpy.ones(shape, dtype=None, order='C')
>>> np.ones(5)
array([1., 1., 1., 1., 1.])

>>> np.ones((5,), dtype=int)
array([1, 1, 1, 1, 1])

>>> np.ones((2, 1))
array([[1.],
       [1.]])

>>> s = (2,2)
>>> np.ones(s)
array([[1.,  1.],
       [1.,  1.]])

初始化数组中的所有元素为10:

>>> import numpy as np
>>> a = np.ones((4,4)) * 10
[[10. 10. 10. 10.]
 [10. 10. 10. 10.]
 [10. 10. 10. 10.]
 [10. 10. 10. 10.]]

3. np.full

np.full返回来一个给定形状和类型的用fill_value填充的数组。

numpy.full(shape, fill_value, dtype=None, order='C')
>>> np.full((3, 5), 7, dtype=int)
array([[7, 7, 7, 7, 7],
       [7, 7, 7, 7, 7],
       [7, 7, 7, 7, 7]])

>>> np.full((2, 2), np.inf)
array([[inf, inf],
       [inf, inf]])

>>> np.full((2, 2), [1, 2])
array([[1, 2],
       [1, 2]])

4. 数组填充-fill

np.empty 方法用来创建一个指定形状(shape)、数据类型(dtype)且未初始化的数组。

numpy.empty(shape, dtype=float, order='C')

np.empy生成的数组元素为随机值。

>>> np.empty([2, 2])
array([[ -9.74499359e+001,   6.69583040e-309],
       [  2.13182611e-314,   3.06959433e-309]])         #uninitialized

>>> np.empty([2, 2], dtype=int)
array([[-1073741821, -1067949133],
       [  496041986,    19249760]])                     #uninitialized
>>> a = np.empty([2, 2])
>>> a.fill(20)
[[20. 20.]
 [20. 20.]]

>>> a[:] = 30
[[30. 30.]
 [30. 30.]]

5. np.repeat

np.repeat实现重复数组元素的功能。

numpy.repeat(a, repeats, axis=None)[source]
>>> np.repeat(3, 4)
array([3, 3, 3, 3])

>>> x = np.array([[1,2],[3,4]])
>>> np.repeat(x, 2)
array([1, 1, 2, 2, 3, 3, 4, 4])

>>> np.repeat(x, 3, axis = 1)
array([[1, 1, 1, 2, 2, 2],
       [3, 3, 3, 4, 4, 4]])

>>> np.repeat(x, [1, 2], axis = 0)
array([[1, 2],
       [3, 4],
       [3, 4]])

6. np.tile

np.tile把数组沿各个方向复制。

numpy.tile(A, reps)
>>> a = np.array([0, 1, 2])
>>> np.tile(a, 2)
array([0, 1, 2, 0, 1, 2])

>>> np.tile(a, (2, 2))
array([[0, 1, 2, 0, 1, 2],
       [0, 1, 2, 0, 1, 2]])

>>> np.tile(a, (2, 1, 2))
array([[[0, 1, 2, 0, 1, 2]],
       [[0, 1, 2, 0, 1, 2]]])
>>> b = np.array([[1, 2], [3, 4]])
>>> np.tile(b, 2)
array([[1, 2, 1, 2],
       [3, 4, 3, 4]])

>>> np.tile(b, (2, 1))
array([[1, 2],
       [3, 4],
       [1, 2],
       [3, 4]])

>>> c = np.array([1,2,3,4])
>>> np.tile(c,(4,1))
array([[1, 2, 3, 4],
       [1, 2, 3, 4],
       [1, 2, 3, 4],
       [1, 2, 3, 4]])

参考材料

https://numpy.org/doc/stable/reference/generated/numpy.repeat.html
https://numpy.org/doc/stable/reference/generated/numpy.tile.html
https://numpy.org/doc/stable/reference/generated/numpy.full.html
https://numpy.org/doc/stable/reference/generated/numpy.ones.html
https://numpy.org/doc/stable/reference/generated/numpy.zeros.html
https://www.itranslater.com/qa/details/2123767830796567552

发表评论