02_numpy

numpy get started

# python: 一切皆对象
# linux: 一切皆文件.
# 数据分析: 一切皆矩阵

导入numpy库,并查看numpy版本

import numpy as np
np.__version__
'1.17.0'

如果没有安装相关包:
!pip3 install matplotlib -i https://pypi.douban.com/simple
!pip3 install pillow -i https://pypi.douban.com/simple

import matplotlib.pyplot as plt

# 读取图片
cat = plt.imread("cat.jpg")

numpy 里面的矩阵不是真正的矩阵, 只是numpy用来表示矩阵的一种数据结构, 叫做ndarray

cat是一个彩色图片.
jpg的结尾的图片,里面RGB颜色值都是0到255的整数.
png结尾的图片,里面RBG颜色值都是0到1的浮点数.

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)
print(cat)
[[[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]]]
type(cat)
numpy.ndarray
plt.imshow(cat)
<matplotlib.image.AxesImage at 0x7fccb0da0908>

png

cat.shape  # 三维的
(456, 730, 3)
#请问电影是什么,nd.array 四维
#(x,456,760,3)

一、创建ndarray

1. 使用np.array()由python list创建

参数为列表:
[1, 4, 2, 5, 3]

注意:

  • numpy默认ndarray的所有元素的类型是相同的
  • 如果传进来的列表中包含不同的类型,则统一为同一类型,优先级:str>float>int
l = [1, 4, 2, 5, 3]
n = np.array(l)
n
array([1, 4, 2, 5, 3])
l = [1, 4, 2, '5', 3.0]
n = np.array(l)
n
array(['1', '4', '2', '5', '3.0'], dtype='<U21')
n[0] = 2
n
array(['2', '4', '2', '5', '3.0'], dtype='<U21')
l
[1, 4, 2, '5', 3.0]
l[0] = 10
l
[10, 4, 2, '5', 3.0]
n
array(['2', '4', '2', '5', '3.0'], dtype='<U21')
n.shape
(5,)
n2 = np.array([[3, 4, 7, 1], [3, 0, 1, 6], [4, 0, 1, 4]])
n2
array([[3, 4, 7, 1],
       [3, 0, 1, 6],
       [4, 0, 1, 4]])
n2.shape
(3, 4)
lis = [[[1,2,3,4], [3,4,5,6]], [[1,2,3,4], [3,4,5,6]]]
n3 = np.array(lis)
n3
array([[[1, 2, 3, 4],
        [3, 4, 5, 6]],

       [[1, 2, 3, 4],
        [3, 4, 5, 6]]])
n3.shape
(2, 2, 4)

2. 使用np的routines函数创建

包含以下常见创建方法:

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

# 用来产生全是1的ndarray
# shape是用来指定ndarray的形状
# dtype是数字的类型
np.ones(shape=(4, 8), dtype=np.int8)
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]], dtype=int8)
n2 = np.ones((2, 3, 4), dtype=int)
n2
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]]])

2) np.zeros(shape, dtype=float, order='C')

# 全是0 的ndarray
np.zeros(shape=(4, 3), dtype=np.float32)
array([[0., 0., 0.],
       [0., 0., 0.],
       [0., 0., 0.],
       [0., 0., 0.]], dtype=float32)
n2 = np.zeros((5, 5))
n2
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.]])

3) np.full(shape, fill_value, dtype=None, order='C')

# 按照指定数字产生ndarray
np.full((3, 5), fill_value=8.)
array([[8., 8., 8., 8., 8.],
       [8., 8., 8., 8., 8.],
       [8., 8., 8., 8., 8.]])

4) np.eye(N, M=None, k=0, dtype=float)
对角线为1其他的位置为0

# 生成对角线全是1,其他位置全是0的2维ndarray
# k=0 表示主对角线的索引.
# 像这种主对角线全是1, 其他位置全是0的矩阵,我们叫做单位矩阵.
# 任何矩阵乘于他的单位矩阵,等于他本身.
# 如果一个矩阵可以通过矩阵的行列式变化为单位矩阵,那么说明这个矩阵有唯一的解.
np.eye(6)
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.],
       [0., 0., 0., 0., 0., 1.]])
np.eye(6, k=1)
array([[0., 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., 0., 0., 0., 0., 0.]])
np.eye(3, M=5, dtype=int)
array([[1, 0, 0, 0, 0],
       [0, 1, 0, 0, 0],
       [0, 0, 1, 0, 0]])

5) np.linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None)

# endpoint是否包含stop的值
# retstep是否返回步长.
n = np.linspace(0, 100, num=50, dtype=int, retstep=True, endpoint=False)
n
(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, 64, 66,
        68, 70, 72, 74, 76, 78, 80, 82, 84, 86, 88, 90, 92, 94, 96, 98]), 2.0)
