【Numerical Python Chapter2】Numpy 学习记录

import numpy as np
np.ndarray #NumPy最重要的一个特点是其N维数组对象ndarray,它是一系列同类型数据的集合,以0下标为开始进行集合中元素的索引
numpy.ndarray
data = np.array([[1,2],[3,4],[5,6]])
data
array([[1, 2],
       [3, 4],
       [5, 6]])

查看数组的类型或者属性(attribute)

type(data)
numpy.ndarray
data.ndim
2
data.shape
(3, 2)
data.size #Size:The total number elements in the array
6
data.dtype #The data type of the elements in the array
dtype('int32')
data.nbytes #Number of bytes used to store the data
24

使用dtype属性创建数组

np.array([1,2,3], dtype = np.int)
array([1, 2, 3])
np.array([1,2,3], dtype = np.float)
array([1., 2., 3.])
np.array([1,2,3], dtype = np.complex) #复数
array([1.+0.j, 2.+0.j, 3.+0.j])

数组一旦创建,dtype不能改,需要创建新的类型来更改(dtype/astype)

data = np.array([1,2,3], dtype = np.float)
data
array([1., 2., 3.])
data.dtype
dtype('float64')
data = np.array(data, dtype = np.int)
data.dtype
dtype('int32')
data
array([1, 2, 3])
#使用astype更改类型
data2 = np.array([1,2,3], dtype = np.float)
data2
array([1., 2., 3.])
data2.astype(np.int)
array([1, 2, 3])

complex + float = complex

d1 = np.array([1,2,3], dtype = np.complex)
d2 = np.array([1,2,3], dtype = np.float)
d1 + d2
array([2.+0.j, 4.+0.j, 6.+0.j])
(d1 + d2).dtype
dtype('complex128')

创建数组最好定好类型,否则进行某些运算回报错

np.sqrt(np.array([-1,0,1])) #默认的dtype是float
<ipython-input-22-cb1a04097a8c>:1: RuntimeWarning: invalid value encountered in sqrt
  np.sqrt(np.array([-1,0,1])) #默认的dtype是float





array([nan,  0.,  1.])
np.sqrt(np.array([-1,0,1], dtype = complex))
array([0.+1.j, 0.+0.j, 1.+0.j])

有关实部和虚部

data = np.array([1,2,3], dtype = complex)
data
array([1.+0.j, 2.+0.j, 3.+0.j])
data.real
array([1., 2., 3.])
data.imag
array([0., 0., 0.])

创建数组

data = np.array([1,2,3,4]) #创建一维数组
data
array([1, 2, 3, 4])
data.ndim
1
data.shape
(4,)
data = np.array([[1,2],[3,4]]) #创建二维数组
data
array([[1, 2],
       [3, 4]])
data.ndim
2
data.shape
(2, 2)
data = np.zeros((2,3))
data
array([[0., 0., 0.],
       [0., 0., 0.]])
data = np.ones(4)
data
array([1., 1., 1., 1.])
data.dtype #默认是float型的
dtype('float64')
data = np.ones(4, dtype=np.int) #转变成int型的
data
array([1, 1, 1, 1])
data.dtype
dtype('int32')
  • 填充方式(fill和full)
x1 = 5.4 * np.ones((5,5))
x1
array([[5.4, 5.4, 5.4, 5.4, 5.4],
       [5.4, 5.4, 5.4, 5.4, 5.4],
       [5.4, 5.4, 5.4, 5.4, 5.4],
       [5.4, 5.4, 5.4, 5.4, 5.4],
       [5.4, 5.4, 5.4, 5.4, 5.4]])
x1 = 5.4 * np.ones(10)
x1
array([5.4, 5.4, 5.4, 5.4, 5.4, 5.4, 5.4, 5.4, 5.4, 5.4])
x2 = np.full(10, 5.4) #x1和x2结果一样,方式不一样
x2
array([5.4, 5.4, 5.4, 5.4, 5.4, 5.4, 5.4, 5.4, 5.4, 5.4])
x3 = np.empty(5)
x3
array([5.4, 5.4, 5.4, 5.4, 5.4])
x3.fill(6.0)
x3
array([6., 6., 6., 6., 6.])
x4 = np.full(5,6.0) #和fill的结果一样
x4
array([6., 6., 6., 6., 6.])

递增序列的数组(arange/linsapce)

np.arange(0.0, 10, 1) #1是间隔
array([0., 1., 2., 3., 4., 5., 6., 7., 8., 9.])
np.linspace(0, 10, 11) #11是总数
array([ 0.,  1.,  2.,  3.,  4.,  5.,  6.,  7.,  8.,  9., 10.])

对数序列

  • np.logspace(start=开始值,stop=结束值,num=元素个数,base=指定对数的底, endpoint=是否包含结束值)
np.logspace(0, 2, 5) #要得到5个数,是由0-2的平均间隔分出来的,0,0.5,1,1.5,2即10^x是结果,如10^0.5=3.16
array([  1.        ,   3.16227766,  10.        ,  31.6227766 ,
       100.        ])

二维坐标矩阵

import numpy as np
x = np.array([-1,0,1])
y = np.array([-2,0,2])
X, Y = np.meshgrid(x, y) #坐标相结合了然后把所有坐标拆成了X和Y,X和Y每个元素都可以凑成坐标
X
array([[-1,  0,  1],
       [-1,  0,  1],
       [-1,  0,  1]])
Y
array([[-2, -2, -2],
       [ 0,  0,  0],
       [ 2,  2,  2]])
X,Y
(array([[-1,  0,  1],
        [-1,  0,  1],
        [-1,  0,  1]]),
 array([[-2, -2, -2],
        [ 0,  0,  0],
        [ 2,  2,  2]]))
Z = (X+Y)**2
Z
array([[9, 4, 1],
       [1, 0, 1],
       [1, 4, 9]], dtype=int32)

创建uninitialized数据

np.empty(3, dtype = np.float) #随机生成
array([1.60216183e-306, 7.56602523e-307, 1.11258446e-306])

继承数组元素 Properties of other arrays

def f(x):
    y = np.ones_like(x)
    #compute with x andy
    return y
f(x) #原x是[-1,0,1],保留size和类型,用1填充
array([1, 1, 1])

创建对角线相关矩阵

np.identity(4) #用1填充对角线diagonal
array([[1., 0., 0., 0.],
       [0., 1., 0., 0.],
       [0., 0., 1., 0.],
       [0., 0., 0., 1.]])
np.eye(3, k = 1) #对角线网上移了
array([[0., 1., 0.],
       [0., 0., 1.],
       [0., 0., 0.]])
np.eye(3, k = -1) #对角线往下移了
array([[0., 0., 0.],
       [1., 0., 0.],
       [0., 1., 0.]])
