python学习——numpy

numpy学习

In [15]:
import numpy as np
In [16]:
np.__version__
Out[16]:
'1.16.2'
In [17]:
#pyplot 显示画图,数据分析与可视化
import matplotlib.pyplot as plt
In [18]:
cat = plt.imread('111.jpg')
In [19]:
type(cat)
Out[19]:
numpy.ndarray
In [20]:
cat2 = cat - 15
plt.imshow(cat2)
plt.show()
 
 

一、创建ndarray

In [30]:
n1 = np.array([3,1,4,5])
n1
Out[30]:
array([3, 1, 4, 5])
In [31]:
n2 = np.array([[2,3,4,5],[4,5,6,2],[3,5,2,4]])
n2
Out[31]:
array([[2, 3, 4, 5],
       [4, 5, 6, 2],
       [3, 5, 2, 4]])
In [32]:
n2.shape
Out[32]:
(3, 4)
In [34]:
n1.shape
Out[34]:
(4,)
In [35]:
#任何一张2维的图片转化成数据3维数组,长宽,最后为颜色
cat.shape
Out[35]:
(576, 1024, 3)
In [38]:
n3 = np.array(list('ABC'))
n3
Out[38]:
array(['A', 'B', 'C'], dtype='<U1')
In [39]:
type(n3)
Out[39]:
numpy.ndarray
 

numpy默认ndarray的所有元素类型是相同的

如果传进来的列表中包含不同的类型,则统一为同一类型,优先级:str>float>int

In [41]:
n4 = np.array([1,2,14,'python'])
n4
Out[41]:
array(['1', '2', '14', 'python'], dtype='<U11')
 

2.使用np的routines函数创建

 

包含以下常见创建方法:

1) np.ones(shape, dtype=None, order='C')

In [42]:
#1
np.ones(shape=(10,8),dtype=int)
Out[42]:
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, 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],
       [1, 1, 1, 1, 1, 1, 1, 1]])
In [45]:
ones = np.ones(shape=(100,80,3),dtype=float)
plt.imshow(ones)
plt.show()
 
In [46]:
#2
np.zeros((4,4))
Out[46]:
array([[0., 0., 0., 0.],
       [0., 0., 0., 0.],
       [0., 0., 0., 0.],
       [0., 0., 0., 0.]])
In [47]:
#3
np.full((10,10),1024)
Out[47]:
array([[1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024],
       [1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024],
       [1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024],
       [1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024],
       [1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024],
       [1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024],
       [1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024],
       [1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024],
       [1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024],
       [1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024]])
In [49]:
#4 np.eye()满秩矩阵-没法进行运算
#x + y = 10
#2x + 2Y = 20

#[[1 1],[2 2]]
np.eye(10)
Out[49]:
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.]])
In [12]:
#5 lin = linear线性 创建等差数列
import numpy as np
np.linspace(0,100,20,endpoint=False)
Out[12]:
array([ 0.,  5., 10., 15., 20., 25., 30., 35., 40., 45., 50., 55., 60.,
       65., 70., 75., 80., 85., 90., 95.])
In [4]:
#6 np.arange([start, ]stop, [step, ]dtype=None)
np.arange(0,100,5)
Out[4]:
array([ 0,  5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 65, 70, 75, 80,
       85, 90, 95])
