Python编程深度学习计算库之numpy

NumPy是python下的计算库,被非常广泛地应用,尤其是近来的深度学习的推广。在这篇文章中,将会介绍使用numpy进行一些最为基础的计算。

NumPy vs SciPy

NumPy和SciPy都可以进行运算,主要区别如下

最近比较热门的深度学习,比如在神经网络的算法,多维数组的使用是一个极为重要的场景。如果你熟悉tensorflow中的tensor的概念,你会非常清晰numpy的作用。所以熟悉Numpy可以说是使用python进行深度学习入门的一个基础知识。

安装

liumiaocn:tmp liumiao$ pip install numpy

Collecting numpy

Downloading https://files.pythonhosted.org/packages/b6/5e/4b2c794fb57a42e285d6e0fae0e9163773c5a6a6a7e1794967fc5d2168f2/numpy-1.14.5-cp27-cp27m-macosx_10_6_intel.macosx_10_9_intel.macosx_10_9_x86_64.macosx_10_10_intel.macosx_10_10_x86_64.whl (4.7MB)

100% |████████████████████████████████| 4.7MB 284kB/s

Installing collected packages: numpy

Successfully installed numpy-1.14.5

liumiaocn:tmp liumiao$

确认

liumiaocn:tmp liumiao$ pip show numpy

Name: numpy

Version: 1.14.5

Summary: NumPy: array processing for numbers, strings, records, and objects.

Home-page: http://www.numpy.org

Author: Travis E. Oliphant et al.

Author-email: None

License: BSD

Location: /usr/local/lib/python2.7/site-packages

Requires:

Required-by:

liumiaocn:tmp liumiao$

使用

使用numpy的数组

使用如下例子简单来理解一下numpy的数组的使用:

liumiaocn:tmp liumiao$ cat np-1.py

#!/usr/local/bin/python

import numpy as np

arr = [1,2,3,4]

print("array arr: ", arr)

np_arr = np.array(arr)

print("numpy array: ", np_arr)

print("doulbe calc : ", 2 * np_arr)

print("ndim: ", np_arr.ndim)

liumiaocn:tmp liumiao$ python np-1.py

('array arr: ', [1, 2, 3, 4])

('numpy array: ', array([1, 2, 3, 4]))

('doulbe calc : ', array([2, 4, 6, 8]))

('ndim: ', 1)

liumiaocn:tmp liumiao$

多维数组&ndim/shape

ndim在numpy中指的是数组的维度,如果是2维值则为2,在下面的例子中构造一个步进为2的等差数列,然后将其进行维度的转换同时输出数组的ndim和shape的值以辅助对于ndim和shape含义的理解。

liumiaocn:tmp liumiao$ cat np-2.py

#!/usr/local/bin/python

import numpy as np

arithmetic = np.arange(0,16,2)

print(arithmetic)

print("ndim: ",arithmetic.ndim," shape:", arithmetic.shape)

#resize to 2*4 2-dim array

arithmetic.resize(2,4)

print(arithmetic)

print("ndim: ",arithmetic.ndim," shape:", arithmetic.shape)

#resize to 2*2*2 3-dim array

array = arithmetic.resize(2,2,2)

print(arithmetic)

print("ndim: ",arithmetic.ndim," shape:", arithmetic.shape)

liumiaocn:tmp liumiao$ python np-2.py

[ 0 2 4 6 8 10 12 14]

('ndim: ', 1, ' shape:', (8,))

[[ 0 2 4 6]

[ 8 10 12 14]]

('ndim: ', 2, ' shape:', (2, 4))

[[[ 0 2]

[ 4 6]]

[[ 8 10]

[12 14]]]

('ndim: ', 3, ' shape:', (2, 2, 2))

liumiaocn:tmp liumiao$

另外也可以使用reshape进行维度的调整。

等差数列&等比数列

numpy和matlab写起来有很多函数基本一样,比如等比数列和等差数列可以使用linspace和logspace进行。

logspace缺省的时候指的是以10给底,但是可以通过指定base进行设定

liumiaocn:tmp liumiao$ cat np-3.py

#!/usr/local/bin/python

import numpy as np

print("np.linspace(1,4,4):", np.linspace(1,4,4))

print("np.logspace(1,4,4):", np.logspace(1,4,4))

print("np.logspace(1,4,4,base=2):",np.logspace(1,4,4,base=2))

liumiaocn:tmp liumiao$ python np-3.py

('np.linspace(1,4,4):', array([1., 2., 3., 4.]))

('np.logspace(1,4,4):', array([ 10., 100., 1000., 10000.]))

('np.logspace(1,4,4,base=2):', array([ 2., 4., 8., 16.]))

liumiaocn:tmp liumiao$

数组初始化

numpy提供了很方便的初始化的函数,比如

liumiaocn:tmp liumiao$ cat np-4.py

#!/usr/local/bin/python

import numpy as np

print("np.zeros(6):",np.zeros(6))

print("np.zeros((2,3)):",np.zeros((2,3)))

print("np.ones(6):",np.ones(6))

print("np.ones((2,3)):",np.ones((2,3)))

print("np.random.random(6):",np.random.random(6))

print("np.random.random(6):",np.random.random(6))

print("np.random.random((2,3)):",np.random.random((2,3)))

print("np.random.seed(1234)")

np.random.seed(1234)

print("np.random.random(6):",np.random.random(6))

print("np.random.seed(1234)")

np.random.seed(1234)

print("np.random.random(6):",np.random.random(6))

liumiaocn:tmp liumiao$ python np-4.py

('np.zeros(6):', array([0., 0., 0., 0., 0., 0.]))

('np.zeros((2,3)):', array([[0., 0., 0.],

[0., 0., 0.]]))

('np.ones(6):', array([1., 1., 1., 1., 1., 1.]))

('np.ones((2,3)):', array([[1., 1., 1.],

[1., 1., 1.]]))

('np.random.random(6):', array([0.06909968, 0.27468844, 0.59127996, 0.56973602, 0.45985047,

0.95384945]))

('np.random.random(6):', array([0.62996648, 0.2824114 , 0.2698051 , 0.09262053, 0.50862503,

0.96600255]))

('np.random.random((2,3)):', array([[0.66880129, 0.8834006 , 0.49458989],

[0.28335563, 0.65711274, 0.76726504]]))

np.random.seed(1234)

('np.random.random(6):', array([0.19151945, 0.62210877, 0.43772774, 0.78535858, 0.77997581,

0.27259261]))

np.random.seed(1234)

('np.random.random(6):', array([0.19151945, 0.62210877, 0.43772774, 0.78535858, 0.77997581,

0.27259261]))

liumiaocn:tmp liumiao$

总结

以上是 Python编程深度学习计算库之numpy 的全部内容, 来源链接: utcz.com/z/312849.html

回到顶部