利用Python进行数据分析

python基础: 数组和矢量计算

第四章: numpy基础

1
2
import numpy as np
data=np.random.randn(4,4)
1
data.shape
(4, 4)
1
arr=np.array(data)
1
arr
array([[-2.92815868, -0.4693437 ,  0.92836325, -0.86445295],
       [-0.9575174 , -0.47274498, -1.98651175, -0.92836188],
       [-0.04815155,  0.11325496, -2.16331438,  0.12950206],
       [ 1.61292064, -1.14361762,  0.43682842, -1.26011755]])
1
data
array([[-2.92815868, -0.4693437 ,  0.92836325, -0.86445295],
       [-0.9575174 , -0.47274498, -1.98651175, -0.92836188],
       [-0.04815155,  0.11325496, -2.16331438,  0.12950206],
       [ 1.61292064, -1.14361762,  0.43682842, -1.26011755]])
1
print(arr)
[[-2.92815868 -0.4693437   0.92836325 -0.86445295]
 [-0.9575174  -0.47274498 -1.98651175 -0.92836188]
 [-0.04815155  0.11325496 -2.16331438  0.12950206]
 [ 1.61292064 -1.14361762  0.43682842 -1.26011755]]
1
np.zeros((10,2))
array([[ 0.,  0.],
       [ 0.,  0.],
       [ 0.,  0.],
       [ 0.,  0.],
       [ 0.,  0.],
       [ 0.,  0.],
       [ 0.,  0.],
       [ 0.,  0.],
       [ 0.,  0.],
       [ 0.,  0.]])
1
np.ones((2,4))
array([[ 1.,  1.,  1.,  1.],
       [ 1.,  1.,  1.,  1.]])
1
np.eye(10,10)
array([[ 1.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.],
       [ 0.,  1.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.],
       [ 0.,  0.,  1.,  0.,  0.,  0.,  0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  1.,  0.,  0.,  0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.,  1.,  0.,  0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.,  0.,  1.,  0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.,  0.,  0.,  1.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  1.,  0.,  0.],
       [ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  1.,  0.],
       [ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  1.]])
1
2
ar1=np.array([[1,2,3],[4,5,6]])
ar1
array([[1, 2, 3],
       [4, 5, 6]])
1
ar2=ar1

注意矩阵操作和python的list的操作的不同

1
ar1*ar2
array([[ 1,  4,  9],
       [16, 25, 36]])
1
ar1-ar2
array([[0, 0, 0],
       [0, 0, 0]])
1
ar1+ar2
array([[ 2,  4,  6],
       [ 8, 10, 12]])
1
%timeit ar1+ar2
386 ns ± 1.56 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)
1
ar1/5
array([[ 0.2,  0.4,  0.6],
       [ 0.8,  1. ,  1.2]])
1
1/ar1
array([[ 1.        ,  0.5       ,  0.33333333],
       [ 0.25      ,  0.2       ,  0.16666667]])
1
2
b=np.arange(10)
b
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
1
b-b
array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0])

slice切片的使用,切片上的操作直接影响到源矩阵

1
bslice=b[4:6]
1
bslice
array([4, 5])
1
bslice[:]=100
1
b
array([  0,   1,   2,   3, 100, 100,   6,   7,   8,   9])

矩阵的布尔型索引

2.1 矩阵的比较运算也是矢量化的, 利用bool操作可以提供很多的方法去操作矩阵中的元素

1
2
names=np.array(["Jordan","Kobe","Magic","Lebron","Oneal","Ducan"])
names
array(['Jordan', 'Kobe', 'Magic', 'Lebron', 'Oneal', 'Ducan'],
      dtype='<U6')
1
names=="Kobe"
array([False,  True, False, False, False, False], dtype=bool)
1
2
arr3=np.random.randn(6,4)
arr3
array([[-0.05387395,  0.63654347,  2.01167972, -0.85860418],
       [ 1.15355691, -0.52870696, -0.60051838, -2.14095282],
       [ 0.6170254 , -0.17970408,  0.5651644 , -0.32689396],
       [-0.9541369 , -0.83096901, -0.51250354,  0.76669871],
       [ 0.26518263, -0.02176463,  0.32238273,  2.26313526],
       [ 0.63746355, -0.96617442, -0.06673749, -1.4751273 ]])
1
2
arr3[names=="Kobe"]=24
arr3
array([[ -5.38739483e-02,   6.36543473e-01,   2.01167972e+00,
         -8.58604177e-01],
       [  2.40000000e+01,   2.40000000e+01,   2.40000000e+01,
          2.40000000e+01],
       [  6.17025403e-01,  -1.79704076e-01,   5.65164401e-01,
         -3.26893956e-01],
       [ -9.54136897e-01,  -8.30969009e-01,  -5.12503540e-01,
          7.66698707e-01],
       [  2.65182629e-01,  -2.17646337e-02,   3.22382726e-01,
          2.26313526e+00],
       [  6.37463550e-01,  -9.66174425e-01,  -6.67374854e-02,
         -1.47512730e+00]])
