3

【Numpy总结】第二节:Numpy 的属性与形状变换

 1 year ago
source link: https://blog.51cto.com/u_15807450/5719096
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Table of Contents

一、最基本的属性

在 NumPy中,每一个线性的数组称为是一个轴(axis),也就是维度(dimensions),维度的数量称为秩(rank);比如说,二维数组相当于是两个一维数组,即 二维数组有两个轴,秩为2 。

<font color=blue > 重点:很多时候可以声明 axis。
axis=0,表示沿着第 0 轴进行操作,即对每一列进行操作;
axis=1,表示沿着第 1 轴进行操作,即对每一行进行操作。</font>

二、Numpy 常用属性

Numpy 常用属性 Numpy 常用属性
ndarray.ndim 秩,即轴的数量或维度的数量
ndarray.shape 数组的维度,对于矩阵,n 行 m 列
ndarray.size 数组元素的总个数,相当于 .shape 中 n*m 的值
ndarray.dtype ndarray 对象的元素类型
ndarray.itemsize ndarray 对象中每个元素的大小,以字节为单位
ndarray.flags ndarray 对象的内存信息
ndarray.real ndarray元素的实部
ndarray.imag ndarray 元素的虚部

2.1 ndarray.ndim 数组维度

秩,即轴的数量或维度的数量

a = np.array([[1,2,3,4],[5,6,7,8]]) 
print ('a ndim:',a.ndim)
# a ndim: 2

2.2 ndarray.shape 数组形状

数组的维度,对于矩阵,n 行 m 列

a = np.array([[1,2,3,4],[5,6,7,8]]) 
print ('a ndim:',a.shape)
b = a.reshape(4,2)
print ('b ndim:',b.shape)
# a ndim: (2, 4)
# b ndim: (4, 2)

2.3 ndarray.dtype 数组类型

ndarray 对象的元素类型;

a = np.array([[1,2,3,4],[5,6,7,8]],dtype=np.int8) 
print ('a type:',a.dtype)
b = a.astype(np.float16)
print ('b type:',b.dtype)
# a type: int8
# b type: float16

2.4 ndarray.itemsize 数组元素大小

ndarray 对象中每个元素的大小,以字节为单位

a = np.array([[1,2,3,4],[5,6,7,8]],dtype=np.int8) 
print ('a itemsize:',a.itemsize)
b = a.astype(np.float16)
print ('b itemsize:',b.itemsize)
# a itemsize: 1
# b itemsize: 2

三、形状变换

常用的形状变换函数如下:

函数名称 功能描述
reshape 不改变数据的条件下修改数组形状
flat 数组元素迭代器
flatten 返回一份数组拷贝,对拷贝所做的修改不会影响原始数组
ravel 返回展开数组

3.1 numpy.reshape 改变形状

不改变数据的条件下修改数组形状 ,函数的格式如下:

numpy.reshape(arr, newshape, order='C')

参数名称 含义
arr 要修改形状的数组
newshape 整数或者整数数组,新的形状应当兼容原有形状
order ‘C’ – 按行,‘F’ – 按列,‘A’ – 原顺序,‘k’ – 元素在内存中的出现顺序。

也可以使用如下格式: arr.reshape(newshape, order='C'),作用与上面的格式完全一致:

举例如下:

a = np.array([[1,2,3,4],[5,6,7,8]]) 
b = np.reshape(a,(4,2))
c = a.reshape((4,2))
print('a:',a)
print('b:',b)
print('c:',c)

输出为:
【Numpy总结】第二节:Numpy 的属性与形状变换_数组元素

3.2 nparray.flat 返回迭代器

可以返回一个数组的迭代器,举例如下:

a = np.array([[1,2,3,4],[5,6,7,8]])
i = 0 
for j in a.flat:
    i = i + 1
    print('第%s个元素为:%s' %(i,j))
# 输出:
# 第1个元素为:1
# 第2个元素为:2
# 第3个元素为:3
# 第4个元素为:4
# 第5个元素为:5
# 第6个元素为:6
# 第7个元素为:7
# 第8个元素为:8

3.3 ndarray.flatten & numpy.ravel 平铺展开

两个函数均的功能基本一直,均为展开数组;格式如下:
ndarray.flatten(order='C')
numpy.ravel(a, order='C')

参数名称 含义
order ‘C’ – 按行(默认),‘F’ – 按列,‘A’ – 原顺序(不常用),‘K’ – 元素在内存中的出现顺序(不常用)
a = np.array([[1,2,3,4],[5,6,7,8]])
b =a.flatten()
print(b)    # [1 2 3 4 5 6 7 8]
c = a.flatten('F')
print(c)    # [1 5 2 6 3 7 4 8]

不同点为:在赋值时,flatten 不改变原数组,ravel会改变原数组,举例如下:
该特点,在赋值时可以使用;

d = np.array([[1,2,3,4],[5,6,7,8]])
d.flatten()[1]=100
print(d)  # 输出:[[1 2 3 4] [5 6 7 8]]
d.ravel()[2]=200
print(d)    # 输出:[[  1   2 200   4]  [  5   6   7   8]]

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK