导入numpy库,一般设置别名为np
import numpy as np
np.__version__
'1.18.1'
创建ndarray
n dimension array
- n维矩阵
1、使用np.array()由python list创建
li = [1, 2, 3, 4, 5]
n = np.array(li)
display(li, n) # li是列表,但是n是array
[1, 2, 3, 4, 5]
array([1, 2, 3, 4, 5])
li1 = [1.0, 2, 3, 4, 5]
li2 = ['1', 2, 3, 4, 5]
n1 = np.array(li1)
n2 = np.array(li2)
display(n1, n2)
array([1., 2., 3., 4., 5.])
array(['1', '2', '3', '4', '5'], dtype='<U1')
- numpy默认ndarray的所有元素类型相同
- 若 传入的列表包含不同类型的元素,则统一转换为同一类型。
- 转换优先级:string > float > int
2、使用np的routines函数创建
np.ones(shape, dtype=None, order=‘C’)
# 全是1的ndarray
np.ones(shape=(3, 4), dtype=np.float32) # 3行4列,float32类型
array([[1., 1., 1., 1.],
[1., 1., 1., 1.],
[1., 1., 1., 1.]], dtype=float32)
np.zeros(shape, dtype=float, order=‘C’)
# 全是0的ndarray
np.zeros(shape=(5, 6), dtype=np.int16)
array([[0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0]], dtype=int16)
np.full(shape, fill_value, dtype=None, order=‘C’)
# 使用指定的值去填充ndarray
np.full(shape=(3, 4), fill_value=666, dtype=np.float16)
array([[666., 666., 666., 666.],
[666., 666., 666., 666.],
[666., 666., 666., 666.]], dtype=float16)
np.eye(N, M=None, k=0, dtype=float)
# 一条对角线全为1,其他位置全为0。只能是二维
np.eye(5, 6, k=0)
# k控制对角线,k为0表示左边第一个数开始画对角线,k增加表示线向上移,k减少表示线向下移
array([[1., 0., 0., 0., 0., 0.],
[0., 1., 0., 0., 0., 0.],
[0., 0., 1., 0., 0., 0.],
[0., 0., 0., 1., 0., 0.],
[0., 0., 0., 0., 1., 0.]])
np.linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None)
# line space 把线分成好几份
n1 = np.linspace(0, 100, num=200, endpoint=False)
# 0-100中,包含0不包含100,把它们分成200个元素,最后一个元素不显示
n2 = np.linspace(0, 100, num=200, retstep=True)
# endpoint不写默认为True,retstep表示显示间距,此时返回的是一个元组(array + 间距)
display(n1, n2)
array([ 0. , 0.5, 1. , 1.5, 2. , 2.5, 3. , 3.5, 4. , 4.5, 5. ,
5.5, 6. , 6.5, 7. , 7.5, 8. , 8.5, 9. , 9.5, 10. , 10.5,
11. , 11.5, 12. , 12.5, 13. , 13.5, 14. , 14.5, 15. , 15.5, 16. ,
16.5, 17. , 17.5, 18. , 18.5, 19. , 19.5, 20. , 20.5, 21. , 21.5,
22. , 22.5, 23. , 23.5, 24. , 24.5, 25. , 25.5, 26. , 26.5, 27. ,
27.5, 28. , 28.5, 29. , 29.5, 30. , 30.5, 31. , 31.5, 32. , 32.5,
33. , 33.5, 34. , 34.5, 35. , 35.5, 36. , 36.5, 37. , 37.5, 38. ,
38.5, 39. , 39.5, 40. , 40.5, 41. , 41.5, 42. , 42.5, 43. , 43.5,
44. , 44.5, 45. , 45.5, 46. , 46.5, 47. , 47.5, 48. , 48.5, 49. ,
49.5, 50. , 50.5, 51. , 51.5, 52. , 52.5, 53. , 53.5, 54. , 54.5,
55. , 55.5, 56. , 56.5, 57. , 57.5, 58. , 58.5, 59. , 59.5, 60. ,
60.5, 61. , 61.5, 62. , 62.5, 63. , 63.5, 64. , 64.5, 65. , 65.5,
66. , 66.5, 67. , 67.5, 68. , 68.5, 69. , 69.5, 70. , 70.5, 71. ,
71.5, 72. , 72.5, 73. , 73.5, 74. , 74.5, 75. , 75.5, 76. , 76.5,
77. , 77.5, 78. , 78.5, 79. , 79.5, 80. , 80.5, 81. , 81.5, 82. ,
82.5, 83. , 83.5, 84. , 84.5, 85. , 85.5, 86. , 86.5, 87. , 87.5,
88. , 88.5, 89. , 89.5, 90. , 90.5, 91. , 91.5, 92. , 92.5, 93. ,
93.5, 94. , 94.5, 95. , 95.5, 96. , 96.5, 97. , 97.5, 98. , 98.5,
99. , 99.5])
(array([ 0. , 0.50251256, 1.00502513, 1.50753769,
2.01005025, 2.51256281, 3.01507538, 3.51758794,
4.0201005 , 4.52261307, 5.02512563, 5.52763819,
6.03015075, 6.53266332, 7.03517588, 7.53768844,
8.04020101, 8.54271357, 9.04522613, 9.54773869,
10.05025126, 10.55276382, 11.05527638, 11.55778894,
12.06030151, 12.56281407, 13.06532663, 13.5678392 ,
14.07035176, 14.57286432, 15.07537688, 15.57788945,
16.08040201, 16.58291457, 17.08542714, 17.5879397 ,
18.09045226, 18.59296482, 19.09547739, 19.59798995,
20.10050251, 20.60301508, 21.10552764, 21.6080402 ,
22.11055276, 22.61306533, 23.11557789, 23.61809045,
24.12060302, 24.62311558, 25.12562814, 25.6281407 ,
26.13065327, 26.63316583, 27.13567839, 27.63819095,
28.14070352, 28.64321608, 29.14572864, 29.64824121,
30.15075377, 30.65326633, 31.15577889, 31.65829146,
32.16080402, 32.66331658, 33.16582915, 33.66834171,
34.17085427, 34.67336683, 35.1758794 , 35.67839196,
36.18090452, 36.68341709, 37.18592965, 37.68844221,
38.19095477, 38.69346734, 39.1959799 , 39.69849246,
40.20100503, 40.70351759, 41.20603015, 41.70854271,
42.21105528, 42.71356784, 43.2160804 , 43.71859296,
44.22110553, 44.72361809, 45.22613065, 45.72864322,
46.23115578, 46.73366834, 47.2361809 , 47.73869347,
48.24120603, 48.74371859, 49.24623116, 49.74874372,
50.25125628, 50.75376884, 51.25628141, 51.75879397,
52.26130653, 52.7638191 , 53.26633166, 53.76884422,
54.27135678, 54.77386935, 55.27638191, 55.77889447,
56.28140704, 56.7839196 , 57.28643216, 57.78894472,
58.29145729, 58.79396985, 59.29648241, 59.79899497,
60.30150754, 60.8040201 , 61.30653266, 61.80904523,
62.31155779, 62.81407035, 63.31658291, 63.81909548,
64.32160804, 64.8241206 , 65.32663317, 65.82914573,
66.33165829, 66.83417085, 67.33668342, 67.83919598,
68.34170854, 68.84422111, 69.34673367, 69.84924623,
70.35175879, 70.85427136, 71.35678392, 71.85929648,
72.36180905, 72.86432161, 73.36683417, 73.86934673,
74.3718593 , 74.87437186, 75.37688442, 75.87939698,
76.38190955, 76.88442211, 77.38693467, 77.88944724,
78.3919598 , 78.89447236, 79.39698492, 79.89949749,
80.40201005, 80.90452261, 81.40703518, 81.90954774,
82.4120603 , 82.91457286, 83.41708543, 83.91959799,
84.42211055, 84.92462312, 85.42713568, 85.92964824,
86.4321608 , 86.93467337, 87.43718593, 87.93969849,
88.44221106, 88.94472362, 89.44723618, 89.94974874,
90.45226131, 90.95477387, 91.45728643, 91.95979899,
92.46231156, 92.96482412, 93.46733668, 93.96984925,
94.47236181, 94.97487437, 95.47738693, 95.9798995 ,
96.48241206, 96.98492462, 97.48743719, 97.98994975,
98.49246231, 98.99497487, 99.49748744, 100. ]),
0.5025125628140703)
np.arange([start, ] stop, [step, ], dtype=None)
# 跟python中的range类似。左闭右开
np.arange(0, 20, 2)
array([ 0, 2, 4, 6, 8, 10, 12, 14, 16, 18])
np.random.randint(low, high=None, size=None, dtype=‘l’)
# 产生随机整数ndarray
np.random.randint(0, 10, size=(3, 4, 5)) # 左闭右开
array([[[2, 6, 9, 3, 2],
[7, 1, 1, 5, 4],
[0, 9, 9, 6, 7],
[3, 8, 4, 6, 4]],
[[3, 1, 9, 0, 3],
[7, 5, 1, 0, 2],
[5, 8, 3, 0, 9],
[0, 2, 9, 2, 1]],
[[0, 6, 3, 7, 1],
[6, 7, 8, 8, 7],
[2, 8, 1, 1, 4],
[6, 4, 0, 2, 7]]])
正太分布
np.random.normal(loc=0.0, scale=1.0, size=None)
# loc 平均值
# scale 方差
#
np.random.normal(loc=10, scale=3, size=(1000, 3))
np.random.normal(loc=10, scale=3, size=(1000, 3)).mean() # 平均值
np.random.normal(loc=10, scale=3, size=(1000, 3)).std() # 方差
2.997653779478764
标准正太分布
平均值为0,方差为1的正态分布
np.random.randn(2, 3, 4)
array([[[ 0.81601988, -0.61631338, -0.10628327, -0.41879767],
[-0.09548007, 0.60270457, 0.10863477, -1.17939855],
[-0.29920504, 0.01000102, 0.68119849, 0.64499051]],
[[-0.94043963, 0.65410364, -0.79888361, -0.50524771],
[ 1.00370329, 0.72906239, -0.96730753, 1.79453576],
[-0.15018307, 0.83972906, -0.48318382, -0.55468954]]])
np.random.random(size=None)
# 生成0到1的随机数。左闭右开
np.random.random(size=(3, 4, 5))
# np.random.rand(3, 4, 5) 另外一种写法
array([[[0.86916286, 0.92407939, 0.49724678, 0.95962036, 0.21745135],
[0.97398458, 0.34599573, 0.63771178, 0.17261302, 0.37152828],
[0.87795621, 0.67606884, 0.62232594, 0.78071815, 0.59318382],
[0.59448583, 0.70523818, 0.18857654, 0.25635888, 0.75201325]],
[[0.74665058, 0.09213689, 0.10140568, 0.1770098 , 0.58856297],
[0.81628145, 0.83075025, 0.29371462, 0.0894518 , 0.92344828],
[0.64019469, 0.37097845, 0.45570794, 0.12394152, 0.29937541],
[0.72568189, 0.74235181, 0.57822036, 0.1865216 , 0.57259669]],
[[0.93678934, 0.96206502, 0.42138011, 0.22805006, 0.77807982],
[0.8862375 , 0.50320728, 0.31796538, 0.85346515, 0.41508747],
[0.89888431, 0.53212699, 0.87284355, 0.44499716, 0.56547908],
[0.25195562, 0.53199532, 0.9355606 , 0.06964199, 0.75108516]]])
ndarray的属性
4个必记参数:
ndim 维度
shape 形状(各维度的长度)
size 总长度
dtype 元素类型
n = np.random.randint(0, 100, size=(3, 4, 5))
n
array([[[83, 4, 30, 21, 60],
[49, 46, 96, 86, 48],
[86, 15, 50, 66, 57],
[41, 44, 0, 82, 73]],
[[26, 57, 28, 29, 38],
[54, 63, 58, 12, 64],
[ 9, 39, 8, 9, 46],
[89, 78, 66, 45, 24]],
[[44, 85, 36, 92, 28],
[20, 36, 35, 90, 81],
[48, 57, 69, 52, 51],
[47, 97, 89, 1, 18]]])
n.ndim
3
n.shape
(3, 4, 5)
n.size
60
n.dtype
dtype('int32')
ndarray的基本操作
1、索引
一维 与 列表 完全一致,多维时同理
n = np.random.randint(0, 100, size=(2, 3, 4))
n
array([[[51, 38, 83, 26],
[67, 52, 62, 38],
[39, 21, 50, 60]],
[[ 0, 89, 40, 19],
[13, 29, 19, 62],
[ 2, 29, 82, 85]]])
n[1, 2, 0]
2
根据索引修改数据
n[0, 2, 0] = 100
2、切片
一维与列表一致,多维度类似普通列表
n[1:3]
array([[[ 0, 89, 40, 19],
[13, 29, 19, 62],
[ 2, 29, 82, 85]]])
n[1:3, 1:3,]
array([[[13, 29, 19, 62],
[ 2, 29, 82, 85]]])
n[1:3, 1:3, 0:1]
array([[[13],
[ 2]]])
切片应用:反转ndarray
n = np.random.randint(0, 100, size=(2, 3, 4))
n
array([[[ 1, 1, 31, 90],
[50, 72, 41, 81],
[51, 94, 6, 6]],
[[67, 79, 16, 39],
[88, 46, 52, 12],
[ 0, 28, 37, 49]]])
n[::-1] # 对第一个维度进行反转
array([[[67, 79, 16, 39],
[88, 46, 52, 12],
[ 0, 28, 37, 49]],
[[ 1, 1, 31, 90],
[50, 72, 41, 81],
[51, 94, 6, 6]]])
n[::-1, ::-1] # 对第一、第二维度进行反转,同理,三个维度也可以反转
array([[[ 0, 28, 37, 49],
[88, 46, 52, 12],
[67, 79, 16, 39]],
[[51, 94, 6, 6],
[50, 72, 41, 81],
[ 1, 1, 31, 90]]])
反转,可以用来做图片左右、上下、颜色翻转
3、变形
使用reshape函数,注意:参数是一个tuple
reshape要求元素数量一致
n = np.random.randint(0, 100, size=(4, 5))
n
array([[40, 93, 88, 88, 48],
[95, 22, 99, 12, 28],
[93, 2, 57, 88, 77],
[43, 60, 26, 14, 72]])
# 进行变形:将原来的4行5列改成5行4列
n.reshape((5, 4))
array([[40, 93, 88, 88],
[48, 95, 22, 99],
[12, 28, 93, 2],
[57, 88, 77, 43],
[60, 26, 14, 72]])
n.reshape(5, 4)
array([[40, 93, 88, 88],
[48, 95, 22, 99],
[12, 28, 93, 2],
[57, 88, 77, 43],
[60, 26, 14, 72]])
np.reshape(n, newshape=(5, 4)) # 不常用
array([[40, 93, 88, 88],
[48, 95, 22, 99],
[12, 28, 93, 2],
[57, 88, 77, 43],
[60, 26, 14, 72]])
4、级联
- np.concatenate()
- 级联的参数是列表,一定要加中括号或小括号
- 维度 必须相同
- 形状相符
- 级联的方向默认是shape这个tuple的第一个值所代表的维度方向
- 可通过axis参数改变级联的方向
n1 = np.random.randint(0, 100, size=(4, 5))
n2 = np.random.randint(0, 100, size=(6, 5))
display(n1, n2)
array([[85, 59, 84, 59, 67],
[95, 37, 68, 13, 12],
[75, 6, 0, 98, 43],
[60, 54, 95, 97, 9]])
array([[77, 7, 33, 19, 91],
[30, 23, 6, 86, 22],
[92, 49, 75, 49, 12],
[18, 59, 63, 47, 38],
[ 8, 52, 53, 80, 85],
[31, 58, 79, 10, 41]])
# 垂直级联 axis = 0
np.concatenate((n1, n2), axis=0)
array([[85, 59, 84, 59, 67],
[95, 37, 68, 13, 12],
[75, 6, 0, 98, 43],
[60, 54, 95, 97, 9],
[77, 7, 33, 19, 91],
[30, 23, 6, 86, 22],
[92, 49, 75, 49, 12],
[18, 59, 63, 47, 38],
[ 8, 52, 53, 80, 85],
[31, 58, 79, 10, 41]])
n3 = np.random.randint(0, 100, size=(4, 5))
n4 = np.random.randint(0, 100, size=(4, 6))
# 水平级联 axis=1
np.concatenate((n3, n4), axis=1)
array([[15, 97, 76, 22, 62, 11, 0, 1, 42, 84, 4],
[27, 70, 63, 22, 71, 74, 65, 53, 56, 70, 83],
[43, 60, 9, 4, 12, 34, 7, 81, 27, 33, 61],
[13, 85, 42, 83, 20, 50, 10, 42, 17, 88, 60]])
np.hstack和np.vstack
水平级联和垂直级联,处理自己,进行维度的变更
np.hstack((n3, n4))
array([[15, 97, 76, 22, 62, 11, 0, 1, 42, 84, 4],
[27, 70, 63, 22, 71, 74, 65, 53, 56, 70, 83],
[43, 60, 9, 4, 12, 34, 7, 81, 27, 33, 61],
[13, 85, 42, 83, 20, 50, 10, 42, 17, 88, 60]])
np.vstack((n1, n2))
array([[77, 37, 9, 90, 27],
[ 5, 84, 4, 19, 64],
[70, 28, 95, 65, 99],
[39, 98, 56, 13, 66],
[82, 68, 28, 9, 1],
[77, 7, 9, 51, 91],
[61, 49, 21, 87, 65],
[46, 34, 34, 36, 53],
[56, 18, 98, 14, 19],
[85, 34, 51, 52, 82]])
5、切分
与级联类似,三个函数完成切分工作:
- np.split
- np.hsplit
- np.vsplit
n = np.random.randint(0, 100, size=(6, 6))
n
array([[67, 73, 37, 77, 81, 17],
[45, 76, 95, 55, 7, 61],
[60, 62, 81, 83, 86, 61],
[ 3, 37, 63, 53, 79, 0],
[86, 62, 84, 68, 80, 1],
[35, 34, 69, 99, 99, 1]])
np.split(n, [2, 4], axis=0) # 左闭右开,对行进行切割
[array([[67, 73, 37, 77, 81, 17],
[45, 76, 95, 55, 7, 61]]),
array([[60, 62, 81, 83, 86, 61],
[ 3, 37, 63, 53, 79, 0]]),
array([[86, 62, 84, 68, 80, 1],
[35, 34, 69, 99, 99, 1]])]
np.split(n, [2, 4], axis=1) # 左闭右开,对列进行切割
[array([[67, 73],
[45, 76],
[60, 62],
[ 3, 37],
[86, 62],
[35, 34]]),
array([[37, 77],
[95, 55],
[81, 83],
[63, 53],
[84, 68],
[69, 99]]),
array([[81, 17],
[ 7, 61],
[86, 61],
[79, 0],
[80, 1],
[99, 1]])]
a = np.hsplit(n, [2, 4]) # axis=1
b = np.vsplit(n, [2, 4]) # axis=0
display(a, b)
[array([[67, 73],
[45, 76],
[60, 62],
[ 3, 37],
[86, 62],
[35, 34]]),
array([[37, 77],
[95, 55],
[81, 83],
[63, 53],
[84, 68],
[69, 99]]),
array([[81, 17],
[ 7, 61],
[86, 61],
[79, 0],
[80, 1],
[99, 1]])]
[array([[67, 73, 37, 77, 81, 17],
[45, 76, 95, 55, 7, 61]]),
array([[60, 62, 81, 83, 86, 61],
[ 3, 37, 63, 53, 79, 0]]),
array([[86, 62, 84, 68, 80, 1],
[35, 34, 69, 99, 99, 1]])]
6、副本
所有赋值运算 不会为ndarray的任何元素创建副本,对复制后的对象 的操作 也对原来的对象生效
n
n2 = n
display(id(n))
display(id(n2))
1943841218272
1943841218272
为了避免操作array后改变了原来的数据内容,可以使用copy()函数创建副本
n3 = n.copy()
id(n3)
# 修改n3,n的内容不会被改变
1943841809472
ndarray的聚合操作
1、求和 np.sum
n
# array([[67, 73, 37, 77, 81, 17],
# [45, 76, 95, 55, 7, 61],
# [60, 62, 81, 83, 86, 61],
# [ 3, 37, 63, 53, 79, 0],
# [86, 62, 84, 68, 80, 1],
# [35, 34, 69, 99, 99, 1]])
n.sum() # axis默认为None,表示 所有的维度 都聚合成 0 维
2077
# axis=0 表示 纵向相加。另一种写法:np.sum(n, axis=0)
n.sum(axis=0)
array([296, 344, 429, 435, 432, 141])
# axis=1 表示 横向相加。另一种写法:np.sum(n, axis=1)
n.sum(axis=1)
array([352, 339, 433, 235, 381, 337])
2、最大/最小值:np.max/min
a = n.max() # 所有元素中的最大值
b = n.max(axis=0) # 每个纵向的最大值
c = n.max(axis=1) # 每个横向的最大值
display(a, b, c)
99
array([86, 76, 95, 99, 99, 61])
array([81, 95, 86, 79, 86, 99])
3、其他聚合操作
- np.sum np.nansum
- np.prod np.nanprod 笛卡尔积
- np.mean np.nanmean
- np.std np.nanstd
- np.var np.nanvar
- np.min np.nanmin 最小值的 索引
- np.max np.nanmax
- np.argmin np.nanargmin
- np.argmax np.nanargmax
- np.median np.nanmedian
- np.percentile np.nanpercentile
- np.any
- np.all
- np.power 幂运算
np.nan
- 类似一种np中的特殊数据类型
- np.nan参与计算时,得到的结果总是nan
- 所以,如果要忽略np.nan,需要用到np.nanxx()之类的函数
# 其中,np.nan 之类的,表示空数据
temp = np.array([1, 2, 3, 4, np.nan])
temp.sum()
nan
np.nansum(temp) # 将np.nan视为0
10.0
ndarray的矩阵操作
1、基本矩阵操作
算术运算符:
- 加减乘除
n = np.random.randint(0, 100, size=(6, 6))
n
array([[10, 11, 66, 41, 49, 45],
[65, 62, 43, 42, 34, 77],
[ 6, 98, 17, 25, 76, 30],
[ 4, 87, 21, 2, 80, 60],
[21, 48, 29, 79, 29, 60],
[53, 28, 84, 99, 52, 3]])
n + 1 # 每个元素分别加1
array([[ 11, 12, 67, 42, 50, 46],
[ 66, 63, 44, 43, 35, 78],
[ 7, 99, 18, 26, 77, 31],
[ 5, 88, 22, 3, 81, 61],
[ 22, 49, 30, 80, 30, 61],
[ 54, 29, 85, 100, 53, 4]])
n2 = n
n + n2 # 对应元素分别相加
array([[ 20, 22, 132, 82, 98, 90],
[130, 124, 86, 84, 68, 154],
[ 12, 196, 34, 50, 152, 60],
[ 8, 174, 42, 4, 160, 120],
[ 42, 96, 58, 158, 58, 120],
[106, 56, 168, 198, 104, 6]])
n1 = np.random.randint(0, 10, size=(4, 5))
n2 = np.random.randint(0, 10, size=(5, 4))
n1 + n2
"""不知道该给确实的地方补上哪一行的数据,导致无法相加,报错:"""
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-9-9e856722bb03> in <module>
1 n1 = np.random.randint(0, 10, size=(4, 5))
2 n2 = np.random.randint(0, 10, size=(5, 4))
----> 3 n1 + n2
4 """不知道该给确实的地方补上哪一行的数据,导致无法相加,报错:"""
ValueError: operands could not be broadcast together with shapes (4,5) (5,4)
2、广播机制
- 为缺失的维度补1
- 假定缺失元素用 已有值 填充
示例:
m = np.ones((2, 3))
a = np.arange(3)
求m + a
# 当ndarray的shape不一致时,numpy会启动 广播机制,以此让ndarray的shape变成一致
m = np.ones((2, 3))
a = np.arange(3)
display(m, a)
array([[1., 1., 1.],
[1., 1., 1.]])
array([0, 1, 2])
m + a
# 相当于先把a变成和m一致的shape,不足的地方用自身已有的值填充
array([[1., 2., 3.],
[1., 2., 3.]])
矩阵积 np.dot()
两个矩阵a和b,相乘时对应元素相乘即可。
矩阵的积,矩阵的点乘,要求第一个矩阵的列 和 第二个矩阵的行 必须相同
矩阵的积是有顺序的,不满足乘法交换律
n1 = np.random.randint(0, 10, size=(4, 5))
n2 = np.random.randint(0, 10, size=(5, 6))
np.dot(n1, n2)
array([[140, 81, 84, 111, 88, 80],
[203, 133, 96, 154, 130, 114],
[185, 115, 101, 155, 120, 114],
[ 85, 67, 34, 56, 57, 44]])
ndarray的排序
排序只对一维数组有意义
1、快速排序
np.sort()和ndarray.sort()。区别:
- np.sort()不改变输入
- ndarray.sort()本地处理,不占用空间,但改变输入
n = np.array([6, 5, 4, 3, 2, 1])
sort_n = np.sort(n)
display(n, sort_n)
array([6, 5, 4, 3, 2, 1])
array([1, 2, 3, 4, 5, 6])
n.sort() # 改变了n的值
n
array([1, 2, 3, 4, 5, 6])
2、部分排序
n = np.random.randint(0, 100, size=100)
n
array([88, 66, 48, 58, 33, 5, 14, 39, 22, 8, 4, 59, 88, 24, 45, 89, 51,
34, 33, 70, 49, 41, 38, 6, 76, 65, 1, 55, 36, 65, 94, 55, 39, 66,
88, 76, 11, 62, 33, 44, 7, 46, 43, 40, 44, 50, 27, 71, 68, 90, 68,
84, 9, 81, 37, 27, 77, 17, 92, 84, 83, 3, 86, 51, 81, 80, 39, 67,
49, 81, 54, 33, 56, 11, 50, 5, 12, 35, 19, 24, 26, 10, 41, 46, 94,
99, 17, 56, 62, 33, 54, 88, 80, 50, 21, 55, 49, 7, 44, 84])
# 最大的5个。最后5个数字是最大的,但并不是排序好的
np.partition(n, kth=-5)
array([54, 84, 48, 58, 33, 5, 14, 39, 22, 8, 4, 59, 44, 24, 45, 7, 51,
34, 33, 70, 49, 41, 38, 6, 76, 65, 1, 55, 36, 65, 49, 55, 39, 66,
55, 76, 11, 62, 33, 44, 7, 46, 43, 40, 44, 50, 27, 71, 68, 66, 68,
84, 9, 81, 37, 27, 77, 17, 21, 84, 83, 3, 86, 51, 81, 80, 39, 67,
49, 81, 54, 33, 56, 11, 50, 5, 12, 35, 19, 24, 26, 10, 41, 46, 50,
80, 17, 56, 62, 33, 88, 88, 89, 88, 88, 90, 94, 94, 92, 99])
# 最小的5个。前面5个数字最小
np.partition(n, kth=5)
array([ 1, 4, 3, 5, 5, 6, 7, 7, 9, 8, 10, 11, 22, 24, 24, 27, 19,
17, 12, 21, 14, 27, 33, 11, 17, 26, 33, 38, 36, 39, 33, 35, 39, 34,
39, 37, 33, 33, 40, 41, 41, 43, 44, 46, 84, 50, 44, 71, 68, 66, 68,
84, 62, 81, 76, 55, 77, 66, 55, 84, 83, 49, 86, 51, 81, 80, 65, 67,
49, 81, 54, 55, 56, 65, 50, 76, 49, 70, 51, 45, 44, 59, 58, 46, 50,
80, 48, 56, 62, 54, 88, 88, 99, 94, 92, 88, 94, 89, 88, 90])