1
names!="Kobe"
array([ True, False,  True,  True,  True,  True], dtype=bool)
1
2
arr3[~(names=="Kobe")]=0
arr3
array([[  0.,   0.,   0.,   0.],
       [ 24.,  24.,  24.,  24.],
       [  0.,   0.,   0.,   0.],
       [  0.,   0.,   0.,   0.],
       [  0.,   0.,   0.,   0.],
       [  0.,   0.,   0.,   0.]])
1
2
mask=(names=="Kobe")|(names=="Jordan")
mask
array([ True,  True, False, False, False, False], dtype=bool)
1
arr3[mask]
array([[  0.,   0.,   0.,   0.],
       [ 24.,  24.,  24.,  24.]])
1
2
arr3[arr3==0]=23
arr3
array([[ 23.,  23.,  23.,  23.],
       [ 24.,  24.,  24.,  24.],
       [ 23.,  23.,  23.,  23.],
       [ 23.,  23.,  23.,  23.],
       [ 23.,  23.,  23.,  23.],
       [ 23.,  23.,  23.,  23.]])
1
2
arr3[names!="Kobe"]=1
arr3
array([[  1.,   1.,   1.,   1.],
       [ 24.,  24.,  24.,  24.],
       [  1.,   1.,   1.,   1.],
       [  1.,   1.,   1.,   1.],
       [  1.,   1.,   1.,   1.],
       [  1.,   1.,   1.,   1.]])

2.2 矩阵的花式索引(Fancy Indexing)

注意花式索引和切片不一样,它总是把数据复制到新的矩阵中

1
2
arr4=np.empty((8,4),dtype=np.int32)
arr4
array([[  -44820527,  1071986160,   439455978,  1072162656],
       [ 2007969062,  1071146177, -2127231235,  1073869197],
       [ 1030511985,  1072188309,   319262802,  1072268317],
       [  -70212269,  1069666599,   882987624,  1069846423],
       [-1786127844,  1073164946,  -346589487,  1072269064],
       [  620329821,  1071448489,  1609482608,  1073348103],
       [ -412357061,  1068019309,  1058021927,  1070818026],
       [-1785317863,  1069873909,  -981128963,  1072298188]], dtype=int32)
1
2
3
for i in range(8):
arr4[i][0:2]=i
arr4
array([[0, 0, 0, 0],
       [1, 1, 1, 1],
       [2, 2, 2, 2],
       [3, 3, 3, 3],
       [4, 4, 4, 4],
       [5, 5, 5, 5],
       [6, 6, 6, 6],
       [7, 7, 7, 7]], dtype=int32)
1
arr4[[5,3,2,7]]
array([[5, 5, 5, 5],
       [3, 3, 3, 3],
       [2, 2, 2, 2],
       [7, 7, 7, 7]], dtype=int32)

矩阵的转置和轴对换

1
arr4.T
array([[0, 1, 2, 3, 4, 5, 6, 7],
       [0, 1, 2, 3, 4, 5, 6, 7],
       [0, 1, 2, 3, 4, 5, 6, 7],
       [0, 1, 2, 3, 4, 5, 6, 7]], dtype=int32)
1
arr4.reshape((1,32)).T
array([[0],
       [0],
       [0],
       [0],
       [1],
       [1],
       [1],
       [1],
       [2],
       [2],
       [2],
       [2],
       [3],
       [3],
       [3],
       [3],
       [4],
       [4],
       [4],
       [4],
       [5],
       [5],
       [5],
       [5],
       [6],
       [6],
       [6],
       [6],
       [7],
       [7],
       [7],
       [7]], dtype=int32)
1
arr4.transpose((1,2))
---------------------------------------------------------------------------

AxisError                                 Traceback (most recent call last)

<ipython-input-120-a96838d4801a> in <module>()
----> 1 arr4.transpose((1,2))


AxisError: axis 2 is out of bounds for array of dimension 2

通用操作: 快速的元素级的函数

1
np.sqrt(arr4)
array([[ 0.        ,  0.        ,  0.        ,  0.        ],
       [ 1.        ,  1.        ,  1.        ,  1.        ],
       [ 1.41421356,  1.41421356,  1.41421356,  1.41421356],
       [ 1.73205081,  1.73205081,  1.73205081,  1.73205081],
       [ 2.        ,  2.        ,  2.        ,  2.        ],
       [ 2.23606798,  2.23606798,  2.23606798,  2.23606798],
       [ 2.44948974,  2.44948974,  2.44948974,  2.44948974],
       [ 2.64575131,  2.64575131,  2.64575131,  2.64575131]])
1
np.square(arr4)
array([[ 0,  0,  0,  0],
       [ 1,  1,  1,  1],
       [ 4,  4,  4,  4],
       [ 9,  9,  9,  9],
       [16, 16, 16, 16],
       [25, 25, 25, 25],
       [36, 36, 36, 36],
       [49, 49, 49, 49]], dtype=int32)