In [57]:
# 左闭右开
np.arange(0,10,50)
Out[57]:
array([0])
In [63]:
#7 随机数
np.random.randint(0,150,5)
Out[63]:
array([115, 125, 119, 122,  89])
In [68]:
#8 正态分布
np.random.randn(100)
Out[68]:
array([ 0.52006589, -1.09873937,  1.05361817, -0.57494794,  0.27141772,
       -1.09256515,  0.10203024,  0.97401859,  0.91182792, -0.43598098,
       -1.11909752,  1.14323918,  1.57521503,  1.25521442,  0.16472707,
        0.2482946 ,  1.08598904, -1.34079893,  0.02396836,  1.37280644,
        0.2015556 ,  0.15661258, -0.38075694, -0.03967491,  0.78603723,
       -1.06296299, -1.43675823,  1.81449513,  1.00346366,  0.01438498,
        0.25089506, -0.49369501, -0.9268509 , -1.84332164, -0.58969243,
        0.52262433,  0.05335362,  2.21829767,  1.37582986,  0.45700643,
       -0.11100864, -0.41265265,  1.32230018,  0.24694395, -1.484009  ,
        2.00596245, -0.14211874,  0.17334688, -0.73926507,  0.32722889,
       -0.46983798, -2.00759935,  0.86147114,  0.32705171,  1.51186177,
       -0.93077223, -1.3824234 ,  2.32272714, -0.40145502, -0.276796  ,
       -0.94055107, -0.08786742,  0.36468672,  0.0985706 , -0.88695209,
       -1.41602216, -0.60214988, -1.77314175,  0.74323097, -0.59342127,
        0.68654509, -1.09238856, -1.39532531,  0.58028488, -1.3520836 ,
       -0.28492456,  0.56942344,  0.97445869, -2.02273723, -0.81899945,
        0.17377014, -0.27394438,  0.54250356, -0.29460708,  1.54034457,
        0.83307592, -1.2667477 ,  0.9899686 ,  0.09287596,  0.77116237,
       -0.73176434, -0.26038515,  0.30993488,  0.26784   ,  0.20432443,
        0.09346971,  0.98769319,  0.39555872,  0.92552326, -0.96064058])
In [71]:
np.random.normal(175,10,100)
Out[71]:
array([176.31311036, 178.47299348, 165.98566908, 174.57242315,
       173.86597346, 173.36308003, 178.3823674 , 165.06364507,
       166.75641579, 173.69097889, 197.57931081, 157.07269486,
       186.85707502, 183.92219397, 174.47005553, 175.40713432,
       166.09844153, 170.04422502, 178.01839064, 172.37480441,
       166.09987592, 164.75725321, 167.03479952, 165.61741745,
       167.38446165, 183.20381858, 193.11730726, 190.50314927,
       170.1462006 , 153.90400513, 166.63380454, 166.40823629,
       177.38624191, 183.78027975, 187.98651342, 176.27416282,
       178.50363467, 184.42434489, 157.88701256, 187.33843892,
       185.5776641 , 173.71721448, 177.83437849, 171.09466349,
       172.50632727, 187.52951972, 174.79371839, 167.66599417,
       177.82945589, 191.3025823 , 170.45700987, 170.07074703,
       162.81410289, 190.52134034, 172.15779473, 170.02757995,
       172.8746392 , 168.41721058, 191.11153491, 177.12867297,
       169.67830767, 183.23631818, 176.55383454, 171.50487241,
       171.64754482, 176.11636769, 195.88581616, 186.169188  ,
       180.2728419 , 180.31310914, 181.59493533, 160.37958336,
       182.47162835, 181.37164427, 155.73629377, 168.35990735,
       159.01528429, 163.63055934, 178.96237585, 177.25279096,
       168.52926717, 182.11196487, 172.69912774, 179.75671516,
       159.56687573, 192.0032421 , 175.19717678, 159.56336486,
       192.95694268, 159.33389645, 179.73469649, 179.90865899,
       175.92073453, 176.58322321, 181.61182465, 189.89283024,
       188.23056428, 161.27702846, 173.41217292, 168.2844078 ])
