当前位置: 移动技术网 > 科技>人工智能>云计算 > numpy学习记录

numpy学习记录

2018年03月11日  | 移动技术网科技  | 我要评论
背 景 断断续续看了一些不错的 numpy 学习资料,发现他们大都是对官网学习文档的摘抄。放空心态,啃官网文档,做个快乐的小学生。 摘 要 NumPy 模块主要提供: 一个可包含任意同

背 景

断断续续看了一些不错的 numpy 学习资料,发现他们大都是对官网学习文档的摘抄。放空心态,啃官网文档,做个快乐的小学生。

摘 要

NumPy 模块主要提供:

一个可包含任意同质元素【arbitrary homogeneous items】的数组对象【array object】 基于 数组 的 各种高性能数学操作

线性代数、傅里叶变换、随机数生成器

这个数组对象的主要属性:
ndarray.ndim、ndarray.shape、ndarray.size、ndarray.dtype、ndarray.itemsize及ndarray.data,具体含义直接看案例:

>>> a = np.arange(15).reshape(3,5)
array([[ 0,  1,  2,  3,  4],
       [ 5,  6,  7,  8,  9],
       [10, 11, 12, 13, 14]])      
>>> type(a)
numpy.ndarray
>>> a.shape
(3, 5)
>>> a.ndim
2
>>> a.dtype.name
'int32'
>>> a.itemsize #元素占用空间大小
4
>>> a.size
15
>>> a = np.array(['tst',[1,2],10.29])
异常:ValueError: setting an array element with a sequence
(体验一下 数组元素的 'homogeneous' 性)
>>> a.data

创 建

import numpy as np
>>> a = np.array([[2,3,4],[3,5,7]])
array([[2, 3, 4],
       [3, 5, 7]])      
>>> b = np.array([[1,2],[3,4]],dtype=complex) #创建时指定元素类型
array([[ 1.+0.j,  2.+0.j],
       [ 3.+0.j,  4.+0.j]])       
>>> c = np.zeros((3,4)) #元素类型默认为 float
array([[ 0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.]])       
>>> d = np.arange(10,30,5)# 步长为 5
array([10, 15, 20, 25])
>>> e = np.linspace(0,2,9) # 0~2之间生成 9 个元素
array([ 0.  ,  0.25,  0.5 ,  0.75,  1.  ,  1.25,  1.5 ,  1.75,  2.  ])
>>> f = np.sin(e)
array([ 0.        ,  0.24740396,  0.47942554,  0.68163876,  0.84147098,
        0.94898462,  0.99749499,  0.98398595,  0.90929743])

基本操作

array的操作都是基于 元素粒度的。

>>> a = np.array( [20,30,40,50] )
>>> b = np.arange( 4 )
>>> a-b # 相减
array([20, 29, 38, 47])
>>> b**2
array([0, 1, 4, 9])
>>> a<35
array([ True,  True, False, False], dtype=bool)
>>> a.dot(b) # 相乘
260
>>> c = np.ones((2,3),dtype=int)
array([[1, 1, 1],
       [1, 1, 1]])
>>> d = np.random.random((2,3)) 
array([[ 0.2405914 ,  0.92889199,  0.53239625],
       [ 0.95156094,  0.03129976,  0.19768109]])
>>> d+=c # 相加
>>> d
array([[ 1.2405914 ,  1.92889199,  1.53239625],
       [ 1.95156094,  1.03129976,  1.19768109]])
>>>e = np.random.random((2,3))
>>> np.sqrt(e) # 开方
array([[ 0.62054761,  0.51564375,  0.78904659],
       [ 0.56813652,  0.67124161,  0.91439529]])
>>> a = np.arange(20).reshape(4,5)
array([[ 0,  1,  2,  3,  4],
       [ 5,  6,  7,  8,  9],
       [10, 11, 12, 13, 14],
       [15, 16, 17, 18, 19]])

>>> np.apply_along_axis(func1d=np.sum,axis=1,arr=a) # 对 a 进行行级别求和
array([10, 35, 60, 85])

检索与遍历

单维度检索

单维度检索与python中的列表操作类似

>>> = np.arange(10)**3
array([  0,   1,   8,  27,  64, 125, 216, 343, 512, 729], dtype=int32)
>>> a[2]
8
>>> a[2:5]
array([ 8, 27, 64], dtype=int32)

>>> def f(x,y):
    return 10*x+y
>>> b = np.fromfunction(f,(5,4),dtype=int)
>>> b
array([[ 0,  1,  2,  3],
       [10, 11, 12, 13],
       [20, 21, 22, 23],
       [30, 31, 32, 33],
       [40, 41, 42, 43]])
>>> b[2,3]
23
>>> b[0:5,1]
array([ 1, 11, 21, 31, 41])
>>> b[:,1]
array([ 1, 11, 21, 31, 41])
>>> b[1:3,:]
array([[10, 11, 12, 13],
       [20, 21, 22, 23]])
>>> b[-1]
array([40, 41, 42, 43])

>>> for row in b: # 行级别的遍历;元素级别的遍历用 b.flat;
    print(row,':')

[0 1 2 3] :
[10 11 12 13] :
[20 21 22 23] :
[30 31 32 33] :
[40 41 42 43] :    

多维度检索

这个功能真心强大,特别是 boolean 数组类型的检索。

>>> a = np.arange(12)**2
>>> i = np.array([1,1,3,8,5])
>>> print(i,a)
[1 1 3 8 5] [  0   1   4   9  16  25  36  49  64  81 100 121]
>>> a[i] # 获取某几个元素
array([ 1,  1,  9, 64, 25])
>>> a = np.arange(12).reshape(3,4)
array([[ 0,  1,  2,  3],
       [ 4,  5,  6,  7],
       [ 8,  9, 10, 11]])
