遥想学习 python 之初,我满腹的怀疑为什么 python 本身已经具备的数据容器,运算子,与内置模块处理数据,还要一个 numpy 重复一遍内置函数的功能,直到开始接触了大量的数据与资料处理的实例后,看着大家都用 numpy 到他们的代码中,终于下定决心想把它了解一番。
In short,numpy module 的应用,相比于 python 自己的函数,大幅提升了应付数据时候的运算速度与功能,numpy 是一个基于 C 语言开发的 python 模块,除了更好的在 python 中被处理,还拓展了很多原本 python 本身没有的功能,使其成为一个在数学处理方面更为全面的体系。
三大优势
- 少的内存占用
- 执行速度快
- 更方便的函数呼叫和数据处理
有这些优势的原因如图,numpy 对付数据的方式是按照顺序整齐排列于记忆体中,每个 element 只有 4 bytes 的大小,然而 python 自己不一样,它需要让每个储存的数据以 object 的形式被储存,并且涵盖信息标记对应到的记忆体位置,每个单元需要 14 bytes,并且每次调用数据都要重新根据该 object 的标记对号去检索位置找东西,速度自然不是同一个量级。
与一般的 python 模块类似,numpy 也含有许多 “类(class)” 与其类分支下的 “方法(method)” ,并且一般来说输入参数的内容里面,出现的基本上就是范围,数据尺寸,平均值,标准差,编译类型,数据点,线段形状颜色... 等,是一个还算易懂的 module。
安装方法 numpy
windows case:打开 CMD 输入 where python 找到路径,利用此路径进入 Scripts 文件夹,在此文件夹下输入:
pip install numpy
# Then everything should be done in our computer
Mac / Linux case:打开 Terminal 不用输入任何路径的情况下,直接输入:
sudo pip3 install numpy
# If we want to install numpy for python3 version, use pip3 as the command.
# On the other hand, if we want to install numpy for python2 verision, use pip instead.
p.s. sudo 用来开启管理员权限去下载,可以避开非管理员状态的不必要限制。检查正确安装的方法就是进入 python 里面,输入 import numpy,没有报错即正确。
推荐相关文章
- more
MENU for The Paragraph
- numpy Operators
- numpy.random
- numpy.array (Crucial in Image Deposition)
- more
1. numpy Operators [点击]
more
2. numpy.random [点击]
- numpy.random.normal(loc=0.0, scale=1.0, size=None)
虽然在介绍输入参数的地方有那些 loc=..., scale=... 东西,但是实际上我们内心知道它们为何物即可。import numpy as np import matplotlib.pyplot as plt NB = np.random.normal(20, 3.5, size=(2000)) plt.plot([i for i in range(2000)], NB, 'ro') plt.show() plt.close() # if we are using ubuntu system, mind to close it down
1. loc 表示此次随机高斯分布数据的均值落点,如范例,输入为 20 ,那么图中的中间值即为 20。
2. scale 表示此次随机高斯分布数据的标准差,如范例,输入为 3.5 ,那么基本上分布中 97.5% 的值都落在 (20-3.5*3=9.5)~(20+3.5*3=30.5) 之间。
3. size 表示此次随机高斯分布数据的元素排部方式,None 这边同为 1 的意思,括号内元素个数 ( x ) 表示有 x 个元素,(x, y) 表示 x 条横列 y 束直行,(x, y, z) 表示 x 组数据团 y 条横列 z 束直行,以此类推。
p.s. 如果要修改其产生的高斯分布值,则需要在整个()定义完后添加「.astype(np.float32)」这类的宣告即可。 - numpy.random.randn()
import numpy as np import matplotlib.pyplot as plt NB = np.random.randn(2000) plt.plot([i for i in range(2000)], NB, 'ro') plt.show() plt.close()
这呼叫出来的分布叫做正态分布,但是其实“正态分布”就是“高斯分布”的一种,只是正态分布不能调整平均值与标准差,统一定死为平均值为 0 ,标准差为 1 的分布结果,参数输入的目的只是为了调整数据结构大小(将要产生几个数据,几个维度)。
- numpy.random.random(size=None)
这是一个随机分布的数据创造方法,创出来的数据以浮点数的形式呈现,并且范围只在 0.0~1.0之间(不包含 1.0)。由此得知,输入的参数使用来规范数据尺寸(几维)用的。如果输入的 size 为 None,则只得出一个随机数。import numpy as np import matplotlib.pyplot as plt NB = np.random.random(1000) plt.plot([i for i in range(1000)], NB, 'ro') plt.show() plt.close()
- numpy.random.rand(d1, d2, d3, ..., dn)
这个功能基本上跟上面的“numpy.random.random(size=None)”重复了,得出来的结果也是一个浮点数形式的数据集,并且落点范围都在 0.0~1.0 之间(不包含 1.0),只是在输入 dimension size 的时候这个function 不用以 tuple 的形式把数值 ( ) 起来,差异如下:>>> X = np.random.rand(3, 2) >>> Y = np.random.random((3, 2)) >>> print(x) [[0.50933683 0.54593109] [0.05237493 0.92438944] [0.82475384 0.28345478]] >>> print(Y) [[0.07349583 0.26044589] [0.89348834 0.87377244] [0.95728742 0.41839420]]
- numpy.random.randint(low, high=None, size=None, dtype='np.int')
它可以让我们指定一个开始与结束范围,并在这个范围内随机选中数字,根据的条件如()中的参数,介绍如下:
1. low: 一个数值范围左边较小的数字值,随机选择的时候有包含该值本身
2. high: 一个数值范围右边较大的数字值,随机选择的时候不包含该值本身
3. size: 在这个范围中一共要选择多少个数字到物件中,可以规定多个维度,使用元组定义
4. dtype: 描述产生出来的数字其数据类型,有 np.int,int64,int 等等
p.s. 如果参数部分只有填入一个数字,那么即默认该数字为最大值
实际代码示例如下:>>> a = np.random.randint(0, 10, size=10, dtype=np.int) >>> b = np.random.randint(0, 10, size=(2, 3), dtype=np.int64) >>> c = np.random.rnadint(0, 10, size=(2, 3, 4)) >>> print(a) [4 7 0 5 7 5 3 9 2 5] >>> print(b) [[1 2 4] [3 9 1]] >>> print(c) [[[5 4 2 2] [7 0 8 8] [7 8 6 0]] [[7 0 0 2] [2 3 9 6] [2 2 0 5]]]
- more
- 未涵盖的内容介绍
link:https://blog.csdn.net/akadiao/article/details/78252840?locationNum=9&fps=1
3. numpy.array [点击]
- numpy.linspace(start, stop, num=30, endpoint=True, retstep=False, dtype=np.float64)
这个函数是用于切割一个给定范围的数字段,num 后面的数字代表的是要切割的段数,根据一开始给定的起始值与终点值,并设定切割段是否涵盖终点值的特殊标注,最后加上数据类型的设定。
其中,初始,结束,切割段数是必要的输入值,剩下的参数可以不输入,不输入的话,则以上是默认值。
start:表示数列从哪里开始被切;stop:表示数列切到哪里停止;num:表示切的段数;endpoint:表示是否包含了最后停止的该点;retstep:表示是否连同结果用 tuple 的表示方式一起显示平均切出来的公差;dtype:表示资料形态。retstep 一旦使用了,则结果就会变成 tuple 的形式输出,切记。>>> import numpy as np >>> data = np.linspace(-1, 1, 5, retstep=True, endpoint=False, dtype=np.float32) # the sequence of the parameters behind doesn't have to be the same as the example >>> print(data) (array([-1, -0.6, -0.2, 0.2, 0.6], dtype=float32), 0.4)
- numpy.newaxis
只能够用在 list 上面,功能就是可以把一个 list 里面的元素变成个别独立的小 list ,硬生生的让原本只有一个维度的资料,变成有跟数据个数一样多的维度。
This is a really convenient small gadget to convert the data structure into a different dimension easily.>>> data[:, np.newaxis] TypeError: data type not understood # because this is a tuple, which can't be applied to this function >>> Dlist = [1, 2, 4, 5] >>> Dlist[:, np.newaxis] # or the code can be written as Dlist[:, None] >>> [[1], [2], [4], [5]]
- numpy.eye(N, M=None, k=0, dtype=np.float, order='C')
此函数主要用来生成单位方阵使用,也可以根据自己的需求调整矩阵的尺寸,并且其第一个元素可以作为 one_hot 的函数功能。
N: 表示有几横列需要被呈现
M: 表示有几直行要被呈现(如果不写此参数,则默认 N=M)
k: 决定第几个数字数过来的元素作为起始点生成对角矩阵
dtype: 表示内部元素数字类型import numpy as np print(np.eye(8, 6, k=3, dtype=np.float)) print('----- Separation -----') print(np.eye(1, 10, k=8, dtype=np.int64)) ### ----- Result as below ----- ### [[0. 0. 0. 1. 0. 0.] [0. 0. 0. 0. 1. 0.] [0. 0. 0. 0. 0. 1.] [0. 0. 0. 0. 0. 0.] [0. 0. 0. 0. 0. 0.] [0. 0. 0. 0. 0. 0.] [0. 0. 0. 0. 0. 0.] [0. 0. 0. 0. 0. 0.]] ----- Separation ----- [[0 0 0 0 0 0 0 0 1 0]] # it can be regarded as one_hot() result
!! 注意:此函数可以配合 one_hot 功能使用,只要在方阵的对角矩阵下在后面添加原列表的索引,就可以有 one_hot 的功能。 !!
代码如下演示:import numpy as np example = [3, 5, 7, 8, 9, 4, 4, 1, 1, 0] cls = np.max(example) + 1 one_hot = np.eye(cls, dtype=float)[example] print(cls) print('----- Separation -----') print(one_hot) ### ----- Result as below ----- ### 10 ----- Separation ----- [[0. 0. 0. 1. 0. 0. 0. 0. 0. 0.] [0. 0. 0. 0. 0. 1. 0. 0. 0. 0.] [0. 0. 0. 0. 0. 0. 0. 1. 0. 0.] [0. 0. 0. 0. 0. 0. 0. 0. 1. 0.] [0. 0. 0. 0. 0. 0. 0. 0. 0. 1.] [0. 0. 0. 0. 1. 0. 0. 0. 0. 0.] [0. 0. 0. 0. 1. 0. 0. 0. 0. 0.] [0. 1. 0. 0. 0. 0. 0. 0. 0. 0.] [0. 1. 0. 0. 0. 0. 0. 0. 0. 0.] [1. 0. 0. 0. 0. 0. 0. 0. 0. 0.]]
- 选取 numpy 列表数据的超快捷方法有两种
1. 根据列表元素的位置选择需要的元素,代码如下:import numpy as np a = np.array([1, 2, 3, 4, 4, 3, 3, 2, 0]) b = np.array([3, 4, 7, 7, 8, 4]) print(a[b]) ### ----- Result as below ----- ### array([4, 4, 2, 2, 0, 4])
2. 根据列表元素对应的布尔值开关选择是否留下元素值,代码如下:
import numpy as np a = np.array([1, 2, 3, 4, 4, 3, 3, 2, 0]) c = np.array([0, 0, 0, 1, 1, 0, 1, 1, 1]) d = (c == True) # When True, it sends back the actual boolean value of the number # When False, it sends back the converted boolean value of the number print(a[d]) # Mind that the len of a and c should be the same ### ----- Result as below ----- ### array([4, 4, 3, 2, 0])
- numpy.where(condition, x, y)
用来回传在一个 array 的元素中,符合 condition 的元素的位置信息,并以元组的方式回传值,如下代码:import numpy as np a = np.array([1, 2, 3, 4, 4, 3, 3, 2, 0]) print(np.where(a==4) ### ----- Result as below ----- ### (array([3, 4]),)