np.linspace(0, 150, num=30, dtype=np.int8)
array([   0,    5,   10,   15,   20,   25,   31,   36,   41,   46,   51,
         56,   62,   67,   72,   77,   82,   87,   93,   98,  103,  108,
        113,  118,  124, -127, -122, -117, -112, -106], dtype=int8)

6) np.arange([start, ]stop, [step, ]dtype=None)

range(0, 10)
range(0, 10)
# 就把np.arange当成numpy版本的range
# 使用np.arange的时候,尽量用整形参数.
np.arange(50)
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, 32, 33,
       34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49])
np.arange(0.1, 11.1, step=0.35)
array([ 0.1 ,  0.45,  0.8 ,  1.15,  1.5 ,  1.85,  2.2 ,  2.55,  2.9 ,
        3.25,  3.6 ,  3.95,  4.3 ,  4.65,  5.  ,  5.35,  5.7 ,  6.05,
        6.4 ,  6.75,  7.1 ,  7.45,  7.8 ,  8.15,  8.5 ,  8.85,  9.2 ,
        9.55,  9.9 , 10.25, 10.6 , 10.95])

7) np.random.randint(low, high=None, size=None, dtype='l')

np.random.randint(10)
9
# 左闭右开区间
n = np.random.randint(0, 100, size=(3, 4, 5))
n
array([[[95, 26, 25,  0,  6],
        [63, 91,  8,  6,  0],
        [28, 62, 80, 20, 69],
        [60,  0, 75,  3, 99]],

       [[52, 36, 55, 72, 14],
        [68,  4, 60, 83, 98],
        [14, 30, 14, 63, 65],
        [96, 69, 67, 13, 20]],

       [[99, 12, 17, 75, 78],
        [18,  7, 34,  0, 89],
        [33, 61, 32, 59, 99],
        [91, 73, 18, 38, 86]]])
n.shape
(3, 4, 5)
image = np.random.randint(0, 255, size=(456, 730, 3))
image.shape
(456, 730, 3)
plt.imshow(image)
<matplotlib.image.AxesImage at 0x7fccb0cac0f0>

png

8) np.random.randn(d0, d1, ..., dn)

标准正太分布

# 平均值为0, 方差为1的正态分布, 就是标准正态分布.
n = np.random.randn(1000, 30, 2)
n
array([[[ 0.4896418 , -0.60883984],
        [ 0.26853052, -0.60839539],
        [ 0.31294487,  1.31865127],
        ...,
        [ 0.26797445,  0.11520259],
        [ 2.04995086,  0.09461803],
        [ 1.07127391, -1.31067053]],

       [[ 0.34593488,  0.24465173],
        [-0.4157803 ,  0.29663043],
        [-0.14079407, -0.41392739],
        ...,
        [ 0.90807284,  1.17782632],
        [ 0.12260718, -1.16451586],
        [ 0.06554271, -1.3787287 ]],

       [[ 0.80285065,  0.08647407],
        [ 1.33162021, -1.02656728],
        [-1.67802604, -1.04042113],
        ...,
        [-0.91478354,  2.76745611],
        [-0.6926192 ,  0.57983548],
        [-0.79930956, -0.57962272]],

       ...,

       [[ 0.20735501,  1.90684661],
        [-1.27024817, -0.41497321],
        [-1.02678114, -0.49408855],
        ...,
        [ 0.82887736,  0.74195174],
        [-0.28360596,  0.85135051],
        [-1.50978638, -1.88802704]],

       [[-0.02460612,  0.74332003],
        [ 0.67222545, -1.60336698],
        [ 0.71210037,  0.87587366],
        ...,
        [-1.16626156, -0.1063151 ],
        [-0.53933263,  1.2383391 ],
        [ 0.33725789, -0.21701002]],

       [[ 0.74405161,  1.06175663],
        [-1.22704475,  0.261197  ],
        [ 0.25032738, -0.10785808],
        ...,
        [ 0.22590857,  2.22273275],
        [-0.72196512, -0.07164419],
        [-0.58561601,  1.46280227]]])

9)np.random.normal(loc=0.0, scale=1.0, size=None)

import numpy as np
# 正态分布,也叫高斯分布
# 概率密度曲线
# loc = location
# scale = 
n = np.random.normal(loc=10, scale=3, size=(1000, 2))
n
array([[ 2.11633679, 10.37104791],
       [12.79906613,  9.32691209],
       [ 8.73445973,  6.49449323],
       ...,
       [10.90201134,  6.89778288],
       [15.53889102,  6.386347  ],
       [12.84620178,  5.08296774]])
n.mean()
10.023737800147947
n.std()
3.04452541108808

10) np.random.random(size=None)
生成0到1的随机数,左闭右开

