欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

numpy 基础操作

程序员文章站 2022-06-27 17:01:07
Numpy 基础操作¶ 以numpy的基本数据例子来学习numpy基本数据处理方法 主要内容有: 创建数组 数组维度转换 数据选区和切片 数组数据计算 随机数 数据合并 数据统计计算 In [1]: import numpy as np 创建一维数组¶ In [2]: data = np.arang ......

 

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]]))
(0)
打赏 numpy 基础操作 微信扫一扫

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

numpy 基础操作
验证码: numpy 基础操作