In [73]:
#9 生成0到1的随机数,左闭右开
np.random.random(100)
Out[73]:
array([0.35500057, 0.24821965, 0.23127503, 0.32292363, 0.91818277,
       0.42703218, 0.36135354, 0.400161  , 0.7534726 , 0.98052483,
       0.10848953, 0.42794136, 0.23265171, 0.8443204 , 0.98392584,
       0.47148276, 0.61674913, 0.55576905, 0.84215495, 0.97307415,
       0.8708647 , 0.82077792, 0.18404943, 0.69703121, 0.61843342,
       0.55472582, 0.48570259, 0.84203674, 0.04890228, 0.09827949,
       0.91943219, 0.9771919 , 0.96382215, 0.9593143 , 0.09203402,
       0.51089096, 0.52454018, 0.70573205, 0.7632323 , 0.94467323,
       0.60679046, 0.47703729, 0.13630913, 0.45559752, 0.23799321,
       0.96415317, 0.28017608, 0.60484936, 0.82097114, 0.98289851,
       0.03740737, 0.80973572, 0.01193012, 0.6106763 , 0.56165642,
       0.47521364, 0.9232115 , 0.10333039, 0.56769135, 0.10744189,
       0.14477555, 0.42492207, 0.62348672, 0.8479516 , 0.78275798,
       0.94315799, 0.66116696, 0.21631206, 0.85700231, 0.57075997,
       0.60499368, 0.28894867, 0.57189681, 0.9084584 , 0.35467943,
       0.31728516, 0.03873277, 0.49106552, 0.30534249, 0.02392965,
       0.5579801 , 0.30190183, 0.19398032, 0.88013875, 0.60863871,
       0.32366776, 0.56950944, 0.05494634, 0.65207076, 0.84706947,
       0.42638488, 0.45242548, 0.9398203 , 0.24042839, 0.09649421,
       0.88870792, 0.07219425, 0.73769056, 0.64121422, 0.68340105])
In [79]:
#随机生成一张图片
r = np.random.random((200,300,3))
plt.imshow(r)
plt.show()
 
 

二、ndarray的属性

4个必记参数: ndim:维度 shape:形状(各维度的长度) size:总长度 dtype:元素类型

In [21]:
cat.ndim
Out[21]:
3
In [22]:
cat.shape
Out[22]:
(576, 1024, 3)
In [23]:
cat.size
Out[23]:
1769472
In [24]:
cat.dtype
Out[24]:
dtype('uint8')
 

三、ndarray的基本操作

 

1.索引

与列表完全一致

In [85]:
n1 = np.random.randint(0,100,(3,4,5))
n1
Out[85]:
array([[[13, 10, 62, 10, 74],
        [44, 13,  7, 80, 93],
        [20, 92, 94,  5, 60],
        [27, 38, 34, 20, 58]],

       [[59, 89, 84,  2, 54],
        [ 6, 32, 19, 47, 47],
        [41, 11, 66, 41, 27],
        [52, 48, 17, 71, 57]],

       [[52, 77, 46, 50,  1],
        [24, 89, 20, 14, 45],
        [99, 42,  2, 48, 51],
        [93, 32,  0, 18, 60]]])
In [86]:
n1[0,0,1]
Out[86]:
10
 

2.切片

切片时左闭右开

In [89]:
n2 = np.random.randint(150,size=10)
n2
Out[89]:
array([ 13, 107, 112,  84,  46,  43,  99,  95, 106, 119])
In [91]:
n2[:5]
Out[91]:
array([ 13, 107, 112,  84,  46])
In [92]:
n1.shape
Out[92]:
(3, 4, 5)
In [103]:
n1[0:2,1:3,-2:]
Out[103]:
array([[[80, 93],
        [ 5, 60]],

       [[47, 47],
        [41, 27]]])
 

将数据反转

In [27]:
n3 = np.arange(0,10,1)
n3
Out[27]:
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
In [29]:
n3[::-1]
Out[29]:
array([9, 8, 7, 6, 5, 4, 3, 2, 1, 0])
In [28]:
n3[::-2]
Out[28]:
array([9, 7, 5, 3, 1])
 

3.变形

使用reshape函数,注意参数是一个tuple

In [112]:
n3.shape
Out[112]:
(10,)
In [113]:
n3.reshape((5,2))
Out[113]:
array([[0, 1],
       [2, 3],
       [4, 5],
       [6, 7],
       [8, 9]])
