Files
ailearning/docs/da/038.md
2020-10-19 21:08:55 +08:00

936 lines
9.0 KiB
Markdown
Raw Blame History

This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# 数组属性方法总结
| | 作用 |
| --- | --- |
| 1 | **基本属性** |
| `a.dtype` | 数组元素类型 `float32,uint8,...` |
| `a.shape` | 数组形状 `(m,n,o,...)` |
| `a.size` | 数组元素数 |
| `a.itemsize` | 每个元素占字节数 |
| `a.nbytes` | 所有元素占的字节 |
| `a.ndim` | 数组维度 |
| 2 | **形状相关** |
| `a.flat` | 所有元素的迭代器 |
| `a.flatten()` | 返回一个1维数组的复制 |
| `a.ravel()` | 返回一个1维数组高效 |
| `a.resize(new_size)` | 改变形状 |
| `a.swapaxes(axis1, axis2)` | 交换两个维度的位置 |
| `a.transpose(*axex)` | 交换所有维度的位置 |
| `a.T` | 转置,`a.transpose()` |
| `a.squeeze()` | 去除所有长度为1的维度 |
| 3 | **填充复制** |
| `a.copy()` | 返回数组的一个复制 |
| `a.fill(value)` | 将数组的元组设置为特定值 |
| 4 | **转化** |
| `a.tolist()` | 将数组转化为列表 |
| `a.tostring()` | 转换为字符串 |
| `a.astype(dtype)` | 转化为指定类型 |
| `a.byteswap(False)` | 转换大小字节序 |
| `a.view(type_or_dtype)` | 生成一个使用相同内存,但使用不同的表示方法的数组 |
| 5 | **复数** |
| `a.imag` | 虚部 |
| `a.real` | 实部 |
| `a.conjugate()` | 复共轭 |
| `a.conj()` | 复共轭(缩写) |
| 6 | **保存** |
| `a.dump(file)` | 将二进制数据存在file中 |
| `a.dump()` | 将二进制数据表示成字符串 |
| `a.tofile(fid, sep="",format="%s")` | 格式化ASCⅡ码写入文件 |
| 7 | **查找排序** |
| `a.nonzero()` | 返回所有非零元素的索引 |
| `a.sort(axis=-1)` | 沿某个轴排序 |
| `a.argsort(axis=-1)` | 沿某个轴,返回按排序的索引 |
| `a.searchsorted(b)` | 返回将b中元素插入a后能保持有序的索引值 |
| 8 | **元素数学操作** |
| `a.clip(low, high)` | 将数值限制在一定范围内 |
| `a.round(decimals=0)` | 近似到指定精度 |
| `a.cumsum(axis=None)` | 累加和 |
| `a.cumprod(axis=None)` | 累乘积 |
| 9 | **约简操作** |
| `a.sum(axis=None)` | 求和 |
| `a.prod(axis=None)` | 求积 |
| `a.min(axis=None)` | 最小值 |
| `a.max(axis=None)` | 最大值 |
| `a.argmin(axis=None)` | 最小值索引 |
| `a.argmax(axis=None)` | 最大值索引 |
| `a.ptp(axis=None)` | 最大值减最小值 |
| `a.mean(axis=None)` | 平均值 |
| `a.std(axis=None)` | 标准差 |
| `a.var(axis=None)` | 方差 |
| `a.any(axis=None)` | 只要有一个不为0返回真逻辑或 |
| `a.all(axis=None)` | 所有都不为0返回真逻辑与 |
In [1]:
```py
from numpy import *
```
## 基本属性
In [2]:
```py
a = array([[0, 1, 2, 3], [4, 5, 6, 7]])
a
```
Out[2]:
```py
array([[0, 1, 2, 3],
[4, 5, 6, 7]])
```
数组元素属性:
In [3]:
```py
a.dtype
```
Out[3]:
```py
dtype('int32')
```
形状:
In [4]:
```py
a.shape
```
Out[4]:
```py
(2L, 4L)
```
元素数目:
In [5]:
```py
a.size
```
Out[5]:
```py
8
```
元素占字节大小:
In [6]:
```py
a.itemsize
```
Out[6]:
```py
4
```
所有元素所占字节:
In [7]:
```py
a.nbytes
```
Out[7]:
```py
32
```
数据维度:
In [8]:
```py
a.ndim
```
Out[8]:
```py
2
```
## 形状相关
In [9]:
```py
for row in a:
print row
```
```py
[0 1 2 3]
[4 5 6 7]
```
所有元素的迭代器:
In [10]:
```py
for elt in a.flat:
print elt
```
```py
0
1
2
3
4
5
6
7
```
所有元素组成的一维数组,按照行排列:
In [11]:
```py
a.flatten()
```
Out[11]:
```py
array([0, 1, 2, 3, 4, 5, 6, 7])
```
In [12]:
```py
a.ravel()
```
Out[12]:
```py
array([0, 1, 2, 3, 4, 5, 6, 7])
```
重新改变形状:
In [13]:
```py
a.resize((4,2))
a
```
Out[13]:
```py
array([[0, 1],
[2, 3],
[4, 5],
[6, 7]])
```
交换这两个轴的顺序:
In [14]:
```py
a.swapaxes(0,1)
```
Out[14]:
```py
array([[0, 2, 4, 6],
[1, 3, 5, 7]])
```
转置:
In [15]:
```py
a.transpose()
```
Out[15]:
```py
array([[0, 2, 4, 6],
[1, 3, 5, 7]])
```
转置:
In [16]:
```py
a.T
```
Out[16]:
```py
array([[0, 2, 4, 6],
[1, 3, 5, 7]])
```
In [17]:
```py
a2 = array([1,2,3])
a2.shape
```
Out[17]:
```py
(3L,)
```
In [18]:
```py
a2.resize((1,3,1))
a2.shape
```
Out[18]:
```py
(1L, 3L, 1L)
```
去除长度为1的维度
In [19]:
```py
a2 = a2.squeeze()
a2.shape
```
Out[19]:
```py
(3L,)
```
## 填充复制
复制:
In [20]:
```py
b = a.copy()
b
```
Out[20]:
```py
array([[0, 1],
[2, 3],
[4, 5],
[6, 7]])
```
复制不影响原来的数组:
In [21]:
```py
b[0][0] = -1
b # First value changed
```
Out[21]:
```py
array([[-1, 1],
[ 2, 3],
[ 4, 5],
[ 6, 7]])
```
In [22]:
```py
a # original not changed because b is a copy
```
Out[22]:
```py
array([[0, 1],
[2, 3],
[4, 5],
[6, 7]])
```
填充:
In [23]:
```py
b.fill(4)
b
```
Out[23]:
```py
array([[4, 4],
[4, 4],
[4, 4],
[4, 4]])
```
## 转化
转化为列表:
In [24]:
```py
a.tolist()
```
Out[24]:
```py
[[0, 1], [2, 3], [4, 5], [6, 7]]
```
转化为字符串:
In [25]:
```py
a.tostring()
```
Out[25]:
```py
'\x00\x00\x00\x00\x01\x00\x00\x00\x02\x00\x00\x00\x03\x00\x00\x00\x04\x00\x00\x00\x05\x00\x00\x00\x06\x00\x00\x00\x07\x00\x00\x00'
```
改变数组元素类型:
In [26]:
```py
a.astype(float)
```
Out[26]:
```py
array([[ 0., 1.],
[ 2., 3.],
[ 4., 5.],
[ 6., 7.]])
```
In [27]:
```py
b = a.copy()
b.byteswap(False)
```
Out[27]:
```py
array([[ 0, 16777216],
[ 33554432, 50331648],
[ 67108864, 83886080],
[100663296, 117440512]])
```
将它看成16位整数
In [28]:
```py
a.view(dtype=int16)
```
Out[28]:
```py
array([[0, 0, 1, 0],
[2, 0, 3, 0],
[4, 0, 5, 0],
[6, 0, 7, 0]], dtype=int16)
```
## 复数
实部:
In [29]:
```py
b = array([1+2j, 3+4j, 5+6j])
b.real
```
Out[29]:
```py
array([ 1., 3., 5.])
```
虚部:
In [30]:
```py
b.imag
```
Out[30]:
```py
array([ 2., 4., 6.])
```
共轭:
In [31]:
```py
b.conj()
```
Out[31]:
```py
array([ 1.-2.j, 3.-4.j, 5.-6.j])
```
In [32]:
```py
b.conjugate()
```
Out[32]:
```py
array([ 1.-2.j, 3.-4.j, 5.-6.j])
```
## 保存
保存成文本:
In [33]:
```py
a.dump("file.txt")
```
字符串:
In [34]:
```py
a.dumps()
```
Out[34]:
```py
'\x80\x02cnumpy.core.multiarray\n_reconstruct\nq\x01cnumpy\nndarray\nq\x02K\x00\x85U\x01b\x87Rq\x03(K\x01\x8a\x01\x04\x8a\x01\x02\x86cnumpy\ndtype\nq\x04U\x02i4K\x00K\x01\x87Rq\x05(K\x03U\x01<NNNJ\xff\xff\xff\xffJ\xff\xff\xff\xffK\x00tb\x89U \x00\x00\x00\x00\x01\x00\x00\x00\x02\x00\x00\x00\x03\x00\x00\x00\x04\x00\x00\x00\x05\x00\x00\x00\x06\x00\x00\x00\x07\x00\x00\x00tb.'
```
写入文件:
In [35]:
```py
a.tofile('foo.csv', sep=',', format="%s")
```
## 查找排序
非零元素的索引:
In [36]:
```py
a.nonzero()
```
Out[36]:
```py
(array([0, 1, 1, 2, 2, 3, 3], dtype=int64),
array([1, 0, 1, 0, 1, 0, 1], dtype=int64))
```
排序:
In [37]:
```py
b = array([3,2,7,4,1])
b.sort()
b
```
Out[37]:
```py
array([1, 2, 3, 4, 7])
```
排序的索引位置:
In [38]:
```py
b = array([2,3,1])
b.argsort(axis=-1)
```
Out[38]:
```py
array([2, 0, 1], dtype=int64)
```
`b` 插入 `a` 中的索引,使得 `a` 保持有序:
In [39]:
```py
a = array([1,3,4,6])
b = array([0,2,5])
a.searchsorted(b)
```
Out[39]:
```py
array([0, 1, 3], dtype=int64)
```
## 元素数学操作
限制在一定范围:
In [40]:
```py
a = array([[4,1,3],[2,1,5]])
a.clip(0,2)
```
Out[40]:
```py
array([[2, 1, 2],
[2, 1, 2]])
```
近似:
In [41]:
```py
a = array([1.344, 2.449, 2.558])
a.round(decimals=2)
```
Out[41]:
```py
array([ 1.34, 2.45, 2.56])
```
累加和:
In [42]:
```py
a = array([[4,1,3],[2,1,5]])
a.cumsum(axis=None)
```
Out[42]:
```py
array([ 4, 5, 8, 10, 11, 16])
```
累乘积:
In [43]:
```py
a.cumprod(axis=None)
```
Out[43]:
```py
array([ 4, 4, 12, 24, 24, 120])
```
## 约简操作
求和:
In [44]:
```py
a = array([[4,1,3],[2,1,5]])
a.sum(axis=None)
```
Out[44]:
```py
16
```
求积:
In [45]:
```py
a.prod(axis=None)
```
Out[45]:
```py
120
```
最小值:
In [46]:
```py
a.min(axis=None)
```
Out[46]:
```py
1
```
最大值:
In [47]:
```py
a.max(axis=None)
```
Out[47]:
```py
5
```
最小值索引:
In [48]:
```py
a.argmin(axis=None)
```
Out[48]:
```py
1
```
最大值索引:
In [49]:
```py
a.argmax(axis=None)
```
Out[49]:
```py
5
```
最大间隔:
In [50]:
```py
a.ptp(axis=None)
```
Out[50]:
```py
4
```
均值:
In [51]:
```py
a.mean(axis=None)
```
Out[51]:
```py
2.6666666666666665
```
标准差:
In [52]:
```py
a.std(axis=None)
```
Out[52]:
```py
1.49071198499986
```
方差:
In [53]:
```py
a.var(axis=None)
```
Out[53]:
```py
2.2222222222222228
```
是否有非零元素:
In [54]:
```py
a.any(axis=None)
```
Out[54]:
```py
True
```
是否全部非零:
In [55]:
```py
a.all()
```
Out[55]:
```py
True
```
删除生成的文件:
In [56]:
```py
import os
os.remove('foo.csv')
os.remove('file.txt')
```