np.random.random(size=(5, 2, 3))  # dimension 纬度
array([[[0.72040153, 0.09664452, 0.27252567],
        [0.06722621, 0.7023822 , 0.84930292]],

       [[0.87268163, 0.81145728, 0.87352575],
        [0.1631786 , 0.80191163, 0.58542209]],

       [[0.60120435, 0.04436833, 0.15913068],
        [0.90660888, 0.42009378, 0.27366765]],

       [[0.00406047, 0.50875819, 0.09918324],
        [0.87496182, 0.24859695, 0.97042554]],

       [[0.3902245 , 0.73079414, 0.93259252],
        [0.96045494, 0.7478134 , 0.25658827]]])
# 和np.random.random一样
np.random.rand(5, 2, 3)
array([[[0.17731902, 0.44709539, 0.34512151],
        [0.0648051 , 0.76571718, 0.80429627]],

       [[0.61488974, 0.79271908, 0.79005958],
        [0.65512793, 0.5050555 , 0.09845543]],

       [[0.30139638, 0.36260231, 0.50582467],
        [0.75408696, 0.13440877, 0.01720899]],

       [[0.41123981, 0.1544165 , 0.52926244],
        [0.76862384, 0.35174404, 0.45054936]],

       [[0.93168058, 0.05395682, 0.59795456],
        [0.77248911, 0.62356132, 0.62255185]]])

二、ndarray的属性

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

n = np.random.randint(0, 100, size=(3, 4, 5))
n
array([[[97, 96, 97, 27,  7],
        [63, 74, 32, 72, 96],
        [45, 53, 69, 47, 38],
        [66, 32, 34, 53,  9]],

       [[96, 52, 42, 93, 47],
        [99, 81, 33, 36, 27],
        [40, 38, 34, 40, 77],
        [88, 16, 95, 98, 49]],

       [[ 6,  2, 54, 32,  2],
        [23, 37, 59,  4, 53],
        [58, 22, 61, 50, 24],
        [72, 94, 75, 39, 16]]])
n.ndim
3
n.shape
(3, 4, 5)
n.size
60
n.dtype
dtype('int64')

三、ndarray的基本操作

1. 索引

一维与列表完全一致
多维时同理

l = [5, 2, 3, 4, 7]
n = np.array(l)
n
array([5, 2, 3, 4, 7])
n[0]
5
l[0]
5
n2 = np.random.randint(0, 100, size=(4, 5))
n2
array([[73, 64, 29, 35, 61],
       [91, 30, 63, 40, 21],
       [22, 69, 83,  9, 42],
       [13, 34,  0, 71, 30]])
n2[0]
array([73, 64, 29, 35, 61])
n2[0][1]
64
n2[0, 1]
64
n2[1, 3]
40
n3 = np.random.randint(0, 50, size=(3, 4, 3, 6))
n3
array([[[[36, 12, 19, 20, 13, 47],
         [ 6, 26, 47, 29,  7,  3],
         [14, 46, 23, 13, 23,  1]],

        [[13, 33, 14, 49, 14, 28],
         [49, 26,  6, 28,  1,  5],
         [ 0, 11, 19, 12, 12, 24]],

        [[28, 38, 21, 10, 33, 23],
         [ 5, 22, 36, 14, 43, 45],
         [30, 38,  7, 39,  8, 17]],

        [[13, 22, 19,  0, 13, 34],
         [13, 31, 35, 30, 38,  5],
         [36, 44,  2,  1, 49, 36]]],


​ [[[10, 28, 22, 49, 4, 24],
​ [36, 32, 11, 24, 3, 13],
​ [ 2, 1, 23, 41, 11, 30]],

        [[13, 24, 46, 36, 10, 19],
         [ 2, 29,  9, 11, 35,  7],
         [ 2, 11, 16, 33, 19, 11]],

        [[ 4, 17, 45, 44, 36, 23],
         [ 4, 21, 49, 33, 12, 33],
         [33, 21, 12,  9, 31, 43]],

        [[12,  0, 27, 12, 24, 24],
         [49,  5, 18, 45, 26, 35],
         [19,  7, 11,  1,  2, 24]]],


​ [[[19, 4, 25, 48, 23, 4],
​ [24, 12, 48, 3, 46, 14],
​ [33, 42, 2, 15, 1, 26]],

        [[24, 25, 41, 23, 31, 34],
         [45,  3, 47, 27, 32, 29],
         [47,  5, 34, 11, 49, 41]],

        [[42, 36, 11, 24, 46, 22],
         [18, 25, 18, 26,  0, 16],
         [34,  9, 31, 11, 14, 48]],

        [[48, 26, 49, 36, 39,  4],
         [ 3, 27, 17, 13, 41,  5],
         [31,  8, 29, 15, 33, 45]]]])
n3.shape
(3, 4, 3, 6)
n3.ndim
4
n3[0, 1, 0, 1]
33
# 根据索引修改数据
n3[0, 1, 0, 1] = 88
n3[0, 1, 0, 1]
88

2. 切片

一维与列表完全一致
多维时同理

