Numpy 的学习笔记
几点约定
全文采用1 ~ 3个井号,对应全书的一级、二级和三级标题。
采用四个井号来记录正式的笔记内容
Quickstart Tutorial
基础
-
Numpy的主要目的,是处理相同类型数据构成的【表元素】Table Elements
-
Numpy 的维度,被称为【轴】axes
# 以下数据有两个轴,第一个轴长度为2, 第二个轴长度为3
# 与线性代数中的约定是相同的,以下为一个2 x 3的矩阵
[[ 1., 0., 0.],
[ 0., 1., 2.]]
ndarray的属性
这里讲述了ndarray的6个属性
ndim : 维度、shape : 形状、size : 大小
dtype : 元素类型、itemsize : 元素体积、data : 缓存空间
-
ndarray.ndim
轴(维度)的数量
-
ndarray.shape
数组的形状,每一个维度上的尺寸,构成一个tuple元组
-
ndarray.size
数组的总元素数,等于size各维度的乘积
-
ndarray.dtype
数组元素的数据类型。除了标准的Python类型之外,NumPy还给了自己的一些类型,包括numpy.int32, numpy.int16, numpy.float64
-
ndarray.itemsize
数组元素占用的位数。比如float64,占用itemsize 8。与ndarray.dtype.itemsize是等价的。
-
ndarray.data
包含数组实际元素的缓冲区。通常不需要使用,而使用检索访问数据。
>>> import numpy as np
>>> a = np.arange(15).reshape(3, 5)
>>> a
array([[ 0, 1, 2, 3, 4],
[ 5, 6, 7, 8, 9],
[10, 11, 12, 13, 14]])
>>> a.shape # shape 形状
(3, 5)
>>> a.ndim # ndim 维度
2
>>> a.dtype.name # dtype 类型
'int32'
>>> a.itemsize # itemsize 元素体积
4
>>> a.size # size 大小
15
>>> a
array([[ 0, 1, 2, 3, 4],
[ 5, 6, 7, 8, 9],
[10, 11, 12, 13, 14]])
>>> type(a) # a的类型
<class 'numpy.ndarray'>
>>> b = np.array([6, 7, 8])
>>> b
array([6, 7, 8])
>>> type(b)
<class 'numpy.ndarray'>
>>> b.size
3
>>> b.ndim
1
>>> b.dtype
dtype('int32')
>>> b.data # 缓存空间
<memory at 0x000001E33E78F588>
>>> b.itemsize
4
创建array(数组)的方法
- 使用常规的Python list 或者tuple,调用【数组方法】array method
注意,采用array方法的时候,一定是一个list或者tuple,而不能是仅仅是一组有序的数字
import numpy as np
a = np.array([2, 3, 4])
b = np.array((1.2, 3.5, 5.1))
- 采用array方法创建多维数组
>>> c = np.array([[1.5, 2.3], [4, 5, 6]])
>>> c # 注意因为上面的两个list尺寸不同,所以创建了两个list构成的数组
array([list([1.5, 2.3]), list([4, 5, 6])], dtype=object)
>>> c = np.array([[1.5, 2.3, 3.2], [4, 5, 6]])
>>> c # 采用相同维度的list,创建了多维array
array([[1.5, 2.3, 3.2],
[4. , 5. , 6. ]])
>>> c = list(zip(a, b)) # array可以采用zip方法,得到新的list
>>> c
[(2, 1.2), (3, 3.5), (4, 5.1)]
>>> c = np.array(list(zip(a,b))) # 新的list可以采用array method 转变成array
>>> c
array([[2. , 1.2],
[3. , 3.5],
[4. , 5.1]])
>>> c = [a, b ]
>>> c # 注意用list来并array,得到的是list
[array([2, 3, 4]), array([1.2, 3.5, 5.1])]
>>> c = np.array((a, b)) # 也用array来并array,可以得到array
>>> c
array([[2. , 3. , 4. ],
[1.2, 3.5, 5.1]])
## 可以显式给出dtype,给定数组的元素类型。
>>> a_complex = np.array([[1,2], [3, 4]], dtype = complex)
>>> a
array([2, 3, 4])
>>> a_complex
array([[1.+0.j, 2.+0.j],
[3.+0.j, 4.+0.j]])
- 采用占位符创建array
- 目前NumPy支持的占位符包括zeros/zeros_like、ones/ones_like、empty/empty_like、full_like、arange、linspace、random.rand、random.randn、fromfunction、fromfile
np.zeros((3,4))
np.zeros((3,4), dtype = np.float64)
>>> np.ones((2,3,4), dtype = np.float32)
array([[[1., 1., 1., 1.],
[1., 1., 1., 1.],
[1., 1., 1., 1.]],
[[1., 1., 1., 1.],
[1., 1., 1., 1.],
[1., 1., 1., 1.]]], dtype=float32)
- arange = array range,是一种模仿Python中range但是生成array的方法
>>> np.arange(10, 5, -0.5)
array([10. , 9.5, 9. , 8.5, 8. , 7.5, 7. , 6.5, 6. , 5.5])
>>> range(10, 5, -0.5) # 注意range不支持浮点数作为步长、起始和终止值
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'float' object cannot be interpreted as an integer
-
因为Python只有有限的小数位,因此采用arange,给出步长step的方法,有可能得出不确定的数组长度。
-
所以通常采用linspace的方法,给出数组的元素个数,而不是采用step给出步长。
>>> np.linspace(0, 2, 9) # 注意linspace的方法给出的是数组包含第一个和最后一个元素。即,在包括括号里的0和2。
array([0. , 0.25, 0.5 , 0.75, 1. , 1.25, 1.5 , 1.75, 2. ])
>>> np.arange(0, 2, 0.25)
array([0. , 0.25, 0.5 , 0.75, 1. , 1.25, 1.5 , 1.75]) # arange不包括最后一个数。
>>> np.arange(0, 2.1, 0.25)
array([0. , 0.25, 0.5 , 0.75, 1. , 1.25, 1.5 , 1.75, 2. ])
打印数组 print arrays
从最后一个轴开始,依次从到右
导数第二个轴开始,依次从上到下
剩下的轴,从上到下,用空行来隔开每一组(对应每一个index数值)
数组太大的时候,NumPy自动跳过中间的部分,只打印数组的角部(默认是角部的3个元素)
>>> c = np.arange(1000000)
>>> c.reshape(100, 100, 100) # 如下面的3维素组,每个维度尺寸都是100,打印的时候,只输出每个维度的[0, 1, 2,]和 [97, 98, 99]对应的元素。
这样的话,输出8 * 3 * 3 *3 = 216 个元素
array([[[ 0, 1, 2, ..., 97, 98, 99],
[ 100, 101, 102, ..., 197, 198, 199],
[ 200, 201, 202, ..., 297, 298, 299],
...,
[ 9700, 9701, 9702, ..., 9797, 9798, 9799],
[ 9800, 9801, 9802, ..., 9897, 9898, 9899],
[ 9900, 9901, 9902, ..., 9997, 9998, 9999]],
[[ 10000, 10001, 10002, ..., 10097, 10098, 10099],
[ 10100, 10101, 10102, ..., 10197, 10198, 10199],
[ 10200, 10201, 10202, ..., 10297, 10298, 10299],
...,
[ 19700, 19701, 19702, ..., 19797, 19798, 19799],
[ 19800, 19801, 19802, ..., 19897, 19898, 19899],
[ 19900, 19901, 19902, ..., 19997, 19998, 19999]],
[[ 20000, 20001, 20002, ..., 20097, 20098, 20099],
[ 20100, 20101, 20102, ..., 20197, 20198, 20199],
[ 20200, 20201, 20202, ..., 20297, 20298, 20299],
...,
[ 29700, 29701, 29702, ..., 29797, 29798, 29799],
[ 29800, 29801, 29802, ..., 29897, 29898, 29899],
[ 29900, 29901, 29902, ..., 29997, 29998, 29999]],
...,
[[970000, 970001, 970002, ..., 970097, 970098, 970099],
[970100, 970101, 970102, ..., 970197, 970198, 970199],
[970200, 970201, 970202, ..., 970297, 970298, 970299],
...,
[979700, 979701, 979702, ..., 979797, 979798, 979799],
[979800, 979801, 979802, ..., 979897, 979898, 979899],
[979900, 979901, 979902, ..., 979997, 979998, 979999]],
[[980000, 980001, 980002, ..., 980097, 980098, 980099],
[980100, 980101, 980102, ..., 980197, 980198, 980199],
[980200, 980201, 980202, ..., 980297, 980298, 980299],
...,
[989700, 989701, 989702, ..., 989797, 989798, 989799],
[989800, 989801, 989802, ..., 989897, 989898, 989899],
[989900, 989901, 989902, ..., 989997, 989998, 989999]],
[[990000, 990001, 990002, ..., 990097, 990098, 990099],
[990100, 990101, 990102, ..., 990197, 990198, 990199],
[990200, 990201, 990202, ..., 990297, 990298, 990299],
...,
[999700, 999701, 999702, ..., 999797, 999798, 999799],
[999800, 999801, 999802, ..., 999897, 999898, 999899],
[999900, 999901, 999902, ..., 999997, 999998, 999999]]])
想要输出更多的元素,需要设置np的参量
np.set_printoptions(threshold = np.nan)
经过测试,100万数组的全面输出,surface会上的python会假死。
>>> c ** 2
array([ 0, 1, 4, ..., -733379959, -731379964,
-729379967], dtype=int32)
>>> d = np.int64(c) # numpy 的格式转换,注意转换后的格式,需要赋予新的数组
>>> d ** 2
array([ 0, 1, 4, ..., 999994000009,
999996000004, 999998000001], dtype=int64)
array的算数运算
通常的加、减、乘、除,都是elementwise,也就是一个元素一个元素计算的。
array的矩阵运算
点乘 - ‘@’
- np.all # 某个轴上是否全True
- np.any # 某个轴上是否有True
- np.apply_along_axis # 将函数作用于某个轴
- np.argmax # 返回最大值所在的坐标
- np.argmin # 返回最小值所在的坐标
- np.argsort # 返回元素沿某个轴的排序
- np.average # 返回平均值
- np.bincount # 返回非负整数的出现次数
- np.ceil #
- np.clip
- np.conj
- np.corrcoef
- np.cov
- np.cross
- np.cumprod
- np.cumsum
- np.diff
- np.dot
- np.floor
- np.inner
- np.inv
- np.lexsort
- np.max
- np.maximum
- np.mean
- np.median
- np.min
- np.minimum
- np.nonzero
- np.outer
- np.prod
- np.re
- np.round
- np.sort
- np.std
- np.sum
- np.trace
- np.transpose
- np.var
- np.vdot
- np.vectorize
- np.where
import numpy as np
import matplotlib.pyplot as plt
from time import time
def timethis()
def mandelbrot( h,w, maxit=40 ):
"""Returns an image of the Mandelbrot fractal of size (h,w)."""
y,x = np.ogrid[ -1.4:1.4:h*1j, -2:0.8:w*1j ]
c = x+y*1j
z = c
divtime = maxit + np.zeros(z.shape, dtype=int)
for i in range(maxit):
z = z**2 + c
diverge = z*np.conj(z) > 2**2 # who is diverging
div_now = diverge & (divtime==maxit) # who is diverging now
divtime[div_now] = i # note when
z[diverge] = 2 # avoid diverging too much
return divtime
plt.imshow(mandelbrot(4000,4000))
plt.show()
[未完待续]
来源:https://blog.csdn.net/Flamsky/article/details/100811827