In [116]:
# 上面图片111.jpg 进行reshape
cat.shape
Out[116]:
(576, 1024, 3)
In [117]:
cat.reshape(576*1024*3)
Out[117]:
array([ 99, 185, 242, ...,  26,  32,  44], dtype=uint8)
In [118]:
#如果是复数直接转换成一维数组ndarray
cat.reshape(-1)
Out[118]:
array([ 99, 185, 242, ...,  26,  32,  44], dtype=uint8)
 

4.级联

1.np.concatenate()级联需要注意:

2.级联的参数是列表:一定要加中括号或小括号

3.维度必须相同

4.形状相符

5.重点:级联的方向默认是shape这个tuple的第一个值锁代表的维度方向

6.可通过axis参数改变级联的方向

In [121]:
n1 = np.random.randint(0,10,size=(5,5))
n1
Out[121]:
array([[4, 6, 2, 6, 5],
       [5, 0, 1, 4, 0],
       [8, 5, 9, 4, 1],
       [1, 4, 7, 1, 7],
       [5, 6, 3, 6, 5]])
In [124]:
n1.ndim#查看维度
Out[124]:
2
In [123]:
np.concatenate((n1,n1),axis=0)
Out[123]:
array([[4, 6, 2, 6, 5],
       [5, 0, 1, 4, 0],
       [8, 5, 9, 4, 1],
       [1, 4, 7, 1, 7],
       [5, 6, 3, 6, 5],
       [4, 6, 2, 6, 5],
       [5, 0, 1, 4, 0],
       [8, 5, 9, 4, 1],
       [1, 4, 7, 1, 7],
       [5, 6, 3, 6, 5]])
In [125]:
np.concatenate((n1,n1),axis=1)
Out[125]:
array([[4, 6, 2, 6, 5, 4, 6, 2, 6, 5],
       [5, 0, 1, 4, 0, 5, 0, 1, 4, 0],
       [8, 5, 9, 4, 1, 8, 5, 9, 4, 1],
       [1, 4, 7, 1, 7, 1, 4, 7, 1, 7],
       [5, 6, 3, 6, 5, 5, 6, 3, 6, 5]])
In [127]:
plt.imshow(cat)
plt.show()
 
In [128]:
cats = np.concatenate((cat,cat))
In [129]:
plt.imshow(cats)
plt.show()
 
 

np.hstack与np.vstack 水平级联与垂直级联,处理自己,进行纬度的变更

In [130]:
n2
Out[130]:
array([ 13, 107, 112,  84,  46,  43,  99,  95, 106, 119])
In [131]:
#vertical 垂直
n3 = np.vstack(n2)
Out[131]:
array([[ 13],
       [107],
       [112],
       [ 84],
       [ 46],
       [ 43],
       [ 99],
       [ 95],
       [106],
       [119]])
In [133]:
n4 = np.array([[2,3,4,5,6,7],[4,5,6,7,8,9]])
np.hstack(n4)
Out[133]:
array([2, 3, 4, 5, 6, 7, 4, 5, 6, 7, 8, 9])
In [135]:
np.hstack(np.hstack(cat))
Out[135]:
array([ 99, 185, 242, ...,  26,  32,  44], dtype=uint8)
 

切分

与级联类似,三个函数完成切分工作:

1.np.split   axis=0默认切分行;axis=1默认切分列
2.np.vsplit竖直方向切分的是行
3.np.hsplit
In [136]:
cat.shape
Out[136]:
(576, 1024, 3)
In [30]:
n5 = np.random.randint(0,150,size=(5,7))
n5
Out[30]:
array([[ 25, 147,  79, 133,   8,  14, 102],
       [ 39,  20,  37, 114,  94,   5,  14],
       [ 88, 112, 139,  84,  69,   7,  17],
       [ 30,  30, 121,  74,  50,  77, 131],
       [ 82, 115,  30,  21,  33,  66, 123]])
In [143]:
np.split(n5,(1,3))
Out[143]:
[array([[ 74,  17,  77, 135, 120,   2,  53]]),
 array([[ 90, 132, 133,  26,  20, 118,  77],
        [ 69,  20,  60,  84, 106, 108,  41]]),
 array([[101,  55, 100,  27,  99, 108,   7],
        [110,  75,  23,  17, 108,  84, 126]])]
