numpy 基础操作

 

Numpy 基础操作¶

以numpy的基本数据例子来学习numpy基本数据处理方法

主要内容有:

  • 创建数组
  • 数组维度转换
  • 数据选区和切片
  • 数组数据计算
  • 随机数
  • 数据合并
  • 数据统计计算
In [1]:
import numpy as np
 

创建一维数组¶

In [2]:
data = np.arange(15)
data
Out[2]:
array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14])
 

reshape进行维度转换¶

  • data.reshape(rows, cols), 转换的行数,列数。
  • -1表示使用默认计算,reshape(3, -1),表示三行,列数根据数据自动计算
  • reshape(-1, 5)表示5列,行数根据计算得出
In [3]:
data = data.reshape(3,5)
data
Out[3]:
array([[ 0,  1,  2,  3,  4],
       [ 5,  6,  7,  8,  9],
       [10, 11, 12, 13, 14]])
 

np.newaxis增加维度¶

为数据增加维度,下面例子展示增加了一个维度

数据变为三行,一列,垂直五的数据

In [4]:
d = data[:, np.newaxis]
d
Out[4]:
array([[[ 0,  1,  2,  3,  4]],

       [[ 5,  6,  7,  8,  9]],

       [[10, 11, 12, 13, 14]]])
In [5]:
d.shape
Out[5]:
(3, 1, 5)
In [6]:
data.shape
Out[6]:
(3, 5)
 

读取逗号分割数据¶

In [7]:
url = 'https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data'

iris_2d = np.genfromtxt(url, delimiter=',', dtype='object')
iris_2d[:10]
Out[7]:
array([[b'5.1', b'3.5', b'1.4', b'0.2', b'Iris-setosa'],
       [b'4.9', b'3.0', b'1.4', b'0.2', b'Iris-setosa'],
       [b'4.7', b'3.2', b'1.3', b'0.2', b'Iris-setosa'],
       [b'4.6', b'3.1', b'1.5', b'0.2', b'Iris-setosa'],
       [b'5.0', b'3.6', b'1.4', b'0.2', b'Iris-setosa'],
       [b'5.4', b'3.9', b'1.7', b'0.4', b'Iris-setosa'],
       [b'4.6', b'3.4', b'1.4', b'0.3', b'Iris-setosa'],
       [b'5.0', b'3.4', b'1.5', b'0.2', b'Iris-setosa'],
       [b'4.4', b'2.9', b'1.4', b'0.2', b'Iris-setosa'],
       [b'4.9', b'3.1', b'1.5', b'0.1', b'Iris-setosa']], dtype=object)
 

数据切片和类型转换¶

使用[m, n]来对数据取值或者切片

  • [:, n] 取列数据
  • [m, :] 取行数据
  • [m:n, m:n] 数据切片

astype来指定数据类型

In [8]:
names = ('sepallength', 'sepalwidth', 'petallength', 'petalwidth', 'species')
sepallength = iris_2d[:, 0].astype('float')
petallength = iris_2d[:, 2].astype('float')
sepallength
Out[8]:
array([5.1, 4.9, 4.7, 4.6, 5. , 5.4, 4.6, 5. , 4.4, 4.9, 5.4, 4.8, 4.8,
       4.3, 5.8, 5.7, 5.4, 5.1, 5.7, 5.1, 5.4, 5.1, 4.6, 5.1, 4.8, 5. ,
       5. , 5.2, 5.2, 4.7, 4.8, 5.4, 5.2, 5.5, 4.9, 5. , 5.5, 4.9, 4.4,
       5.1, 5. , 4.5, 4.4, 5. , 5.1, 4.8, 5.1, 4.6, 5.3, 5. , 7. , 6.4,
       6.9, 5.5, 6.5, 5.7, 6.3, 4.9, 6.6, 5.2, 5. , 5.9, 6. , 6.1, 5.6,
       6.7, 5.6, 5.8, 6.2, 5.6, 5.9, 6.1, 6.3, 6.1, 6.4, 6.6, 6.8, 6.7,
       6. , 5.7, 5.5, 5.5, 5.8, 6. , 5.4, 6. , 6.7, 6.3, 5.6, 5.5, 5.5,
       6.1, 5.8, 5. , 5.6, 5.7, 5.7, 6.2, 5.1, 5.7, 6.3, 5.8, 7.1, 6.3,
       6.5, 7.6, 4.9, 7.3, 6.7, 7.2, 6.5, 6.4, 6.8, 5.7, 5.8, 6.4, 6.5,
       7.7, 7.7, 6. , 6.9, 5.6, 7.7, 6.3, 6.7, 7.2, 6.2, 6.1, 6.4, 7.2,
       7.4, 7.9, 6.4, 6.3, 6.1, 7.7, 6.3, 6.4, 6. , 6.9, 6.7, 6.9, 5.8,
       6.8, 6.7, 6.7, 6.3, 6.5, 6.2, 5.9])