np.diag(np.arange(0, 20, 5)) #对角线是0-20以5为步长创建的,20取不到,所以到15
array([[ 0,  0,  0,  0],
       [ 0,  5,  0,  0],
       [ 0,  0, 10,  0],
       [ 0,  0,  0, 15]])

索引和切片 index and slices

a = np.arange(0,11)
a
array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10])
a[0]
0
a[-1]
10
a[4]
4
a[1:-1]
array([1, 2, 3, 4, 5, 6, 7, 8, 9])
a[1:-1:2]
array([1, 3, 5, 7, 9])
a[:5]
array([0, 1, 2, 3, 4])
a[-5:]
array([ 6,  7,  8,  9, 10])
a[::-2] #逆
array([10,  8,  6,  4,  2,  0])

多维数组

  • 用到了lambda函数:lambda作为一个表达式,定义了一个匿名函数
f = lambda m,n: n+10*m
A = np.fromfunction(f, (6,6), dtype = int)
A
array([[ 0,  1,  2,  3,  4,  5],
       [10, 11, 12, 13, 14, 15],
       [20, 21, 22, 23, 24, 25],
       [30, 31, 32, 33, 34, 35],
       [40, 41, 42, 43, 44, 45],
       [50, 51, 52, 53, 54, 55]])
A[:,1] #第二列
array([ 1, 11, 21, 31, 41, 51])
A[1,:] #第二行
array([10, 11, 12, 13, 14, 15])
A[:3, :3] #取对角线上面的3*3
array([[ 0,  1,  2],
       [10, 11, 12],
       [20, 21, 22]])
A[3:,:3] #lower left off-diagonal block matrix 看结果是取下面的3*3
array([[30, 31, 32],
       [40, 41, 42],
       [50, 51, 52]])
A[::2, ::2] #从(0,0)开始取
array([[ 0,  2,  4],
       [20, 22, 24],
       [40, 42, 44]])
A[1::2, 1::3] #从(1,1)开始取
array([[11, 14],
       [31, 34],
       [51, 54]])

View的方式,如果没有.copy,原来的数据是会被更改的

B = A[1:5,1:5]
B
array([[11, 12, 13, 14],
       [21, 22, 23, 24],
       [31, 32, 33, 34],
       [41, 42, 43, 44]])
B[:,:] = 0
B
array([[0, 0, 0, 0],
       [0, 0, 0, 0],
       [0, 0, 0, 0],
       [0, 0, 0, 0]])
A #这里原本B取出来的位置都变成0了:When elements in a view are assigned new values, the values of the original also updated
array([[ 0,  1,  2,  3,  4,  5],
       [10,  0,  0,  0,  0, 15],
       [20,  0,  0,  0,  0, 25],
       [30,  0,  0,  0,  0, 35],
       [40,  0,  0,  0,  0, 45],
       [50, 51, 52, 53, 54, 55]])
C = B[1:3, 1:3].copy() #np.copy或copy=True
C
array([[0, 0],
       [0, 0]])
C[:,:] = 1
C
array([[1, 1],
       [1, 1]])
B #B没有影响,因为C是.copy过去的
array([[0, 0, 0, 0],
       [0, 0, 0, 0],
       [0, 0, 0, 0],
       [0, 0, 0, 0]])

有关索引 Fancy indexing and Boolean-Valued Indexing

A = np.linspace(0,1,11)
A
array([0. , 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1. ])
A[np.array([0,2,4])] #通过np的方式取
array([0. , 0.2, 0.4])
A[[0,2,4]] #通过pythonlist的方式取,是一样的结果
array([0. , 0.2, 0.4])
A > 0.5
array([False, False, False, False, False, False,  True,  True,  True,
        True,  True])
A[A>0.5]
array([0.6, 0.7, 0.8, 0.9, 1. ])
A = np.arange(10)
A
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
indices = [2,4,6]
B = A[indices]
B
array([2, 4, 6])
B[0] = -1
B
array([-1,  4,  6])
A #A没有变
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
A[indices] = -1 #这个变了
A
array([ 0,  1, -1,  3, -1,  5, -1,  7,  8,  9])
A = np.arange(10)
A
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
B = A[ A > 5 ]
B
array([6, 7, 8, 9])
B[0] = -1
B
array([-1,  7,  8,  9])
A #A没有受到B[0]=-1的影响
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
A[A > 5] = -1 #这种方式改变了A
A
array([ 0,  1,  2,  3,  4,  5, -1, -1, -1, -1])
f = lambda m,n: n+10*m
test = np.fromfunction(f, (6,6), dtype = int)
test
array([[ 0,  1,  2,  3,  4,  5],
       [10, 11, 12, 13, 14, 15],
       [20, 21, 22, 23, 24, 25],
       [30, 31, 32, 33, 34, 35],
       [40, 41, 42, 43, 44, 45],
       [50, 51, 52, 53, 54, 55]])

这种选取表达的方式特别注意,是拼凑的类似x和y的组合!!!!

test[[1,3],[0,3]] #拼凑的,(1,0)和(3,3)的位置
array([10, 33])
import numpy as np

重新定义形状和大小 Reshaping and Resizing

data = np.array([[1,2],[3,4]])
data
array([[1, 2],
       [3, 4]])
np.reshape(data, (1,4))
array([[1, 2, 3, 4]])
data
array([[1, 2],
       [3, 4]])
data.reshape(4)
array([1, 2, 3, 4])
data
array([[1, 2],
       [3, 4]])
data = np.array([[1,2],[3,4]])
data
array([[1, 2],
       [3, 4]])
data.flatten() #flatten是降维的,默认是按行降维
array([1, 2, 3, 4])
data.flatten().shape
(4,)
data.flatten("F") #按列降维
array([1, 3, 2, 4])
data = np.arange(0,5)
data
array([0, 1, 2, 3, 4])
column = data[:, np.newaxis]
column
array([[0],
       [1],
       [2],
       [3],
       [4]])
row = data[np.newaxis, :]
row
array([[0, 1, 2, 3, 4]])
data = np.arange(5)
data
array([0, 1, 2, 3, 4])
np.vstack((data,data,data)) #竖着复制,vertical垂直
array([[0, 1, 2, 3, 4],
       [0, 1, 2, 3, 4],
       [0, 1, 2, 3, 4]])
np.hstack((data,data,data)) #横着复制,horizontal水平
array([0, 1, 2, 3, 4, 0, 1, 2, 3, 4, 0, 1, 2, 3, 4])
data
array([0, 1, 2, 3, 4])
data = data[:, np.newaxis]
data
array([[0],
       [1],
       [2],
       [3],
       [4]])
np.vstack((data,data,data))
array([[0],
       [1],
       [2],
       [3],
       [4],
       [0],
       [1],
       [2],
       [3],
       [4],
       [0],
       [1],
       [2],
       [3],
       [4]])
np.hstack((data,data,data))
array([[0, 0, 0],
       [1, 1, 1],
       [2, 2, 2],
       [3, 3, 3],
       [4, 4, 4]])

四则运算

