numpy基础用法学习
numpy get started
导入numpy库,并查看numpy版本
import numpy as np
np.__version__
'1.14.0'
一、创建ndarray
1. 使用np.array()由python list创建
参数为列表:
[1, 4, 2, 5, 3]
注意:
- numpy默认ndarray的所有元素的类型是相同的
- 如果传进来的列表中包含不同的类型,则统一为同一类型,优先级:str>float>int
data = [1, 2, 3]
nd = np.array(data)
nd
array([1, 2, 3])
type(nd)
#ndarray 这样的数据
#查看整体的类型
numpy.ndarray
type(data)
list
nd.dtype
#查看数据的类型
dtype('int32')
nd1 = np.array([1,2,3.2])
nd1.dtype
#统一的原则 int < float < string
dtype('float64')
nd2 = np.array([1,2,3.4, "qwe"])
nd2.dtype
dtype('<U32')
nd3 = np.array([[1,2],[3,4]])
nd3
array([[1, 2],
[3, 4]])
nd4 = np.array([[[1,2],[3,4]],[[1,2],[3,4]]])
nd4
array([[[1, 2],
[3, 4]],
[[1, 2],
[3, 4]]])
nd4.shape
(2, 2, 2)
#扩展
import matplotlib.pyplot as plt
cat = plt.imread("./cat.jpg")
type(cat)
numpy.ndarray
plt.imshow(cat[:300,:200])
plt.show()
cat.shape
#查看形状的 rgb jpg 0-255
(456, 730, 3)
cat
#三维的数据
array([[[231, 186, 131],
[232, 187, 132],
[233, 188, 133],
...,
[100, 54, 54],
[ 92, 48, 47],
[ 85, 43, 44]],
[[232, 187, 132],
[232, 187, 132],
[233, 188, 133],
...,
[100, 54, 54],
[ 92, 48, 47],
[ 84, 42, 43]],
[[232, 187, 132],
[233, 188, 133],
[233, 188, 133],
...,
[ 99, 53, 53],
[ 91, 47, 46],
[ 83, 41, 42]],
...,
[[199, 119, 82],
[199, 119, 82],
[200, 120, 83],
...,
[189, 99, 65],
[187, 97, 63],
[187, 97, 63]],
[[199, 119, 82],
[199, 119, 82],
[199, 119, 82],
...,
[188, 98, 64],
[186, 96, 62],
[188, 95, 62]],
[[199, 119, 82],
[199, 119, 82],
[199, 119, 82],
...,
[188, 98, 64],
[188, 95, 62],
[188, 95, 62]]], dtype=uint8)
2. 使用np的routines函数创建
包含以下常见创建方法:
- np.ones(shape, dtype=None, order='C')
ones = np.ones((456,730,3), dtype = "float")
#shape 形状, 元祖
ones
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., 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.]]])
plt.imshow(ones)
plt.show()
#0-1 0 代表黑色的 1 白色的 png格式的图片
#切片赋值
ones[::,::,1:] = 0
ones
array([[[1., 0., 0.],
[1., 0., 0.],
[1., 0., 0.],
...,
[1., 0., 0.],
[1., 0., 0.],
[1., 0., 0.]],
[[1., 0., 0.],
[1., 0., 0.],
[1., 0., 0.],
...,
[1., 0., 0.],
[1., 0., 0.],
[1., 0., 0.]],
[[1., 0., 0.],
[1., 0., 0.],
[1., 0., 0.],
...,
[1., 0., 0.],
[1., 0., 0.],
[1., 0., 0.]],
...,
[[1., 0., 0.],
[1., 0., 0.],
[1., 0., 0.],
...,
[1., 0., 0.],
[1., 0., 0.],
[1., 0., 0.]],
[[1., 0., 0.],
[1., 0., 0.],
[1., 0., 0.],
...,
[1., 0., 0.],
[1., 0., 0.],
[1., 0., 0.]],
[[1., 0., 0.],
[1., 0., 0.],
[1., 0., 0.],
...,
[1., 0., 0.],
[1., 0., 0.],
[1., 0., 0.]]])
plt.imshow(ones)
plt.show()
#1 0 0
ones[::,::,0] = 0.3
ones
array([[[0.3, 0. , 0. ],
[0.3, 0. , 0. ],
[0.3, 0. , 0. ],
...,
[0.3, 0. , 0. ],
[0.3, 0. , 0. ],
[0.3, 0. , 0. ]],
[[0.3, 0. , 0. ],
[0.3, 0. , 0. ],
[0.3, 0. , 0. ],
...,
[0.3, 0. , 0. ],
[0.3, 0. , 0. ],
[0.3, 0. , 0. ]],
[[0.3, 0. , 0. ],
[0.3, 0. , 0. ],
[0.3, 0. , 0. ],
...,
[0.3, 0. , 0. ],
[0.3, 0. , 0. ],
[0.3, 0. , 0. ]],
...,
[[0.3, 0. , 0. ],
[0.3, 0. , 0. ],
[0.3, 0. , 0. ],
...,
[0.3, 0. , 0. ],
[0.3, 0. , 0. ],
[0.3, 0. , 0. ]],
[[0.3, 0. , 0. ],
[0.3, 0. , 0. ],
[0.3, 0. , 0. ],
...,
[0.3, 0. , 0. ],
[0.3, 0. , 0. ],
[0.3, 0. , 0. ]],
[[0.3, 0. , 0. ],
[0.3, 0. , 0. ],
[0.3, 0. , 0. ],
...,
[0.3, 0. , 0. ],
[0.3, 0. , 0. ],
[0.3, 0. , 0. ]]])
plt.imshow(ones)
plt.show()
- np.zeros(shape, dtype=float, order='C')
zeros = np.zeros((456,730,3), dtype = "float")
zeros
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.],
[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.],
[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.]],
[[0., 0., 0.],
[0., 0., 0.],
[0., 0., 0.],
...,
[0., 0., 0.],
[0., 0., 0.],
[0., 0., 0.]]])
plt.imshow(zeros)
<matplotlib.image.AxesImage at 0x9992e80>
- np.full(shape, fill_value, dtype=None, order='C')
nd4 = np.full(12, fill_value=1024)
nd4
array([1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024,
1024])
#变形,注意 你要变形的数据有多少? 不能超过变形总的长度
#cannot reshape array of size 12 into shape (3,5)
nd5 = nd4.reshape((3,5))
nd5
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-40-0baec20d7413> in <module>()
1 #变形
----> 2 nd5 = nd4.reshape((3,5))
3 nd5
ValueError: cannot reshape array of size 12 into shape (3,5)
nd5 = nd4.reshape((1,12))
nd5
#reshape在咱们以后的学习经常使用
array([[1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024,
1024]])
#扩展
cat
array([[[231, 186, 131],
[232, 187, 132],
[233, 188, 133],
...,
[100, 54, 54],
[ 92, 48, 47],
[ 85, 43, 44]],
[[232, 187, 132],
[232, 187, 132],
[233, 188, 133],
...,
[100, 54, 54],
[ 92, 48, 47],
[ 84, 42, 43]],
[[232, 187, 132],
[233, 188, 133],
[233, 188, 133],
...,
[ 99, 53, 53],
[ 91, 47, 46],
[ 83, 41, 42]],
...,
[[199, 119, 82],
[199, 119, 82],
[200, 120, 83],
...,
[189, 99, 65],
[187, 97, 63],
[187, 97, 63]],
[[199, 119, 82],
[199, 119, 82],
[199, 119, 82],
...,
[188, 98, 64],
[186, 96, 62],
[188, 95, 62]],
[[199, 119, 82],
[199, 119, 82],
[199, 119, 82],
...,
[188, 98, 64],
[188, 95, 62],
[188, 95, 62]]], dtype=uint8)
cat.shape
(456, 730, 3)
#需求:把猫的图片颠倒一下
cat2 = cat[::-1,::-1,::]
plt.imshow(cat2)
<matplotlib.image.AxesImage at 0x99d7eb8>
- np.eye(N, M=None, k=0, dtype=float)
对角线为1其他的位置为0
#产生一个 单元矩阵
np.eye(5)
array([[1., 0., 0., 0., 0.],
[0., 1., 0., 0., 0.],
[0., 0., 1., 0., 0.],
[0., 0., 0., 1., 0.],
[0., 0., 0., 0., 1.]])
#每一个有解的矩阵 到最后都可以化成单元矩阵 满秩矩阵
2x + 3y + 4z = 12
4x + 7y + 8z = 32
2 3 4
4 7 8 => 最后化成单元矩阵 是没办法化成的单元矩阵
4 6 8
- np.linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None)
#lin linear 线性的
np.linspace(0,10,num = 13)
array([ 0. , 0.83333333, 1.66666667, 2.5 , 3.33333333,
4.16666667, 5. , 5.83333333, 6.66666667, 7.5 ,
8.33333333, 9.16666667, 10. ])
np.log2(4)
2.0
np.logspace(-3,1,2)
array([1.e-03, 1.e+01])
- np.arange([start, ]stop, [step, ]dtype=None)
#我会经常写
np.arange(start = 5, stop = 10, step = 2,dtype = "float")
array([5., 7., 9.])
np.arange(0,150,10)
array([ 0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120,
130, 140])
- np.random.randint(low, high=None, size=None, dtype='l')
np.random.randint(0,150,size = 10)
array([ 57, 116, 133, 84, 141, 32, 63, 91, 93, 16])
dog = np.random.randint(0,255,size = (456,730,3))
dog.shape
(456, 730, 3)
cat.dtype
dtype('uint8')
dog.dtype
dtype('int32')
#数据类型转换
dog = dog.astype('uint8')
plt.imshow(dog)
<matplotlib.image.AxesImage at 0xfcca828>
- np.random.randn(d0, d1, ..., dn)
标准正太分布
np.random.randn(10,2,1)
#正太分布会搞出来数据,是两边低,中间高的数据
array([[[ 1.39438673],
[-0.78456615]],
[[-0.59132977],
[ 2.23663625]],
[[ 0.61258477],
[-0.84729158]],
[[ 1.37855508],
[ 1.697815 ]],
[[-0.06004384],
[ 0.98147252]],
[[-1.20190404],
[-0.77774525]],
[[ 1.34400589],
[ 0.23112796]],
[[-0.31579586],
[-0.11644608]],
[[-0.11822406],
[ 0.26001606]],
[[ 0.03766789],
[ 0.80169127]]])
9)np.random.normal(loc=0.0, scale=1.0, size=None)
#只是需要知道他可以搞出来数据
np.random.normal(loc = 175, scale =10,size = 10 )
array([176.45148898, 179.9089715 , 173.65923279, 172.36118888,
169.66272673, 158.76980334, 165.3742424 , 173.52898147,
175.84535943, 183.92875259])
- np.random.random(size=None)
生成0到1的随机数,左闭右开
np.random.random(size = (2,2))
array([[0.64468214, 0.54496107],
[0.20529068, 0.0482465 ]])
使用随机数成成一张图片
二、ndarray的属性
4个必记参数:
ndim:维度
shape:形状(各维度的长度)
size:总长度
dtype:元素类型
cat = plt.imread("./cat.jpg")
cat
array([[[231, 186, 131],
[232, 187, 132],
[233, 188, 133],
...,
[100, 54, 54],
[ 92, 48, 47],
[ 85, 43, 44]],
[[232, 187, 132],
[232, 187, 132],
[233, 188, 133],
...,
[100, 54, 54],
[ 92, 48, 47],
[ 84, 42, 43]],
[[232, 187, 132],
[233, 188, 133],
[233, 188, 133],
...,
[ 99, 53, 53],
[ 91, 47, 46],
[ 83, 41, 42]],
...,
[[199, 119, 82],
[199, 119, 82],
[200, 120, 83],
...,
[189, 99, 65],
[187, 97, 63],
[187, 97, 63]],
[[199, 119, 82],
[199, 119, 82],
[199, 119, 82],
...,
[188, 98, 64],
[186, 96, 62],
[188, 95, 62]],
[[199, 119, 82],
[199, 119, 82],
[199, 119, 82],
...,
[188, 98, 64],
[188, 95, 62],
[188, 95, 62]]], dtype=uint8)
cat.ndim
#dimension 维度的意思
3
cat.shape
#这个每天都要用,特别是数据分析
(456, 730, 3)
cat.size
998640
456*730*3
998640
cat.dtype
dtype('uint8')
三、ndarray的基本操作
1. 索引
一维与列表完全一致
多维时同理
l = [1,2,3,4,5,6]
l[-1]
#list
6
nd = np.random.randint(0,100,size = 12)
nd[2]
97
nd = np.random.randint(0,100,size =(4,4))
nd
array([[48, 85, 53, 22],
[24, 36, 26, 31],
[38, 26, 56, 47],
[22, 80, 50, 9]])
nd[0, 1]
#通过索引取出来数据
85
根据索引修改数据
nd[2,2] = 2100
nd
array([[ 48, 85, 53, 22],
[ 24, 36, 26, 31],
[ 38, 26, 2100, 47],
[ 22, 80, 50, 9]])
2. 切片
一维与列表完全一致
多维时同理
nd
array([[ 48, 85, 53, 22],
[ 24, 36, 26, 31],
[ 38, 26, 2100, 47],
[ 22, 80, 50, 9]])
nd[:-2]
array([[48, 85, 53, 22],
[24, 36, 26, 31]])
nd[:-2] = 10
nd
array([[ 10, 10, 10, 10],
[ 10, 10, 10, 10],
[ 38, 26, 2100, 47],
[ 22, 80, 50, 9]])
np.random.randint(0,10,size = (2,1))
array([[2],
[8]])
nd[:2, 1:3] = np.random.randint(0,10,size = (2,1))
nd
#广播机制,如果在赋值的时候,不充分,numpy会自动进行复制
array([[ 10, 0, 0, 10],
[ 10, 8, 8, 10],
[ 38, 26, 2100, 47],
[ 22, 80, 50, 9]])
将数据反转,例如[1,2,3]---->[3,2,1]
nd1 = np.random.randint(0,100,size = 11)
nd1
array([53, 0, 12, 83, 98, 46, 36, 96, 21, 51, 34])
nd1[::-1]
array([34, 51, 21, 96, 36, 46, 98, 83, 12, 0, 53])
#::叫步幅
nd1[::-2]
array([34, 21, 36, 98, 12, 53])
nd
array([[ 10, 0, 0, 10],
[ 10, 8, 8, 10],
[ 38, 26, 2100, 47],
[ 22, 80, 50, 9]])
nd[::1,::-1]
array([[ 10, 0, 0, 10],
[ 10, 8, 8, 10],
[ 47, 2100, 26, 38],
[ 9, 50, 80, 22]])
nd[::-1,::-1]
array([[ 9, 50, 80, 22],
[ 47, 2100, 26, 38],
[ 10, 8, 8, 10],
[ 10, 0, 0, 10]])
两个::进行切片
3. 变形
使用reshape函数,注意参数是一个tuple!
#扩展
cat = plt.imread("./cat.jpg")
cat_f = cat/255.0
plt.imshow(cat_f)
<matplotlib.image.AxesImage at 0xd380b70>
fish = plt.imread("./fish.png")
plt.imshow(fish)
<matplotlib.image.AxesImage at 0xd2b3940>
fish
array([[[0.29411766, 0.39215687, 0.46666667],
[0.46666667, 0.4862745 , 0.49803922],
[0.4627451 , 0.4862745 , 0.5019608 ],
...,
[0.4627451 , 0.48235294, 0.49803922],
[0.45882353, 0.47843137, 0.49803922],
[0.21960784, 0.33333334, 0.44313726]],
[[0.2901961 , 0.3764706 , 0.44313726],
[0.627451 , 0.6156863 , 0.60784316],
[0.85490197, 0.85490197, 0.84705883],
...,
[0.8627451 , 0.85882354, 0.8509804 ],
[0.8509804 , 0.8509804 , 0.84313726],
[0.30588236, 0.42352942, 0.5254902 ]],
[[0.28235295, 0.37254903, 0.4392157 ],
[0.6666667 , 0.6627451 , 0.654902 ],
[1. , 1. , 1. ],
...,
[1. , 1. , 1. ],
[1. , 1. , 1. ],
[0.35686275, 0.4745098 , 0.5764706 ]],
...,
[[0.4509804 , 0.45882353, 0.45882353],
[0.6509804 , 0.6509804 , 0.64705884],
[0.99215686, 0.99215686, 0.9843137 ],
...,
[1. , 0.99607843, 0.9882353 ],
[0.9843137 , 0.9882353 , 0.98039216],
[0.36078432, 0.49019608, 0.6 ]],
[[0.4509804 , 0.45882353, 0.45882353],
[0.6509804 , 0.6509804 , 0.64705884],
[0.99215686, 0.99215686, 0.9843137 ],
...,
[1. , 0.99607843, 0.9882353 ],
[0.9843137 , 0.9882353 , 0.98039216],
[0.36078432, 0.49019608, 0.6 ]],
[[0.44705883, 0.45490196, 0.45490196],
[0.65882355, 0.654902 , 0.654902 ],
[1. , 1. , 1. ],
...,
[1. , 1. , 1. ],
[1. , 1. , 1. ],
[0.36078432, 0.49411765, 0.6 ]]], dtype=float32)
#拿出来鱼头
fish_head = fish[50:175, 50:180]
plt.imshow(fish_head)
<matplotlib.image.AxesImage at 0xd41bd30>
#把猫挑选出来一部分,把鱼头贴上去
cat_f[120:245,220:350] = fish_head
# cat.flags.writeable = True
# fish.flags.writeable = True
plt.imshow(cat_f)
#肯定会报错,一个是png 一个是jpg
<matplotlib.image.AxesImage at 0xd4be9b0>
4. 级联
- np.concatenate()
级联需要注意的点:
- 级联的参数是列表:一定要加中括号或小括号
- 维度必须相同
- 形状相符
- 【重点】级联的方向默认是shape这个tuple的第一个值所代表的维度方向
- 可通过axis参数改变级联的方向
import numpy as np
nd1 = np.random.randint(0,10,size = (4,6))
nd2 = np.random.randint(50,100,size =(2,6))
np.concatenate([nd1, nd2])
#第一个参数 可以传一个list或者tuple 第二个参数是轴 axis
#默认的轴等于0 行上面进行级联
array([[ 3, 0, 8, 6, 9, 4],
[ 8, 4, 4, 1, 0, 1],
[ 4, 2, 5, 9, 4, 9],
[ 0, 6, 6, 3, 5, 4],
[50, 84, 96, 68, 78, 76],
[96, 51, 61, 65, 67, 57]])
nd1 = np.random.randint(10,20,size = (3,4))
nd2 = np.random.randint(40,60,size = (3,2))
np.concatenate((nd1,nd2), axis = 1)
array([[12, 17, 14, 12, 56, 44],
[13, 17, 17, 17, 51, 49],
[13, 16, 18, 15, 45, 59]])
- np.hstack与np.vstack
水平级联与垂直级联,处理自己,进行维度的变更
nd3 = np.random.randint(0,10,size = (10,1))
nd3
array([[8],
[5],
[1],
[2],
[3],
[8],
[0],
[7],
[9],
[5]])
#hstack 水平级联
#horizontal :水平的
np.hstack(nd3)
#变成水平的之后,维度也变了
array([8, 5, 1, 2, 3, 8, 0, 7, 9, 5])
#vstack 垂直级联
#vertical :垂直的
nd4 = np.random.randint(-10,10,size = 10)
nd4
array([-10, -2, 3, 7, 1, 6, 6, -7, 0, -1])
np.vstack(nd4)
array([[-10],
[ -2],
[ 3],
[ 7],
[ 1],
[ 6],
[ 6],
[ -7],
[ 0],
[ -1]])
5. 切分
与级联类似,三个函数完成切分工作:
- np.split
- np.vsplit
- np.hsplit
nd = np.random.randint(0,100,size = (5,6))
nd
array([[44, 65, 84, 1, 83, 71],
[14, 71, 39, 21, 11, 27],
[27, 2, 89, 5, 13, 70],
[97, 63, 91, 45, 26, 71],
[86, 22, 3, 90, 56, 54]])
np.vsplit(nd,[1,4])
[array([[44, 65, 84, 1, 83, 71]]), array([[14, 71, 39, 21, 11, 27],
[27, 2, 89, 5, 13, 70],
[97, 63, 91, 45, 26, 71]]), array([[86, 22, 3, 90, 56, 54]])]
np.hsplit(nd,[1,3,8])
[array([[44],
[14],
[27],
[97],
[86]]), array([[65, 84],
[71, 39],
[ 2, 89],
[63, 91],
[22, 3]]), array([[ 1, 83, 71],
[21, 11, 27],
[ 5, 13, 70],
[45, 26, 71],
[90, 56, 54]]), array([], shape=(5, 0), dtype=int32)]
nd
array([[44, 65, 84, 1, 83, 71],
[14, 71, 39, 21, 11, 27],
[27, 2, 89, 5, 13, 70],
[97, 63, 91, 45, 26, 71],
[86, 22, 3, 90, 56, 54]])
np.split(nd,[2], axis = 1)
#axis = 0 默认的一种情况 行上面
[array([[44, 65],
[14, 71],
[27, 2],
[97, 63],
[86, 22]]), array([[84, 1, 83, 71],
[39, 21, 11, 27],
[89, 5, 13, 70],
[91, 45, 26, 71],
[ 3, 90, 56, 54]])]
6. 副本
所有赋值运算不会为ndarray的任何元素创建副本。对赋值后的对象的操作也对原来的对象生效。
nd = np.random.randint(0,10,size = 6)
nd
array([9, 8, 4, 4, 1, 7])
nd[5] = 1000
nd
array([ 9, 8, 4, 4, 1, 1000])
可使用copy()函数创建副本
nd_copy = nd.copy()
nd_copy
array([ 9, 8, 4, 4, 1, 1000])
四、ndarray的聚合操作
1. 求和np.sum
np.power([2,3,4],3)
array([ 8, 27, 64], dtype=int32)
nd = np.random.randint(0,10,size = (3,4))
nd
array([[1, 1, 6, 7],
[2, 7, 0, 1],
[6, 6, 8, 0]])
np.power(nd, 2)
array([[ 1, 1, 36, 49],
[ 4, 49, 0, 1],
[36, 36, 64, 0]], dtype=int32)
nd = np.random.randint(0,10,size =(2,3))
nd
array([[4, 8, 6],
[0, 7, 0]])
nd.sum()/6
4.166666666666667
nd.sum(axis = 1)
array([18, 7])
nd.sum(axis = 0)
array([ 4, 15, 6])
nd.mean()
#求平均值
4.166666666666667
nd.mean(axis = 0)
array([2. , 7.5, 3. ])
nd
array([[4, 8, 6],
[0, 7, 0]])
nd.argmin()
3
nd1 = np.random.randint(12,34,size = (4,5))
nd1
array([[27, 25, 30, 30, 20],
[15, 30, 15, 27, 28],
[31, 13, 27, 12, 26],
[29, 22, 23, 15, 20]])
nd1.argmin()
#非常有用!!!!
13
nd1.argmax()
10
2. 最大最小值:np.max/ np.min
同理
nd1.max()
31
3. 其他聚合操作
Function Name NaN-safe Version Description
np.sum np.nansum Compute sum of elements
np.prod np.nanprod Compute product of elements
np.mean np.nanmean Compute mean of elements
np.std np.nanstd Compute standard deviation
np.var np.nanvar Compute variance
np.min np.nanmin Find minimum value
np.max np.nanmax Find maximum value
np.argmin np.nanargmin Find index of minimum value
np.argmax np.nanargmax Find index of maximum value
np.median np.nanmedian Compute median of elements
np.percentile np.nanpercentile Compute rank-based statistics of elements
np.any N/A Evaluate whether any elements are true
np.all N/A Evaluate whether all elements are true
np.power 幂运算
np.sum 和 np.nansum 的区别
nan not a number
操作文件
使用pandas打开文件president_heights.csv
获取文件中的数据
五、ndarray的矩阵操作
1. 基本矩阵操作
- 算术运算符:
- 加减乘除
nd = np.random.randint(0,10,size = (5,5))
nd
array([[6, 9, 2, 8, 5],
[5, 8, 3, 3, 6],
[3, 6, 3, 0, 5],
[4, 0, 6, 7, 2],
[6, 6, 8, 8, 5]])
nd + 3
#广播机制在里面
array([[ 9, 12, 5, 11, 8],
[ 8, 11, 6, 6, 9],
[ 6, 9, 6, 3, 8],
[ 7, 3, 9, 10, 5],
[ 9, 9, 11, 11, 8]])
nd/2
array([[3. , 4.5, 1. , 4. , 2.5],
[2.5, 4. , 1.5, 1.5, 3. ],
[1.5, 3. , 1.5, 0. , 2.5],
[2. , 0. , 3. , 3.5, 1. ],
[3. , 3. , 4. , 4. , 2.5]])
#系统还给咱们提供了一些方法
np.multiply(nd, 2)
#乘法
array([[12, 18, 4, 16, 10],
[10, 16, 6, 6, 12],
[ 6, 12, 6, 0, 10],
[ 8, 0, 12, 14, 4],
[12, 12, 16, 16, 10]])
np.subtract(nd,100)
#减法
array([[ -94, -91, -98, -92, -95],
[ -95, -92, -97, -97, -94],
[ -97, -94, -97, -100, -95],
[ -96, -100, -94, -93, -98],
[ -94, -94, -92, -92, -95]])
- 矩阵积np.dot()
矩阵乘法
nd1 = np.random.randint(0,10,size = (2,3))
nd2 = np.random.randint(0,10,size = (3,4))
np.dot(nd1,nd2)
array([[65, 54, 63],
[76, 66, 66]])
2. 广播机制
【重要】ndarray广播机制的两条规则
- 规则一:为缺失的维度补1
- 规则二:假定缺失元素用已有值填充
例1:
m = np.ones((2, 3))
a = np.arange(3)
求M+a
例2:
a = np.arange(3).reshape((3, 1))
b = np.arange(3)
求a+b
习题
a = np.ones((4, 1))
b = np.arange(4)
求a+b
六、ndarray的排序
小测验:
使用以上所学numpy的知识,对一个ndarray对象进行选择排序。
def Sortn(x):
代码越短越好
#必须会默写至少两个排序
nd = np.random.randint(0,100,size = 10)
nd
array([40, 20, 21, 24, 88, 42, 30, 38, 35, 76])
def sort_nd(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
sort_nd(nd)
array([20, 21, 24, 30, 35, 38, 40, 42, 76, 88])
nd = np.random.randint(0,100,size = 10)
nd
array([12, 56, 48, 39, 64, 15, 58, 83, 10, 0])
def sort_nd2(nd):
for i in range(nd.size):
#argmin
#获取最小值的索引值
index_min = np.argmin(nd[i:]) + i
#当i = 0 index_min = 9
#当i = 1 index_min =
nd[i] ,nd[index_min]= nd[index_min],nd[i]
return nd
sort_nd2(nd)
array([ 0, 10, 12, 15, 39, 48, 56, 58, 64, 83])
1. 快速排序
np.sort()与ndarray.sort()都可以,但有区别:
- np.sort()不改变输入
- ndarray.sort()本地处理,不占用空间,但改变输入
nd = np.random.randint(0,100,size = 10)
nd
array([84, 73, 91, 38, 3, 56, 43, 70, 61, 72])
np.sort(nd)
array([ 3, 38, 43, 56, 61, 70, 72, 73, 84, 91])
nd
array([84, 73, 91, 38, 3, 56, 43, 70, 61, 72])
nd.sort()
nd
array([ 3, 38, 43, 56, 61, 70, 72, 73, 84, 91])
2. 部分排序
np.partition(a,k)
有的时候我们不是对全部数据感兴趣,我们可能只对最小或最大的一部分感兴趣。
- 当k为正时,我们想要得到最小的k个数
- 当k为负时,我们想要得到最大的k个数
nd = np.random.randint(0,1000,size = 500)
nd
array([679, 723, 152, 187, 847, 859, 843, 762, 239, 132, 183, 369, 168,
949, 533, 97, 480, 851, 309, 70, 140, 741, 383, 725, 478, 762,
553, 919, 935, 408, 295, 610, 601, 74, 986, 889, 600, 210, 945,
285, 209, 719, 111, 874, 347, 630, 978, 451, 500, 366, 773, 62,
506, 610, 619, 151, 667, 936, 234, 358, 846, 767, 865, 524, 126,
856, 832, 466, 428, 341, 474, 117, 891, 579, 287, 286, 947, 687,
368, 770, 838, 7, 246, 327, 513, 425, 794, 226, 144, 692, 423,
313, 457, 31, 900, 822, 781, 678, 548, 204, 687, 872, 134, 852,
264, 720, 894, 487, 780, 959, 633, 570, 54, 949, 336, 138, 319,
683, 115, 209, 56, 469, 326, 400, 362, 373, 726, 971, 948, 376,
575, 680, 122, 657, 961, 467, 586, 136, 763, 926, 533, 698, 960,
307, 609, 636, 649, 153, 308, 906, 520, 148, 465, 567, 231, 446,
456, 757, 388, 683, 946, 412, 671, 946, 959, 867, 673, 837, 518,
369, 494, 166, 808, 188, 253, 780, 511, 888, 332, 332, 8, 645,
779, 542, 998, 512, 287, 430, 835, 608, 759, 114, 740, 107, 552,
279, 885, 491, 346, 892, 739, 711, 908, 76, 233, 715, 915, 869,
673, 458, 21, 576, 297, 389, 35, 295, 25, 486, 664, 326, 260,
7, 87, 47, 242, 579, 889, 654, 465, 250, 364, 471, 758, 329,
579, 964, 774, 722, 710, 437, 763, 252, 551, 939, 765, 988, 186,
929, 767, 548, 583, 307, 775, 147, 936, 779, 959, 915, 673, 924,
456, 127, 472, 157, 287, 427, 449, 987, 174, 469, 148, 733, 846,
193, 725, 197, 988, 833, 498, 701, 696, 369, 915, 205, 81, 978,
218, 18, 984, 937, 169, 67, 617, 711, 177, 755, 691, 983, 360,
939, 313, 11, 54, 612, 626, 774, 442, 833, 547, 304, 967, 928,
85, 552, 231, 865, 227, 71, 997, 492, 484, 782, 498, 139, 361,
27, 925, 988, 842, 279, 185, 924, 932, 799, 972, 150, 107, 875,
949, 974, 445, 908, 733, 303, 909, 658, 941, 590, 14, 992, 800,
702, 409, 84, 62, 757, 865, 917, 711, 960, 448, 417, 961, 826,
215, 406, 208, 796, 12, 208, 86, 799, 533, 755, 806, 869, 245,
493, 128, 39, 572, 171, 951, 798, 101, 676, 715, 388, 707, 98,
35, 340, 397, 743, 166, 53, 568, 460, 545, 430, 349, 971, 370,
939, 138, 346, 96, 983, 393, 297, 615, 565, 805, 665, 435, 957,
991, 726, 489, 358, 86, 278, 124, 617, 643, 150, 583, 462, 658,
802, 848, 74, 807, 201, 354, 261, 408, 759, 361, 157, 829, 687,
963, 603, 617, 54, 306, 447, 952, 440, 972, 217, 808, 341, 586,
176, 852, 682, 770, 299, 108, 975, 440, 83, 807, 968, 131, 824,
428, 996, 556, 602, 159, 613, 711, 262, 342, 355, 191, 43, 666,
209, 766, 737, 829, 857, 263, 231, 992, 605, 479, 967, 168, 770,
885, 924, 986, 867, 130, 249])
np.partition(nd,20)
array([ 7, 8, 7, 27, 11, 21, 18, 25, 12, 14, 31, 43, 47,
54, 54, 35, 54, 53, 39, 35, 56, 76, 67, 74, 62, 74,
71, 62, 70, 81, 86, 84, 86, 85, 83, 87, 108, 148, 131,
205, 153, 176, 111, 115, 101, 98, 159, 183, 132, 138, 136, 171,
168, 197, 193, 151, 157, 148, 107, 150, 127, 166, 168, 169, 126,
201, 191, 174, 177, 147, 140, 117, 130, 150, 128, 185, 124, 166,
122, 97, 187, 152, 134, 188, 107, 157, 114, 204, 144, 138, 139,
208, 96, 186, 208, 297, 287, 227, 231, 226, 253, 304, 246, 279,
264, 278, 279, 286, 287, 261, 297, 234, 306, 295, 303, 218, 260,
217, 242, 209, 209, 285, 231, 299, 249, 210, 295, 250, 262, 215,
252, 209, 287, 307, 245, 263, 239, 231, 233, 307, 533, 533, 467,
493, 406, 376, 417, 388, 308, 448, 520, 373, 465, 400, 326, 446,
456, 469, 388, 409, 319, 412, 336, 445, 340, 397, 487, 460, 518,
369, 494, 545, 430, 349, 370, 361, 511, 346, 332, 332, 393, 498,
484, 542, 492, 512, 457, 430, 313, 423, 435, 489, 425, 513, 327,
358, 547, 491, 346, 442, 368, 462, 313, 474, 341, 360, 428, 466,
524, 458, 354, 408, 358, 389, 361, 506, 447, 486, 366, 326, 500,
451, 440, 347, 341, 440, 428, 369, 465, 408, 364, 471, 498, 329,
342, 355, 478, 469, 383, 437, 449, 309, 427, 480, 533, 472, 369,
479, 456, 362, 548, 583, 775, 723, 936, 779, 959, 915, 673, 924,
767, 929, 988, 765, 939, 551, 763, 987, 710, 722, 774, 733, 846,
964, 725, 579, 988, 833, 758, 701, 696, 654, 915, 889, 579, 978,
664, 576, 984, 937, 673, 869, 617, 711, 915, 755, 691, 983, 715,
939, 908, 711, 739, 612, 626, 774, 892, 833, 885, 552, 967, 928,
740, 552, 759, 865, 608, 835, 997, 998, 779, 782, 645, 888, 780,
808, 925, 988, 842, 837, 673, 924, 932, 799, 972, 867, 959, 875,
949, 974, 946, 908, 733, 671, 909, 658, 941, 590, 946, 992, 800,
702, 683, 757, 567, 757, 865, 917, 711, 960, 906, 649, 961, 826,
636, 609, 960, 796, 698, 926, 763, 799, 586, 755, 806, 869, 961,
657, 680, 575, 572, 948, 951, 798, 971, 676, 715, 726, 707, 683,
949, 570, 633, 743, 959, 780, 568, 894, 720, 852, 872, 971, 687,
939, 548, 678, 781, 983, 822, 900, 615, 565, 805, 665, 692, 957,
991, 726, 794, 838, 770, 687, 947, 617, 643, 579, 583, 891, 658,
802, 848, 832, 807, 856, 865, 767, 846, 759, 936, 667, 829, 687,
963, 603, 617, 619, 610, 773, 952, 978, 972, 630, 808, 874, 586,
719, 852, 682, 770, 945, 600, 975, 889, 986, 807, 968, 601, 824,
610, 996, 556, 602, 935, 613, 711, 919, 553, 762, 725, 741, 666,
851, 766, 737, 829, 857, 949, 762, 992, 605, 843, 967, 859, 770,
885, 924, 986, 867, 847, 679])