• 用于机器学习的 NumPy(ML)


    🔎大家好,我是Sonhhxg_柒,希望你看完之后,能对你有所帮助,不足请指正!共同学习交流🔎

    📝个人主页-Sonhhxg_柒的博客_CSDN博客 📃

    🎁欢迎各位→点赞👍 + 收藏⭐️ + 留言📝​

    📣系列专栏 - 机器学习【ML】 自然语言处理【NLP】  深度学习【DL】

    ​​

     🖍foreword

    ✔说明⇢本人讲解主要包括Python、机器学习(ML)、深度学习(DL)、自然语言处理(NLP)等内容。

    如果你对这个系列感兴趣的话,可以关注订阅哟👋

    文章目录

    设置

    基本

    索引

    算术

    点积

    轴操作

    播送

    陷阱

    转置

    重塑(reshape)

    Joining

    扩大/缩小


    设置

    首先,我们将导入 NumPy 包并设置可重复性的种子,以便我们每次都能收到完全相同的结果。

    1. import numpy as np

    基本

    1. # Scalar
    2. x = np.array(6)
    3. print ("x: ", x)
    4. print ("x ndim: ", x.ndim) # number of dimensions
    5. print ("x shape:", x.shape) # dimensions
    6. print ("x size: ", x.size) # size of elements
    7. print ("x dtype: ", x.dtype) # data type
    x:  6
    x ndim:  0
    x shape: ()
    x size:  1
    x dtype:  int64
    
    1. # Vector
    2. x = np.array([1.3 , 2.2 , 1.7])
    3. print ("x: ", x)
    4. print ("x ndim: ", x.ndim)
    5. print ("x shape:", x.shape)
    6. print ("x size: ", x.size)
    7. print ("x dtype: ", x.dtype) # notice the float datatype
    x:  [1.3 2.2 1.7]
    x ndim:  1
    x shape: (3,)
    x size:  3
    x dtype:  float64
    
    1. # Matrix
    2. x = np.array([[1,2], [3,4]])
    3. print ("x:\n", x)
    4. print ("x ndim: ", x.ndim)
    5. print ("x shape:", x.shape)
    6. print ("x size: ", x.size)
    7. print ("x dtype: ", x.dtype)
    x:
     [[1 2]
     [3 4]]
    x ndim:  2
    x shape: (2, 2)
    x size:  4
    x dtype:  int64
    
    1. # 3-D Tensor
    2. x = np.array([[[1,2],[3,4]],[[5,6],[7,8]]])
    3. print ("x:\n", x)
    4. print ("x ndim: ", x.ndim)
    5. print ("x shape:", x.shape)
    6. print ("x size: ", x.size)
    7. print ("x dtype: ", x.dtype)
    x:
     [[[1 2]
      [3 4]]
    
     [[5 6]
      [7 8]]]
    x ndim:  3
    x shape: (2, 2, 2)
    x size:  8
    x dtype:  int64
    

    NumPy 还带有几个函数,可以让我们快速创建张量。

    1. # Functions
    2. print ("np.zeros((2,2)):\n", np.zeros((2,2)))
    3. print ("np.ones((2,2)):\n", np.ones((2,2)))
    4. print ("np.eye((2)):\n", np.eye((2))) # identity matrix
    5. print ("np.random.random((2,2)):\n", np.random.random((2,2)))
    np.zeros((2,2)):
     [[0. 0.]
     [0. 0.]]
    np.ones((2,2)):
     [[1. 1.]
     [1. 1.]]
    np.eye((2)):
     [[1. 0.]
     [0. 1.]]
    np.random.random((2,2)):
     [[0.19151945 0.62210877]
     [0.43772774 0.78535858]]
    

    索引

    我们可以使用索引从我们的张量中提取特定值。

    请记住,索引行和列时,索引从0. 和使用列表索引一样,我们也可以使用负索引(-1最后一项在哪里)。

    1. # Indexing
    2. x = np.array([1, 2, 3])
    3. print ("x: ", x)
    4. print ("x[0]: ", x[0])
    5. x[0] = 0
    6. print ("x: ", x)
    x:  [1 2 3]
    x[0]:  1
    x:  [0 2 3]
    
    1. # Slicing
    2. x = np.array([[1,2,3,4], [5,6,7,8], [9,10,11,12]])
    3. print (x)
    4. print ("x column 1: ", x[:, 1])
    5. print ("x row 0: ", x[0, :])
    6. print ("x rows 0,1 & cols 1,2: \n", x[0:2, 1:3])
    [[ 1  2  3  4]
     [ 5  6  7  8]
     [ 9 10 11 12]]
    x column 1:  [ 2  6 10]
    x row 0:  [1 2 3 4]
    x rows 0,1 & cols 1,2:
     [[2 3]
     [6 7]]
    
    1. # Integer array indexing
    2. print (x)
    3. rows_to_get = np.array([0, 1, 2])
    4. print ("rows_to_get: ", rows_to_get)
    5. cols_to_get = np.array([0, 2, 1])
    6. print ("cols_to_get: ", cols_to_get)
    7. # Combine sequences above to get values to get
    8. print ("indexed values: ", x[rows_to_get, cols_to_get]) # (0, 0), (1, 2), (2, 1)
    [[ 1  2  3  4]
     [ 5  6  7  8]
     [ 9 10 11 12]]
    rows_to_get:  [0 1 2]
    cols_to_get:  [0 2 1]
    indexed values:  [ 1  7 10]
    
    1. # Boolean array indexing
    2. x = np.array([[1, 2], [3, 4], [5, 6]])
    3. print ("x:\n", x)
    4. print ("x > 2:\n", x > 2)
    5. print ("x[x > 2]:\n", x[x > 2])
    x:
     [[1 2]
     [3 4]
     [5 6]]
    x > 2:
     [[False False]
     [ True  True]
     [ True  True]]
    x[x > 2]:
     [3 4 5 6]
    

    算术

    1. # Basic math
    2. x = np.array([[1,2], [3,4]], dtype=np.float64)
    3. y = np.array([[1,2], [3,4]], dtype=np.float64)
    4. print ("x + y:\n", np.add(x, y)) # or x + y
    5. print ("x - y:\n", np.subtract(x, y)) # or x - y
    6. print ("x * y:\n", np.multiply(x, y)) # or x * y
    x + y:
     [[2. 4.]
     [6. 8.]]
    x - y:
     [[0. 0.]
     [0. 0.]]
    x * y:
     [[ 1.  4.]
     [ 9. 16.]]
    

    点积

    我们将在机器学习中使用的最常见的 NumPy 运算之一是使用点积的矩阵乘法。假设我们想取两个形状为[2 X 3]和的矩阵的点积[3 X 2]。我们取第一个矩阵 (2) 的行和第二个矩阵 (2) 的列来确定点积,输出为[2 X 2]. 唯一的要求是内部尺寸匹配,在这种情况下,第一个矩阵有 3 列,第二个矩阵有 3 行。

    1. # Dot product
    2. a = np.array([[1,2,3], [4,5,6]], dtype=np.float64) # we can specify dtype
    3. b = np.array([[7,8], [9,10], [11, 12]], dtype=np.float64)
    4. c = a.dot(b)
    5. print (f"{a.shape} · {b.shape} = {c.shape}")
    6. print (c)

    (2, 3) · (3, 2) = (2, 2)
    [[ 58.  64.]
     [139. 154.]]
    

    轴操作

    我们还可以跨特定轴进行操作。

    1. # Sum across a dimension
    2. x = np.array([[1,2],[3,4]])
    3. print (x)
    4. print ("sum all: ", np.sum(x)) # adds all elements
    5. print ("sum axis=0: ", np.sum(x, axis=0)) # sum across rows
    6. print ("sum axis=1: ", np.sum(x, axis=1)) # sum across columns

    [[1 2]
     [3 4]]
    sum all:  10
    sum axis=0:  [4 6]
    sum axis=1:  [3 7]
    
    1. # Min/max
    2. x = np.array([[1,2,3], [4,5,6]])
    3. print ("min: ", x.min())
    4. print ("max: ", x.max())
    5. print ("min axis=0: ", x.min(axis=0))
    6. print ("min axis=1: ", x.min(axis=1))
    min:  1
    max:  6
    min axis=0:  [1 2 3]
    min axis=1:  [1 4]
    

    播送

    当我们尝试对形状看似不兼容的张量进行运算时会发生什么?它们的尺寸不兼容,但 NumPy 如何仍然给我们正确的结果?这就是广播的用武之地。标量通过向量进行广播,以便它们具有兼容的形状。

    1. # Broadcasting
    2. x = np.array([1,2]) # vector
    3. y = np.array(3) # scalar
    4. z = x + y
    5. print ("z:\n", z)

    z:
     [4 5]
    

    陷阱

    在下面的情况下,它的价值c和维度是什么?

    1. a = np.array((3, 4, 5))
    2. b = np.expand_dims(a, axis=1)
    3. c = a + b
    1. a.shape # (3,)
    2. b.shape # (3, 1)
    3. c.shape # (3, 3)
    4. print (c)
    array([[ 6,  7,  8],
            [ 7,  8,  9],
            [ 8,  9, 10]])
    

    我们如何解决这个问题?我们需要小心确保它a的形状与b我们不希望这种无意的广播行为相同。

    1. a = a.reshape(-1, 1)
    2. a.shape # (3, 1)
    3. c = a + b
    4. c.shape # (3, 1)
    5. print (c)

    array([[ 6],
           [ 8],
           [10]])
    

    这种意外广播发生的频率比你想象的要多,因为这正是我们从列表创建数组时发生的情况。因此,我们需要确保在将其用于任何操作之前应用适当的重塑

    1. a = np.array([3, 4, 5])
    2. a.shape # (3,)
    3. a = a.reshape(-1, 1)
    4. a.shape # (3, 1)

    转置

    我们经常需要更改张量的维度以进行点积等操作。如果我们需要切换两个维度,我们可以转置张量。

    1. # Transposing
    2. x = np.array([[1,2,3], [4,5,6]])
    3. print ("x:\n", x)
    4. print ("x.shape: ", x.shape)
    5. y = np.transpose(x, (1,0)) # flip dimensions at index 0 and 1
    6. print ("y:\n", y)
    7. print ("y.shape: ", y.shape)

    x:
     [[1 2 3]
     [4 5 6]]
    x.shape:  (2, 3)
    y:
     [[1 4]
     [2 5]
     [3 6]]
    y.shape:  (3, 2)
    

    重塑(reshape)

    有时,我们需要改变矩阵的维度。重塑允许我们将张量转换为不同的允许形状。下面,我们重塑的张量具有与原始张量相同数量的值。( 1X62X3)。我们也可以-1在维度上使用,NumPy 将根据我们的输入张量推断维度。

    1. # Reshaping
    2. x = np.array([[1,2,3,4,5,6]])
    3. print (x)
    4. print ("x.shape: ", x.shape)
    5. y = np.reshape(x, (2, 3))
    6. print ("y: \n", y)
    7. print ("y.shape: ", y.shape)
    8. z = np.reshape(x, (2, -1))
    9. print ("z: \n", z)
    10. print ("z.shape: ", z.shape)
    [[1 2 3 4 5 6]]
    x.shape:  (1, 6)
    y:
     [[1 2 3]
     [4 5 6]]
    y.shape:  (2, 3)
    z:
     [[1 2 3]
     [4 5 6]]
    z.shape:  (2, 3)
    

    reshape 的工作方式是查看新张量的每个维度,并将我们的原始张量分成那么多单元。所以这里新张量的索引 0 处的维度是 2,所以我们将原始张量分成 2 个单位,每个单位有 3 个值。

    意外重塑

    虽然重塑对于操纵张量非常方便,但我们也必须小心它的陷阱。让我们看看下面的例子。假设我们有x,其中有形状[2 X 3 X 4]

    1. x = np.array([[[1, 1, 1, 1], [2, 2, 2, 2], [3, 3, 3, 3]],
    2.             [[10, 10, 10, 10], [20, 20, 20, 20], [30, 30, 30, 30]]])
    3. print ("x:\n", x)
    4. print ("x.shape: ", x.shape)
    x:
    [[[ 1  1  1  1]
    [ 2  2  2  2]
    [ 3  3  3  3]]
    

    [[10 10 10 10] .

    [20 20 20 20]

    [30 30 30 30]]]

    x.shape: (2, 3, 4)

    我们想要重塑 x 使其具有形状[3 X 8],但我们希望输出看起来像这样:

    [[ 1  1  1  1 10 10 10 10]
    [ 2  2  2  2 20 20 20 20]
    [ 3  3  3  3 30 30 30 30]]
    
    

    and not like:

    [[ 1  1  1  1  2  2  2  2]
    [ 3  3  3  3 10 10 10 10]
    [20 20 20 20 30 30 30 30]]
    

    我们想要重塑 x 使其具有形状[3 X 8],但我们希望输出看起来像这样:

    当我们天真地进行重塑时,我们得到了正确的形状,但值并不是我们想要的。

    1. # Unintended reshaping
    2. z_incorrect = np.reshape(x, (x.shape[1], -1))
    3. print ("z_incorrect:\n", z_incorrect)
    4. print ("z_incorrect.shape: ", z_incorrect.shape)
    1. z_incorrect:
    2. [[ 1 1 1 1 2 2 2 2]
    3. [ 3 3 3 3 10 10 10 10]
    4. [20 20 20 20 30 30 30 30]]
    5. z_incorrect.shape: (3, 8)

    相反,如果我们转置张量然后进行整形,我们会得到我们想要的张量。Transpose 允许我们将我们想要组合的两个向量放在一起,然后我们使用 reshape 将它们连接在一起。作为一般规则,我们应该始终将我们的维度放在一起,然后再重新塑造以组合它们。

    1. # Intended reshaping
    2. y = np.transpose(x, (1,0,2))
    3. print ("y:\n", y)
    4. print ("y.shape: ", y.shape)
    5. z_correct = np.reshape(y, (y.shape[0], -1))
    6. print ("z_correct:\n", z_correct)
    7. print ("z_correct.shape: ", z_correct.shape)

    和:
    [[[ 1 1 1 1] 
    [10 10 10 10]]

    [[ 2 2 2 2] [20 20 20 20]]

    [[ 3 3 3 3] [30 30 30 30]]]

    y.shape: (3, 2, 4)

    z_correct: [[ 1 1 1 1 10 10 10 10]

    [ 2 2 2 2 20 20 20 20]

    [ 3 3 3 3 30 30 30 30]]

    z_correct.shape: (3, 8)

    当我们在许多机器学习任务中处理具有随机值的权重张量时,这变得很困难。所以一个好主意是当你不确定重塑时总是创建一个像这样的虚拟示例。盲目地使用张量形状可能会导致很多下游问题。

    Joining

    我们也可以通过连接堆叠加入我们的张量。

    1. x = np.random.random((2, 3))
    2. print (x)
    3. print (x.shape)
    [[0.79564718 0.73023418 0.92340453]
     [0.24929281 0.0513762  0.66149188]]
    (2, 3)
    
    1. # Concatenation
    2. y = np.concatenate([x, x], axis=0) # concat on a specified axis
    3. print (y)
    4. print (y.shape)
    [[0.79564718 0.73023418 0.92340453]
     [0.24929281 0.0513762  0.66149188]
     [0.79564718 0.73023418 0.92340453]
     [0.24929281 0.0513762  0.66149188]]
    (4, 3)
    
    1. # Stacking
    2. z = np.stack([x, x], axis=0) # stack on new axis
    3. print (z)
    4. print (z.shape)
    [[[0.79564718 0.73023418 0.92340453]
      [0.24929281 0.0513762  0.66149188]]
    
     [[0.79564718 0.73023418 0.92340453]
      [0.24929281 0.0513762  0.66149188]]]
    (2, 2, 3)
    
    

    扩大/缩小

    我们还可以轻松地为我们的张量添加和删除维度,我们希望这样做以使张量与某些操作兼容。

    1. # Adding dimensions
    2. x = np.array([[1,2,3],[4,5,6]])
    3. print ("x:\n", x)
    4. print ("x.shape: ", x.shape)
    5. y = np.expand_dims(x, 1) # expand dim 1
    6. print ("y: \n", y)
    7. print ("y.shape: ", y.shape) # notice extra set of brackets are added
    x:
     [[1 2 3]
      [4 5 6]]
    x.shape:  (2, 3)
    y:
     [[[1 2 3]]
      [[4 5 6]]]
    y.shape:  (2, 1, 3)
    
    1. # Removing dimensions
    2. x = np.array([[[1,2,3]],[[4,5,6]]])
    3. print ("x:\n", x)
    4. print ("x.shape: ", x.shape)
    5. y = np.squeeze(x, 1) # squeeze dim 1
    6. print ("y: \n", y)
    7. print ("y.shape: ", y.shape) # notice extra set of brackets are gone
    x:
     [[[1 2 3]]
      [[4 5 6]]]
    x.shape:  (2, 1, 3)
    y:
     [[1 2 3]
      [4 5 6]]
    y.shape:  (2, 3)
    

    查看Dask以扩展 NumPy 工作流,只需对现有代码进行最少的更改

  • 相关阅读:
    优化器算法
    搭建网站七牛云CDN加速配置
    从0开始学汇编第二天:寄存器(CPU工作原理)
    java电商系统怎么设计秒杀?
    免费SSL证书
    探索Dubbo的服务引用:XML配置方式
    python开发实验管理系统(lims)中的标准管理--检测项目
    Python数据容器——字典的常用操作(增、删、改、查)
    vue中使用ali-oss上传文件到阿里云上
    高创伺服驱动器CDHD2和sick伺服编码器hiperface通讯时的故障解决
  • 原文地址:https://blog.csdn.net/sikh_0529/article/details/126886127