x = np.array([[1,2],[3,4]])
y = np.array([[5,6],[7,8]])
x + y
array([[ 6,  8],
       [10, 12]])
y - x
array([[4, 4],
       [4, 4]])
x * y
array([[ 5, 12],
       [21, 32]])
y / x
array([[5.        , 3.        ],
       [2.33333333, 2.        ]])
x
array([[1, 2],
       [3, 4]])
x*2
array([[2, 4],
       [6, 8]])
2**x #2^4
array([[ 2,  4],
       [ 8, 16]], dtype=int32)
y
array([[5, 6],
       [7, 8]])
y / 2
array([[2.5, 3. ],
       [3.5, 4. ]])
(y/2).dtype
dtype('float64')
x = np.array([1,2,3,4]).reshape(2,2)
x
array([[1, 2],
       [3, 4]])
z = np.array([1,2,3,4])
z
array([1, 2, 3, 4])
x / z
---------------------------------------------------------------------------

ValueError                                Traceback (most recent call last)

<ipython-input-38-c325a0617380> in <module>
----> 1 x / z


ValueError: operands could not be broadcast together with shapes (2,2) (4,) 
z = np.array([[2,4]])
z
array([[2, 4]])
z.shape
(1, 2)
x
array([[1, 2],
       [3, 4]])
z
array([[2, 4]])
x / z
array([[0.5, 0.5],
       [1.5, 1. ]])
zz = np.concatenate([z,z], axis=0)
zz
array([[2, 4],
       [2, 4]])
x / zz
array([[0.5, 0.5],
       [1.5, 1. ]])
z = np.array([[2],[4]])
z
array([[2],
       [4]])
z.shape
(2, 1)
x
array([[1, 2],
       [3, 4]])
z
array([[2],
       [4]])
x / z
array([[0.5 , 1.  ],
       [0.75, 1.  ]])
zz = np.concatenate([z,z],axis=1) #注意axis=0和axis=1的区别
zz
array([[2, 2],
       [4, 4]])
x
array([[1, 2],
       [3, 4]])
zz
array([[2, 2],
       [4, 4]])
x / zz
array([[0.5 , 1.  ],
       [0.75, 1.  ]])

元素

  • 三角函数
x = np.linspace(-1,1,11)
x
array([-1. , -0.8, -0.6, -0.4, -0.2,  0. ,  0.2,  0.4,  0.6,  0.8,  1. ])
y = np.sin(np.pi*x) #pi是Π
y
array([-1.22464680e-16, -5.87785252e-01, -9.51056516e-01, -9.51056516e-01,
       -5.87785252e-01,  0.00000000e+00,  5.87785252e-01,  9.51056516e-01,
        9.51056516e-01,  5.87785252e-01,  1.22464680e-16])
np.round(y, decimals=4) #保留4位小数
array([-0.    , -0.5878, -0.9511, -0.9511, -0.5878,  0.    ,  0.5878,
        0.9511,  0.9511,  0.5878,  0.    ])
np.add(np.sin(x)**2, np.cos(x)**2)
array([1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.])
np.sin(x)**2 + np.cos(x)**2
array([1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.])
def heaviside(x):
    return 1 if x>0 else 0

heaviside(1)
1
heaviside(-3)
0
x = np.linspace(-5,5,11)
x
array([-5., -4., -3., -2., -1.,  0.,  1.,  2.,  3.,  4.,  5.])
heaviside(x)
---------------------------------------------------------------------------

ValueError                                Traceback (most recent call last)

<ipython-input-68-52f34380192d> in <module>
----> 1 heaviside(x)


<ipython-input-63-18287190c1c3> in heaviside(x)
      1 def heaviside(x):
----> 2     return 1 if x>0 else 0
      3 
      4 heaviside(1)


ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
heaviside = np.vectorize(heaviside) #vectorize向量化
heaviside(x)
array([0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1])
def heaviside2(x):
    return 1.0*(x>0)
heaviside(1)
1.0
heaviside2(-2)
0.0

总数、计数相关

  • 计算平均数、总和什么的
data = np.random.normal(size = (15,15))
data
array([[ 1.43088824,  1.51571217,  0.52670946, -0.34542564, -0.70126183,
        -2.44378873, -1.29813206, -2.3268224 , -0.3489461 , -0.10176507,
        -1.02750134,  0.12531294, -0.14099252,  0.08134219,  0.88637806],
       [-0.41358198,  0.92949256, -0.08587294, -0.04605617, -0.39924993,
         0.91131498, -0.73653667,  0.5719167 ,  0.00673892, -0.20842546,
         0.51381317, -0.51923504, -0.22598366, -0.87425031,  0.83990335],
       [-0.31197064,  1.70337218,  1.17859752,  0.16950692, -0.43801858,
         0.12130103, -0.45201703,  0.92633083,  0.81167772,  1.2559648 ,
        -0.50515668,  0.20165478, -1.35453058, -0.26521462, -0.04986282],
       [ 1.85145516, -0.50104167, -0.21086733,  0.38125702, -0.95351227,
         0.3993938 , -0.31370351,  0.28553525,  0.41040412,  0.53012437,
         1.45119986, -0.88248177, -1.65451565,  2.21608242,  1.45174845],
       [-0.29868976,  0.13266081,  0.01634686,  1.19433289, -0.56030104,
        -2.25789596, -0.88604635,  1.81565004,  0.9948457 ,  0.78040653,
        -0.55449696,  2.21439536, -0.1922111 , -1.21617158,  0.1242001 ],
       [-0.54270972,  2.8227005 , -0.50773086, -0.35975485,  0.51540293,
         1.6404092 ,  1.45496711,  1.38004685,  0.09703503, -1.19577838,
        -1.58929066, -0.21048832, -0.6686364 , -0.99911507,  0.70284707],
       [-1.7802127 ,  0.53532621, -0.35080266,  0.11656855,  1.27822765,
         2.15402511, -1.04767521,  1.6377075 ,  0.35579167, -0.08570567,
         0.82103488, -1.47850509,  0.42363549,  0.8484873 , -0.84299665],
       [-1.13511667,  0.35115767, -0.01880783, -0.26642697, -0.22279734,
         0.60294904, -1.26441502,  0.58218214, -0.91016664,  0.30271717,
         1.22931484,  1.93467292, -0.40756753, -0.93974604, -1.13876845],
       [ 1.43018867,  0.25137748, -0.1505559 , -0.06403885,  2.03592237,
         0.80592101,  0.76424118,  0.44606865,  1.37564883, -0.81287533,
         1.23606923, -0.07951118,  2.38930812,  0.18566578, -0.53663317],
       [ 0.79352961, -0.68058074,  0.32909654,  0.15934725,  0.39151372,
        -0.76112958, -0.41815944, -0.9140549 ,  0.44247588,  1.5730282 ,
        -2.32208712,  1.42190903,  0.28452742,  1.20259892,  0.61270399],
       [ 0.58295314, -1.10537815,  0.55311059,  0.78622582, -2.06645838,
         0.37049451, -0.72856425,  0.41802866, -0.84295505, -1.07550047,
         1.42117043, -1.78700804, -1.03699317, -0.62054461, -0.79101834],
       [ 0.54606453, -0.7621674 ,  0.94237074,  0.52025093,  1.35263237,
        -1.26665431,  0.62765047, -0.59331758,  0.74984684,  2.1505826 ,
        -0.1663075 ,  0.32906921,  0.66200668,  0.11458963, -0.59034852],
       [ 1.72662814,  0.46115251,  1.47657386,  0.97308989, -0.23692138,
         1.12640401,  0.95507564,  0.19051837,  1.3152752 ,  1.33832113,
        -1.28577824,  0.61863811,  1.47829621, -0.40574491, -0.01757032],
       [ 0.75173132,  1.4606146 ,  0.47053013, -0.15103482,  0.04669727,
         0.78976392,  0.25421869, -0.5406318 , -0.4084321 , -1.69723171,
         1.74664376, -0.45450465, -0.77912579, -0.07081363,  0.37353395],
       [-1.32137248,  1.36221455,  0.94742106,  1.23242303,  0.2296807 ,
        -2.32770769, -1.22861506, -0.8557108 ,  0.46248013, -1.55486723,
         1.03994015,  0.2723901 ,  0.38106312, -0.96783779, -0.81415071]])