In [9]:
petallength
Out[9]:
array([1.4, 1.4, 1.3, 1.5, 1.4, 1.7, 1.4, 1.5, 1.4, 1.5, 1.5, 1.6, 1.4,
       1.1, 1.2, 1.5, 1.3, 1.4, 1.7, 1.5, 1.7, 1.5, 1. , 1.7, 1.9, 1.6,
       1.6, 1.5, 1.4, 1.6, 1.6, 1.5, 1.5, 1.4, 1.5, 1.2, 1.3, 1.5, 1.3,
       1.5, 1.3, 1.3, 1.3, 1.6, 1.9, 1.4, 1.6, 1.4, 1.5, 1.4, 4.7, 4.5,
       4.9, 4. , 4.6, 4.5, 4.7, 3.3, 4.6, 3.9, 3.5, 4.2, 4. , 4.7, 3.6,
       4.4, 4.5, 4.1, 4.5, 3.9, 4.8, 4. , 4.9, 4.7, 4.3, 4.4, 4.8, 5. ,
       4.5, 3.5, 3.8, 3.7, 3.9, 5.1, 4.5, 4.5, 4.7, 4.4, 4.1, 4. , 4.4,
       4.6, 4. , 3.3, 4.2, 4.2, 4.2, 4.3, 3. , 4.1, 6. , 5.1, 5.9, 5.6,
       5.8, 6.6, 4.5, 6.3, 5.8, 6.1, 5.1, 5.3, 5.5, 5. , 5.1, 5.3, 5.5,
       6.7, 6.9, 5. , 5.7, 4.9, 6.7, 4.9, 5.7, 6. , 4.8, 4.9, 5.6, 5.8,
       6.1, 6.4, 5.6, 5.1, 5.6, 6.1, 5.6, 5.5, 4.8, 5.4, 5.6, 5.1, 5.1,
       5.9, 5.7, 5.2, 5. , 5.2, 5.4, 5.1])
 

数据运算¶

numpy数据运算语法与python基本一致,但是是数据中的每个数据进行运算

  • arr数据与单个数据运算,arr中每个数据与单数进行计算,返回新的arr
  • arr之间运算,arr对用位置的数据一一计算,返回新的arr