l = [5, 2, 3, 8, 4]
n = np.array(l)
n
array([5, 2, 3, 8, 4])
l[1:]
[2, 3, 8, 4]
n[1:]
array([2, 3, 8, 4])
# 将列表反转
l[::-1]
[4, 8, 3, 2, 5]
# 将array反转
n[::-1]
array([4, 8, 3, 2, 5])
n2 = np.random.randint(0, 255, size=(4, 5))
n2
array([[129, 125,  60,  10, 114],
       [ 90, 209, 119,  37,  66],
       [218, 247,  30, 111, 211],
       [ 65, 165,  15, 247,  23]])
# 二维array反转
n2[::-1]
array([[ 65, 165,  15, 247,  23],
       [218, 247,  30, 111, 211],
       [ 90, 209, 119,  37,  66],
       [129, 125,  60,  10, 114]])
n2[0:2]
array([[129, 125,  60,  10, 114],
       [ 90, 209, 119,  37,  66]])
n2[0:2][0][0:3]
array([129, 125,  60])

3. 变形

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

n = np.random.randint(0, 100, size=(3, 4, 5))
n
array([[[17, 80, 74, 62, 53],
        [26, 85,  4, 13, 61],
        [73, 84, 14, 97, 99],
        [61, 54,  9, 54, 35]],

       [[31,  1, 68,  4, 67],
        [21, 71, 27, 47, 97],
        [19,  0, 65, 48,  2],
        [58,  6,  1, 19, 49]],

       [[60, 98, 27, 76, 51],
        [25,  3, 49, 30, 32],
        [74, 89, 29, 26, 71],
        [13,  5, 14, 24,  2]]])
n.size
60
n.reshape(6, 10)  # 新的array的size必须和原array的size相同
array([[17, 80, 74, 62, 53, 26, 85,  4, 13, 61],
       [73, 84, 14, 97, 99, 61, 54,  9, 54, 35],
       [31,  1, 68,  4, 67, 21, 71, 27, 47, 97],
       [19,  0, 65, 48,  2, 58,  6,  1, 19, 49],
       [60, 98, 27, 76, 51, 25,  3, 49, 30, 32],
       [74, 89, 29, 26, 71, 13,  5, 14, 24,  2]])
n.size  # 新的array的size必须和原array的size相同
60
# # reshape之后的size不能发生变化.
n.reshape(5, 2, 6)
array([[[17, 80, 74, 62, 53, 26],
        [85,  4, 13, 61, 73, 84]],

       [[14, 97, 99, 61, 54,  9],
        [54, 35, 31,  1, 68,  4]],

       [[67, 21, 71, 27, 47, 97],
        [19,  0, 65, 48,  2, 58]],

       [[ 6,  1, 19, 49, 60, 98],
        [27, 76, 51, 25,  3, 49]],

       [[30, 32, 74, 89, 29, 26],
        [71, 13,  5, 14, 24,  2]]])
np.reshape(n, (4, 15))
array([[17, 80, 74, 62, 53, 26, 85,  4, 13, 61, 73, 84, 14, 97, 99],
       [61, 54,  9, 54, 35, 31,  1, 68,  4, 67, 21, 71, 27, 47, 97],
       [19,  0, 65, 48,  2, 58,  6,  1, 19, 49, 60, 98, 27, 76, 51],
       [25,  3, 49, 30, 32, 74, 89, 29, 26, 71, 13,  5, 14, 24,  2]])

4. 级联

  1. np.concatenate()
    • 级联的参数是列表:一定要加中括号或小括号
    • 维度必须相同
    • 形状相符
    • 【重点】级联的方向默认是shape这个tuple的第一个值所代表的维度方向
    • 可通过axis参数改变级联的方向
n = np.random.randint(0, 255, size=(5, 6))
n2 = np.random.randint(0, 244, size=(5, 6))
display(n, n2)
array([[156, 180, 226, 253, 245,  90],
       [154,  46,  81, 158, 170,  50],
       [231,  71,  17,  95, 205, 111],
       [ 24, 104, 198,   2,   2,   7],
       [ 29, 189,  33, 206,  55, 148]])
array([[178, 163, 174,  28,  47, 144],
       [ 85, 138, 120,   2,   5,  13],
       [ 60,  53,  24,  60, 243, 227],
       [ 26, 156,  22, 146, 105, 121],
       [215, 130, 160, 161, 105,  49]])
# axis=0 , 增加了行数, 垂直方向级联
np.concatenate((n, n2), axis=0)
array([[156, 180, 226, 253, 245,  90],
       [154,  46,  81, 158, 170,  50],
       [231,  71,  17,  95, 205, 111],
       [ 24, 104, 198,   2,   2,   7],
       [ 29, 189,  33, 206,  55, 148],
       [178, 163, 174,  28,  47, 144],
       [ 85, 138, 120,   2,   5,  13],
       [ 60,  53,  24,  60, 243, 227],
       [ 26, 156,  22, 146, 105, 121],
       [215, 130, 160, 161, 105,  49]])
