首页 Numpy 介绍
文章
取消

Numpy 介绍

介绍

在数据客户和机器学习中,经常会用到数据计算,这就不得不提 Numpy ,很多的库类也是以 Numpy 为基础开发的。

numpy的优点

  • 比python列表效率更高
  • 可以扩展到N维对象
  • 计算速度更快
  • 广播功能
  • 目前所学习的数据科学和机器学习库都是Numpy构建的

安装 Numpy

1
pip install numpy

使用

1
2
# 导入包
import numpy as np

生成数组

转化为 Numpy 数组

1
2
3
arr = [1,3,4]
arr1 = np.array([1,2,3])
arr2 = np.array([[1,2,3],[4,5,6]])
1
2
3
4
5
6
print(arr)
print(type(arr))
print(arr1)
print(type(arr1))
print(arr2)
print(type(arr2))
1
2
3
4
5
6
7
    [1, 3, 4]
    <class 'list'>
    [1 2 3]
    <class 'numpy.ndarray'>
    [[1 2 3]
     [4 5 6]]
    <class 'numpy.ndarray'>

给定范围生成数组

1
2
3
arr3 = np.arange(0,10)
print(arr3)
print(type(arr3))
1
2
    [0 1 2 3 4 5 6 7 8 9]
    <class 'numpy.ndarray'>
1
2
3
arr4 = np.arange(0,10,2)
print(arr4)
print(type(arr4))
1
2
    [0 2 4 6 8]
    <class 'numpy.ndarray'>

生成均为0的数组

1
2
3
arr5 = np.zeros(5)
print(arr5)
print(type(arr5))
1
2
    [0. 0. 0. 0. 0.]
    <class 'numpy.ndarray'>
1
2
3
arr6 = np.zeros((3,4))
print(arr6)
print(type(arr6))
1
2
3
4
    [[0. 0. 0. 0.]
     [0. 0. 0. 0.]
     [0. 0. 0. 0.]]
    <class 'numpy.ndarray'>

生成均为1的数组

1
2
3
arr7 = np.ones(5)
print(arr7)
print(type(arr7))
1
2
    [1. 1. 1. 1. 1.]
    <class 'numpy.ndarray'>
1
2
3
arr8 = np.ones((2,5))
print(arr8)
print(type(arr8))
1
2
3
    [[1. 1. 1. 1. 1.]
     [1. 1. 1. 1. 1.]]
    <class 'numpy.ndarray'>

生成线性等差数列

1
2
3
arr9 = np.linspace(0,10,5)
print(arr9)
print(type(arr9))
1
2
    [ 0.   2.5  5.   7.5 10. ]
    <class 'numpy.ndarray'>

创建单位矩阵

1
2
3
arr10 = np.eye(5)
print(arr10)
print(type(arr10))
1
2
3
4
5
6
    [[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.]]
    <class 'numpy.ndarray'>

创建[0,1)之间的均匀分布的随机样本

1
2
3
arr11 = np.random.rand(10)
print(arr11)
print(type(arr11))
1
2
3
    [0.35113694 0.42836244 0.36335337 0.96886346 0.68962208 0.21901492
     0.15671415 0.09976772 0.09699825 0.53141851]
    <class 'numpy.ndarray'>

创建一个正态分布的随机数组,默认均值为0,标准层为1

1
2
3
arr12 = np.random.randn(10)
print(arr12)
print(type(arr12))
1
2
3
    [-0.31401474  0.46383825  0.143065    0.97946198  1.43620692  1.2018041
      1.06476174 -0.97725817  0.5797658  -0.77204852]
    <class 'numpy.ndarray'>

创建随机整数列

1
2
3
arr13 = np.random.randint(10)
print(arr13)
print(type(arr13))
1
2
    6
    <class 'int'>
1
2
3
arr14 = np.random.randint(1,10,4)
print(arr14)
print(type(arr14))
1
2
    [5 6 4 9]
    <class 'numpy.ndarray'>

设置随机种子

1
2
3
4
5
6
7
8
9
10
np.random.seed(1)
arr15 = np.random.randint(1,10,4)
print(arr15)
print(type(arr15))
arr16 = np.random.rand(10)
print(arr16)
print(type(arr16))
arr17 = np.random.randn(10)
print(arr17)
print(type(arr17))
1
2
3
4
5
6
7
8
    [6 9 6 1]
    <class 'numpy.ndarray'>
    [0.09233859 0.18626021 0.34556073 0.39676747 0.53881673 0.41919451
     0.6852195  0.20445225 0.87811744 0.02738759]
    <class 'numpy.ndarray'>
    [-0.85990661  1.77260763 -1.11036305  0.18121427  0.56434487 -0.56651023
      0.7299756   0.37299379  0.53381091 -0.0919733 ]
    <class 'numpy.ndarray'>

方法与属性

数组内容的类型

1
2
3
4
arr18 = np.arange(0,10)
print(arr18)
print(type(arr18))
print(arr3.dtype)
1
2
3
    [0 1 2 3 4 5 6 7 8 9]
    <class 'numpy.ndarray'>
    int64

数组形状