In [10]:
volume = (np.pi * petallength * (sepallength**2))/3
volume
Out[10]:
array([ 38.13265163,  35.20049849,  30.07237208,  33.23805027,
        36.65191429,  51.91167701,  31.02218026,  39.26990817,
        28.38324243,  37.71481981,  45.80442089,  38.60389053,
        33.77840421,  21.29895099,  42.27327075,  51.03517266,
        39.69716477,  38.13265163,  57.83986235,  40.85641246,
        51.91167701,  40.85641246,  22.15870018,  46.30393412,
        45.84212   ,  41.88790205,  41.88790205,  42.47433268,
        39.6427105 ,  37.01215025,  38.60389053,  45.80442089,
        42.47433268,  44.34881629,  37.71481981,  31.41592654,
        41.1810437 ,  37.71481981,  26.35586797,  40.85641246,
        34.03392041,  27.56747554,  26.35586797,  41.88790205,
        51.75145578,  33.77840421,  43.58017329,  31.02218026,
        44.12366882,  36.65191429, 241.16959604, 193.01945264,
       244.29966952, 126.71090369, 203.52284408, 153.10551797,
       195.34737279,  82.97260357, 209.83325652, 110.43326496,
        91.62978573, 153.10237638, 150.79644737, 183.14123814,
       118.22441474, 206.83827152, 147.78051842, 144.43367505,
       181.14423241, 128.0764493 , 174.97414443, 155.86488352,
       203.66002695, 183.14123814, 184.4408103 , 200.71007145,
       232.42759088, 235.04349037, 169.64600329, 119.08206953,
       120.37535851, 117.20758592, 137.38812993, 192.2654704 ,
       137.41326267, 169.64600329, 220.94088094, 182.87839155,
       134.64447234, 126.71090369, 139.38199406, 179.24461605,
       140.91090249,  86.39379797, 137.92848386, 142.89848344,
       142.89848344, 173.09337763,  81.71282492, 139.4961386 ,
       249.37962484, 179.66140067, 311.45644848, 232.75431652,
       256.61575992, 399.20846168, 113.14445942, 351.57249227,
       272.65044882, 331.14899843, 225.64489234, 227.33402199,
       266.32328122, 170.11724219, 179.66140067, 227.33402199,
       243.34253096, 415.99189683, 428.40956539, 188.49555922,
       284.18532985, 160.91656451, 415.99189683, 203.66002695,
       267.94957902, 325.72032632, 193.22051457, 190.93448231,
       240.2019855 , 314.86298211, 349.80168121, 418.27583469,
       240.2019855 , 211.97268112, 218.21083693, 378.73889114,
       232.75431652, 235.91266433, 180.95573685, 269.22820723,
       263.24870921, 254.2710846 , 179.66140067, 285.69224713,
       267.94957902, 244.44522998, 207.81635403, 230.069302  ,
       217.37307889, 185.91002846])
 

数据维度转换¶

volume原来是一维的,增加维度后,变为二维

In [11]:
volume = volume[:, np.newaxis]
volume
Out[11]:
array([[ 38.13265163],
       [ 35.20049849],
       [ 30.07237208],
       [ 33.23805027],
       [ 36.65191429],
       [ 51.91167701],
       [ 31.02218026],
       [ 39.26990817],
       [ 28.38324243],
       [ 37.71481981],
       [ 45.80442089],
       [ 38.60389053],
       [ 33.77840421],
       [ 21.29895099],
       [ 42.27327075],
       [ 51.03517266],
       [ 39.69716477],
       [ 38.13265163],
       [ 57.83986235],
       [ 40.85641246],
       [ 51.91167701],
       [ 40.85641246],
       [ 22.15870018],
       [ 46.30393412],
       [ 45.84212   ],
       [ 41.88790205],
       [ 41.88790205],
       [ 42.47433268],
       [ 39.6427105 ],
       [ 37.01215025],
       [ 38.60389053],
       [ 45.80442089],
       [ 42.47433268],
       [ 44.34881629],
       [ 37.71481981],
       [ 31.41592654],
       [ 41.1810437 ],
       [ 37.71481981],
       [ 26.35586797],
       [ 40.85641246],
       [ 34.03392041],
       [ 27.56747554],
       [ 26.35586797],
       [ 41.88790205],
       [ 51.75145578],
       [ 33.77840421],
       [ 43.58017329],
       [ 31.02218026],
       [ 44.12366882],
       [ 36.65191429],
       [241.16959604],
       [193.01945264],
       [244.29966952],
       [126.71090369],
       [203.52284408],
       [153.10551797],
       [195.34737279],
       [ 82.97260357],
       [209.83325652],
       [110.43326496],
       [ 91.62978573],
       [153.10237638],
       [150.79644737],
       [183.14123814],
       [118.22441474],
       [206.83827152],
       [147.78051842],
       [144.43367505],
       [181.14423241],
       [128.0764493 ],
       [174.97414443],
       [155.86488352],
       [203.66002695],
       [183.14123814],
       [184.4408103 ],
       [200.71007145],
       [232.42759088],
       [235.04349037],
       [169.64600329],
       [119.08206953],
       [120.37535851],
       [117.20758592],
       [137.38812993],
       [192.2654704 ],
       [137.41326267],
       [169.64600329],
       [220.94088094],
       [182.87839155],
       [134.64447234],
       [126.71090369],
       [139.38199406],
       [179.24461605],
       [140.91090249],
       [ 86.39379797],
       [137.92848386],
       [142.89848344],
       [142.89848344],
       [173.09337763],
       [ 81.71282492],
       [139.4961386 ],
       [249.37962484],
       [179.66140067],
       [311.45644848],
       [232.75431652],
       [256.61575992],
       [399.20846168],
       [113.14445942],
       [351.57249227],
       [272.65044882],
       [331.14899843],
       [225.64489234],
       [227.33402199],
       [266.32328122],
       [170.11724219],
       [179.66140067],
       [227.33402199],
       [243.34253096],
       [415.99189683],
       [428.40956539],
       [188.49555922],
       [284.18532985],
       [160.91656451],
       [415.99189683],
       [203.66002695],
       [267.94957902],
       [325.72032632],
       [193.22051457],
       [190.93448231],
       [240.2019855 ],
       [314.86298211],
       [349.80168121],
       [418.27583469],
       [240.2019855 ],
       [211.97268112],
       [218.21083693],
       [378.73889114],
       [232.75431652],
       [235.91266433],
       [180.95573685],
       [269.22820723],
       [263.24870921],
       [254.2710846 ],
       [179.66140067],
       [285.69224713],
       [267.94957902],
       [244.44522998],
       [207.81635403],
       [230.069302  ],
       [217.37307889],
       [185.91002846]])
 

