NumPy ndarray
import numpy as np
# NumPy N-dimensional array(N-维数组):
# NumPy提供一个N-维数组类型,ndarray 描述一个相同类型的多数据项的集合;多个数据项可以通过索引访问
# 所有ndarrays是同质的:每一个数据项占用相同的内存块长度
# ndarray通常是一个固定size,数据项的类型和长度相同的多维容器;
# ndarray 可以通过索引或切割 (indexing or slicing)访问和修改,也可以同方法和属性来操作ndarray;
# 2-维 数组[2 x 3]
x = np.array([[1,2,3], [4,5,6]], np.int32)
print(type(x)) # <class 'numpy.ndarray'>
print(x.shape) # (2, 3)
print(x.dtype) # int32
print(x)
# [[1 2 3]
# [4 5 6]]
# 下标访问数组元素 (indexing)
print(x[0, 0]) # 1
print(x[1, 2]) # 6
# 2-维数组 切割(slicing)
# y = x[i行, :] # 取矩阵数组x的i行
# y = x[:, j列] # 取矩阵数组x的j列
y = x[:, 0] # 取下标0的列
print(y) # [1 4]
y = x[:, 1] # 取下标1的列
print(y) # [2 5]
y[0] = 9 # 这里的改变同样回改变x对应的值
print(x) # x的值对应y[0]的元素被修改
# [[1 9 3]
# [4 5 6]]
y = x[0, :] # 取下标0行
print(y) # [1 9 3]
y = x[1, :] # 取下标1行
print(y) # [4 5 6]
# 删除x,y变量
del x, y
# 创建3-dimensional array
x = np.array([[ [1,2,3], [4,5,6], [7,8,9] ]])
print(x.ndim) # 3
print(x.shape) # (1, 3, 3)
print(x)
# [[[1 2 3]
# [4 5 6]
# [7 8 9]]]
# 3-维数组 切割(slicing)
# y = x[0, i行, :] # 取矩阵数组x的i行
# y = x[0, :, j列] # 取矩阵数组x的j列
y = x[0, 0, :] # 下标0行
print(y) # [1 2 3]
y = x[0, 1, :] # 下标1行
print(y) # [4 5 6]
y = x[0, 2, :] # 下标2行
print(y) # [7 8 9]
y = x[0, :, 0] # 下标0列
print(y) # [1 4 7]
y = x[0, :, 1] # 下标1列
print(y) # [2 5 8]
y = x[0, :, 2] # 下标2列
print(y) # [3 6 9]
# 注:以上代码对数组y[]的操作,同时数组x[]的对应元素也会被操作到
# NumPy 构建数组 (Constructing arrays)
# 语法numpy.ndarray(shape[, dtype, buffer,offset,...])
# 参数(Parameters)
# shape:
# [tuple of ints] Shape of created array
# dtype:
# [data-type, optional] Any object that can be interpreted as a numpy data type.
# buffer:
# [object exposing buffer interface, optional] Used to fill the array with data.
# offset:
# [int, optional] Offset of array data in buffer.
# strides:
# [tuple of ints, optional] Strides of data in memory.
# order:
# [{‘C’, ‘F’}, optional] Row-major (C-style) or column-major (Fortran-style) order.
x = np.ndarray(shape=(2,2), dtype=float, order='F')
print(x)
# [[5.e-324 4.e-323] # random shape=(2,2)的随机数
# [4.e-323 4.e-323]]
x = np.ndarray(shape=(2,2), dtype=int, order='C')
print(x)
# [[ 2140045480 -507854386] # random
# [ 1630103286 -1707663749]]
x = np.ndarray((4,), buffer=np.array([1,2,3,4,5,6,7,8,9]), offset=2*np.int_().itemsize, dtype=int) # offset = 1*itemsize, i.e. skip first element
print(x) # [3 4 5 6] 2*itemsize 跳过前2个元素
# NumPy ndarray属性(Attributes)
# 属性 说明
# ndarray.T : [ndarray] The transposed array.
# ndarray.data : [buffer] Python buffer object pointing to the start of the array’s data.
# 数组数据的Python缓存对象指针开始。x.data[i] buffer对象指针通过索引访问第i个元素。
# ndarray.dtype : [dtype object] Data-type of the array’s elements.
# 数组元素的数据类型。
# ndarray.flags : [dict] Information about the memory layout of the array.
# 数组内存布局的相关信息。
# ndarray.flat : [numpy.flatiter object] A 1-D iterator over the array.
# flatiter对象 一个数组的1维迭代器。
# ndarray.imag : [ndarray] The imaginary part of the array.
# 数组的虚数部分。
# ndarray.real : [ndarray] The real part of the array.
# 数组的实数部分。
# ndarray.size : [int] Number of elements in the array.
# 数组包含的元素数。
# ndarray.itemsize : [int] Length of one array element in bytes.
# 一个数组元素的字节长度。
# ndarray.nbytes : [int] Total bytes consumed by the elements of the array.
# 数组的字节总数 = size * itemsize。
# ndarray.ndim : [int] Number of array dimensions.
# 数组的维度数。
# ndarray.shape : [tuple of ints] Tuple of array dimensions.
# 数组的维度元组。
# ndarray.strides : [tuple of ints] Tuple of bytes to step in each dimension when traversing an array.
#
# ndarray.ctypes : [ctypes object] An object to simplify the interaction of the array with the ctypes module.
# ndarray.base : [ndarray] Base object if memory is from some other object.
x = np.arange(10)
print(x) # [0 1 2 3 4 5 6 7 8 9]
print(x.T) # [0 1 2 3 4 5 6 7 8 9]
print(x.T+2) # [ 2 3 4 5 6 7 8 9 10 11] #对象开始指针偏移2
print(x.data) # <memory at 0x00000261FADCCC40>
print(x.data[5]) # 5 x.data[i] buffer对象指针通过索引访问第i个元素
print(x.dtype) # int32
print(x.flags)
# C_CONTIGUOUS : True
# F_CONTIGUOUS : True
# OWNDATA : True
# WRITEABLE : True
# ALIGNED : True
# WRITEBACKIFCOPY : False
# UPDATEIFCOPY : False
print(x.flat) # <numpy.flatiter object at 0x0000022B9D951330>
x = np.arange(10, dtype=complex)
x.imag += np.arange(10, 20)
print(x) # [0.+10.j 1.+11.j 2.+12.j 3.+13.j 4.+14.j 5.+15.j 6.+16.j 7.+17.j 8.+18.j 9.+19.j] #复数
print(x.real) # [0. 1. 2. 3. 4. 5. 6. 7. 8. 9.] #实数部分
print(x.imag) # [10. 11. 12. 13. 14. 15. 16. 17. 18. 19.] #虚数部分
print(x.size) # 10 # 数组x的长度
print(x.itemsize) # 16 # 复数类型长度16bytes
print(x.nbytes) # 160 # 数组长度*元素长度
print(x.ndim) # 1 # 数组的维度
print(x.shape) # (10,) # 数组的shape
print(x.strides) # (16,) #
print(x.ctypes) # <numpy.core._internal._ctypes object at 0x000001FE6162F220>
print(x.base) # None