# axis=1, 增加列数, 在水平方向上级联
np.concatenate((n, n2), axis=1)
array([[156, 180, 226, 253, 245,  90, 178, 163, 174,  28,  47, 144],
       [154,  46,  81, 158, 170,  50,  85, 138, 120,   2,   5,  13],
       [231,  71,  17,  95, 205, 111,  60,  53,  24,  60, 243, 227],
       [ 24, 104, 198,   2,   2,   7,  26, 156,  22, 146, 105, 121],
       [ 29, 189,  33, 206,  55, 148, 215, 130, 160, 161, 105,  49]])
  1. np.hstack与np.vstack
    • 水平级联与垂直级联,处理自己,进行维度的变更

array级联

n1 = np.random.randint(0, 100, size=(3, 4))
n2 = np.random.randint(0, 100, size=(3, 4))
display(n1, n2)
array([[46,  1, 47,  3],
       [54, 84, 77, 27],
       [52, 99, 88, 25]])
array([[10, 95, 45, 64],
       [58, 67, 53, 86],
       [63, 70, 58, 89]])
np.hstack((n1, n2))  # h = horizontal
array([[46,  1, 47,  3, 10, 95, 45, 64],
       [54, 84, 77, 27, 58, 67, 53, 86],
       [52, 99, 88, 25, 63, 70, 58, 89]])
np.vstack((n1, n2))  # v = vertical
array([[46,  1, 47,  3],
       [54, 84, 77, 27],
       [52, 99, 88, 25],
       [10, 95, 45, 64],
       [58, 67, 53, 86],
       [63, 70, 58, 89]])

图片级联

import matplotlib.pyplot as plt
cat = plt.imread('cat.jpg')
cat.shape
(456, 730, 3)
type(cat)
numpy.ndarray
plt.imshow(cat)
<matplotlib.image.AxesImage at 0x7eff3ddb4438>

png

image = np.random.randint(0, 255, size=(456, 730, 3))
image.shape
(456, 730, 3)
plt.imshow(image)
<matplotlib.image.AxesImage at 0x7eff3d901d30>

png

new_image = np.hstack((cat, image))
plt.imshow(new_image)
<matplotlib.image.AxesImage at 0x7eff3c07dc88>

png

new_image = np.vstack((cat, image))
plt.imshow(new_image)
<matplotlib.image.AxesImage at 0x7eff368f9278>

png

5. 切分

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

  • np.split
  • np.vsplit
  • np.hsplit
n = np.random.randint(0, 100, size=(4, 5))
n
array([[82, 42, 91, 36, 93],
       [ 5, 83, 45, 66, 51],
       [13, 18,  5, 56,  5],
       [10, 48, 62, 47, 27]])
# 左闭右开区间
np.split(n, [1, 2])
[array([[82, 42, 91, 36, 93]]),
 array([[ 5, 83, 45, 66, 51]]),
 array([[13, 18,  5, 56,  5],
        [10, 48, 62, 47, 27]])]
np.split(n, (2, 2))
[array([[82, 42, 91, 36, 93],
        [ 5, 83, 45, 66, 51]]),
 array([], shape=(0, 5), dtype=int64),
 array([[13, 18,  5, 56,  5],
        [10, 48, 62, 47, 27]])]
n2 = np.random.randint(0, 100, size=(7, 5))
n2
array([[99, 27, 16, 63, 93],
       [85, 65,  7, 37, 25],
       [97, 76, 94, 87, 75],
       [93, 93, 26, 63, 62],
       [ 6, 35,  7, 45, 22],
       [12,  9,  6, 75,  8],
       [ 6, 66, 72, 94, 58]])
np.split(n2, (2, 5))
[array([[99, 27, 16, 63, 93],
        [85, 65,  7, 37, 25]]), array([[97, 76, 94, 87, 75],
        [93, 93, 26, 63, 62],
        [ 6, 35,  7, 45, 22]]), array([[12,  9,  6, 75,  8],
        [ 6, 66, 72, 94, 58]])]
np.split(n2, (2,), axis=1)
[array([[99, 27],
        [85, 65],
        [97, 76],
        [93, 93],
        [ 6, 35],
        [12,  9],
        [ 6, 66]]), array([[16, 63, 93],
        [ 7, 37, 25],
        [94, 87, 75],
        [26, 63, 62],
        [ 7, 45, 22],
        [ 6, 75,  8],
        [72, 94, 58]])]
cat = plt.imread('cat.jpg')
cat.shape
(456, 730, 3)
plt.imshow(cat)
<matplotlib.image.AxesImage at 0x7eff3686aa20>

png