1
2
arr5=np.empty(arr4.shape,dtype=np.float32)
arr5
array([[  3.86403988e+36,   4.58364728e-41,   3.86403988e+36,
          4.58364728e-41],
       [  4.84138582e+36,   4.58364728e-41,   8.40779079e-45,
          4.58364728e-41],
       [  5.17916907e+36,   1.26116862e-44,   5.75883764e+34,
          4.58364728e-41],
       [  1.68155816e-44,   4.58364728e-41,   5.85750672e+31,
          2.10194770e-44],
       [  4.77523712e+08,   4.58364728e-41,   2.52233724e-44,
          0.00000000e+00],
       [  0.00000000e+00,   2.94272678e-44,   0.00000000e+00,
          0.00000000e+00],
       [  3.36311631e-44,   4.58364728e-41,   0.00000000e+00,
          3.78350585e-44],
       [  0.00000000e+00,   0.00000000e+00,   4.20389539e-44,
          0.00000000e+00]], dtype=float32)
1
np.subtract(arr4,arr5)
array([[ -3.86403988e+36,  -4.58364728e-41,  -3.86403988e+36,
         -4.58364728e-41],
       [ -4.84138582e+36,   1.00000000e+00,   1.00000000e+00,
          1.00000000e+00],
       [ -5.17916907e+36,   2.00000000e+00,  -5.75883764e+34,
          2.00000000e+00],
       [  3.00000000e+00,   3.00000000e+00,  -5.85750672e+31,
          3.00000000e+00],
       [ -4.77523708e+08,   4.00000000e+00,   4.00000000e+00,
          4.00000000e+00],
       [  5.00000000e+00,   5.00000000e+00,   5.00000000e+00,
          5.00000000e+00],
       [  6.00000000e+00,   6.00000000e+00,   6.00000000e+00,
          6.00000000e+00],
       [  7.00000000e+00,   7.00000000e+00,   7.00000000e+00,
          7.00000000e+00]])

利用矩阵进行数据处理