np.mean(data) #两种表达方式——1
0.12056907151327091
np.mean(data) #——2
0.12056907151327091
data = np.random.normal(size=(5,10,15)) #生成了5大组,每个10个,10个里头都有15个小的
data
array([[[-2.03752167e-01,  1.02990834e+00,  5.91076632e-01,
          1.02097361e+00, -1.58069668e+00, -4.18192491e-01,
         -4.53517704e-01, -1.60338867e+00, -6.01524011e-01,
          8.55374292e-01,  1.29454699e-01,  1.07590798e+00,
          1.36385377e+00,  8.92191158e-01,  9.10894617e-01],
        [-5.69404598e-01,  3.76979238e-01, -1.42092155e-01,
         -1.64801886e+00, -9.50822005e-01,  6.72120386e-01,
          1.12010239e+00, -4.95189886e-02,  1.36743077e+00,
          1.75976718e+00,  1.48164054e-01,  5.82553066e-01,
         -2.35209603e-01, -5.43143889e-01, -1.54722282e-01],
        [ 1.95164513e-01, -1.67376126e+00, -1.20394233e+00,
         -5.77266886e-01,  7.57584456e-01, -1.02130559e+00,
          9.88075333e-01, -5.14669828e-01,  1.01435182e+00,
          8.16007318e-01,  8.32766264e-01, -5.57924408e-01,
          1.90636735e+00,  1.69693627e-01,  3.93410070e-01],
        [ 2.05162221e-01, -9.13583232e-01, -1.63466829e+00,
          1.36522807e+00, -4.97785592e-01, -2.95227855e-01,
         -9.67320912e-01,  1.79588109e+00, -1.50214549e-02,
          1.49032203e-01, -9.73834070e-01,  1.08607817e+00,
         -3.85623953e-01, -6.79823056e-02,  1.96302633e-01],
        [-4.48452279e-01,  7.47780896e-02, -9.75784265e-02,
          7.33343259e-01, -1.71248573e-01, -1.29970153e+00,
          5.60937605e-02,  2.91320916e-01,  1.37629873e+00,
          7.90311477e-01,  7.47920944e-01, -2.17761247e-02,
          1.57669553e+00, -6.97776708e-01, -1.27361882e+00],
        [-1.57695654e-01, -2.06137448e+00,  1.17312312e+00,
          2.35461377e-01, -8.62987063e-01,  9.23744473e-02,
         -3.32917742e-01, -2.37176724e-01, -5.51398609e-01,
         -1.65991903e+00, -1.24328619e-01,  2.79000092e-01,
         -3.12425631e-01,  5.38427009e-01,  2.11952118e-01],
        [-1.63159211e+00, -1.87250961e+00,  1.65747914e+00,
          2.92232688e-01, -8.30430496e-01,  1.27131682e-01,
         -2.02863614e-01, -1.57664549e+00,  2.20506384e+00,
          1.04898958e+00, -1.36036834e+00,  6.45240807e-01,
          6.92681953e-01, -8.62162987e-01, -3.47837506e-01],
        [-3.41220230e-01, -5.51886865e-01, -2.87481272e-01,
         -1.32163171e+00,  1.12892723e-01, -1.04314769e-01,
          5.12955673e-02,  8.56160884e-01,  4.69510903e-01,
         -1.29325490e-01,  6.50571366e-02, -2.96871459e-01,
         -5.20624613e-01,  5.63443190e-01,  8.37605530e-01],
        [-8.76229348e-01,  2.09392100e+00, -8.32099648e-01,
          1.28780628e+00,  8.16401845e-01,  1.00856154e-01,
         -4.61605475e-01,  6.16367868e-02,  1.40591452e+00,
         -4.77268940e-01, -3.87433399e+00,  1.00671287e+00,
          1.40161599e+00, -6.75798873e-01, -8.63155837e-01],
        [-1.54240016e+00, -1.12922338e+00, -2.26551550e-02,
         -4.18741661e-01,  1.57998253e+00,  7.11344932e-01,
         -1.47545614e-01,  5.60005526e-01, -1.31274292e-02,
         -1.28915536e+00,  3.83972080e-01,  6.74057165e-01,
         -6.35670392e-01,  5.52154762e-01, -7.16394128e-01]],

       [[ 5.18199863e-01,  2.04789232e-01,  6.82808370e-02,
          3.06075466e-01,  3.80550747e-01,  1.35975637e-02,
          1.87978965e-01, -2.50476838e-01,  5.33238754e-01,
          1.65903708e+00, -1.03250168e+00,  1.07625632e+00,
          1.98170272e+00,  3.44930368e-01, -1.24733576e-01],
        [ 2.75277160e-01, -7.48504675e-01, -4.88867331e-01,
          1.29370572e+00, -1.63337699e+00,  4.03749960e-01,
          7.46834003e-01,  3.57697370e-01,  2.92383970e-01,
          7.90626483e-01, -1.26256605e+00,  1.27575404e+00,
         -1.68884718e+00, -3.23169893e-01,  1.04466192e+00],
        [ 7.29547790e-02,  6.42300790e-01,  3.43654716e-01,
          1.12808954e+00,  6.21352343e-01, -7.36769048e-01,
          3.54094703e-01, -1.64739735e+00, -2.78590013e-01,
         -3.63120460e-01,  3.50187082e-02,  1.12901230e+00,
         -4.48671517e-01, -1.21148609e+00,  9.75004226e-02],
        [-1.16648443e+00, -5.10096402e-01, -5.71936141e-01,
         -8.03871365e-01,  5.57373429e-02, -2.01484055e+00,
          1.19262340e+00,  7.61406600e-02, -7.28609466e-01,
          5.74598081e-01, -8.36215401e-01, -1.29706820e+00,
         -7.24527268e-01, -9.78023232e-01,  4.71194583e-01],
        [-1.35910487e+00, -7.83230213e-01, -9.03166263e-01,
          2.32508802e-01, -4.57183837e-01,  1.86005710e+00,
         -1.64069849e+00,  2.88876245e+00,  6.67689146e-01,
          8.74763416e-01,  3.65348314e-01,  9.83777460e-01,
         -1.71428880e-01,  6.88680954e-01, -7.99606260e-01],
        [-6.71964353e-01, -1.42623360e+00, -5.95376128e-01,
         -8.14518301e-01,  9.22502098e-01,  5.38587013e-01,
         -1.35164512e+00,  8.64508875e-01,  9.45070885e-01,
         -7.17692311e-01,  8.09220491e-01,  1.35939349e+00,
         -7.92265364e-01,  1.10446419e+00,  5.28795999e-01],
        [-1.47423980e-01, -5.33674888e-02,  7.25190051e-01,
         -5.97037749e-01, -1.13404270e+00, -1.05206574e+00,
         -8.59286268e-01,  9.96003767e-01, -7.10875967e-01,
          1.91286893e-01,  1.30027082e+00,  4.49013754e-01,
          8.18833601e-01,  7.62459213e-01, -1.57816376e-01],
        [ 3.56650298e-01,  6.11661629e-02,  1.94488347e-02,
          6.24538624e-01,  2.00248819e+00,  5.43110888e-01,
         -5.90671530e-01, -1.41962158e-01,  6.95455107e-01,
         -1.33024612e+00,  7.12341524e-01, -2.22271568e-04,
          5.01291997e-01, -5.97304610e-01, -3.20040967e-01],
        [ 2.98683052e-01, -3.02577713e-01,  1.22047788e+00,
          6.49139920e-01,  5.57844342e-01,  3.65738694e-01,
         -4.75169279e-01, -4.72643838e-01,  8.53936069e-01,
         -3.71063892e-01,  2.74086078e-01, -1.50191063e-01,
          8.97481562e-02, -6.99461825e-01, -1.35716887e-01],
        [ 2.29409793e+00,  7.25641929e-01, -5.70019991e-02,
          4.15911802e-01,  9.58314016e-01, -7.20974383e-01,
          7.24643715e-01, -9.70502107e-01,  7.08032364e-01,
         -1.12841650e+00,  1.35795024e-01, -6.56466074e-01,
         -5.59891094e-01,  1.11788415e+00, -3.47134922e-01]],

       [[ 5.48781756e-01, -7.98238540e-02, -1.11820354e+00,
          9.30350250e-01,  3.68039863e-01, -9.12664104e-01,
         -1.60585993e+00,  1.77362668e+00,  1.51896062e+00,
          1.01531938e+00,  2.39970817e-01, -1.20474537e+00,
         -6.94705232e-01, -6.02326211e-01,  6.43782713e-01],
        [-1.05339294e+00,  8.08131936e-02,  1.15949415e+00,
          1.39561772e-01, -4.24451836e-01,  8.98982077e-01,
          4.81602011e-01, -7.51282729e-01, -5.44307138e-01,
          1.58702165e-01, -4.31839542e-01, -1.77956222e+00,
         -2.37593155e+00,  7.00177374e-01,  7.37279742e-01],
        [-3.51258143e-01, -1.83083068e+00,  2.17939636e-01,
          1.35868973e+00, -5.98527526e-01, -1.47197089e+00,
          5.60722213e-01, -2.50933589e-01,  5.93412652e-01,
         -7.84884402e-03, -1.22800586e+00, -1.00006020e-02,
          6.48541478e-01, -6.70443900e-01,  1.08720005e+00],
        [-2.54841706e+00, -4.34239283e-01,  1.07807614e+00,
         -1.33738333e+00,  4.97521655e-01, -1.62661994e+00,
          6.65604502e-01,  2.90739872e-01, -4.70111242e-01,
          2.02078195e-01,  5.83895501e-01, -7.87459708e-01,
          1.18010936e+00, -2.67717082e-01, -2.90193735e-01],
        [ 6.35066730e-01, -1.54882723e-01,  4.19105667e-01,
          9.92844585e-01,  5.87016405e-01,  2.31737073e-01,
          3.83056523e-02,  1.14421559e+00, -8.31172878e-01,
         -8.51049603e-01, -5.17011956e-01,  1.65691577e+00,
          1.76028203e-01,  2.75517333e-01,  2.65760578e-01],
        [-3.24802792e-01,  3.33024903e-01,  1.07591847e+00,
          6.81754824e-01,  2.43561371e+00, -1.25284459e+00,
         -2.32647423e-02,  7.95220908e-01,  8.85573110e-01,
          5.68927455e-01,  2.01366682e+00, -1.01171404e+00,
         -1.00633931e-01, -1.35583457e+00,  2.48688626e-01],
        [-1.68094684e+00,  1.40841622e+00,  9.55109182e-01,
          1.48157039e+00, -2.54197843e-01, -7.23154747e-02,
         -4.66148692e-01,  1.51855642e+00, -1.40647534e-01,
         -2.82215626e-01,  6.67958802e-01, -4.49507963e-01,
          6.36877771e-02,  2.53320603e-01, -3.85235446e-02],
        [ 4.26875504e-01, -1.69675434e-01, -3.28726578e-01,
          2.13831329e+00, -4.67602028e-01, -3.71154402e-01,
          6.63078633e-02,  9.42155147e-01, -6.17660557e-01,
         -1.03784428e-01, -1.61582443e+00, -7.26301097e-01,
         -6.26879805e-01,  1.51944552e-01,  1.03204206e+00],
        [-1.36580993e-01,  8.28277754e-01, -2.04523495e+00,
         -1.05864539e+00, -3.76073756e-01,  7.01922777e-02,
          3.94201248e-01, -1.26043491e-01,  3.14960330e-01,
         -1.37297623e+00,  6.03775244e-01,  6.65248440e-01,
          2.04881871e-01,  6.53128277e-02, -6.00934862e-01],
        [ 2.10181264e-01, -7.64369288e-02, -1.52974819e+00,
         -9.15735791e-01, -3.67118066e-01,  2.38006434e+00,
          9.61799249e-01,  1.06688435e+00,  1.45180958e-01,
         -1.02869249e-01, -1.72124824e+00,  5.10650220e-01,
         -1.76949925e+00,  2.01272739e+00, -1.32516059e+00]],

       [[ 2.54560317e+00,  7.38002884e-01, -6.54789032e-01,
         -9.92110075e-01,  1.65037257e+00,  9.59982929e-01,
         -5.77998637e-01,  1.46129572e-01, -5.27797150e-01,
          4.05378960e-01,  1.18111924e+00,  8.11042775e-01,
          1.07647165e-01,  4.29897524e-01,  3.13271191e-01],
        [-2.43355114e-01,  7.32162166e-01,  4.44871407e-01,
          3.95257266e-01, -1.57529164e-01, -2.05671535e-01,
         -4.36001952e-01,  1.84688921e+00,  6.22667910e-01,
         -5.49663575e-01, -1.56400023e+00,  1.17843092e+00,
         -1.12467334e+00,  7.56475116e-01, -5.71812739e-01],
        [ 6.44536560e-01,  6.35202388e-01, -2.25755167e-01,
         -3.55131133e-01,  9.94924518e-02, -1.54274470e+00,
          1.12017580e+00, -9.04110326e-01,  1.38920417e-01,
          3.45893160e-01,  3.24518572e-02, -1.31101899e+00,
         -1.01089753e-02, -1.08474444e-01, -1.94984587e+00],
        [ 1.13146211e+00, -2.90213171e-01, -2.03805749e-01,
         -1.64703040e+00, -7.33787233e-01, -1.61020709e-01,
          1.01792060e-01, -2.35065480e-01, -8.16665640e-01,
         -1.60212226e-01,  1.53489370e-01,  2.09879135e-01,
          1.78637098e+00,  1.02554250e+00,  5.61850736e-01],
        [-1.55803428e-01,  1.33355666e+00,  2.15203217e-01,
         -1.77705319e+00,  2.07605606e+00,  9.17236049e-01,
          3.98370098e-02, -7.30908996e-01, -1.25278298e+00,
          4.50748283e-01,  1.04818796e+00,  3.01518417e-01,
          5.58679940e-01,  2.23212184e-01, -8.04699659e-01],
        [-5.03126749e-01, -9.33228485e-01,  1.11826343e+00,
         -6.10820896e-01, -1.05621976e-01, -1.31578767e+00,
         -4.05004810e-01,  9.28646942e-02, -1.25604329e+00,
         -5.85917440e-01, -6.25421902e-02, -9.35910920e-01,
          6.61734382e-01,  3.47562423e-01,  9.98171708e-01],
        [-1.21942415e+00, -1.51775601e+00, -4.26335179e-02,
         -1.80854220e+00,  1.42769169e-01, -5.67924364e-01,
         -6.03873225e-01,  8.89339916e-01,  2.78189523e-01,
         -3.48026210e-02, -6.67775529e-01,  1.18404371e+00,
          7.20988126e-01,  1.19929955e+00, -4.92343784e-01],
        [ 8.51188422e-02,  5.44792897e-01, -8.70680685e-03,
         -5.99704948e-01,  2.45010380e-01, -9.79308082e-01,
         -4.06772613e-01, -1.06958870e+00, -4.04588207e-01,
          1.24548807e+00, -2.60164027e-01,  1.05688605e+00,
          1.62633325e-01,  7.10572924e-01, -4.54656969e-01],
        [ 4.31204212e-01, -3.63370728e-01, -3.14237441e-01,
          4.72816804e-02, -4.73009888e-02, -8.98329537e-01,
          1.79515973e+00,  2.03344708e-01, -4.62254250e-01,
         -9.27451877e-01, -4.49863740e-01,  1.85945745e-01,
          1.16746435e+00,  6.67226932e-01, -6.78851748e-01],
        [ 1.72933271e+00, -3.09208834e-01,  1.71307367e-01,
          4.35409023e-01, -2.33572193e-01, -3.56758221e-01,
          6.59507765e-01,  9.58950459e-01,  1.14845951e+00,
         -7.13622411e-01,  6.39498226e-01,  1.42894818e+00,
         -3.38636174e-01,  6.86315015e-01, -4.42654691e-01]],

       [[ 1.23230796e+00, -1.07206734e+00,  1.33606593e+00,
          9.20849256e-01,  9.51916834e-01, -4.42322228e-01,
         -4.66448658e-01, -3.22728230e-02, -6.36495998e-01,
         -7.88052286e-01,  1.06072144e+00, -1.91465598e+00,
         -1.75776884e+00, -1.55002501e+00, -2.11482924e+00],
        [-9.78501340e-01,  1.35877543e-01, -5.45392018e-01,
          1.13505496e+00,  3.93358644e-01, -1.86920642e+00,
         -1.50339418e+00,  1.47914506e+00,  3.12743198e-01,
         -1.11666761e-01,  1.56299458e+00,  8.11318983e-01,
         -6.33187049e-01, -7.99414045e-01, -2.32409439e-01],
        [-1.17438100e+00, -6.75295957e-01, -5.72781787e-01,
         -6.13867916e-02,  1.12694178e-01,  1.98741609e-01,
          1.19243630e-01, -7.71174120e-01,  4.98032670e-01,
          3.42073978e-01,  7.11466431e-01, -9.71419985e-03,
          5.37526077e-01, -5.19903253e-01,  2.40012586e+00],
        [ 7.04553871e-02, -3.12069547e-01,  1.04435520e+00,
         -1.33737024e-01, -1.04662120e+00,  2.60550660e-01,
         -2.14954391e+00, -3.10507378e-01,  7.03322888e-01,
         -3.99108846e-01, -9.87540458e-01,  3.57968448e-01,
         -5.97802444e-01,  8.17546586e-01, -2.03526681e+00],
        [ 1.07349485e-01,  3.93378612e-01, -1.36148895e+00,
         -7.62467121e-01, -4.92892829e-01, -4.62399863e-01,
          6.44816691e-01,  1.35418593e+00,  3.95614802e-01,
          6.39040060e-01,  2.58917348e-01,  4.67348096e-01,
          6.82408193e-01, -2.62337384e-01, -4.15342816e-01],
        [ 2.46613538e+00, -2.88706917e-01,  4.94078404e-01,
         -2.61768025e-01, -1.12002247e+00, -8.14462338e-01,
         -5.98812484e-01, -2.08280871e+00, -2.06998337e+00,
          9.10045091e-01, -1.14452034e-01,  5.10822790e-01,
         -8.98875149e-01,  2.11352152e-01, -1.34725099e-01],
        [-7.28709521e-01,  7.08778935e-01,  7.83983774e-02,
          3.30028548e-01, -1.51588349e+00,  3.88185056e-01,
          8.52944405e-01, -7.14316790e-01, -1.87765043e-01,
         -8.22369159e-01, -6.36618671e-02, -6.51202415e-01,
         -4.31975467e-01,  1.76022443e-02,  1.60186739e+00],
        [-8.74026339e-01,  6.29119177e-01, -8.14946280e-01,
         -5.05871618e-01, -1.57850996e+00, -1.55191724e+00,
         -1.30718981e+00, -1.73375059e+00, -4.23991420e-01,
          8.96079000e-01,  3.56483906e-02, -7.83213560e-01,
          1.73474516e-01, -1.00723415e+00,  5.74736565e-01],
        [ 1.10003821e+00,  9.38208667e-01, -1.07493908e+00,
          1.04026339e+00, -2.85200861e-01,  4.02833681e-01,
         -2.00249154e-01, -4.18675191e-01, -1.46995604e+00,
          1.63365158e-01,  1.40530030e+00,  8.57389851e-01,
         -5.24648379e-01, -1.93279357e+00, -3.79007096e-01],
        [-1.40074375e+00, -1.13978579e-01, -7.71586924e-01,
         -2.09206256e-01, -1.88083868e+00,  1.23096304e+00,
         -9.68290292e-01,  2.63569894e-01,  3.02665388e-01,
          1.74173609e+00,  1.01673047e+00,  4.74906522e-01,
          1.95314734e-01,  1.52036531e-02,  3.77099302e-01]]])