In [144]:
np.split(n5,(1,3))[0]
Out[144]:
array([[ 74,  17,  77, 135, 120,   2,  53]])
In [145]:
cat2 = np.split(cat,(200,350))[1]
plt.imshow(cat2)
plt.show()
 
In [146]:
# axis=0默认切分行
# axis=1默认切分列
np.split(n5,(1,3),axis=1)
Out[146]:
[array([[ 74],
        [ 90],
        [ 69],
        [101],
        [110]]), array([[ 17,  77],
        [132, 133],
        [ 20,  60],
        [ 55, 100],
        [ 75,  23]]), array([[135, 120,   2,  53],
        [ 26,  20, 118,  77],
        [ 84, 106, 108,  41],
        [ 27,  99, 108,   7],
        [ 17, 108,  84, 126]])]
In [147]:
#v 竖直方向切分的是行
np.vsplit(n5,(1,3))
Out[147]:
[array([[ 74,  17,  77, 135, 120,   2,  53]]),
 array([[ 90, 132, 133,  26,  20, 118,  77],
        [ 69,  20,  60,  84, 106, 108,  41]]),
 array([[101,  55, 100,  27,  99, 108,   7],
        [110,  75,  23,  17, 108,  84, 126]])]
In [148]:
np.hsplit(n5,(1,3))
Out[148]:
[array([[ 74],
        [ 90],
        [ 69],
        [101],
        [110]]), array([[ 17,  77],
        [132, 133],
        [ 20,  60],
        [ 55, 100],
        [ 75,  23]]), array([[135, 120,   2,  53],
        [ 26,  20, 118,  77],
        [ 84, 106, 108,  41],
        [ 27,  99, 108,   7],
        [ 17, 108,  84, 126]])]
 

6.副本

所有赋值运算不会为ndarray的任何元素创建副本。对赋值后的对象的操作也对原来的对象生效。
In [152]:
l = [1,2,3,4]
n = np.array(l)
n
Out[152]:
array([1, 2, 3, 4])
In [157]:
n[2] = 512
n,l
Out[157]:
(array([  1,   2, 512,   4]), [1, 2, 3, 4])
In [164]:
#当我们的数据是ndarray时,如果我们用=赋值,内存没有改变
n2 = n
print(n2,n)
 
[  1   2 512   4] [  1   2 512   4]
 

可以使用copy()函数创建副本

In [163]:
n3 = n.copy()
n3[0] = 1024
print(n3,n)
 
[1024    2  512    4] [  1   2 512   4]
 

四、ndarray的聚合操作

 

1.求和np.sum

In [167]:
#cat求最大值、最小值:display和print相同都可展示内容
print(cat.max(),cat.min())
display(cat.max(),cat.min())
 
255 0
 
255
 
0
In [187]:
#(4,4,4) ==0, 1, 2
n = np.random.randint(0,150,size=(4,4,4))
n
Out[187]:
array([[[  3,  76,  77,  19],
        [ 26,  69,  81, 120],
        [ 81,  37, 123, 138],
        [ 52,  50,  15,  23]],

       [[  7,  43, 137, 123],
        [ 48,  95,  96,  34],
        [ 25,  15, 119,  74],
        [ 11,  21, 130,   9]],

       [[123, 137,  27,  80],
        [ 31, 147,  27, 146],
        [ 83,  89, 147, 106],
        [ 79, 147,  92,  57]],

       [[ 17, 133,  39,   6],
        [ 64, 147,  27,  10],
        [ 52,  72,  26,  37],
        [125,  20,  52,  11]]])
In [202]:
#0 代表行每个数组对应位置
n.max(axis=0)
Out[202]:
array([[123, 137, 137, 123],
       [ 64, 147,  96, 146],
       [ 83,  89, 147, 138],
       [125, 147, 130,  57]])