1
2
points=np.arange(-5,5,0.01)
points
array([ -5.00000000e+00,  -4.99000000e+00,  -4.98000000e+00,
        -4.97000000e+00,  -4.96000000e+00,  -4.95000000e+00,
        -4.94000000e+00,  -4.93000000e+00,  -4.92000000e+00,
        -4.91000000e+00,  -4.90000000e+00,  -4.89000000e+00,
        -4.88000000e+00,  -4.87000000e+00,  -4.86000000e+00,
        -4.85000000e+00,  -4.84000000e+00,  -4.83000000e+00,
        -4.82000000e+00,  -4.81000000e+00,  -4.80000000e+00,
        -4.79000000e+00,  -4.78000000e+00,  -4.77000000e+00,
        -4.76000000e+00,  -4.75000000e+00,  -4.74000000e+00,
        -4.73000000e+00,  -4.72000000e+00,  -4.71000000e+00,
        -4.70000000e+00,  -4.69000000e+00,  -4.68000000e+00,
        -4.67000000e+00,  -4.66000000e+00,  -4.65000000e+00,
        -4.64000000e+00,  -4.63000000e+00,  -4.62000000e+00,
        -4.61000000e+00,  -4.60000000e+00,  -4.59000000e+00,
        -4.58000000e+00,  -4.57000000e+00,  -4.56000000e+00,
        -4.55000000e+00,  -4.54000000e+00,  -4.53000000e+00,
        -4.52000000e+00,  -4.51000000e+00,  -4.50000000e+00,
        -4.49000000e+00,  -4.48000000e+00,  -4.47000000e+00,
        -4.46000000e+00,  -4.45000000e+00,  -4.44000000e+00,
        -4.43000000e+00,  -4.42000000e+00,  -4.41000000e+00,
        -4.40000000e+00,  -4.39000000e+00,  -4.38000000e+00,
        -4.37000000e+00,  -4.36000000e+00,  -4.35000000e+00,
        -4.34000000e+00,  -4.33000000e+00,  -4.32000000e+00,
        -4.31000000e+00,  -4.30000000e+00,  -4.29000000e+00,
        -4.28000000e+00,  -4.27000000e+00,  -4.26000000e+00,
        -4.25000000e+00,  -4.24000000e+00,  -4.23000000e+00,
        -4.22000000e+00,  -4.21000000e+00,  -4.20000000e+00,
        -4.19000000e+00,  -4.18000000e+00,  -4.17000000e+00,
        -4.16000000e+00,  -4.15000000e+00,  -4.14000000e+00,
        -4.13000000e+00,  -4.12000000e+00,  -4.11000000e+00,
        -4.10000000e+00,  -4.09000000e+00,  -4.08000000e+00,
        -4.07000000e+00,  -4.06000000e+00,  -4.05000000e+00,
        -4.04000000e+00,  -4.03000000e+00,  -4.02000000e+00,
        -4.01000000e+00,  -4.00000000e+00,  -3.99000000e+00,
        -3.98000000e+00,  -3.97000000e+00,  -3.96000000e+00,
        -3.95000000e+00,  -3.94000000e+00,  -3.93000000e+00,
        -3.92000000e+00,  -3.91000000e+00,  -3.90000000e+00,
        -3.89000000e+00,  -3.88000000e+00,  -3.87000000e+00,
        -3.86000000e+00,  -3.85000000e+00,  -3.84000000e+00,
        -3.83000000e+00,  -3.82000000e+00,  -3.81000000e+00,
        -3.80000000e+00,  -3.79000000e+00,  -3.78000000e+00,
        -3.77000000e+00,  -3.76000000e+00,  -3.75000000e+00,
        -3.74000000e+00,  -3.73000000e+00,  -3.72000000e+00,
        -3.71000000e+00,  -3.70000000e+00,  -3.69000000e+00,
        -3.68000000e+00,  -3.67000000e+00,  -3.66000000e+00,
        -3.65000000e+00,  -3.64000000e+00,  -3.63000000e+00,
        -3.62000000e+00,  -3.61000000e+00,  -3.60000000e+00,
        -3.59000000e+00,  -3.58000000e+00,  -3.57000000e+00,
        -3.56000000e+00,  -3.55000000e+00,  -3.54000000e+00,
        -3.53000000e+00,  -3.52000000e+00,  -3.51000000e+00,
        -3.50000000e+00,  -3.49000000e+00,  -3.48000000e+00,
        -3.47000000e+00,  -3.46000000e+00,  -3.45000000e+00,
        -3.44000000e+00,  -3.43000000e+00,  -3.42000000e+00,
        -3.41000000e+00,  -3.40000000e+00,  -3.39000000e+00,
        -3.38000000e+00,  -3.37000000e+00,  -3.36000000e+00,
        -3.35000000e+00,  -3.34000000e+00,  -3.33000000e+00,
        -3.32000000e+00,  -3.31000000e+00,  -3.30000000e+00,
        -3.29000000e+00,  -3.28000000e+00,  -3.27000000e+00,
        -3.26000000e+00,  -3.25000000e+00,  -3.24000000e+00,
        -3.23000000e+00,  -3.22000000e+00,  -3.21000000e+00,
        -3.20000000e+00,  -3.19000000e+00,  -3.18000000e+00,
        -3.17000000e+00,  -3.16000000e+00,  -3.15000000e+00,
        -3.14000000e+00,  -3.13000000e+00,  -3.12000000e+00,
        -3.11000000e+00,  -3.10000000e+00,  -3.09000000e+00,
        -3.08000000e+00,  -3.07000000e+00,  -3.06000000e+00,
        -3.05000000e+00,  -3.04000000e+00,  -3.03000000e+00,
        -3.02000000e+00,  -3.01000000e+00,  -3.00000000e+00,
        -2.99000000e+00,  -2.98000000e+00,  -2.97000000e+00,
        -2.96000000e+00,  -2.95000000e+00,  -2.94000000e+00,
        -2.93000000e+00,  -2.92000000e+00,  -2.91000000e+00,
        -2.90000000e+00,  -2.89000000e+00,  -2.88000000e+00,
        -2.87000000e+00,  -2.86000000e+00,  -2.85000000e+00,
        -2.84000000e+00,  -2.83000000e+00,  -2.82000000e+00,
        -2.81000000e+00,  -2.80000000e+00,  -2.79000000e+00,
        -2.78000000e+00,  -2.77000000e+00,  -2.76000000e+00,
        -2.75000000e+00,  -2.74000000e+00,  -2.73000000e+00,
        -2.72000000e+00,  -2.71000000e+00,  -2.70000000e+00,
        -2.69000000e+00,  -2.68000000e+00,  -2.67000000e+00,
        -2.66000000e+00,  -2.65000000e+00,  -2.64000000e+00,
        -2.63000000e+00,  -2.62000000e+00,  -2.61000000e+00,
        -2.60000000e+00,  -2.59000000e+00,  -2.58000000e+00,
        -2.57000000e+00,  -2.56000000e+00,  -2.55000000e+00,
        -2.54000000e+00,  -2.53000000e+00,  -2.52000000e+00,
        -2.51000000e+00,  -2.50000000e+00,  -2.49000000e+00,
        -2.48000000e+00,  -2.47000000e+00,  -2.46000000e+00,
        -2.45000000e+00,  -2.44000000e+00,  -2.43000000e+00,
        -2.42000000e+00,  -2.41000000e+00,  -2.40000000e+00,
        -2.39000000e+00,  -2.38000000e+00,  -2.37000000e+00,
        -2.36000000e+00,  -2.35000000e+00,  -2.34000000e+00,
        -2.33000000e+00,  -2.32000000e+00,  -2.31000000e+00,
        -2.30000000e+00,  -2.29000000e+00,  -2.28000000e+00,
        -2.27000000e+00,  -2.26000000e+00,  -2.25000000e+00,
        -2.24000000e+00,  -2.23000000e+00,  -2.22000000e+00,
        -2.21000000e+00,  -2.20000000e+00,  -2.19000000e+00,
        -2.18000000e+00,  -2.17000000e+00,  -2.16000000e+00,
        -2.15000000e+00,  -2.14000000e+00,  -2.13000000e+00,
        -2.12000000e+00,  -2.11000000e+00,  -2.10000000e+00,
        -2.09000000e+00,  -2.08000000e+00,  -2.07000000e+00,
        -2.06000000e+00,  -2.05000000e+00,  -2.04000000e+00,
        -2.03000000e+00,  -2.02000000e+00,  -2.01000000e+00,
        -2.00000000e+00,  -1.99000000e+00,  -1.98000000e+00,
        -1.97000000e+00,  -1.96000000e+00,  -1.95000000e+00,
        -1.94000000e+00,  -1.93000000e+00,  -1.92000000e+00,
        -1.91000000e+00,  -1.90000000e+00,  -1.89000000e+00,
        -1.88000000e+00,  -1.87000000e+00,  -1.86000000e+00,
        -1.85000000e+00,  -1.84000000e+00,  -1.83000000e+00,
        -1.82000000e+00,  -1.81000000e+00,  -1.80000000e+00,
        -1.79000000e+00,  -1.78000000e+00,  -1.77000000e+00,
        -1.76000000e+00,  -1.75000000e+00,  -1.74000000e+00,
        -1.73000000e+00,  -1.72000000e+00,  -1.71000000e+00,
        -1.70000000e+00,  -1.69000000e+00,  -1.68000000e+00,
        -1.67000000e+00,  -1.66000000e+00,  -1.65000000e+00,
        -1.64000000e+00,  -1.63000000e+00,  -1.62000000e+00,
        -1.61000000e+00,  -1.60000000e+00,  -1.59000000e+00,
        -1.58000000e+00,  -1.57000000e+00,  -1.56000000e+00,
        -1.55000000e+00,  -1.54000000e+00,  -1.53000000e+00,
        -1.52000000e+00,  -1.51000000e+00,  -1.50000000e+00,
        -1.49000000e+00,  -1.48000000e+00,  -1.47000000e+00,
        -1.46000000e+00,  -1.45000000e+00,  -1.44000000e+00,
        -1.43000000e+00,  -1.42000000e+00,  -1.41000000e+00,
        -1.40000000e+00,  -1.39000000e+00,  -1.38000000e+00,
        -1.37000000e+00,  -1.36000000e+00,  -1.35000000e+00,
        -1.34000000e+00,  -1.33000000e+00,  -1.32000000e+00,
        -1.31000000e+00,  -1.30000000e+00,  -1.29000000e+00,
        -1.28000000e+00,  -1.27000000e+00,  -1.26000000e+00,
        -1.25000000e+00,  -1.24000000e+00,  -1.23000000e+00,
        -1.22000000e+00,  -1.21000000e+00,  -1.20000000e+00,
        -1.19000000e+00,  -1.18000000e+00,  -1.17000000e+00,
        -1.16000000e+00,  -1.15000000e+00,  -1.14000000e+00,
        -1.13000000e+00,  -1.12000000e+00,  -1.11000000e+00,
        -1.10000000e+00,  -1.09000000e+00,  -1.08000000e+00,
        -1.07000000e+00,  -1.06000000e+00,  -1.05000000e+00,
        -1.04000000e+00,  -1.03000000e+00,  -1.02000000e+00,
        -1.01000000e+00,  -1.00000000e+00,  -9.90000000e-01,
        -9.80000000e-01,  -9.70000000e-01,  -9.60000000e-01,
        -9.50000000e-01,  -9.40000000e-01,  -9.30000000e-01,
        -9.20000000e-01,  -9.10000000e-01,  -9.00000000e-01,
        -8.90000000e-01,  -8.80000000e-01,  -8.70000000e-01,
        -8.60000000e-01,  -8.50000000e-01,  -8.40000000e-01,
        -8.30000000e-01,  -8.20000000e-01,  -8.10000000e-01,
        -8.00000000e-01,  -7.90000000e-01,  -7.80000000e-01,
        -7.70000000e-01,  -7.60000000e-01,  -7.50000000e-01,
        -7.40000000e-01,  -7.30000000e-01,  -7.20000000e-01,
        -7.10000000e-01,  -7.00000000e-01,  -6.90000000e-01,
        -6.80000000e-01,  -6.70000000e-01,  -6.60000000e-01,
        -6.50000000e-01,  -6.40000000e-01,  -6.30000000e-01,
        -6.20000000e-01,  -6.10000000e-01,  -6.00000000e-01,
        -5.90000000e-01,  -5.80000000e-01,  -5.70000000e-01,
        -5.60000000e-01,  -5.50000000e-01,  -5.40000000e-01,
        -5.30000000e-01,  -5.20000000e-01,  -5.10000000e-01,
        -5.00000000e-01,  -4.90000000e-01,  -4.80000000e-01,
        -4.70000000e-01,  -4.60000000e-01,  -4.50000000e-01,
        -4.40000000e-01,  -4.30000000e-01,  -4.20000000e-01,
        -4.10000000e-01,  -4.00000000e-01,  -3.90000000e-01,
        -3.80000000e-01,  -3.70000000e-01,  -3.60000000e-01,
        -3.50000000e-01,  -3.40000000e-01,  -3.30000000e-01,
        -3.20000000e-01,  -3.10000000e-01,  -3.00000000e-01,
        -2.90000000e-01,  -2.80000000e-01,  -2.70000000e-01,
        -2.60000000e-01,  -2.50000000e-01,  -2.40000000e-01,
        -2.30000000e-01,  -2.20000000e-01,  -2.10000000e-01,
        -2.00000000e-01,  -1.90000000e-01,  -1.80000000e-01,
        -1.70000000e-01,  -1.60000000e-01,  -1.50000000e-01,
        -1.40000000e-01,  -1.30000000e-01,  -1.20000000e-01,
        -1.10000000e-01,  -1.00000000e-01,  -9.00000000e-02,
        -8.00000000e-02,  -7.00000000e-02,  -6.00000000e-02,
        -5.00000000e-02,  -4.00000000e-02,  -3.00000000e-02,
        -2.00000000e-02,  -1.00000000e-02,  -1.06581410e-13,
         1.00000000e-02,   2.00000000e-02,   3.00000000e-02,
         4.00000000e-02,   5.00000000e-02,   6.00000000e-02,
         7.00000000e-02,   8.00000000e-02,   9.00000000e-02,
         1.00000000e-01,   1.10000000e-01,   1.20000000e-01,
         1.30000000e-01,   1.40000000e-01,   1.50000000e-01,
         1.60000000e-01,   1.70000000e-01,   1.80000000e-01,
         1.90000000e-01,   2.00000000e-01,   2.10000000e-01,
         2.20000000e-01,   2.30000000e-01,   2.40000000e-01,
         2.50000000e-01,   2.60000000e-01,   2.70000000e-01,
         2.80000000e-01,   2.90000000e-01,   3.00000000e-01,
         3.10000000e-01,   3.20000000e-01,   3.30000000e-01,
         3.40000000e-01,   3.50000000e-01,   3.60000000e-01,
         3.70000000e-01,   3.80000000e-01,   3.90000000e-01,
         4.00000000e-01,   4.10000000e-01,   4.20000000e-01,
         4.30000000e-01,   4.40000000e-01,   4.50000000e-01,
         4.60000000e-01,   4.70000000e-01,   4.80000000e-01,
         4.90000000e-01,   5.00000000e-01,   5.10000000e-01,
         5.20000000e-01,   5.30000000e-01,   5.40000000e-01,
         5.50000000e-01,   5.60000000e-01,   5.70000000e-01,
         5.80000000e-01,   5.90000000e-01,   6.00000000e-01,
         6.10000000e-01,   6.20000000e-01,   6.30000000e-01,
         6.40000000e-01,   6.50000000e-01,   6.60000000e-01,
         6.70000000e-01,   6.80000000e-01,   6.90000000e-01,
         7.00000000e-01,   7.10000000e-01,   7.20000000e-01,
         7.30000000e-01,   7.40000000e-01,   7.50000000e-01,
         7.60000000e-01,   7.70000000e-01,   7.80000000e-01,
         7.90000000e-01,   8.00000000e-01,   8.10000000e-01,
         8.20000000e-01,   8.30000000e-01,   8.40000000e-01,
         8.50000000e-01,   8.60000000e-01,   8.70000000e-01,
         8.80000000e-01,   8.90000000e-01,   9.00000000e-01,
         9.10000000e-01,   9.20000000e-01,   9.30000000e-01,
         9.40000000e-01,   9.50000000e-01,   9.60000000e-01,
         9.70000000e-01,   9.80000000e-01,   9.90000000e-01,
         1.00000000e+00,   1.01000000e+00,   1.02000000e+00,
         1.03000000e+00,   1.04000000e+00,   1.05000000e+00,
         1.06000000e+00,   1.07000000e+00,   1.08000000e+00,
         1.09000000e+00,   1.10000000e+00,   1.11000000e+00,
         1.12000000e+00,   1.13000000e+00,   1.14000000e+00,
         1.15000000e+00,   1.16000000e+00,   1.17000000e+00,
         1.18000000e+00,   1.19000000e+00,   1.20000000e+00,
         1.21000000e+00,   1.22000000e+00,   1.23000000e+00,
         1.24000000e+00,   1.25000000e+00,   1.26000000e+00,
         1.27000000e+00,   1.28000000e+00,   1.29000000e+00,
         1.30000000e+00,   1.31000000e+00,   1.32000000e+00,
         1.33000000e+00,   1.34000000e+00,   1.35000000e+00,
         1.36000000e+00,   1.37000000e+00,   1.38000000e+00,
         1.39000000e+00,   1.40000000e+00,   1.41000000e+00,
         1.42000000e+00,   1.43000000e+00,   1.44000000e+00,
         1.45000000e+00,   1.46000000e+00,   1.47000000e+00,
         1.48000000e+00,   1.49000000e+00,   1.50000000e+00,
         1.51000000e+00,   1.52000000e+00,   1.53000000e+00,
         1.54000000e+00,   1.55000000e+00,   1.56000000e+00,
         1.57000000e+00,   1.58000000e+00,   1.59000000e+00,
         1.60000000e+00,   1.61000000e+00,   1.62000000e+00,
         1.63000000e+00,   1.64000000e+00,   1.65000000e+00,
         1.66000000e+00,   1.67000000e+00,   1.68000000e+00,
         1.69000000e+00,   1.70000000e+00,   1.71000000e+00,
         1.72000000e+00,   1.73000000e+00,   1.74000000e+00,
         1.75000000e+00,   1.76000000e+00,   1.77000000e+00,
         1.78000000e+00,   1.79000000e+00,   1.80000000e+00,
         1.81000000e+00,   1.82000000e+00,   1.83000000e+00,
         1.84000000e+00,   1.85000000e+00,   1.86000000e+00,
         1.87000000e+00,   1.88000000e+00,   1.89000000e+00,
         1.90000000e+00,   1.91000000e+00,   1.92000000e+00,
         1.93000000e+00,   1.94000000e+00,   1.95000000e+00,
         1.96000000e+00,   1.97000000e+00,   1.98000000e+00,
         1.99000000e+00,   2.00000000e+00,   2.01000000e+00,
         2.02000000e+00,   2.03000000e+00,   2.04000000e+00,
         2.05000000e+00,   2.06000000e+00,   2.07000000e+00,
         2.08000000e+00,   2.09000000e+00,   2.10000000e+00,
         2.11000000e+00,   2.12000000e+00,   2.13000000e+00,
         2.14000000e+00,   2.15000000e+00,   2.16000000e+00,
         2.17000000e+00,   2.18000000e+00,   2.19000000e+00,
         2.20000000e+00,   2.21000000e+00,   2.22000000e+00,
         2.23000000e+00,   2.24000000e+00,   2.25000000e+00,
         2.26000000e+00,   2.27000000e+00,   2.28000000e+00,
         2.29000000e+00,   2.30000000e+00,   2.31000000e+00,
         2.32000000e+00,   2.33000000e+00,   2.34000000e+00,
         2.35000000e+00,   2.36000000e+00,   2.37000000e+00,
         2.38000000e+00,   2.39000000e+00,   2.40000000e+00,
         2.41000000e+00,   2.42000000e+00,   2.43000000e+00,
         2.44000000e+00,   2.45000000e+00,   2.46000000e+00,
         2.47000000e+00,   2.48000000e+00,   2.49000000e+00,
         2.50000000e+00,   2.51000000e+00,   2.52000000e+00,
         2.53000000e+00,   2.54000000e+00,   2.55000000e+00,
         2.56000000e+00,   2.57000000e+00,   2.58000000e+00,
         2.59000000e+00,   2.60000000e+00,   2.61000000e+00,
         2.62000000e+00,   2.63000000e+00,   2.64000000e+00,
         2.65000000e+00,   2.66000000e+00,   2.67000000e+00,
         2.68000000e+00,   2.69000000e+00,   2.70000000e+00,
         2.71000000e+00,   2.72000000e+00,   2.73000000e+00,
         2.74000000e+00,   2.75000000e+00,   2.76000000e+00,
         2.77000000e+00,   2.78000000e+00,   2.79000000e+00,
         2.80000000e+00,   2.81000000e+00,   2.82000000e+00,
         2.83000000e+00,   2.84000000e+00,   2.85000000e+00,
         2.86000000e+00,   2.87000000e+00,   2.88000000e+00,
         2.89000000e+00,   2.90000000e+00,   2.91000000e+00,
         2.92000000e+00,   2.93000000e+00,   2.94000000e+00,
         2.95000000e+00,   2.96000000e+00,   2.97000000e+00,
         2.98000000e+00,   2.99000000e+00,   3.00000000e+00,
         3.01000000e+00,   3.02000000e+00,   3.03000000e+00,
         3.04000000e+00,   3.05000000e+00,   3.06000000e+00,
         3.07000000e+00,   3.08000000e+00,   3.09000000e+00,
         3.10000000e+00,   3.11000000e+00,   3.12000000e+00,
         3.13000000e+00,   3.14000000e+00,   3.15000000e+00,
         3.16000000e+00,   3.17000000e+00,   3.18000000e+00,
         3.19000000e+00,   3.20000000e+00,   3.21000000e+00,
         3.22000000e+00,   3.23000000e+00,   3.24000000e+00,
         3.25000000e+00,   3.26000000e+00,   3.27000000e+00,
         3.28000000e+00,   3.29000000e+00,   3.30000000e+00,
         3.31000000e+00,   3.32000000e+00,   3.33000000e+00,
         3.34000000e+00,   3.35000000e+00,   3.36000000e+00,
         3.37000000e+00,   3.38000000e+00,   3.39000000e+00,
         3.40000000e+00,   3.41000000e+00,   3.42000000e+00,
         3.43000000e+00,   3.44000000e+00,   3.45000000e+00,
         3.46000000e+00,   3.47000000e+00,   3.48000000e+00,
         3.49000000e+00,   3.50000000e+00,   3.51000000e+00,
         3.52000000e+00,   3.53000000e+00,   3.54000000e+00,
         3.55000000e+00,   3.56000000e+00,   3.57000000e+00,
         3.58000000e+00,   3.59000000e+00,   3.60000000e+00,
         3.61000000e+00,   3.62000000e+00,   3.63000000e+00,
         3.64000000e+00,   3.65000000e+00,   3.66000000e+00,
         3.67000000e+00,   3.68000000e+00,   3.69000000e+00,
         3.70000000e+00,   3.71000000e+00,   3.72000000e+00,
         3.73000000e+00,   3.74000000e+00,   3.75000000e+00,
         3.76000000e+00,   3.77000000e+00,   3.78000000e+00,
         3.79000000e+00,   3.80000000e+00,   3.81000000e+00,
         3.82000000e+00,   3.83000000e+00,   3.84000000e+00,
         3.85000000e+00,   3.86000000e+00,   3.87000000e+00,
         3.88000000e+00,   3.89000000e+00,   3.90000000e+00,
         3.91000000e+00,   3.92000000e+00,   3.93000000e+00,
         3.94000000e+00,   3.95000000e+00,   3.96000000e+00,
         3.97000000e+00,   3.98000000e+00,   3.99000000e+00,
         4.00000000e+00,   4.01000000e+00,   4.02000000e+00,
         4.03000000e+00,   4.04000000e+00,   4.05000000e+00,
         4.06000000e+00,   4.07000000e+00,   4.08000000e+00,
         4.09000000e+00,   4.10000000e+00,   4.11000000e+00,
         4.12000000e+00,   4.13000000e+00,   4.14000000e+00,
         4.15000000e+00,   4.16000000e+00,   4.17000000e+00,
         4.18000000e+00,   4.19000000e+00,   4.20000000e+00,
         4.21000000e+00,   4.22000000e+00,   4.23000000e+00,
         4.24000000e+00,   4.25000000e+00,   4.26000000e+00,
         4.27000000e+00,   4.28000000e+00,   4.29000000e+00,
         4.30000000e+00,   4.31000000e+00,   4.32000000e+00,
         4.33000000e+00,   4.34000000e+00,   4.35000000e+00,
         4.36000000e+00,   4.37000000e+00,   4.38000000e+00,
         4.39000000e+00,   4.40000000e+00,   4.41000000e+00,
         4.42000000e+00,   4.43000000e+00,   4.44000000e+00,
         4.45000000e+00,   4.46000000e+00,   4.47000000e+00,
         4.48000000e+00,   4.49000000e+00,   4.50000000e+00,
         4.51000000e+00,   4.52000000e+00,   4.53000000e+00,
         4.54000000e+00,   4.55000000e+00,   4.56000000e+00,
         4.57000000e+00,   4.58000000e+00,   4.59000000e+00,
         4.60000000e+00,   4.61000000e+00,   4.62000000e+00,
         4.63000000e+00,   4.64000000e+00,   4.65000000e+00,
         4.66000000e+00,   4.67000000e+00,   4.68000000e+00,
         4.69000000e+00,   4.70000000e+00,   4.71000000e+00,
         4.72000000e+00,   4.73000000e+00,   4.74000000e+00,
         4.75000000e+00,   4.76000000e+00,   4.77000000e+00,
         4.78000000e+00,   4.79000000e+00,   4.80000000e+00,
         4.81000000e+00,   4.82000000e+00,   4.83000000e+00,
         4.84000000e+00,   4.85000000e+00,   4.86000000e+00,
         4.87000000e+00,   4.88000000e+00,   4.89000000e+00,
         4.90000000e+00,   4.91000000e+00,   4.92000000e+00,
         4.93000000e+00,   4.94000000e+00,   4.95000000e+00,
         4.96000000e+00,   4.97000000e+00,   4.98000000e+00,
         4.99000000e+00])