res = np.split(cat, 2, axis=0)
res
[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]],
 
        ...,
 
        [[181, 110,  92],
         [182, 111,  93],
         [184, 111,  94],
         ...,
         [ 94,  46,  44],
         [ 89,  43,  43],
         [ 89,  43,  43]],
 
        [[179, 108,  90],
         [181, 110,  92],
         [183, 110,  93],
         ...,
         [ 94,  46,  44],
         [ 91,  43,  43],
         [ 89,  43,  43]],
 
        [[179, 108,  90],
         [181, 110,  92],
         [182, 111,  93],
         ...,
         [ 95,  45,  44],
         [ 91,  43,  43],
         [ 89,  43,  43]]], dtype=uint8), array([[[178, 110,  91],
         [181, 110,  92],
         [182, 111,  93],
         ...,
         [ 95,  45,  44],
         [ 91,  43,  43],
         [ 91,  43,  43]],
 
        [[178, 110,  91],
         [180, 112,  93],
         [182, 111,  93],
         ...,
         [ 95,  45,  44],
         [ 91,  41,  42],
         [ 90,  42,  42]],
 
        [[176, 109,  90],
         [179, 111,  92],
         [180, 112,  93],
         ...,
         [ 96,  45,  44],
         [ 92,  42,  43],
         [ 91,  43,  43]],
 
        ...,
 
        [[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)]
plt.imshow(res[0])
<matplotlib.image.AxesImage at 0x7eff3653c828>

png

res = np.split(cat, 2, axis=1)
len(res)
2
type(res)
list
plt.imshow(res[0])
<matplotlib.image.AxesImage at 0x7eff36478588>

png

plt.imshow(res[1])
<matplotlib.image.AxesImage at 0x7eff364466a0>

png

6. 副本

所有赋值运算不会为ndarray的任何元素创建副本。对赋值后的对象的操作也对原来的对象生效。

l = [1, 2, 3, 5]
l2 = l
l2[2] = 8
l
[1, 2, 8, 5]
import numpy as np

n1 = np.arange(10)
n2 = n1
n2[3] = 100
n1
array([  0,   1,   2, 100,   4,   5,   6,   7,   8,   9])

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

n3 = np.random.randint(0, 20, size=(3, 5))
n3
array([[13, 11, 19,  8,  9],
       [ 2,  5, 17, 19, 11],
       [ 4,  5, 11,  0, 15]])
n4 = n3.copy()
n4[0, 1] = 88
n4
array([[13, 88, 19,  8,  9],
       [ 2,  5, 17, 19, 11],
       [ 4,  5, 11,  0, 15]])
n3
array([[13, 11, 19,  8,  9],
       [ 2,  5, 17, 19, 11],
       [ 4,  5, 11,  0, 15]])

四、ndarray的聚合操作

1. 求和np.sum

n = np.arange(11)
n
array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10])
np.sum(n)
55
n2 = np.random.randint(0, 100, size=(3, 4))
n2
array([[89, 44, 29, 66],
       [ 2,  4, 56, 95],
       [86, 41,  2, 90]])
np.sum(n2, axis=1)
array([228, 157, 219])

2. 最大最小值:np.max/ np.min

同理

n = np.arange(11)
n
array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10])
np.median(n)
5.0
np.mean(n)
5.0
n2 = np.random.randint(0, 100, size=10)
n2
array([63, 26, 83, 31,  2, 68, 38, 76, 96,  4])
np.mean(n2)
48.7
np.median(n2)
50.5
np.max(n2)
96
np.min(n2)
2
n3 = np.random.randint(0, 100, size=(5, 6))
n3
array([[ 4, 12,  9, 53, 51,  4],
       [24, 72,  8, 59, 42, 81],
       [58, 38,  2, 81, 34, 74],
       [70, 89, 11, 50, 66, 86],
       [ 2, 77, 86, 11, 84, 59]])
np.max(n3, axis=0)
array([70, 89, 86, 81, 84, 86])
np.max(n3, axis=1)
array([53, 81, 81, 89, 86])

3. 其他聚合操作

Function NameNaN-safe VersionDescription
np.sumnp.nansumCompute sum of elements
np.prodnp.nanprodCompute product of elements
np.meannp.nanmeanCompute mean of elements
np.stdnp.nanstdCompute standard deviation
np.varnp.nanvarCompute variance
np.minnp.nanminFind minimum value
np.maxnp.nanmaxFind maximum value
np.argminnp.nanargminFind index of minimum value
np.argmaxnp.nanargmaxFind index of maximum value
np.mediannp.nanmedianCompute median of elements
np.percentilenp.nanpercentileCompute rank-based statistics of elements
np.anyN/AEvaluate whether any elements are true
np.allN/AEvaluate whether all elements are true
np.power幂运算
n = np.random.randint(0, 100, size=(5, 6))
n
array([[35, 87, 31, 31, 62, 48],
       [19, 15, 25, 39, 31, 23],
       [43, 61, 58, 49, 30, 80],
       [83, 48, 94, 38, 10,  9],
       [ 9, 11,  1, 28, 31, 70]])
np.argmin(n, axis=0)
array([4, 4, 4, 4, 3, 3])
n2 = np.random.randint(0, 10, size=(2, 5))
n2
array([[1, 5, 1, 8, 4],
       [1, 6, 9, 8, 5]])
