Matrix01-2: ndarray数组(numpy)的索引与

2018-10-19  本文已影响32人  杨强AT南京
  1. ndarray数组的属性
  2. ndarray数组的索引与切片
  3. ndarray数组的特殊索引与切片

1、ndarray数组的属性

属性 属性描述
T 等价于self.transpose(), 矩阵转置(self.ndim < 2),如果是向量( self.ndim = 1),返回本身.
data 返回指向数组数据开始位置的Python缓冲对象。
dtype 数组元素的数据类型。
flags 数组的内存分配信息。
flat 返回数组的一维迭代器。
imag 数组的虚部。
real 数组的实部。
size 数组元素个数。
itemsize 数组元素的长度。
nbytes 数组所有元素占用的字节数。
ndim 数组的维数个数(二维,三维)
shape 元组格式的数组维数(数组的形状)。
strides 每个维度在数组内存中获取的步长。
ctypes 返回一个ctypes模块中对象。
base 如果内存是从其他对象累的,则返回Base object对象.

例子说明代码:

import numpy as np
#先构建一个ndarray对象\
vector = np.array ( [ 1, 2, 3 ] )     #向量
matrix = np.array( [                   #矩阵
    [ 1, 2, 3, 4, 5 ] ,
    [ 6, 7, 8, 9, 0 ] ,
    [ 5, 4, 3, 2, 1 ]
] )
# T 属性(返回转置矩阵)
print ( vector.T )
print ( matrix.T )
[1 2 3]
[[1 6 5]
 [2 7 4]
 [3 8 3]
 [4 9 2]
 [5 0 1]]
# ndim 属性(维数,三维返回3,一维饭返回1)
print ( vector.ndim )
print ( matrix.ndim )

1
2
# shape 属性:返回每个维度的维数
print ( vector.shape )
print ( matrix.shape )
(3,)
(3, 5)
# flat属性(返回1维迭代器)
print ( vector.flat )
print ( matrix.flat )
print ( [ i for i in vector.flat ] )
print ( [ i for i in matrix.flat ] )
<numpy.flatiter object at 0x7fdef18cda00>
<numpy.flatiter object at 0x7fdef18cda00>
[1, 2, 3]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 5, 4, 3, 2, 1]
# size 属性 (数组元素的个数,或者flat的长度)
print ( vector.size )
print ( matrix.size )
3
15
# itemsize 属性(数组元素的大小,单位字节)
print ( vector.itemsize)
print ( matrix.itemsize)
8
8
# nbytes 属性 (所有元素的字节大小,nbytes = size *  itemsize )
print ( vector.nbytes)
print ( matrix.nbytes)
24
120
# strides 属性(步长:按照一维数组的获取的元素的方式)
print ( vector.strides)
print ( matrix.strides)
(8,)
(40, 8)
# dtype 属性(数组元素的标量类型)
print ( vector.dtype)
print ( matrix.dtype)
int64
int64
# real与imag 属性(实部与虚部)
print ( vector.imag)
print ( matrix.imag)
print ( vector.real)
print ( matrix.real)
[0 0 0]
[[0 0 0 0 0]
 [0 0 0 0 0]
 [0 0 0 0 0]]
[1 2 3]
[[1 2 3 4 5]
 [6 7 8 9 0]
 [5 4 3 2 1]]
# 其他属性
print ( vector.ctypes)     #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 0x112187cf8>
<numpy.core._internal._ctypes object at 0x112187f98>
<memory at 0x112975dc8>
<memory at 0x112b68120>
  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

2、ndarray数组的索引与切片

Python内置list类型支持的切片ndarray都支持,不过ndarray 支持元组索引与切片(2维与2维以上才支持)

# 1. 索引
#索引取值
print ( vector [ 1 ] )
print ( matrix [ 1 ] )
#索引设置值
vector [ 1 ] = 88
print ( vector )
matrix [ 1 ] = [ 8, 8, 8 ,8, 8 ]    # 必须与原来对应行的形状一样
print ( matrix )
matrix [ 1 ] [ 1 ] =88               #访问向量的元素
print ( matrix )

matrix [ 1 ] = np. array ( [ 9, 9, 9 ,9, 9 ] )    # 必须与原来对应行的形状一样
print ( matrix )
88
[ 8 99  8  7  7]
[ 1 88  3]
[[1 2 3 4 5]
 [8 8 8 8 8]
 [5 4 3 7 7]]