1
xs,ys=np.meshgrid(points,points)
1
xs
array([[-5.  , -4.99, -4.98, ...,  4.97,  4.98,  4.99],
       [-5.  , -4.99, -4.98, ...,  4.97,  4.98,  4.99],
       [-5.  , -4.99, -4.98, ...,  4.97,  4.98,  4.99],
       ..., 
       [-5.  , -4.99, -4.98, ...,  4.97,  4.98,  4.99],
       [-5.  , -4.99, -4.98, ...,  4.97,  4.98,  4.99],
       [-5.  , -4.99, -4.98, ...,  4.97,  4.98,  4.99]])
1
ys
array([[-5.  , -5.  , -5.  , ..., -5.  , -5.  , -5.  ],
       [-4.99, -4.99, -4.99, ..., -4.99, -4.99, -4.99],
       [-4.98, -4.98, -4.98, ..., -4.98, -4.98, -4.98],
       ..., 
       [ 4.97,  4.97,  4.97, ...,  4.97,  4.97,  4.97],
       [ 4.98,  4.98,  4.98, ...,  4.98,  4.98,  4.98],
       [ 4.99,  4.99,  4.99, ...,  4.99,  4.99,  4.99]])
1
2
3
import matplotlib. pyplot as plt
%matplotlib inline
z = np.sqrt(xs**2+ys**2)
1
2
3
plt.imshow(z,cmap=plt.cm.gray)
plt.colorbar()
plt.title("Image plot of $\sqrt{x^2+y^2}$ for a grid of values")
<matplotlib.text.Text at 0x7fc6481e3390>

png