In [204]:
#1 代表大数组下一层数组
n.max(axis=1)
Out[204]:
array([[ 77, 120, 138,  52],
       [137,  96, 119, 130],
       [137, 147, 147, 147],
       [133, 147,  72, 125]])
In [205]:
#2 代表大数组下两层数组
n.max(axis=2)
Out[205]:
array([[ 77, 120, 138,  52],
       [137,  96, 119, 130],
       [137, 147, 147, 147],
       [133, 147,  72, 125]])
 

2.求平均值np.mean() 、求和np.sum()

In [206]:
np.mean(n,axis=0)
Out[206]:
array([[ 37.5 ,  97.25,  70.  ,  57.  ],
       [ 42.25, 114.5 ,  57.75,  77.5 ],
       [ 60.25,  53.25, 103.75,  88.75],
       [ 66.75,  59.5 ,  72.25,  25.  ]])
In [207]:
np.sum(n,axis=0)
Out[207]:
array([[150, 389, 280, 228],
       [169, 458, 231, 310],
       [241, 213, 415, 355],
       [267, 238, 289, 100]])
 

np.std方差/标准差

np.argmax最大值索引

In [222]:
import numpy as np
n = np.random.randint(0,100,size=10)
print(n)
n.argmin()
 
[57 49 48  5 72 64  8 93 51 15]
Out[222]:
3
In [225]:
np.argmin(n)
Out[225]:
3
In [234]:
index = np.argwhere(n>50)
index
Out[234]:
array([[0],
       [4],
       [5],
       [7],
       [8]], dtype=int64)
In [235]:
n[index]
Out[235]:
array([[57],
       [72],
       [64],
       [93],
       [51]])
In [237]:
n[np.array([[0],[1]])]
Out[237]:
array([[57],
       [49]])
 

操作文件

使用pandas打开文件  获取文件中的数据
In [241]:
import pandas as pd
df = pd.read_csv('../data/president_heights.csv')
df
Out[241]:
 order name height
0 1 gorge 189
1 2 james 170
2 3 bob 163
3 4 jim 183
4 5 lucy 171
5 6 lilei 192
In [246]:
df.values
Out[246]:
array([['    1      gorge      189'],
       ['    2      james      170'],
       ['    3      bob       163'],
       ['    4      jim       183'],
       ['    5      lucy       171'],
       ['    6      lilei      192']], dtype=object)
 

五、ndarray的矩阵操作

1.基本矩阵操作

1)算数运算符:

·加减乘除
In [247]:
import numpy as np
In [248]:
n = np.random.randint(0,10,size=(4,5))
n
Out[248]:
array([[6, 2, 9, 6, 9],
       [1, 2, 3, 8, 3],
       [6, 6, 7, 2, 1],
       [2, 3, 1, 3, 1]])
In [249]:
n+10
Out[249]:
array([[16, 12, 19, 16, 19],
       [11, 12, 13, 18, 13],
       [16, 16, 17, 12, 11],
       [12, 13, 11, 13, 11]])
In [251]:
n2 = n/2
n2
Out[251]:
array([[3. , 1. , 4.5, 3. , 4.5],
       [0.5, 1. , 1.5, 4. , 1.5],
       [3. , 3. , 3.5, 1. , 0.5],
       [1. , 1.5, 0.5, 1.5, 0.5]])
In [252]:
np.add(n,n)
Out[252]:
array([[12,  4, 18, 12, 18],
       [ 2,  4,  6, 16,  6],
       [12, 12, 14,  4,  2],
       [ 4,  6,  2,  6,  2]])
 
2)矩阵积np.dot()
In [254]:
n1 = np.random.randint(0,10,(2,3))
n2 = np.random.randint(0,10,(3,2))
display(n1,n2)
 
array([[1, 7, 2],
       [2, 1, 8]])
 
array([[9, 6],
       [4, 9],
       [4, 5]])
In [255]:
np.dot(n1,n2)
Out[255]:
array([[45, 79],
       [54, 61]])
In [257]:
np.dot(n2,n1)
Out[257]:
array([[21, 69, 66],
       [22, 37, 80],
       [14, 33, 48]])
 