data.sum(axis=0).shape #相当于10行15列,把最里头的15个竖着排想象一下
(10, 15)
data.sum(axis=(0,2)).shape
(10,)
data.sum()
-5.02522068797436
data = np.arange(1,10).reshape(3,3)
data
array([[1, 2, 3],
       [4, 5, 6],
       [7, 8, 9]])
data.sum()
45
data.sum(axis=0)
array([12, 15, 18])
data.sum(axis=1)
array([ 6, 15, 24])

布尔型的数组和判断表达和运算

a = np.array([1,2,3,4])
b = np.array([4,3,2,1])
a < b
array([ True,  True, False, False])
np.all(a<b)
False
np.any(a<b)
True
if np.all(a<b):
    print("a中所有的元素都小于b中对应的元素")
elif np.any(a<b):
    print("a中有一些元素是小于b中对应元素的")
else:
    print("a中所有元素都大于b中对应的元素")
a中有一些元素是小于b中对应元素的
x = np.array([-2,-1,0,1,2])
x
array([-2, -1,  0,  1,  2])
x > 0
array([False, False, False,  True,  True])
1 * (x>0) #0代表False,1代表True
array([0, 0, 0, 1, 1])
x * (x>0)
array([0, 0, 0, 1, 2])
def pulse(x,position,height,width):
    return height * (x>=position) * (x<=(position+width))