np.reshape(n2, (-1,))
array([1, 5, 1, 8, 4, 1, 6, 9, 8, 5])

np.sum 和 np.nansum 的区别
nan not a number

a = np.array([1, 2, np.nan])
a
array([ 1.,  2., nan])
np.sum(a)
nan
np.nansum(a)
3.0
操作文件

使用pandas打开文件president_heights.csv
获取文件中的数据

!pip3 install pandas
import pandas as pd
data = pd.read_csv('president_heights.csv')
type(data)
pandas.core.frame.DataFrame
data
ordernameheight(cm)
01George Washington189
12John Adams170
23Thomas Jefferson189
34James Madison163
45James Monroe183
56John Quincy Adams171
67Andrew Jackson185
78Martin Van Buren168
89William Henry Harrison173
910John Tyler183
1011James K. Polk173
1112Zachary Taylor173
1213Millard Fillmore175
1314Franklin Pierce178
1415James Buchanan183
1516Abraham Lincoln193
1617Andrew Johnson178
1718Ulysses S. Grant173
1819Rutherford B. Hayes174
1920James A. Garfield183
2021Chester A. Arthur183
2123Benjamin Harrison168
2225William McKinley170
2326Theodore Roosevelt178
2427William Howard Taft182
2528Woodrow Wilson180
2629Warren G. Harding183
2730Calvin Coolidge178
2831Herbert Hoover182
2932Franklin D. Roosevelt188
3033Harry S. Truman175
3134Dwight D. Eisenhower179
3235John F. Kennedy183
3336Lyndon B. Johnson193
3437Richard Nixon182
3538Gerald Ford183
3639Jimmy Carter177
3740Ronald Reagan185
3841George H. W. Bush188
3942Bill Clinton188
4043George W. Bush182
4144Barack Obama185
heights = data['height(cm)']
heights
0     189
1     170
2     189
3     163
4     183
5     171
6     185
7     168
8     173
9     183
10    173
11    173
12    175
13    178
14    183
15    193
16    178
17    173
18    174
19    183
20    183
21    168
22    170
23    178
24    182
25    180
26    183
27    178
28    182
29    188
30    175
31    179
32    183
33    193
34    182
35    183
36    177
37    185
38    188
39    188
40    182
41    185
Name: height(cm), dtype: int64
type(heights)
pandas.core.series.Series
import numpy as np

np.max(heights)
193
np.mean(heights)
179.73809523809524
np.std(heights)
6.931843442745892

五、ndarray的矩阵操作

1. 基本矩阵操作

1) 算术运算符:

  • 加减乘除
n = np.random.randint(0, 10, size=(4, 5))
n
array([[8, 0, 0, 9, 3],
       [5, 4, 3, 1, 0],
       [1, 8, 1, 2, 9],
       [7, 5, 0, 0, 9]])
# 加
n + 1
array([[ 9,  1,  1, 10,  4],
       [ 6,  5,  4,  2,  1],
       [ 2,  9,  2,  3, 10],
       [ 8,  6,  1,  1, 10]])
# 减
n - 1
array([[ 7, -1, -1,  8,  2],
       [ 4,  3,  2,  0, -1],
       [ 0,  7,  0,  1,  8],
       [ 6,  4, -1, -1,  8]])
n2 = np.random.randint(0, 10, size=(4, 5))
n2
array([[2, 4, 9, 2, 9],
       [8, 8, 0, 6, 1],
       [8, 8, 7, 9, 2],
       [4, 2, 3, 0, 3]])
n + n2
array([[10,  4,  9, 11, 12],
       [13, 12,  3,  7,  1],
       [ 9, 16,  8, 11, 11],
       [11,  7,  3,  0, 12]])
n3 = np.random.randint(0, 10, size=(4, 5))
n3
array([[6, 8, 6, 4, 8],
       [3, 4, 7, 1, 1],
       [7, 9, 4, 6, 2],
       [3, 3, 6, 2, 5]])
n2 + n3
array([[ 8, 12, 15,  6, 17],
       [11, 12,  7,  7,  2],
       [15, 17, 11, 15,  4],
       [ 7,  5,  9,  2,  8]])

2) 矩阵积np.dot()

n1 = np.random.randint(0, 10, size=(2, 3))
n1
array([[5, 3, 6],
       [9, 4, 4]])
n2 = np.random.randint(0, 10, size=(3, 4))
n2
array([[6, 5, 8, 8],
       [0, 7, 6, 2],
       [3, 3, 7, 4]])
np.dot(n1, n2)
array([[ 48,  64, 100,  70],
       [ 66,  85, 124,  96]])

2. 广播机制

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

  • 规则一:为缺失的维度补1
  • 规则二:假定缺失元素用已有值填充

例1:
m = np.ones((2, 3))
a = np.arange(3)
求M+a