计算示例¶

In [12]:
data
Out[12]:
array([[ 0,  1,  2,  3,  4],
       [ 5,  6,  7,  8,  9],
       [10, 11, 12, 13, 14]])
In [13]:
# 取第四列数据,每个数据+10
d = data[:,4] +10
d
Out[13]:
array([14, 19, 24])
In [14]:
d = d[:, np.newaxis]
d
Out[14]:
array([[14],
       [19],
       [24]])
 

数据纵向堆叠¶

  • np.hstack([data1, data2]), 把data2在列的方向上添加到data1上
  • np.vstack([data1, data2]), data2在行的方向上添加到data1上
In [15]:
np.hstack([data,d])
Out[15]:
array([[ 0,  1,  2,  3,  4, 14],
       [ 5,  6,  7,  8,  9, 19],
       [10, 11, 12, 13, 14, 24]])
 

查找数据¶

np.partition快速查找第n大的值

In [16]:
l=[3,4,5,2,1]
np.partition(l,kth=-4)
Out[16]:
array([1, 2, 5, 4, 3])
 

随机数¶

In [17]:
b = np.random.rand(10)
b
Out[17]:
array([0.68561367, 0.01516211, 0.53513034, 0.87153709, 0.06330117,
       0.86803698, 0.11665939, 0.11038331, 0.391967  , 0.94081668])
In [18]:
b = np.random.uniform(0,10,5)
b
Out[18]:
array([2.06978576, 7.1781825 , 2.43242777, 8.59237313, 7.29724532])
In [19]:
np.random.randn(5)
Out[19]:
array([ 0.00160165,  1.16678333, -0.67973919, -0.3598035 , -0.00290401])
In [20]:
np.random.randint(0,10,5)
Out[20]:
array([8, 9, 3, 2, 4])
In [21]:
np.random.randint(0,10,5)
Out[21]:
array([2, 3, 3, 2, 4])
In [22]:
np.random.seed(5)
np.random.randint(0,10,5)
Out[22]:
array([3, 6, 6, 0, 9])
In [23]:
np.random.seed(6)
np.random.randint(0,10,5)
Out[23]:
array([9, 3, 4, 0, 9])
In [24]:
np.random.uniform(10,100,10)
Out[24]:
array([18.9524523 , 64.76262553, 57.73568176, 53.45494409, 84.2688357 ,
       77.9511675 , 45.09780233, 31.58324146, 24.00535865, 75.18958605])