x = np.linspace(-5,5,11)
x
array([-5., -4., -3., -2., -1.,  0.,  1.,  2.,  3.,  4.,  5.])
pulse(x, position=-2, height=1, width=5) #如果是否定False,返回的就是0
array([0, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0])
pulse(x, position=1, height=1, width=5)
array([0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1])
def pulse2(x, position, height, width):
    return height*np.logical_and(x>=position, x<=(position+width)) ## 对每个元素进行 and 运算,返回 bool 值组成的数组

pulse2(x,position=1,height=1,width=5) #跟上面的结果一样,应该是输出方式不一样
array([0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1])
x = np.linspace(-4,4,9)
x
array([-4., -3., -2., -1.,  0.,  1.,  2.,  3.,  4.])
np.where(x<0, x**2, x**3) #x<0的部分x**2运算,x>0的部分x**3运算
array([16.,  9.,  4.,  1.,  0.,  1.,  8., 27., 64.])
x
array([-4., -3., -2., -1.,  0.,  1.,  2.,  3.,  4.])
np.select([x<-1, x>2, x>=2], [x**2, x**3, x**4]) #分别对应,没有的对应就是0
array([16.,  9.,  4.,  0.,  0.,  0., 16., 27., 64.])
np.choose([0,0,0,1,1,1,2,2,2],[x**2, x**3, x**4]) #映射的是下标,有三个计算对应012,然后前面的就是每个元素要做的对应的运算
array([ 16.,   9.,   4.,  -1.,   0.,   1.,  16.,  81., 256.])
np.nonzero(abs(x)>2) #用于得到数组array中非零元素的位置(数组索引)的函数,找绝对值大于2的值的位置
(array([0, 1, 7, 8], dtype=int64),)
x[np.nonzero(abs(x)>2)] #把符合上一个条件的元素找出来
array([-4., -3.,  3.,  4.])
x[abs(x)>2] #直接用pythonlist找也行
array([-4., -3.,  3.,  4.])