m = np.ones((2, 3), dtype=int)
m
array([[1, 1, 1],
       [1, 1, 1]])
n = np.arange(3)
n
array([0, 1, 2])
m + n
array([[1, 2, 3],
       [1, 2, 3]])

例2:
a = np.arange(3).reshape((3, 1))
b = np.arange(3)
求a+b

a = np.arange(3).reshape((3, 1))
a
array([[0],
       [1],
       [2]])
b = np.arange(3)
b
array([0, 1, 2])
a + b
array([[0, 1, 2],
       [1, 2, 3],
       [2, 3, 4]])

习题
a = np.ones((4, 1))
b = np.arange(4)
求a+b

a = np.ones((4, 1))
a
array([[1.],
       [1.],
       [1.],
       [1.]])
b = np.arange(4)
b
array([0, 1, 2, 3])
a + b
array([[1., 2., 3., 4.],
       [1., 2., 3., 4.],
       [1., 2., 3., 4.],
       [1., 2., 3., 4.]])

六、ndarray的排序

小测验:
使用以上所学numpy的知识,对一个ndarray对象进行选择排序。

def Sortn(x):

代码越短越好

n = [5, 2, 3, 6, 9]
def bubble(n):
    for i in range(len(n) - 1):
        for j in range(i+1, len(n)):
            if n[i] > n[j]:
                n[i], n[j] = n[j], n[i]
bubble(n)
n
[2, 3, 5, 6, 9]
# 选择排序
def select(n):
    for i in range(len(n)):
        # 选出最小直的索引
        index = np.argmin(n[i:]) + i
        # 把最小值和当前值的位置换一下
        n[i], n[index] = n[index], n[i]
n = [4, 6, 1, 0, 3]
select(n)
n
[0, 1, 3, 4, 6]

1. 快速排序

np.sort()与ndarray.sort()都可以,但有区别:

  • np.sort()不改变输入
  • ndarray.sort()本地处理,不占用空间,但改变输入
# 排序只对1维的数据有意义
n = np.arange(0, 10)
n
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
np.random.shuffle(n)
n
array([7, 1, 3, 9, 0, 8, 6, 5, 2, 4])
# 第一种np.sort()
np.sort(n)
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
n
array([7, 1, 3, 9, 0, 8, 6, 5, 2, 4])
# 对象的sort方法
n.sort()
n
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])

部分排序

n = np.arange(0, 100)
np.random.shuffle(n)
n
array([76, 23, 49, 39, 17, 79,  9, 91, 56, 84, 40, 11,  4, 83,  7, 60, 25,
       75, 88, 26, 99, 24, 43, 85, 63, 59, 68, 22, 71, 51, 34, 86, 21, 36,
       13, 20, 10, 45, 37, 81, 53, 69, 78, 14, 92, 41, 65, 74, 87, 95, 44,
       72, 96, 35,  3, 46, 73, 38,  0, 16, 97, 64, 62, 89, 28, 55, 19,  5,
       18, 48,  2, 54, 94, 32, 98,  8, 27, 52, 67, 29, 93, 82, 58, 77,  1,
       33, 47, 42, 90, 31, 70, 30, 50, 57, 12,  6, 15, 61, 66, 80])
# 找出前五个最大的数
np.partition(n, kth=-5)
array([58, 76, 49, 39, 17, 79,  9, 66, 56, 61, 40, 11,  4, 15,  7, 60, 25,
       75,  6, 26, 12, 24, 43, 57, 63, 59, 68, 22, 71, 51, 34, 50, 21, 36,
       13, 20, 10, 45, 37, 30, 53, 69, 78, 14, 70, 41, 65, 74, 31, 23, 44,
       72, 42, 35,  3, 46, 73, 38,  0, 16, 47, 64, 62, 33, 28, 55, 19,  5,
       18, 48,  2, 54,  1, 32, 77,  8, 27, 52, 67, 29, 80, 86, 82, 91, 84,
       89, 83, 88, 90, 87, 85, 81, 92, 94, 93, 95, 99, 96, 98, 97])
# 找出最小的五个数
np.partition(n, kth=5)
array([ 0,  1,  2,  3,  4,  5,  6,  7,  8, 10, 12, 11,  9, 13, 29, 28, 25,
       16, 15, 26, 19, 24, 18, 17, 23, 14, 27, 22, 20, 21, 30, 50, 34, 36,
       51, 71, 68, 45, 37, 76, 53, 69, 78, 59, 70, 41, 65, 74, 31, 63, 44,
       72, 42, 35, 57, 46, 73, 38, 43, 75, 47, 64, 62, 33, 60, 55, 40, 61,
       56, 48, 66, 54, 79, 32, 77, 39, 49, 52, 67, 58, 80, 82, 93, 98, 94,
       89, 97, 96, 90, 87, 92, 81, 86, 85, 99, 88, 83, 84, 91, 95])

转载于:https://www.cnblogs.com/pankypan/p/11480967.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值