Python学习(十)——NumPy入门

来源:互联网 发布:java锁机木马源代码 编辑:程序博客网 时间:2024/06/08 18:50

导入模块

导入整个模块:

>>> import numpy>>> a=numpy.array((1,2,3))>>> print a[1 2 3]

导入模块中的指定函数:

>>> from numpy import array>>> b=array((4,5,6))>>> print b[4 5 6]

将numpy模块命名为np:

>>> import numpy as np>>> c=np.array((7,8,9))>>> print c[7 8 9]

==============================

处理数组

1、使用ndarray创建数组

ndarray对象(N-dimensional array object)
Python中的list的每个元素本质是对象,对于数值运算较为浪费。因此numpy提供了ndarray对象来存储单一数据类型的多维数组。

①使用array创建:

通过向array函数传递序列创建多维数组:
传递list、tuple:

import numpy as npa=np.array([4,5,6])b=np.array((1,2,3))print a,b

输出:
[4 5 6] [1 2 3]
传递多层嵌套序列:

a=np.array([[4,5,6],[7,8,9],[0,1,2]])b=np.array(((1,2,3),(4,5,6),(7,8,9)))c=np.array([(1,4,7),(2,5,8),(3,6,9)])d=np.array(([9,8,7],[6,5,4],[3,2,1]))print a,'\n',b,'\n',c,'\n',d

输出:
[[4 5 6]
[7 8 9]
[0 1 2]]
[[1 2 3]
[4 5 6]
[7 8 9]]
[[1 4 7]
[2 5 8]
[3 6 9]]
[[9 8 7]
[6 5 4]
[3 2 1]]

②查看数据类型:

a=np.array([(1,4,7),(2,5,8),(3,6,9)])print type(a)

输出:

③通过shape属性查看数组大小:

a=np.array([(0,1,4,7,10),(-1,2,5,8,11)])b=np.array((0,1,2,3,4,5))print a.shapeprint b.shape

输出:
(2L, 5L)
(6L,)
结果(2L, 5L) 表示2行5列,L表示长整型;

④修改shape

a=np.array([(1,4,7,10),(2,5,8,11),(21,15,8,11)])a.shape=2,6print a

输出:
[[ 1 4 7 10 2 5]
[ 8 11 21 15 8 11]]

行或列置为-1,则系统会根据设置的列或行值自动计算;

a=np.array([(1,4,7,10),(2,5,8,11),(21,15,8,11)])a.shape=4,-1print a

输出:
[[ 1 4 7]
[10 2 5]
[ 8 11 21]
[15 8 11]]

⑤使用reshape,保持原数组的shape不变:

a=np.array([(1,4,7,10),(2,5,8,11),(21,15,8,11)])b=a.reshape((2,-1))print 'a=\n',aprint 'b=\n',b

输出:
a=
[[ 1 4 7 10]
[ 2 5 8 11]
[21 15 8 11]]
b=
[[ 1 4 7 10 2 5]
[ 8 11 21 15 8 11]]

注意:reshape并非新建了一个数据,二者共享内存,任意一个改变都会将数据修改;

a=np.array([(1,4,7,10),(2,5,8,11),(21,15,8,11)])b=a.reshape((2,-1))b[0][0]=100print 'a=\n',aprint 'b=\n',b

输出:
a=
[[100 4 7 10]
[ 2 5 8 11]
[ 21 15 8 11]]
b=
[[100 4 7 10 2 5]
[ 8 11 21 15 8 11]]

⑥通过dtype属性获取数组元素的类型或指定元素类型:

1)获取元素类型:

a=np.array([(1,4,7,10),(2,5,8,11),(21,15,8,11)])print a.dtype

输出:
int32
即32位的整型
2)指定元素类型:

a=np.array([(1,4,7,10),(2,5,8,11),(21,15,8,11)],dtype=np.float)print a.dtypeprint a

输出:
float64
[[ 1. 4. 7. 10.]
[ 2. 5. 8. 11.]
[ 21. 15. 8. 11.]]

⑦通过astype改变元素类型:

a=np.array([(1,4,7,10),(2,5,8,11),(21,15,8,11)],dtype=np.float)b=a.astype(np.complex)print bprint b.dtype

输出:
[[ 1.+0.j 4.+0.j 7.+0.j 10.+0.j]
[ 2.+0.j 5.+0.j 8.+0.j 11.+0.j]
[ 21.+0.j 15.+0.j 8.+0.j 11.+0.j]]
complex128

2、使用函数创建数组

①arange函数左闭右开区间+步长:

a=np.arange(1,5,0.2)print a

