Numpy学习手册

标准安装的Python中用列表(list)保存一组值,可以用来当作数组使用,不过由于列表的元素可以是任何对象,因此列表中所保存的是对象的指针。这样为了保存一个简单的[1,2,3],需要有3个指针和三个整数对象。对于数值运算来说这种结构显然比较浪费内存和CPU计算时间。

此外Python还提供了一个array模块,array对象和列表不同,它直接保存数值,和C语言的一维数组比较类似。但是由于它不支持多维,也没有各种运算函数,因此也不适合做数值运算。

NumPy的诞生弥补了这些不足,NumPy提供了两种基本的对象:ndarray(N-dimensional array object)和 ufunc(universal function object)。ndarray(下文统一称之为数组)是存储单一数据类型的多维数组,而ufunc则是能够对数组进行处理的函数。

学习numpy的一个很好的tutorial,讲得很详细,不过后面的比较深入,有些地方可以跳过。
http://old.sebug.net/paper/books/scipydoc/numpy_intro.html#id8

接下来我对机器学习中主要用到的几种形式总结一下。

1.
rand(d0, d1, …, dn)

np.random.rand(3,2)
array([[ 0.14022471,  0.96360618],  #random
[ 0.37601032,  0.25528411],  #random
[ 0.49313049,  0.94909878]]) #random

random_sample([size])
返回随机的浮点数,在半开区间 [0.0, 1.0)。
(b - a) * random_sample() + a

>>>np.random.random_sample()
>>>0.47108547995356098
>>>np.random.random_sample((5,))
>>>array([ 0.30220482,  0.86820401,  0.1654503 ,  0.11659149,  0.54323428])

shuffle(x)

现场修改序列,改变自身内容。(类似洗牌,打乱顺序)

>>>arr = np.arange(10)
>>>np.random.shuffle(arr)
>>>arr
[1 7 5 2 9 4 3 6 0 8]

>>> arr = np.arange(9).reshape((3, 3))
>>> np.random.shuffle(arr)
>>> arr
array([[3, 4, 5],
   [6, 7, 8],
   [0, 1, 2]])

normal([loc, scale, size])正态(高斯)分布

使用 np.random.RandomState() 获取随机数生成器

>> rng = np.random.RandomState(22)
>> rng.rand(2, 3)
array([[ 0.48168106,  0.42053804,  0.859182  ],
   [ 0.17116155,  0.33886396,  0.27053283]])

numpy.asarray
Convert the input to an array.
Convert a list into an array:

>>>
>>> a = [1, 2]
>>> np.asarray(a)
array([1, 2])

numpy.zeros
Return a new array of given shape and type, filled with zeros.

>>> np.zeros(5)
array([ 0.,  0.,  0.,  0.,  0.])
>>>
>>> np.zeros((5,), dtype=np.int)
array([0, 0, 0, 0, 0])
>>>
>>> np.zeros((2, 1))
array([[ 0.],
   [ 0.]])
>>>
>>> s = (2,2)
>>> np.zeros(s)
array([[ 0.,  0.],
   [ 0.,  0.]])

numpy.sqrt
Return the positive square-root of an array, element-wise.

>>> np.sqrt([1,4,9])
array([ 1.,  2.,  3.])
>>>
>>> np.sqrt([4, -1, -3+4J])
array([ 2.+0.j,  0.+1.j,  1.+2.j])

numpy.ones
Return a new array of given shape and type, filled with ones.

    >>> np.ones(5)
array([ 1.,  1.,  1.,  1.,  1.])
>>>
>>> np.ones((5,), dtype=np.int)
array([1, 1, 1, 1, 1])

numpy.prod
Return the product of array elements over a given axis.

By default, calculate the product of all elements:

>>>
>>> np.prod([1.,2.])
2.0
Even when the input array is two-dimensional:

>>>
>>> np.prod([[1.,2.],[3.,4.]])
24.0
But we can also specify the axis over which to multiply:

>>>
>>> np.prod([[1.,2.],[3.,4.]], axis=1)
array([  2.,  12.])

numpy.savez
Save several arrays into a single file in uncompressed .npz format.

numpy.savetxt
Save an array to a text file.

numpy.load
Load arrays or pickled objects from .npy, .npz or pickled files.

基本上一般的机器学习实验过程中会用到的形式以上都包括了,其他一些不常用的则没举出了。
遇到没见过的形式可以参考下面。
Numpy的官方文档:https://docs.scipy.org/doc/numpy/index.html
PS拒绝眼高手低,这些东西看看简单,但是没写几个theano or TensorFlow程序绝对不容易记住。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值