有关设置 Set operation

  • np.unique 去重
a = np.unique([1,2,3,3])
b = np.unique([2,3,4,4,5,6,5])
a
array([1, 2, 3])
b
array([2, 3, 4, 5, 6])
np.in1d(a,b)
array([False,  True,  True])
1 in a
True
1 in b
False
np.all(np.in1d(a,b)) #in1d表示a是否在b里面
False
c = [1,2,3]
np.all(np.in1d(a,c))
True
print(a)
print(b)
[1 2 3]
[2 3 4 5 6]
np.union1d(a,b)
array([1, 2, 3, 4, 5, 6])
np.intersect1d(a,b) #重复的
array([2, 3])
np.setdiff1d(a,b) #返回a有b没有的
array([1])
np.setdiff1d(b,a) #b有a没有的
array([4, 5, 6])

对数组的操作 Operations on Arrays

data = np.arange(9).reshape(3,3)
data
array([[0, 1, 2],
       [3, 4, 5],
       [6, 7, 8]])
np.transpose(data)
array([[0, 3, 6],
       [1, 4, 7],
       [2, 5, 8]])
  • 有点疑问这个维度太多了
data = np.random.randn(1,2,3,4,5)
data
array([[[[[ 0.07638856, -2.76825039, -0.76128472, -0.28551387,
           -0.64904937],
          [-1.89204845, -0.3736312 ,  0.37365919,  1.4333082 ,
           -0.4833306 ],
          [-1.39303895, -0.30634518,  0.26564942,  0.77706625,
           -0.09466797],
          [ 0.7093459 ,  0.01138559,  0.73548345,  1.45994845,
            0.18609298]],

         [[ 0.44148616,  1.4286259 ,  1.3444921 , -0.51921079,
            0.12303095],
          [ 0.15210441, -1.26191144, -0.71871825,  0.13874948,
            0.13720807],
          [-0.83537003,  0.26890164, -0.11217582,  0.35947749,
           -1.4776633 ],
          [ 0.00579209, -0.64581119, -0.3716978 ,  0.04412398,
            0.75901064]],

         [[ 0.97368514,  1.30983902, -0.72426458, -0.57824187,
           -0.46364744],
          [-1.01014603, -0.20679678, -0.02905534,  0.13909377,
           -1.13454013],
          [ 0.31090429, -0.72006295, -0.48680631,  0.50330729,
            1.13125257],
          [ 0.71080136, -0.80383201,  2.68066626,  0.03053986,
            0.62856903]]],


        [[[-2.20233836, -0.4221874 ,  0.71739058, -0.27651795,
           -2.46510683],
          [-0.20139649,  0.18609453, -0.09661651, -1.63877311,
           -0.06019454],
          [ 0.38382371,  1.77708061, -0.2365673 ,  0.44033216,
           -0.1296616 ],
          [ 0.06503614,  0.89513916,  0.33981108, -0.52867593,
            1.2270521 ]],

         [[-0.28522669, -0.11820284, -0.36496461, -0.07223976,
            1.58469431],
          [ 0.94863967, -1.30380716, -0.84285942,  1.7422622 ,
           -0.33259789],
          [-0.71064147, -0.74273917,  0.59465618,  0.36199743,
            0.83512131],
          [-0.04029088, -0.10381291, -2.7711599 ,  1.49361284,
            1.46727327]],

         [[-0.34682262, -0.16751807,  0.85041397, -0.14888091,
           -0.30392357],
          [ 0.88306242,  0.3026863 ,  0.01950192, -0.12398973,
           -0.7351325 ],
          [-1.51972788, -1.43435684,  1.96873049,  0.78381719,
           -0.85361619],
          [-0.32127623,  1.43196406, -0.01681594,  0.09608456,
           -0.15642501]]]]])