输出:
[ 1. 1.2 1.4 1.6 1.8 2. 2.2 2.4 2.6 2.8 3. 3.2 3.4 3.6 3.8
4. 4.2 4.4 4.6 4.8]
arrange函数前两个参数为输出数列的区间[1,5),为一个左闭右开区间;第三个参数为步长。

②linspace函数给定起始值、终止值+元素个数:

a=np.linspace(1,5,5)print a

输出:
[ 1. 2. 3. 4. 5.]
linspace函数通过给定起始值、终止值和元素个数来创建数组。
也可以通过endpoint关键字指定是否包含终止值;

a=np.linspace(1,5,5,endpoint=False)print a

输出:
[ 1. 1.8 2.6 3.4 4.2]

③logspace函数创建等比数列:

a=np.logspace(1,3,10)print a

输出:
[ 10. 16.68100537 27.82559402 46.41588834 77.42636827
129.1549665 215.443469 359.38136638 599.48425032 1000. ]
即,起始值为10^1,终止值为10^3,元素个数为10的等比数列。

④使用formbuffer、formstring、formfile等函数将字符序列转为数组:

s='ABCDabcd'a=np.fromstring(s,dtype=np.int8)print a

输出:
[ 65 66 67 68 97 98 99 100]

3、对数组中缺失和无穷的处理

缺失:np.nan
无穷:np.inf
np.nan_to_num():使用0代替数组x中的nan元素,使用有限的数字代替inf元素

x = np.array([-12, np.inf, np.nan, -np.inf, np.nan, 91])# 使用0代替数组x中的nan元素,使用有限的数字代替inf元素y = np.nan_to_num(x)print xprint y

输出:

[-12.  inf  nan -inf  nan  91.][ -1.20000000e+001   1.79769313e+308   0.00000000e+000  -1.79769313e+308   0.00000000e+000   9.10000000e+001]

==============================

处理矩阵

tile(A,reps) 通过重复A构建一个数组,由reps给出的次数。

import numpy as npm0= np.tile([10, 50], (3, 2))print m0

输出:

[[10 50 10 50] [10 50 10 50] [10 50 10 50]]

注意:求矩阵行列式和矩阵的逆时必须为n*n矩阵

np.prod()返回数组元素在给定轴上的乘积

默认为全部元素乘积:

import numpy as npx = [[1, 2, 3], [6, 5, 4],[1, 4, 7]]y = np.prod(x)print yprint 1*2*3*6*5*4*1*4*7

输出

2016020160

沿列方向(axis=0)/沿行方向(axis=1):

import numpy as npx = [[1, 2, 3], [6, 5, 4],[1, 4, 7]]y = np.prod(x,axis=0)print y

输出:

[ 6 40 84]

np.linalg.det():求矩阵行列式的值(标量)

import numpy as npm1 = [[1, 5, 9],      [3, 7, 7],      [3, 9, 1]]print np.linalg.det(m1)

输出:

88.0

np.linalg.inv():求矩阵的逆

import numpy as npm1 = [[1, 5, 9],      [3, 7, 7],      [3, 9, 1]]print np.linalg.inv(m1)

输出:

[[-0.63636364  0.86363636 -0.31818182] [ 0.20454545 -0.29545455  0.22727273] [ 0.06818182  0.06818182 -0.09090909]]

np.linalg.norm()求解向量及矩阵的范数

向量:

import numpy as npm1 = [1, 2, 3, 1]# ℓ1范数print np.linalg.norm(m1, 1)# ℓ2范数print np.linalg.norm(m1, 2)# 默认为ℓ2范数print np.linalg.norm(m1)# ℓ∞范数print np.linalg.norm(m1, np.inf)

输出:

7.03.872983346213.872983346213.0

矩阵:

import numpy as npm1 = [[1, 9, 2],      [3, 7, 7],      [3, 8, 1]]# ℓ1范数print np.linalg.norm(m1, 1)# ℓ2范数 print np.linalg.norm(m1, 2)# 默认为ℓ2范数print np.linalg.norm(m1)# ℓ∞范数print np.linalg.norm(m1, np.inf)

输出:

24.015.576622847716.340134638417.0

注:
矩阵的ℓ1范数,又名列和范数,即矩阵列向量中绝对值之和的最大值。
这里写图片描述
矩阵的ℓ2范数,又名谱范数,计算方法为(A转置)A`A矩阵的最大特征值的开平方。(其中λ1为A`A的最大特征值。)
这里写图片描述
矩阵的ℓ∞范数,又名行和范数。即矩阵行向量中绝对值之和的最大值。
这里写图片描述


这里写图片描述

原创粉丝点击