Numpy

一个科学计算库,内置丰富的矩阵操作


In [3]:
#导入库
import numpy as np
print(np.version.version)


1.11.1

In [4]:
#创建一个矩阵
matA = np.array([[1,2,3],[4,5,6]])   #list和tuple都可以来创建
matA = np.array(((1,2,3),(4,5,6)))
print(matA)
print(matA.dtype)  #类型
print(matA.shape)  #大小


[[1 2 3]
 [4 5 6]]
int32
(2L, 3L)

In [5]:
matA.shape = 3,2  #更改矩阵的长宽
print(matA)
print(" ")

matA.shape = 2,-1  #-1 会让其自动计算长宽
print(matA)


[[1 2]
 [3 4]
 [5 6]]
 
[[1 2 3]
 [4 5 6]]

In [6]:
matB = matA.reshape(3,2) #reshape会更改矩阵长宽,但是两个矩阵共享一个内存
print(matA)              #矩阵较大时,会提高效率
print(" ")

print(matB)
print(" ")

matB[0][0] =20   #由于共享内存,所以更改matB也会更改matA
print(matA)


[[1 2 3]
 [4 5 6]]
 
[[1 2]
 [3 4]
 [5 6]]
 
[[20  2  3]
 [ 4  5  6]]

In [7]:
#另一种创建方式
matA = np.array([1,2,3,4],dtype="int32")
print(matA.dtype)


int32

In [8]:
#python中按照步长创建list
a = [i for i in range(1,10,2)]
print(a)
print(" ")

#numpy中可以通过arange来创建固定步长的矩阵
matA = np.arange(0,10,0.5)
print(matA)
print(" ")

#通过linspace创建固定个数的矩阵
matB = np.linspace(0,10,20)
print(matB)


[1, 3, 5, 7, 9]
 
[ 0.   0.5  1.   1.5  2.   2.5  3.   3.5  4.   4.5  5.   5.5  6.   6.5  7.
  7.5  8.   8.5  9.   9.5]
 
[  0.           0.52631579   1.05263158   1.57894737   2.10526316
   2.63157895   3.15789474   3.68421053   4.21052632   4.73684211
   5.26315789   5.78947368   6.31578947   6.84210526   7.36842105
   7.89473684   8.42105263   8.94736842   9.47368421  10.        ]

In [9]:
#高级特性 fromfunction ,可以根据某个函数来创建矩阵
def func(i,j):        #i指的是下标
    return (i+1)*(j+1)
matA = np.fromfunction(func,(9,9))
print(matA)


[[  1.   2.   3.   4.   5.   6.   7.   8.   9.]
 [  2.   4.   6.   8.  10.  12.  14.  16.  18.]
 [  3.   6.   9.  12.  15.  18.  21.  24.  27.]
 [  4.   8.  12.  16.  20.  24.  28.  32.  36.]
 [  5.  10.  15.  20.  25.  30.  35.  40.  45.]
 [  6.  12.  18.  24.  30.  36.  42.  48.  54.]
 [  7.  14.  21.  28.  35.  42.  49.  56.  63.]
 [  8.  16.  24.  32.  40.  48.  56.  64.  72.]
 [  9.  18.  27.  36.  45.  54.  63.  72.  81.]]

元素的存取


In [13]:
matA = np.arange(1,10)
print(matA)
print(" ")

print(matA[0])
print(" ")

print(matA[1:-1:2]) #从1到-1,按照步长读取
print(" ")

print(matA[9:0:-1]) #反过来读
print(" ")

print(matA[:])  #省略参数,默认从最低到最高,读取所有
print(matA[::])
print(matA[::-1]) #反过来
print(" ")

print(matA[[1,2,3]]) #获取下标为1,2,3的元素


[1 2 3 4 5 6 7 8 9]
 
1
 
[2 4 6 8]
 
[9 8 7 6 5 4 3 2]
 
[1 2 3 4 5 6 7 8 9]
[1 2 3 4 5 6 7 8 9]
[9 8 7 6 5 4 3 2 1]
 
[2 3 4]

In [88]:
matB = matA[1:-1]  #注意matB和matA共享一个内存
print(matB)


[2 3 4 5 6 7 8]

In [14]:
#简单运算
print(matA > 3)   #判断哪些元素>3


[False False False  True  True  True  True  True  True]

In [16]:
print(matA[matA>3]) #将>3的元素全部取出来


[4 5 6 7 8 9]

多维数组


In [23]:
matA = np.array([[1,2,3],[4,5,6]])
print(matA)


[[1 2 3]
 [4 5 6]]

In [31]:
subMatA = np.array([[1],[4]])
print(subMatA)
print(" ")

subMatB = np.array([0,1,2])
print(subMatB)
print(" ")

print(subMatA+subMatB)  #不同纬度矩阵相加


[[1]
 [4]]
 
[0 1 2]
 
[[1 2 3]
 [4 5 6]]

常用操作


In [9]:
import numpy as np
a = np.array([1,2,3,4,5])
b = np.array([2,4,6,8,10])
print(a+b)
print(a*b)
print(a/b)
print(a**b)
print(" ")

print(a>b)
print(a==b)


[ 3  6  9 12 15]
[ 2  8 18 32 50]
[0 0 0 0 0]
[      1      16     729   65536 9765625]
 
[False False False False False]
[False False False False False]

In [7]:
a = np.array([True,False,True,False])
b = np.array([False,True,True,False])
print(a | b)
print(a & b)
print(~a)


[ True  True  True False]
[False False  True False]
[False  True False  True]

In [28]:
#构造特殊矩阵
print(np.zeros((3,3)))
print(" ")

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

print(np.eye((3)))
print(" ")

print(np.zeros((3,3,3))) #三维矩阵
print(" ")


[[ 0.  0.  0.]
 [ 0.  0.  0.]
 [ 0.  0.  0.]]
 
[[ 1.  1.  1.]
 [ 1.  1.  1.]
 [ 1.  1.  1.]]
 
[[ 1.  0.  0.]
 [ 0.  1.  0.]
 [ 0.  0.  1.]]
 
[[[ 0.  0.  0.]
  [ 0.  0.  0.]
  [ 0.  0.  0.]]

 [[ 0.  0.  0.]
  [ 0.  0.  0.]
  [ 0.  0.  0.]]

 [[ 0.  0.  0.]
  [ 0.  0.  0.]
  [ 0.  0.  0.]]]
 

In [32]:
#使用copy进行深拷贝
a = np.array([1,2,3])
b = a.copy()
b[0] = 10
print(a)

print(b is a)


[1 2 3]
False

In [41]:
a = np.array([[1,2],[3,4]])
print(a)
print(" ")
print(a.transpose())
print("")


[[1 2]
 [3 4]]
 
[[1 3]
 [2 4]]


In [ ]: