创建对象
numpy.array(object, dtype = None, copy = True, order = None, subok = False, ndmin = 0) object: 数列; order: 样式,C行方向,F列方向,A任意方向(默认) ndmin: 最小维度
import numpy as np
a = np.array([1,5,5,5,3],ndmin=2,dtype=int)
print(a)
# [[1 5 5 5 3]]
类型
numpy.dtype(object, align, copy) int8, int16, int32, int64 四种数据类型可以使用字符串 'i1', 'i2','i4','i8' 代替
import numpy as np
dt = np.dtype('i1')
print(dt)
# int8
import numpy as np
dt = np.dtype([('xxx',np.float64)])
a = np.array([(10,),(20,),(30,)], dtype = dt)
print(a)
# [(10.,) (20.,) (30.,)]
import numpy as np
student = np.dtype([('str','S20'), ('int', 'i1'), ('float', 'f4')])
a = np.array([('abc', 21, 50),('xyz', 18, 75)], dtype = student)
print(a)
# [(b'abc', 21, 50.) (b'xyz', 18, 75.)]
属性 ndim 秩,轴/维度的数量 shape 数组的维度 size 总数 itemsize 大小
a = np.arange(24)
print(a.ndim)
b = a.reshape(2, 4, 3)
print(b.ndim)
c = np.array([[1,4],[2,3]])
c.shape= (2,2) # == c.reshape(2,2)
print(c)
print(c.itemsize)
print(c.flags)
创建数组
numpy.empty(shape, dtype = float, order = 'C')
import numpy as np
x = np.empty([3,2])
print(x)
# [[0. 0.15]
# [0.25 0.5 ]
# [0.75 1. ]]
创建数组,以 0 填充 numpy.zeros(shape, dtype = float, order = 'C')
x = np.zeros((3),dtype=int)
print(x)
# [0 0 0]
创建数组,以 1 填充 numpy.ones(shape, dtype = None, order = 'C')
x = np.ones([2,2], dtype = int)
print(x)
# [[1 1]
# [1 1]]
numpy.zeros_like 都是用于创建一个指定形状的数组,其中所有元素都是 0 numpy.zeros_like(a, dtype=None, order='K', subok=True, shape=None) numpy.ones_like(a, dtype=None, order='K', subok=True, shape=None)
import numpy as np
arr = np.array([1,2,3])
print(np.zeros_like(arr))
# [0 0 0]
NumPy 从已有的数组创建数组 numpy.asarray(param, dtype = None, order = None)
import numpy as np
x = (1, 2, 3)
a = np.asarray(x)
print(a)
# [1 2 3]
numpy.frombuffer 实现动态数组 numpy.frombuffer(xxx, dtype = float, count = -1, offset = 0)
import numpy as np
s = b'xxx'
a = np.frombuffer(s, dtype='S1')
print(a)
# [b'x' b'x' b'x']
numpy.fromiter 转为一维数组 numpy.fromiter(iterable, dtype, count=-1)
x = iter(range(5))
print(np.fromiter(x,dtype=int))
# [0 1 2 3 4]
数值范围创建数组 numpy.arange(start, stop, step, dtype)
import numpy as np
x = np.arange(1,10,2)
print(x)
# [1 3 5 7 9]
创建一个一维数组,等差数组 np.linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None)
import numpy as np
a = np.linspace(1,5,5)
print(a)
# [1. 2. 3. 4. 5.]
创建一个一维数组,等比数组 np.logspace(start, stop, num=50, endpoint=True, base=10.0, dtype=None) base 对数 log 的底数。
import numpy as np
a = np.logspace(1,4,4,base=2)
print (a)
# [ 2. 4. 8. 16.]
切片和索引
import numpy as np
a = np.arange(6)
s = slice(1, 5, 2) # 从索引 1 开始到索引 5 停止,间隔为2
print(a[s])
# [1 3]
import numpy as np
a = np.arange(10)
b = a[1:5:2] # 从索引 1 开始到索引 5 停止,间隔为 2
print(b)
print(a[5:])
# [1 3]
# [5 6 7 8 9]
省略号 ... 切片
import numpy as np
a = np.array([[1, 2, 3], [3, 4, 5], [4, 5, 6]])
print(a[..., 1]) # 第2列元素
print(a[1, ...]) # 第2行元素
print(a[..., 1:]) # 第2列及剩下的所有元素
# [2 4 5]
# [3 4 5]
# [[2 3]
# [4 5]
# [5 6]]
高级索引
整数数组索引: 使用一个数组来访问另一个数组的元素
import numpy as np
x = np.array([[1, 2], [3, 4], [5, 6]])
y = x[[0, 1, 2], [0, 1, 0]]
print(y)
# [1 4 5] # 匹配 (0,0),(1,1) 和 (2,0) 位置元素
import numpy as np
x = np.array([[0, 1, 2], [3, 4, 5], [6, 7, 8], [9, 10, 11]])
rows = np.array([[0, 0], [3, 3]])
cols = np.array([[0, 2], [0, 2]])
y = x[rows, cols]
print(y)
# [[ 0 2]
# [ 9 11]]
: 或 ... 切片
a = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
b = a[1:3, 1:3]
c = a[1:3, [1, 2]]
d = a[..., 1:]
print(b)
print(c)
print(d)
# [[5 6]
# [8 9]]
# [[5 6]
# [8 9]]
# [[2 3]
# [5 6]
# [8 9]]
布尔索引
import numpy as np
x = np.array([[0, 1, 2], [3, 4, 5], [6, 7, 8], [9, 10, 11]])
print(x[x > 5])
# [ 6 7 8 9 10 11]
过滤
import numpy as np
a = np.array([1, 2 + 6j, 5, 3.5 + 5j])
print(a[np.iscomplex(a)])
# [2. +6.j 3.5+5.j]
花式索引: 索引数组的值作为目标数组的某个轴的下标来取值
一维数组
import numpy as np
x = np.arange(9)
x2 = x[[0, 6]]
print(x2)
print(x2[0])
print(x2[1])
# [0 6]
# 0
# 6
二维数组
import numpy as np
x = np.arange(9).reshape((3, 3))
print(x[[0, 2, 1]])
# [[0 1 2]
# [6 7 8]
# [3 4 5]]
2024.3.26 更新中。。。