Numpy-01: ndarray数组 indexing and
| ndarray.flags
| Information about the memory layout of the array. |
| ndarray.shape
| Tuple of array dimensions. |
| ndarray.strides
| Tuple of bytes to step in each dimension when traversing an array. |
| ndarray.ndim
| Number of array dimensions. |
| ndarray.data
| Python buffer object pointing to the start of the array’s data. |
| ndarray.size
| Number of elements in the array. |
| ndarray.itemsize
| Length of one array element in bytes. |
| ndarray.nbytes
| Total bytes consumed by the elements of the array. |
| ndarray.base
| Base object if memory is from some other object. |
Other attributes
| ndarray.T
| The transposed array. |
| ndarray.real
| The real part of the array. |
| ndarray.imag
| The imaginary part of the array. |
| ndarray.flat
| A 1-D iterator over the array. |
| ndarray.ctypes
| An object to simplify the interaction of the array with the ctypes module. |
Numpy数组属性
import numpy as np
vector = np.array([1,2,3])
matrix = np.array([[1,2,3],[4,5,6],[7,8,9],[10,11,12]])
#print(vector.shape)
#print(vector.ndim)
#print(matrix.ndim)
print(vector)
print(matrix)
# matrix = np.array([1,2,3],[4,5,6],[7,8,9])这种表示方法是错误的
#print(vector.T)
#print(matrix.T)
[1 2 3]
[[ 1 2 3]
[ 4 5 6]
[ 7 8 9]
[10 11 12]]
print(vector.ndim)
print(matrix.ndim)
1
2
vector2 = np.array([[1,2,3]])
print(vector2.shape)
print(vector.shape)
print(matrix.shape)
print(matrix[1])
matrix[1][2]
(1, 3)
(3,)
(4, 3)
[4 5 6]
6
#转置矩阵
print(vector.T)
print(vector2.T)
print(matrix.T)
[1 2 3]
[[1]
[2]
[3]]
[[ 1 4 7 10]
[ 2 5 8 11]
[ 3 6 9 12]]
# flat返回一维迭代器
print(vector.flat)
print(matrix.flat)
print([x for x in vector.flat])
print([x for x in matrix.flat])
<numpy.flatiter object at 0x000001DF394DE0C0>
<numpy.flatiter object at 0x000001DF394DE0C0>
[1, 2, 3]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
# size属性 计算元素个数
print(vector.size)
print(matrix.size)
3
12
# itemsize 计算数组元素的位数
print(vector.itemsize)
print(matrix.itemsize)
a = np.array([1,2,3],dtype = float)
print(a.itemsize)
4
4
8
# nbytes 属性 (所有元素的字节大小,nbytes = size * itemsize )
print(vector.nbytes)
print(matrix.nbytes)
12
48
# strides 属性(步长:按照一维数组的获取的元素的方式)
print ( vector.strides)
print ( matrix.strides) # 二维按照一维形式获取元素位12个位,每个元素中的单个元素为4个位
(4,)
(12, 4)
# dtype输出数组元素的类型
print(vector.dtype)
print(a.dtype)
print(matrix.dtype)
int32
float64
int32
# 其他属性
print ( vector.ctypes) #输出数组对象
print ( matrix.ctypes)
print ( vector.data) #数据内存地址。
print ( matrix.data)
print ( vector.flags) #内存分布信息
print ( matrix.flags)
print ( vector.base) #来自其他对象的Base对象
print ( matrix.base)
<numpy.core._internal._ctypes object at 0x000001DF394640C8>
<numpy.core._internal._ctypes object at 0x000001DF394646C8>
<memory at 0x000001DF38FD87C8>
<memory at 0x000001DF394919E8>
C_CONTIGUOUS : True
F_CONTIGUOUS : True
OWNDATA : True
WRITEABLE : True
ALIGNED : True
WRITEBACKIFCOPY : False
UPDATEIFCOPY : False
C_CONTIGUOUS : True
F_CONTIGUOUS : False
OWNDATA : True
WRITEABLE : True
ALIGNED : True
WRITEBACKIFCOPY : False
UPDATEIFCOPY : False
None
None
Numpy数组的索引和切片
print(vector)
print(matrix)
[1 2 3]
[[ 1 2 3]
[ 4 5 6]
[ 7 8 9]
[10 11 12]]
# 一维数组的索引和切片
print(vector[1])
print(vector[0:2])
print(vector[:2])
# 反向索引切片
print(vector[-1:-3:-1])
#设置值
vector[1]=2000
print(vector)
2
[1 2]
[1 2]
[3 2]
[ 1 2000 3]
# 二维数组的索引和切片
print(matrix)
print(matrix[1][2])
[[ 1 2 3]
[ 4 5 6]
[ 7 8 9]
[10 11 12]]
6
# 索引设置值
matrix[0] = [4,5,6]
# matrix[0] = 1 这种形式是错误的值设置必须维数要与原先维数一致
print(matrix)
matrix[1][1] = 0 #必须与位置对应形式一致
print(matrix)
matrix[1][1] = 1.1 #数组中的元素其数据类型必须一致,浮点型自动转换成整型
print(matrix)
#matrix[1][1] = [1,2,3] 此种方式错误
#print(matrix)
[[ 4 5 6]
[ 4 5 6]
[ 7 8 9]
[10 11 12]]
[[ 4 5 6]
[ 4 0 6]
[ 7 8 9]
[10 11 12]]
[[ 4 5 6]
[ 4 1 6]
[ 7 8 9]
[10 11 12]]
matrix2 = np.array([[[1,2,3],[1,2,3],[1,2,3]],[[4,5,6],[4,5,6],[4,5,6]]])
print(matrix2)
print(matrix2.ndim)
matrix2 = np.array([[[1,2],[1,2,3],[1,2,3]],[[4,5,6],[4,5,6],[4,5,6]]])
print(matrix2)
print(matrix2.ndim)
'''
数组的每一个维度其元素个数必须一致
如若不一致则进行降低维度打包处理使得维度变得一致
如matrix2中有[1,2]与其同级别维度元素个数不一致则将其看作一整体
即元素变成list形式处理
'''
[[[1 2 3]
[1 2 3]
[1 2 3]]
[[4 5 6]
[4 5 6]
[4 5 6]]]
3
[[list([1, 2]) list([1, 2, 3]) list([1, 2, 3])]
[list([4, 5, 6]) list([4, 5, 6]) list([4, 5, 6])]]
2
'\n数组的每一个维度其元素个数必须一致\n如若不一致则进行降低维度打包处理使得维度变得一致\n如matrix2中有[1,2]与其同级别维度元素个数不一致则将其看作一整体\n即元素变成list形式处理\n'
# ndarray与list索引特殊之处可以使用元组组合索引下标依次选取
print(matrix)
# 下面两种方式一致
print(matrix[1,1])
print(matrix[1][1])
[[ 4 5 6]
[ 4 1 2]
[ 7 2 1]
[10 11 12]]
1
1
# 切片
# 子矩阵的选取
print(matrix[1:3,1:3])
# 这种形式是错误的print(matrix[1:2][1:2])
# 反向索引选取子矩阵
print(matrix[-1:-3:-1,-1:-3:-1])
[[1 6]
[8 9]]
[[12 11]
[ 9 8]]
# set values
print(matrix)
matrix[1:3,1:3] = [[1,2],[2,1]]
print(matrix)
[[ 4 5 6]
[ 4 1 6]
[ 7 8 9]
[10 11 12]]
[[ 4 5 6]
[ 4 1 2]
[ 7 2 1]
[10 11 12]]
ndarry的特殊索引和切片
print(vector)
print(vector[[2,1,0]]) # 依次取数组下标为2,1,0的元素构成新的数组
vector[[2,0]] = [1000,3000]
print(vector)
[ 1 2000 3]
[ 3 2000 1]
[3000 2000 1000]
# 二维数组特殊的索引
matrix = np.array([[1,2,3,4],[5,6,7,8],[9,10,11,12],[13,14,15,16]])
print(matrix)
# 返回0,1、2,3位置的元素构成新数组下面两种方式一致
print(matrix[[0,2],[1,3]])
print(matrix[[0,2],[1,3],])
'''
print(matrix[[0,1],[1,2],[0,1]])这是错误的,因为我想将
matrix中0,1,0、1,2,1位置的元素取出来做成一个新的数组
但是我定义的matrix是二维的不是三维的
'''
[[ 1 2 3 4]
[ 5 6 7 8]
[ 9 10 11 12]
[13 14 15 16]]
[ 2 12]
[ 2 12]
'\nprint(matrix[[0,1],[1,2],[0,1]])这是错误的,因为我想将\nmatrix中0,1,0、1,2,1位置的元素取出来做成一个新的数组\n但是我定义的matrix是二维的不是三维的\n'
print(matrix[[[0,2],[1,3]],[1,3]]) #相当于把[0,2],[1,3]与[1,3]做笛卡尔积
#上面的式子等价与下面式子
print(matrix[[[0,2],[1,3]],[[1,3],[1,3]]])
print(matrix)
[[ 2 12]
[ 6 16]]
[[ 2 12]
[ 6 16]]
# 取第一行的第二列和三列元素
print(matrix)
print(matrix[0][1:3])#这个一般用于取子矩阵或者是取行列交叉的元素
#取矩阵的对角元素
print(matrix[[0,1,2,3],[0,1,2,3]]) #这个一般用于取不同行不同列的元素
[[ 1 2 3 4]
[ 5 6 7 8]
[ 9 10 11 12]
[13 14 15 16]]
[2 3]
[ 1 6 11 16]
print(matrix)
print ( matrix [ [ [ 0, 1] , [1, 2 ] ] ,] ) #第0,1、1,2行构成的矩阵
print ( matrix [ [ [ 0, 1] , [1, 2 ] ] , [ [2, 3 ] ] ] ) # 取0,11,2行,分别再[取0行2列 ,1行3列],[1行2列,2行3列]
[[ 1 2 3 4]
[ 5 6 7 8]
[ 9 10 11 12]
[13 14 15 16]]
[[[ 1 2 3 4]
[ 5 6 7 8]]
[[ 5 6 7 8]
[ 9 10 11 12]]]
[[ 3 8]
[ 7 12]]
# 返回布尔值表这个对于我们数字图像处理灰度值的时候蛮有用
print(matrix)
print(matrix > 8)
print(matrix)
# 根据布尔值修改元素值
matrix[matrix<9] = 0
print(matrix)
[[ 0 0 0 0]
[ 0 0 0 0]
[ 9 10 11 12]
[13 14 15 16]]
[[False False False False]
[False False False False]
[ True True True True]
[ True True True True]]
[[ 0 0 0 0]
[ 0 0 0 0]
[ 9 10 11 12]
[13 14 15 16]]
[[ 0 0 0 0]
[ 0 0 0 0]
[ 9 10 11 12]
[13 14 15 16]]