1
2
3
4
5
6
7
arr19 = np.arange(0,10)
print(arr19)
print(type(arr19))
print(arr19.shape)
arr20 = arr19.reshape(5,2)
print(arr19.shape)
print(arr20.shape)
1
2
3
4
5
    [0 1 2 3 4 5 6 7 8 9]
    <class 'numpy.ndarray'>
    (10,)
    (10,)
    (5, 2)
1
#### 获取与赋值
1
2
3
4
5
6
7
arr21 = np.arange(0,10)
print(arr21)
print(type(arr21))
print(arr21[0:4])
print(arr21[5])
arr21[0:4] = 100
print(arr21)
1
2
3
4
5
    [0 1 2 3 4 5 6 7 8 9]
    <class 'numpy.ndarray'>
    [0 1 2 3]
    5
    [100 100 100 100   4   5   6   7   8   9]
1
#### 运算
1
2
3
4
5
6
7
8
9
10
11
12
# 加,对位相加,广播
arr22 = np.arange(100,110)
print(arr22)
print(type(arr22))
arr23 = arr22+10
print(arr23)

arr24 = np.arange(100,110)
print(arr24)
print(type(arr24))
arr25 = arr22+arr24
print(arr25)
1
2
3
4
5
6
    [100 101 102 103 104 105 106 107 108 109]
    <class 'numpy.ndarray'>
    [110 111 112 113 114 115 116 117 118 119]
    [100 101 102 103 104 105 106 107 108 109]
    <class 'numpy.ndarray'>
    [200 202 204 206 208 210 212 214 216 218]
1
2
3
4
5
6
7
8
9
10
11
12
# 减,对位相减,广播
arr22 = np.arange(100,110)
print(arr22)
print(type(arr22))
arr23 = arr22-10
print(arr23)

arr24 = np.arange(100,110)
print(arr24)
print(type(arr24))
arr25 = arr22-arr24
print(arr25)
1
2
3
4
5
6
    [100 101 102 103 104 105 106 107 108 109]
    <class 'numpy.ndarray'>
    [90 91 92 93 94 95 96 97 98 99]
    [100 101 102 103 104 105 106 107 108 109]
    <class 'numpy.ndarray'>
    [0 0 0 0 0 0 0 0 0 0]
1
2
3
4
5
6
7
8
9
10
11
12
# 乘,对位相乘,广播
arr22 = np.arange(100,110)
print(arr22)
print(type(arr22))
arr23 = arr22*10
print(arr23)

arr24 = np.arange(100,110)
print(arr24)
print(type(arr24))
arr25 = arr22*arr24
print(arr25)
1
2
3
4
5
6
    [100 101 102 103 104 105 106 107 108 109]
    <class 'numpy.ndarray'>
    [1000 1010 1020 1030 1040 1050 1060 1070 1080 1090]
    [100 101 102 103 104 105 106 107 108 109]
    <class 'numpy.ndarray'>
    [10000 10201 10404 10609 10816 11025 11236 11449 11664 11881]
1
2
3
4
5
6
7
8
9
10
11
12
# 除,对位相除,广播
arr22 = np.arange(100,110)
print(arr22)
print(type(arr22))
arr23 = arr22/10
print(arr23)

arr24 = np.arange(100,110)
print(arr24)
print(type(arr24))
arr25 = arr22/arr24
print(arr25)
1
2
3
4
5
6
    [100 101 102 103 104 105 106 107 108 109]
    <class 'numpy.ndarray'>
    [10.  10.1 10.2 10.3 10.4 10.5 10.6 10.7 10.8 10.9]
    [100 101 102 103 104 105 106 107 108 109]
    <class 'numpy.ndarray'>
    [1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]
1
2
3
4
5
6
7
8
9
10
# 点积
arr26 = np.arange(100,110)
print(arr26)
print(type(arr26))

arr27 = np.arange(100,110)
print(arr27)
print(type(arr27))
arr28 = arr26.dot(arr27)
print(arr28)
1
2
3
4
5
    [100 101 102 103 104 105 106 107 108 109]
    <class 'numpy.ndarray'>
    [100 101 102 103 104 105 106 107 108 109]
    <class 'numpy.ndarray'>
    109285
1
2
3
4
5
6
7
8
9
10
11
12
13
14
# 注意形状
arr26 = np.arange(100,110).reshape(2,5)
print(arr26)
print(type(arr26))

arr27 = np.arange(100,110).reshape(5,2)
print(arr27)
print(type(arr27))

arr28 = arr26.dot(arr27)
print(arr28)

arr29 = arr26 @ arr27
print(arr29)
1
2
3
4
5
6
7
8
9
10
11
12
13
    [[100 101 102 103 104]
     [105 106 107 108 109]]
    <class 'numpy.ndarray'>
    [[100 101]
     [102 103]
     [104 105]
     [106 107]
     [108 109]]
    <class 'numpy.ndarray'>
    [[53060 53570]
     [55660 56195]]
    [[53060 53570]
     [55660 56195]]

参考资料

1、Python–第三方库Numpy(Jupyter)

2、Numpy 官网

本文由作者按照 CC BY 4.0 进行授权

Hugging Face下载大模型的相关文件说明

PyTorch常用模块简介