[[ 1  2  3  4  5]
 [ 8 88  8  8  8]
 [ 5  4  3  7  7]]
[[1 2 3 4 5]
 [9 9 9 9 9]
 [5 4 3 7 7]]
# ndarray 与 list不一样的索引使用,可以使用元组作为索引来访问元素
print ( matrix [ 1, 1 ] )

matrix [ 1, 1 ] =99    #设置值
print ( matrix )
99
[[ 1  2  3  4  5]
 [ 8 99  8  8  8]
 [ 5  4  3  2  1]]
# 切片
print ( vector [ : : ] )    #正向
print ( vector [ : :-1 ] ) #逆向

print ( matrix [ 0 : 2 ] [ 0 : 1 ] )   #先从矩阵取 2 行,再从两行取 1 行
[ 1 88  3]
[ 3 88  1]
[[1 2 3 4 5]]
# ndarray 与 list不一样的切片
print ( matrix )
# 取子矩阵
print ( matrix [ 1 : 3, 3: 5] )

#使用子矩阵设置值
matrix [ 1 : 3, 3: 5] = [ [ 7, 7 ] , [ 7, 7 ] ]
print ( matrix )
[[ 1  2  3  4  5]
 [ 8 99  8  8  8]
 [ 5  4  3  2  1]]
[[8 8]
 [2 1]]
[[ 1  2  3  4  5]
 [ 8 99  8  7  7]
 [ 5  4  3  7  7]]

3、ndarray数组的特殊索引与切片

# 1. 列表形式的索引
print ( vector )
print ( vector [ [ 1, 2, 0 ] ] )    #取下标为1,2,0的元素构成新的ndarray数组对象

print ( vector )
vector [ [0,2 ] ] = [ 6, 6 ]    #设置值
print ( vector )

# 下标是元组,表示多维,所以不能用在1维数组。
# print ( vector [ ( 1, 2 ) ] ) 不能使用

# 二维数组列表作为索引
print ( matrix )
print ( matrix [ [ [ 0, 2] , [1, 3 ] ] ] )  #返回[0,1] 与[2, 3] 两个位置的元素构成新的数组

print ( matrix [ [ 0, 2] , [1, 3 ] ] )    #效果于上面一样

print ( matrix )
matrix = np.array ( [
    [  1,  2,  3,  4,  5,  6],
    [  7,  8,  9, 10, 11, 12],
    [ 13,14, 15, 16, 17, 18]
] )
print ( matrix [ [ [ 0, 1] , [1, 2 ] ] , ] )    #第0,2,1,2行构成的矩阵
print ( matrix [ [ [ 0, 1] , [1, 2 ] ] , [ [2, 3 ] ] ] )   # 取0,1,1,2行,分别再[取0行2列 ,1行3列],[1行2列,2行3列]
print ( matrix [ [ [ 0, 1] , [1, 2 ] ] , [ [2, 3 ] ,[4,5] ] ] )    
[ 6 88  6]
[88  6  6]
[ 6 88  6]
[ 6 88  6]
[[ 1  2  3  4  5  6]
 [ 7  8  9 10 11 12]
 [13 14 15 16 17 18]]
[ 2 16]
[ 2 16]
[[ 1  2  3  4  5  6]
 [ 7  8  9 10 11 12]
 [13 14 15 16 17 18]]
[[[ 1  2  3  4  5  6]
  [ 7  8  9 10 11 12]]

 [[ 7  8  9 10 11 12]
  [13 14 15 16 17 18]]]
[[ 3 10]
 [ 9 16]]
[[ 3 10]
 [11 18]]
# 2.  布尔值列表作为索引
#返回与matrix一样shape的ndarray数组
print ( matrix > 5)
[[False False False False False  True]
 [ True  True  True  True  True  True]
 [ True  True  True  True  True  True]]
#使用布尔数组作为索引
print ( matrix [ matrix > 5 ] )
[ 6  7  8  9 10 11 12 13 14 15 16 17 18]
#通过布尔数组索引,修改矩阵的值。
matrix [ matrix >= 5 ] = 88
print ( matrix )
[[ 1  2  3  4 88 88]
 [88 88 88 88 88 88]
 [88 88 88 88 88 88]]

资源

文件列表:ndarray_operation.ipynb

上一篇下一篇

猜你喜欢

热点阅读