Numpy 笔记——数组创建

前言

  记录各种创建数组相关 API 使用方法,一些不太常用的参数功能选择性忽略,例如 np.array(object, dtype=None, *, copy=True, order=‘K’, subok=False, ndmin=0),常用的就前两个输入,后续的忽略。

1. np.array 与 np.asarray

  功能基本相同,通常用于把列表、元组等转为 numpy.ndarray
  区别:当输入的 object 本身就是 numpy.ndarray 时,np.array() 默认会拷贝原数组,修改原数组后新数组的值不变;而 np.asarray() 改变一个数组的值后另一个数组会跟着变。

np.array(object, dtype=None)
np.asarray(object, dtype=None)
----------------------------------------------------------------
a1 = np.random.random((2, 3))
a2 = np.array(a1)
a3 = np.asarray(a1)
a3[0, 0] = 0
print(a1)
print(a2)
print(a3)
'''
[[0.         0.80722796 0.58824907]
 [0.46028017 0.15263737 0.08888952]]
[[0.19299596 0.80722796 0.58824907]
 [0.46028017 0.15263737 0.08888952]]
[[0.         0.80722796 0.58824907]
 [0.46028017 0.15263737 0.08888952]]
'''

2. 默认值

2.1 np.empty、np.zeros、np.ones

  shape 通常为列表、元组、int;prototype 通常为列表、元组、数组,按 prototype 的形状创建数组。

np.empty(shape, dtype=float)
np.zeros(shape, dtype=float)
np.ones(shape, dtype=float)

np.empty_like(prototype, dtype=None)
np.zeros_like(prototype, dtype=None)
np.ones_like(prototype, dtype=None)

2.2 np.full

  np.full() 在设定 fill_value 时有较大的操作空间:

np.full(shape, fill_value, dtype=None)
np.full_like(prototype, fill_value, dtype=None)
----------------------------------------------------------------
>>> np.full([2, 3], np.inf)
[[inf inf inf]
 [inf inf inf]]
>>> np.full([2, 3], [1, 2, 3])
[[1 2 3]
 [1 2 3]]
>>> np.full([2, 3], [[1], [2]])
[[1 1 1]
 [2 2 2]]

2.3 np.eye

np.eye(N, M=None, k=0, dtype=float)
----------------------------------------------------------------
>>> np.eye(3)
[[1. 0. 0.]
 [0. 1. 0.]
 [0. 0. 1.]]
>>> np.eye(3, M=4)
[[1. 0. 0. 0.]
 [0. 1. 0. 0.]
 [0. 0. 1. 0.]]
>>> np.eye(3, k=1)
[[0. 1. 0.]
 [0. 0. 1.]
 [0. 0. 0.]]

3. 固定间隔

3.1 np.arange

  np.arange()range() 功能类似,但支持用浮点数作为 step

np.arange([start,] stop[, step,], dtype=None)
----------------------------------------------------------------
>>> np.arange(5)
[0, 1, 2, 3, 4]
>>> np.arange(2, 5)
[2, 3, 4]
>>> np.arange(2, 5, 0.5)
[2., 2.5, 3., 3.5, 4., 4.5]

3.2 np.linspace

np.linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None, axis=0)
----------------------------------------------------------------
>>> np.linspace(2, 3, 5)
[2., 2.25, 2.5, 2.75, 3.]
>>> np.linspace(2, 3, 5, endpoint=False)
[2., 2.2, 2.4, 2.6, 2.8]
>>> np.linspace(2, 3, 5, retstep=True)
(array([2., 2.25, 2.5, 2.75, 3.]), 0.25)

3.3 np.logspace

np.logspace(start, stop, num=50, endpoint=True, base=10, dtype=None, axis=0)
----------------------------------------------------------------
>>> np.logspace(2, 4, 3)
[100., 1000., 10000.]
>>> np.logspace(2, 4, 3, base=2)
[4., 8., 16.]

3.4 np.meshgrid

np.meshgrid(*xi, copy=True, sparse=False, indexing='xy')
----------------------------------------------------------------
x = np.arange(1, 4)
y = np.arange(1, 3)
xv, yv = np.meshgrid(x, y)
'''
xv: [[1 2 3]
 	 [1 2 3]] 
yv: [[1 1 1]
 	 [2 2 2]]
'''
xv, yv = np.meshgrid(x, y, indexing='ij')
'''
xv: [[1 1]
 	 [2 2]
 	 [3 3]]
yv: [[1 2]
 	 [1 2]
 	 [1 2]]
'''

x = np.arange(1, 4)
y = np.arange(1, 4)
z = np.arange(1, 4)
xv, yv, zv = np.meshgrid(x, y, z)
'''
xv: [[[1 1 1]
  	  [2 2 2]
  	  [3 3 3]]
 	 [[1 1 1]
  	  [2 2 2]
  	  [3 3 3]]
 	 [[1 1 1]
  	  [2 2 2]
  	  [3 3 3]]] 
yv: [[[1 1 1]
  	  [1 1 1]
  	  [1 1 1]]
 	 [[2 2 2]
  	  [2 2 2]
  	  [2 2 2]]
 	 [[3 3 3]
  	  [3 3 3]
  	  [3 3 3]]]
zv: [[[1 2 3]
  	  [1 2 3]
  	  [1 2 3]]
 	 [[1 2 3]
  	  [1 2 3]
  	  [1 2 3]]
 	 [[1 2 3]
  	  [1 2 3]
  	  [1 2 3]]]
'''

3.5 生成图像索引或 xy 坐标网格

x = np.arange(3)
y = np.arange(2)
xv, yv = np.meshgrid(x, y)
np.stack((yv, xv), axis=2)
'''
[[[0 0] [0 1] [0 2]]
 [[1 0] [1 1] [1 2]]]
'''
np.stack((xv, yv), axis=2)
'''
[[[0 0] [1 0] [2 0]]
 [[0 1] [1 1] [2 1]]]
'''

4. 随机数

4.1 np.random.rand

  创建给定形状的数组,数值为 [ 0 , 1 ) [0,1) [0,1) 上均匀分布的随机数。

np.random.rand(d0, d1, ..., dn)
----------------------------------------------------------------
>>> np.random.rand(2, 3)
[[0.42572141 0.81458374 0.73539729]
 [0.8680032  0.38338077 0.97945663]]

4.2 np.random.random

  和 np.random.rand 效果相同,输入的 size 以列表、元组、int 的形式。

np.random.random(size=None)
----------------------------------------------------------------
np.random.random([2, 3])
[[0.42572141 0.81458374 0.73539729]
 [0.8680032  0.38338077 0.97945663]]

4.3 np.random.randn

  数值为 [ 0 , 1 ) [0,1) [0,1) 上均值为0,方差为1的正态分布的随机数。

np.random.randn(d0, d1, ..., dn)
----------------------------------------------------------------
>>> np.random.randn(2, 3)
[[ 1.28560542 -0.30355338  0.61907566]
 [ 0.39599855  0.22340565 -0.05433942]]

4.4 np.random.randint

  数值为 [ l o w , h i g h ) [low,high) [low,high) 上的随机整数,若 high=None,范围为 [ 0 , l o w ) [0,low) [0,low)

np.random.randint(low, high=None, size=None, dtype=int)
----------------------------------------------------------------
>>> np.random.randint(0, 10, size=[2, 3])
[[3 8 8]
 [8 0 5]]

猜你喜欢

转载自blog.csdn.net/weixin_43605641/article/details/126051597