In [25]:
np.random.standard_normal(10)
Out[25]:
array([-0.05607706,  0.86108782,  1.02566315,  0.60680207,  1.97193091,
       -0.47434219, -0.39388112, -0.97062796, -1.02770216,  2.08217624])
In [26]:
np.linspace(1,20, 10)
Out[26]:
array([ 1.        ,  3.11111111,  5.22222222,  7.33333333,  9.44444444,
       11.55555556, 13.66666667, 15.77777778, 17.88888889, 20.        ])
In [27]:
np.arange(0,5)
Out[27]:
array([0, 1, 2, 3, 4])
In [28]:
a = np.arange(10).reshape(10)
a
Out[28]:
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
In [29]:
a+a
Out[29]:
array([ 0,  2,  4,  6,  8, 10, 12, 14, 16, 18])
In [30]:
a-a
Out[30]:
array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
In [31]:
a*a
Out[31]:
array([ 0,  1,  4,  9, 16, 25, 36, 49, 64, 81])
In [32]:
a/a
 
c:\python36\lib\site-packages\ipykernel_launcher.py:1: RuntimeWarning: invalid value encountered in true_divide
  """Entry point for launching an IPython kernel.
Out[32]:
array([nan,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.])
In [33]:
a.dot(a)
Out[33]:
285
In [34]:
a
Out[34]:
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
In [35]:
b = np.arange(12).reshape(3,4)
b
Out[35]:
array([[ 0,  1,  2,  3],
       [ 4,  5,  6,  7],
       [ 8,  9, 10, 11]])
In [36]:
b[0]
Out[36]:
array([0, 1, 2, 3])
In [37]:
b[:,0]
Out[37]:
array([0, 4, 8])
In [38]:
b[0,:]
Out[38]:
array([0, 1, 2, 3])
In [39]:
b[-1]
Out[39]:
array([ 8,  9, 10, 11])
In [40]:
b.size
Out[40]:
12
In [41]:
b.shape
Out[41]:
(3, 4)
 

数据拉平¶

将多维数据将为一维

  • ravel 方法是返回的视图,修改数据会影响原数据
  • flat 方法是返回原数据的copy,不会影响原数据
In [42]:
b.ravel()
Out[42]:
array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11])
In [43]:
[x for x in b.flat]
Out[43]:
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
In [44]:
b.flat[3]
Out[44]:
3
 

数据resize和reshape¶

  • reshape:有返回值,所谓有返回值,即不对原始多维数组进行修改;
  • resize:无返回值,所谓有返回值,即会对原始多维数组进行修改;
In [45]:
b.reshape(3,4)
Out[45]:
array([[ 0,  1,  2,  3],
       [ 4,  5,  6,  7],
       [ 8,  9, 10, 11]])
In [46]:
c = b.resize(3,4)
c
In [47]:
b
Out[47]:
array([[ 0,  1,  2,  3],
       [ 4,  5,  6,  7],
       [ 8,  9, 10, 11]])
 

arr平均值¶

np.mean(d, axis=0),计算平均值,axis来指定按那个轴运算

In [48]:
np.mean(b, axis=0)
Out[48]:
array([4., 5., 6., 7.])
In [49]:
a = np.array([1,0.2,3])
In [50]:
a
Out[50]:
array([1. , 0.2, 3. ])
In [51]:
a.dtype
Out[51]:
dtype('float64')
In [52]:
np.arange(10)
Out[52]:
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
 

特殊矩阵¶

In [53]:
np.ones((5,5))
Out[53]:
array([[1., 1., 1., 1., 1.],
       [1., 1., 1., 1., 1.],
       [1., 1., 1., 1., 1.],
       [1., 1., 1., 1., 1.],
       [1., 1., 1., 1., 1.]])
In [54]:
b
Out[54]:
array([[ 0,  1,  2,  3],
       [ 4,  5,  6,  7],
       [ 8,  9, 10, 11]])
In [55]:
np.ones_like(b)
Out[55]:
array([[1, 1, 1, 1],
       [1, 1, 1, 1],
       [1, 1, 1, 1]])
In [56]:
np.identity(4)
Out[56]:
array([[1., 0., 0., 0.],
       [0., 1., 0., 0.],
       [0., 0., 1., 0.],
       [0., 0., 0., 1.]])
In [57]:
np.eye(4)
Out[57]:
array([[1., 0., 0., 0.],
       [0., 1., 0., 0.],
       [0., 0., 1., 0.],
       [0., 0., 0., 1.]])
 

取数据示例¶

In [58]:
a
Out[58]:
array([1. , 0.2, 3. ])
In [59]:
b
Out[59]:
array([[ 0,  1,  2,  3],
       [ 4,  5,  6,  7],
       [ 8,  9, 10, 11]])
In [60]:
b[:,2]
Out[60]:
array([ 2,  6, 10])
In [61]:
# 布尔值取数据
b[b>3]
Out[61]:
array([ 4,  5,  6,  7,  8,  9, 10, 11])
In [62]:
b
Out[62]:
array([[ 0,  1,  2,  3],
       [ 4,  5,  6,  7],
       [ 8,  9, 10, 11]])
In [63]:
b[b==3]
Out[63]:
array([3])
In [64]:
b
Out[64]:
array([[ 0,  1,  2,  3],
       [ 4,  5,  6,  7],
       [ 8,  9, 10, 11]])
In [65]:
c = b[b!=3]
In [66]:
c
Out[66]:
array([ 0,  1,  2,  4,  5,  6,  7,  8,  9, 10, 11])
 

花式索引¶

取特定的行,并按照指定顺序排序

In [67]:
b
Out[67]:
array([[ 0,  1,  2,  3],
       [ 4,  5,  6,  7],
       [ 8,  9, 10, 11]])
In [68]:
b[[2,0,1]]
Out[68]:
array([[ 8,  9, 10, 11],
       [ 0,  1,  2,  3],
       [ 4,  5,  6,  7]])
In [69]:
b = np.arange(32).reshape(8,4)
b
Out[69]:
array([[ 0,  1,  2,  3],
       [ 4,  5,  6,  7],
       [ 8,  9, 10, 11],
       [12, 13, 14, 15],
       [16, 17, 18, 19],
       [20, 21, 22, 23],
       [24, 25, 26, 27],
       [28, 29, 30, 31]])
In [70]:
b[[1,5,7,2],[0,3,1,2]]
Out[70]:
array([ 4, 23, 29, 10])
In [71]:
b[[1,5,7,2]][:,[0,3,1,2]]
Out[71]:
array([[ 4,  7,  5,  6],
       [20, 23, 21, 22],
       [28, 31, 29, 30],
       [ 8, 11,  9, 10]])
In [72]:
b
Out[72]:
array([[ 0,  1,  2,  3],
       [ 4,  5,  6,  7],
       [ 8,  9, 10, 11],
       [12, 13, 14, 15],
       [16, 17, 18, 19],
       [20, 21, 22, 23],
       [24, 25, 26, 27],
       [28, 29, 30, 31]])
 

矩阵转置,轴向交换¶

In [73]:
b.swapaxes(0,1)
Out[73]:
array([[ 0,  4,  8, 12, 16, 20, 24, 28],
       [ 1,  5,  9, 13, 17, 21, 25, 29],
       [ 2,  6, 10, 14, 18, 22, 26, 30],
       [ 3,  7, 11, 15, 19, 23, 27, 31]])
In [74]:
b.swapaxes(1,0)
Out[74]:
array([[ 0,  4,  8, 12, 16, 20, 24, 28],
       [ 1,  5,  9, 13, 17, 21, 25, 29],
       [ 2,  6, 10, 14, 18, 22, 26, 30],
       [ 3,  7, 11, 15, 19, 23, 27, 31]])
In [75]:
b
Out[75]:
array([[ 0,  1,  2,  3],
       [ 4,  5,  6,  7],
       [ 8,  9, 10, 11],
       [12, 13, 14, 15],
       [16, 17, 18, 19],
       [20, 21, 22, 23],
       [24, 25, 26, 27],
       [28, 29, 30, 31]])
 

矩阵运算示例¶

In [76]:
np.add(b,b)
Out[76]:
array([[ 0,  2,  4,  6],
       [ 8, 10, 12, 14],
       [16, 18, 20, 22],
       [24, 26, 28, 30],
       [32, 34, 36, 38],
       [40, 42, 44, 46],
       [48, 50, 52, 54],
       [56, 58, 60, 62]])
In [77]:
np.sort(b[[2,0,1]])
Out[77]:
array([[ 8,  9, 10, 11],
       [ 0,  1,  2,  3],
       [ 4,  5,  6,  7]])
In [78]:
b[[2,0,1]]
Out[78]:
array([[ 8,  9, 10, 11],
       [ 0,  1,  2,  3],
       [ 4,  5,  6,  7]])
In [79]:
np.linalg.eig(b[0:4])
Out[79]:
(array([ 3.24642492e+01, -2.46424920e+00,  2.14966418e-15, -1.17200157e-16]),
 array([[-0.11417645,  0.7327781 , -0.40377562,  0.05533605],
        [-0.3300046 ,  0.28974835,  0.81421492,  0.33237807],
        [-0.54583275, -0.15328139, -0.41710299, -0.83076428],
        [-0.76166089, -0.59631113,  0.00666369,  0.44305017]]))
In [80]:
np.linalg.det
Out[80]:
In [81]:
b
Out[81]:
array([[ 0,  1,  2,  3],
       [ 4,  5,  6,  7],
       [ 8,  9, 10, 11],
       [12, 13, 14, 15],
       [16, 17, 18, 19],
       [20, 21, 22, 23],
       [24, 25, 26, 27],
       [28, 29, 30, 31]])
In [82]:
b[0:4]
Out[82]:
array([[ 0,  1,  2,  3],
       [ 4,  5,  6,  7],
       [ 8,  9, 10, 11],
       [12, 13, 14, 15]])
In [83]:
np.random.randn(10)
Out[83]:
array([-0.8099314 ,  0.14817512, -0.69258651, -0.67934563, -0.15739984,
        1.2889822 ,  1.45221671,  0.2307127 ,  0.88579292,  0.96533113])
In [84]:
np.random.uniform(0,1,10)
Out[84]:
array([0.79782627, 0.88189639, 0.82160469, 0.30594132, 0.74537515,
       0.38821925, 0.46966765, 0.71004438, 0.36512663, 0.92725033])
In [85]:
np.random.random(5)
Out[85]:
array([0.73218711, 0.24383709, 0.67966439, 0.0595789 , 0.85628818])
In [86]:
w = 5000
n = 1000
draws = np.random.randint(0,2,size=(w,n))
draws
Out[86]:
array([[0, 0, 1, ..., 0, 1, 1],
       [1, 0, 1, ..., 0, 1, 1],
       [0, 0, 0, ..., 0, 0, 0],
       ...,
       [1, 0, 1, ..., 0, 0, 0],
       [0, 1, 1, ..., 1, 0, 0],
       [0, 1, 0, ..., 0, 0, 0]])
In [87]:
steps = np.where(draws > 0, 1, -1)
steps
Out[87]:
array([[-1, -1,  1, ..., -1,  1,  1],
       [ 1, -1,  1, ..., -1,  1,  1],
       [-1, -1, -1, ..., -1, -1, -1],
       ...,
       [ 1, -1,  1, ..., -1, -1, -1],
       [-1,  1,  1, ...,  1, -1, -1],
       [-1,  1, -1, ..., -1, -1, -1]])
In [88]:
# 累计求和
walks = steps.cumsum(1)
walks
Out[88]:
array([[ -1,  -2,  -1, ...,  16,  17,  18],
       [  1,   0,   1, ..., -34, -33, -32],
       [ -1,  -2,  -3, ..., -24, -25, -26],
       ...,
       [  1,   0,   1, ...,  56,  55,  54],
       [ -1,   0,   1, ..., -34, -35, -36],
       [ -1,   0,  -1, ...,   0,  -1,  -2]], dtype=int32)



你可能感兴趣的:(numpy 基础操作)