>>> i = np.array([[0,1],[1,2]])
>>> j = np.array([[2,1],[3,3]])
>>> a[i,j] # 第一个元素 :第 0 行 2列,第二个元素:第 1 行 1 列
array([[ 2,  5],
       [ 7, 11]])
>>> a = np.arange(20).reshape(4,5)
array([[ 0,  1,  2,  3,  4],
       [ 5,  6,  7,  8,  9],
       [10, 11, 12, 13, 14],
       [15, 16, 17, 18, 19]])
>>> a[:,[1,2]] #获取矩阵的某几行与几列
array([[ 1,  2],
       [ 6,  7],
       [11, 12],
       [16, 17]])
>>> a[[0,2],:]
array([[ 0,  1,  2,  3,  4],
       [10, 11, 12, 13, 14]])
a[np.ix_([0,2],[2,3])] # 使用 np.ix_ 获取第0行、2行与第2、3列交叉的元素。
array([[ 2,  3],
       [12, 13]])

通过boolean数组检索

>>> a = np.arange(12).reshape(3,4)
array([[ 0,  1,  2,  3],
       [ 4,  5,  6,  7],
       [ 8,  9, 10, 11]])
>>> b = a>4
>>> b
array([[False, False, False, False],
       [False,  True,  True,  True],
       [ True,  True,  True,  True]], dtype=bool)
>>> a[b]
array([ 5,  6,  7,  8,  9, 10, 11])
>>> a[b]=0
>>> a
array([[0, 1, 2, 3],
       [4, 0, 0, 0],
       [0, 0, 0, 0]])
a = np.arange(12).reshape(3,4)
a
array([[ 0,  1,  2,  3],
       [ 4,  5,  6,  7],
       [ 8,  9, 10, 11]])
b1 = np.array([False,True,True])
b2 = np.array([True,False,True,False])
a[b1,:] 
array([[ 4,  5,  6,  7],
       [ 8,  9, 10, 11]])
a[:,b2]
array([[ 0,  2],
       [ 4,  6],
       [ 8, 10]])

变 型

自变

>>> a = np.floor(10*np.random.random((3,4)))
>>> a
array([[ 1.,  5.,  0.,  8.],
       [ 5.,  1.,  4.,  8.],
       [ 2.,  9.,  3.,  6.]])
>>> a.ravel()
array([ 1.,  5.,  0.,  8.,  5.,  1.,  4.,  8.,  2.,  9.,  3.,  6.])
>>> a.reshape(6,2)
array([[ 1.,  5.],
       [ 0.,  8.],
       [ 5.,  1.],
       [ 4.,  8.],
       [ 2.,  9.],
       [ 3.,  6.]])
>>> a.T
array([[ 1.,  5.,  2.],
       [ 5.,  1.,  9.],
       [ 0.,  4.,  3.],
       [ 8.,  8.,  6.]])
>>> a.resize((2,6))
array([[ 1.,  5.,  0.,  8.,  5.,  1.],
       [ 4.,  8.,  2.,  9.,  3.,  6.]])

协变

两个 array 通过 np.vstack,np.hstack 等方式,组合成新的 array 。

>>> a = np.floor(10*np.random.random((2,2)))
array([[ 5.,  3.],
       [ 0.,  4.]])
>>> b = np.floor(10*np.random.random((2,2)))
array([[ 2.,  5.],
       [ 7.,  3.]])
>>> np.vstack((a,b))
array([[ 5.,  3.],
       [ 0.,  4.],
       [ 2.,  5.],
       [ 7.,  3.]])
>>> np.column_stack((a,b))
array([[ 5.,  3.,  2.,  5.],
       [ 0.,  4.,  7.,  3.]])

数组切分

a = np.floor(10*np.random.random((2,12)))
array([[ 3.,  9.,  2.,  5.,  2.,  7.,  3.,  7.,  9.,  1.,  9.,  6.],
       [ 8.,  9.,  1.,  4.,  7.,  1.,  0.,  7.,  4.,  7.,  5.,  4.]])
[array([[ 3.,  9.,  2.,  5.],
        [ 8.,  9.,  1.,  4.]]), array([[ 2.,  7.,  3.,  7.],
        [ 7.,  1.,  0.,  7.]]), array([[ 9.,  1.,  9.,  6.],
        [ 4.,  7.,  5.,  4.]])]

复 制

a = np.arange(12)
b = a #no copy
c = a.view() #shallow copy
d = a.copy() #deep copy

常用的几个分布

>>> np.random.seed(123) # 让结果可再现
>>> r1 = np.random.binomial(n=10,p=0.2,size=10) # 二项分布
array([3, 1, 1, 2, 3, 2, 5, 3, 2, 2])
>>> r2 = np.random.binomial(n=10,p=0.2,size=(2,3))
array([[1, 3, 2],
       [0, 2, 3]])
>>> r3 = np.random.normal(loc=2,scale=3,size=(3,5))#均值2标准差为3的正态分布
array([[ 0.66805412,  0.69694617,  8.61779025,  8.56035827,  5.01216169],
       [ 3.1585592 ,  4.21210573,  6.47219608, -0.80750161,  5.52748713],
       [-1.761642  ,  0.08674549,  4.72131559, -2.2860421 ,  1.57979384]])

 

如您对本文有疑问或者有任何想说的,请 点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网