2.广播机制

【重要】ndarray广播机制的两条规则

规则1:为缺失维度补1

规则2:假定缺失元素用已有值填充

In [ ]:
例1m = np.ones((2,3))   a = np.arange(3)   求m + a
In [260]:
m = np.ones((2,3))
a = np.arange(3)
print(m,a)
display(m,a)
 
[[1. 1. 1.]
 [1. 1. 1.]] [0 1 2]
 
array([[1., 1., 1.],
       [1., 1., 1.]])
 
array([0, 1, 2])
In [261]:
#numpy的广播机制,维度不对应,自动进行不全
m + a
Out[261]:
array([[1., 2., 3.],
       [1., 2., 3.]])
In [ ]:
例2a = np.arange(3).reshape((3,1))   b = np.arange(3) 求a+b
In [262]:
a = np.arange(3).reshape((3,1))
b = np.arange(3)
display(a,b)
 
array([[0],
       [1],
       [2]])
 
array([0, 1, 2])
In [263]:
a + b
Out[263]:
array([[0, 1, 2],
       [1, 2, 3],
       [2, 3, 4]])
 

六、ndarray的排序

小测试: 使用以上所学numpy的知识,对一个ndarray对象进行选择排序 def sort(x): 代码越短越好

In [275]:
n1 = np.array([2,5,1,7,4])
In [265]:
n1.size
Out[265]:
5
In [266]:
#冒泡排序
def sort(nd):
    for i in range(nd.size):
        for j in range(i,nd.size):
            if nd[i]>nd[j]:
                nd[i],nd[j] = nd[j],nd[i]
    return nd
In [271]:
sort(n1)
Out[271]:
array([1, 2, 4, 5, 7])
In [281]:
#为了降低运算的空间和时间复杂度,用一层for实现冒泡排序
def sortd(nd):
    for i in range(nd.size):
        #切片,索引不对应
        min_index = np.argmin(nd[i:]) + i
        nd[i],nd[min_index] = nd[min_index],nd[i]
    return nd
In [282]:
sortd(n1)
Out[282]:
array([1, 2, 4, 5, 7])
 

1.快速排序

np.sort()与ndarray.sort()都可以,但有区别:
·np.sort()不改变输入
·ndarray.sort()本地处理,不占用空间,但改变输入
In [287]:
n1 = np.random.randint(0,150,10)
n1
Out[287]:
array([ 94,  64,  38,  13,   5,  56, 127,  87,  12, 106])
In [294]:
#使用ndarray.sort(),原来的数据进行了改变,不占用内存
n1.sort()
n1
Out[294]:
array([  5,  12,  13,  38,  56,  64,  87,  94, 106, 127])
In [292]:
n2 = np.sort(n1)
display(n2,n1)
 
array([  5,  12,  13,  38,  56,  64,  87,  94, 106, 127])
 
array([ 94,  64,  38,  13,   5,  56, 127,  87,  12, 106])
 

2.部分排序

np.partition(a,k)

有时候我们不是对全部数据感兴趣,我们可能只对最小或最大的一部分感兴趣。

当k为正时,我们想要得到最小的k个数

当k为负时,我们想要得到最大的k个数

In [296]:
nd = np.random.randint(0,150,20)
nd
Out[296]:
array([ 46, 137,   3,  69, 136, 121,  94, 120, 118,  90,  62,  89,   8,
        90,  23, 113,  53, 129,  33,  89])
In [297]:
np.partition(nd,-5)
Out[297]:
array([ 62,  46,   3,  69,  33,  53,  23,   8,  89,  89,  90,  90,  94,
       113, 118, 120, 121, 129, 136, 137])
In [298]:
np.partition(nd,5)
Out[298]:
array([  8,   3,  23,  33,  46,  53,  62,  69,  89,  89, 137, 118, 120,
        90,  94, 113, 121, 129, 136,  90])
posted @ 2019-09-11 11:41  陪伴is最长情的告白  阅读(286)  评论(0编辑  收藏  举报