data.shape
(1, 2, 3, 4, 5)
data.T.shape
(5, 4, 3, 2, 1)

矩阵和向量的运行 Matrix and Vector Operations

A = np.arange(1,7).reshape(2,3)
A
array([[1, 2, 3],
       [4, 5, 6]])
B = np.arange(1,7).reshape(3,2)
B
array([[1, 2],
       [3, 4],
       [5, 6]])
np.dot(A,B) #A和B相乘 2*3 3*2
array([[22, 28],
       [49, 64]])
np.dot(B,A) #B*A,注意顺序 3*2 2*3
array([[ 9, 12, 15],
       [19, 26, 33],
       [29, 40, 51]])
A = np.arange(9).reshape(3,3)
A
array([[0, 1, 2],
       [3, 4, 5],
       [6, 7, 8]])
x = np.arange(3)
x
array([0, 1, 2])
np.dot(A,x) #特别注意这个是把x向量和A每一行的去做乘法了
array([ 5, 14, 23])
A.dot(x) #和上面的做法结果是一样的
array([ 5, 14, 23])
A = np.random.rand(3,3)
B = np.random.rand(3,3)
A
array([[0.65917051, 0.04149481, 0.13114148],
       [0.48336411, 0.91824819, 0.98314358],
       [0.33345641, 0.84574605, 0.82119249]])
B
array([[0.32710979, 0.98441945, 0.72966906],
       [0.29562009, 0.08911795, 0.0841273 ],
       [0.10230343, 0.61755188, 0.45790444]])
#np.linalg.inv():矩阵求逆
Ap = np.dot(B, np.dot(A, np.linalg.inv(B)))
Ap
array([[-39.64825613,  25.57919502,  61.99572833],
       [ -5.33273145,   3.96412789,   8.19626529],
       [-24.31828   ,  15.4840001 ,  38.08273943]])
Ap = B.dot(A.dot(np.linalg.inv(B)))
Ap
array([[-39.64825613,  25.57919502,  61.99572833],
       [ -5.33273145,   3.96412789,   8.19626529],
       [-24.31828   ,  15.4840001 ,  38.08273943]])
A = np.matrix(A) #该模块中的函数返回的是一个矩阵,而不是 ndarray 对象。
B = np.matrix(B)
Ap = B*A*B.I
Ap
matrix([[-39.64825613,  25.57919502,  61.99572833],
        [ -5.33273145,   3.96412789,   8.19626529],
        [-24.31828   ,  15.4840001 ,  38.08273943]])
  • asmatrix有点疑问
A = np.asmatrix(A)
B = np.asmatrix(B)
Ap = B*A*B.I
Ap
matrix([[-39.64825613,  25.57919502,  61.99572833],
        [ -5.33273145,   3.96412789,   8.19626529],
        [-24.31828   ,  15.4840001 ,  38.08273943]])
Ap = np.asarray(Ap)
Ap #数组和矩阵的区别,matrix包含于array
array([[-39.64825613,  25.57919502,  61.99572833],
       [ -5.33273145,   3.96412789,   8.19626529],
       [-24.31828   ,  15.4840001 ,  38.08273943]])
x = np.arange(3)
x
array([0, 1, 2])
np.inner(x,x) #inner必须是相同维度的
5
np.dot(x,x) #dot不要求相同维度
5
y = x[:, np.newaxis]
y
array([[0],
       [1],
       [2]])
np.dot(y.T, y)
array([[5]])
x = np.array([1,2,3])
x
array([1, 2, 3])
np.outer(x,x) #外积,a,b是两个数组,如果a,b是高维数组,函数会自动将其flatten成1维
array([[1, 2, 3],
       [2, 4, 6],
       [3, 6, 9]])
np.kron(x,x) #也是外积,只是把outer的结果弄成一维了
array([1, 2, 3, 2, 4, 6, 3, 6, 9])
x
array([1, 2, 3])
x[:, np.newaxis]
array([[1],
       [2],
       [3]])
x[np.newaxis, :]
array([[1, 2, 3]])
#为了通过kron变成原来outer结果,要用这种方式
np.kron(x[:,np.newaxis], x[np.newaxis,:])
array([[1, 2, 3],
       [2, 4, 6],
       [3, 6, 9]])
  • 有疑问
np.ones((2,2))
array([[1., 1.],
       [1., 1.]])
np.identity(2)
array([[1., 0.],
       [0., 1.]])
np.kron(np.ones((2,2)),np.identity(2))
array([[1., 0., 1., 0.],
       [0., 1., 0., 1.],
       [1., 0., 1., 0.],
       [0., 1., 0., 1.]])
np.kron(np.identity(2), np.ones((2,2)))
array([[1., 1., 0., 0.],
       [1., 1., 0., 0.],
       [0., 0., 1., 1.],
       [0., 0., 1., 1.]])
  • einsum 全称 Einstein summation convention(爱因斯坦求和约定),用简单的方式来代表多维数组运算
x = np.array([1,2,3,4])
y = np.array([5,6,7,8])
x
array([1, 2, 3, 4])
y
array([5, 6, 7, 8])
np.einsum("n,n",x,y) #可以表示内积,einsum中"n,n"可以代表,当然还有别的方式可以计算
70
np.inner(x,y) #原来输出内积的方式
70
#矩阵相乘的方式,使用einsum完成
A = np.arange(9).reshape(3,3)
B = A.T
A
array([[0, 1, 2],
       [3, 4, 5],
       [6, 7, 8]])
B
array([[0, 3, 6],
       [1, 4, 7],
       [2, 5, 8]])
np.einsum("mk,kn",A,B)
array([[  5,  14,  23],
       [ 14,  50,  86],
       [ 23,  86, 149]])
np.alltrue(np.einsum("mk,kn",A,B) == np.dot(A,B))
True

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值