1.1NumPy是什么?
NumPy是一个开源的Python科学计算库,NumericalPython简写。
1.2NumPy有什么功能?
NumPy主要的功能之一用来操作数组和矩阵。
NumPy是科学计算、深度学习等高端领域的必备工具。
使用Tensor Flow、Caffe框架训练神经网络模型时,需要进行大量矩阵、数组等复杂的运算,可以直接使用NumPy里面的API.
NumPy还包含了很多常用的数学函数,覆盖了很多的数学领域,比如线性代数、傅里叶变换、随机数生成等。
Python+NumPy==Matlab
1.3为什么要使用NumPy?
(1)NumPy提供了很多高端的函数,可以对数组和矩阵进行复杂运算,比直接使用Python编码更高效;
(2)NumPy有超过10年历史,核心算法经过了长时间和多人验证,非常稳定;
(3)NumPy的核心算法都是由C语言编写,执行效率更高(参见1.6的实例);
(4)NumPy扩展性非常好,很容易集成到其他语言中(Java, C#,JavaScript);
(5)NumPy是开源的,免费,由广泛的社区支持。
1.4NumPy开发环境
Anaconda:包含NumPy
Eclips+pydev
Pycharm:
NumPy官网:http://www.numpy.org/
源码可以从git获取:
git clone git://github.com/numpy/numpy.git numpy
python setup.py build
sudo python setup.py install --prefix=/usr/local
还是建议安装Anaconda包,其中包含多个常用python工具包;
Anaconda官网:https://www.anaconda.com/download/
官网有时比较慢,国内清华镜像:https://mirrors.tuna.tsinghua.edu.cn/anaconda/archive/
1.5 NumPy hello world
用arange()创建一个一维数组:
from numpy import arange
def sum(n):
a=arange(n)**2 #数组每个元素2次方
b = arange(n)*4
print("a:",a)
print("b:",b)
c=a+b
return c
print("arange(5):",arange(5))
print("sum(5):",sum(5))
输出结果:
arange(5): [0 1 2 3 4]
a: [ 0 1 4 9 16]
b: [ 0 4 8 12 16]
sum(5): [ 0 5 12 21 32]
创建并访问多维数组:
b=array([arange(5),arange(6)])
print(b)
print(b[0])
print(b[1])
print(b[0][2])
[array([0, 1, 2, 3, 4]) array([0, 1, 2, 3, 4, 5])]
[0 1 2 3 4]
[0 1 2 3 4 5]
2
1.6 举个向量加法实例,测试numpy效率
需求:
向量a: 1~n的平方和;
向量b: 1~n的立方和
求a+b,分别用基于numpy和原始python计算,对比运行效率;
import sys
from datetime import datetime
import numpy as np
def numpysum(n):
a = np.arange(n)**2
b = np.arange(n)**3
c = a+b
return c
def pythonsum(n):
c = []
for i in range(n):
a = i**2
b = i**3
c.append(a+b)
return c
size = int(sys.argv[1])
start = datetime.now()
c=pythonsum(size)
delta = datetime.now()-start
print("The last 2 elements of the sum",c[-2:])
print("PythonSum elapsed time in microsseconds:",delta.microseconds)
start = datetime.now()
c = numpysum(size)
delta = datetime.now()-start
print("The last 2 elements of the sum:",c[-2:])
print("NumpySum elapsed time in microseconds:",delta.microseconds)
运行结果:
leon@pc:~/work/py/numpy/test$ python numpy_time.py 10
The last 2 elements of the sum [576, 810]
PythonSum elapsed time in microsseconds: 10
The last 2 elements of the sum: [576 810]
NumpySum elapsed time in microseconds: 20
test 1: 求1!10时,numpy 需要20ms,比python 10ms慢
leon@pc:~/work/py/numpy/test$ python numpy_time.py 100
The last 2 elements of the sum [950796, 980100]
PythonSum elapsed time in microsseconds: 41
The last 2 elements of the sum: [950796 980100]
NumpySum elapsed time in microseconds: 20
test 2: 求1!100时,numpy 需要20ms,比python 41ms快一倍
leon@pc:~/work/py/numpy/test$ python numpy_time.py 1000
The last 2 elements of the sum [995007996, 998001000]
PythonSum elapsed time in microsseconds: 365
The last 2 elements of the sum: [995007996 998001000]
NumpySum elapsed time in microseconds: 23
test 3: 求1!1000时,numpy 需要23ms,比python 365ms快约16倍
leon@pc:~/work/py/numpy/test$ python numpy_time.py 10000
The last 2 elements of the sum [999500079996, 999800010000]
PythonSum elapsed time in microsseconds: 3690
The last 2 elements of the sum: [999500079996 999800010000]
NumpySum elapsed time in microseconds: 132
test 4: 求1!10000时,numpy 需要132ms,比python 3690ms快约28倍
leon@pc:~/work/py/numpy/test$ python numpy_time.py 100000
The last 2 elements of the sum [999950000799996, 999980000100000]
PythonSum elapsed time in microsseconds: 37803
The last 2 elements of the sum: [999950000799996 999980000100000]
NumpySum elapsed time in microseconds: 1654
test 5: 求1!100000时,numpy 需要132ms,比python 3690ms快约22倍
当然以上非严格统计,只是粗略测算运行效率,可见数据量略大后,效率相差二十多倍,numpy计算效率明显高于python