栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

机器学习必备神器:Numpy库常用函数实操(持续更新)

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

机器学习必备神器:Numpy库常用函数实操(持续更新)

NumPy(Numerical Python) 是 Python 语言的一个扩展程序库,支持大量的维度数组与矩阵运算,此外也针对数组运算提供大量的数学函数库。

目录
  • 一、安装
  • 二、使用
    • 1. Numpy.array()
    • 2. Numpy.where()
    • 3. Numpy.argwhere()
    • 4. Numpy.arange()
    • 5. Numpy.abs()
    • 6. Numpy.sqrt()
    • 7. Numpy.square()
    • 8. Numpy.sum()
    • 9. Numpy.maximum()
    • 10. Numpy.searchsorted()
    • 11. Numpy.logical_or()
    • 12. Numpy.logical_and()
    • 13. Numpy.repeat()
    • 14. Numpy.count_nonzero()
    • 15. Numpy.copy()
    • 16. Numpy.bitwise_and()
    • 17. Numpy.bitwise_or()
    • 18. Numpy.equal()
    • 19. Numpy.not_equal()
    • 20. Numpy.zeros()
    • 21. Numpy.polyval()
    • 22. Numpy.power()
    • 23. Numpy.concatenate()
    • 24. Numpy.sort()
    • 25. Numpy.argsort()
    • 26. Numpy.unique()
    • 27. Numpy.mat()
    • 28. Numpy.reshape()
    • 29. Numpy.sin()
    • 30. Numpy.cos()
    • 31. Numpy.min()
    • 32. Numpy.max()
    • 33. Numpy.diff()

一、安装
pip install numpy
二、使用 1. Numpy.array()

功能:初始化一个numpy数组

import numpy as np

arr = [1, 2, 3, 4, 5]
arr_np = np.array(arr)
print(arr_np, type(arr_np))
[1 2 3 4 5] 
2. Numpy.where()

功能:返回满足表达式的数组索引,注意返回是tuple结构

import numpy as np

arr_np = np.array([0, 1, 2, 3, 4])
rst = np.where(arr_np > 1)
print(rst, type(rst))
(array([2, 3, 4], dtype=int64),) 
3. Numpy.argwhere()

功能:返回满足表达式的数组索引,注意返回是二维数组

import numpy as np

arr_np = np.array([[0, 1, 2, 3, 4], [-1, 0, 1, 2, 3]])
rst = np.argwhere(arr_np > 1)
print(rst, type(rst))
# 矩阵计算,返回值依次返回维度索引
[[0 2]
 [0 3]
 [0 4]
 [1 3]
 [1 4]] 
4. Numpy.arange()

功能:根据起止数字,生成对应numpy数组

import numpy as np

arr_np = np.array([[0, 1, 2, 3, 4], [-1, 0, 1, 2, 3]])
rst = np.arange(2, 12, 3)  # st et offset
print(rst, type(rst))
[ 2  5  8 11] 
5. Numpy.abs()

功能:计算数组所有元素的绝对值

import numpy as np

arr_np = np.array([[0, 1, -2, 3, 4], [-1, 0, 1, 2, 3]])
rst = np.abs(arr_np)
print(rst, type(rst))
[[0 1 2 3 4]
 [1 0 1 2 3]] 
6. Numpy.sqrt()

功能:计算数字的平方根

import numpy as np

arr_np = np.array([0, 1, 5, 9, 16])
rst = np.sqrt(arr_np)
print(rst, type(rst))
[0.         1.         2.23606798 3.         4.        ] 
7. Numpy.square()

功能:计算源数组的平方

import numpy as np

arr_np = np.array([0, 1, 5, 9, 16])
rst = np.square(arr_np)
print(rst, type(rst))
[  0   1  25  81 256] 
8. Numpy.sum()

功能:计算数组的和,如果数组有多维,会降维计算

import numpy as np

arr_np = np.array([[0, 1, 5, 9, 16], [0, 1, 5, 9, 16]])
rst = np.sum(arr_np)
print(rst, type(rst))
62 
9. Numpy.maximum()

功能:计算两个数组的较大值,也可用来计算数组和数字的较大值

import numpy as np

arr_np_1 = np.array([0, 1, 5, 9, 16])
arr_np_2 = np.array([1, 3, 4, 9, 16])
rst = np.maximum(arr_np_1, arr_np_2)
print(rst, type(rst))
[ 1  3  5  9 16] 
10. Numpy.searchsorted()

功能:返回数组中某个值的lower_bound索引

import numpy as np

arr_np_1 = np.array([0, 1, 5, 9, 16])
rst = np.searchsorted(arr_np_1, 5)
print(rst, type(rst))
2 
11. Numpy.logical_or()

功能:计算两个表达式结果的逻辑或

import numpy as np

arr_np_1 = np.array([0, 1, 5, 9, 16])
rst = np.logical_or(arr_np_1 < 2, arr_np_1 > 14)
print(rst, type(rst))
[ True  True False False  True] 
12. Numpy.logical_and()

功能:计算两个表达式结果的逻辑与

import numpy as np

arr_np_1 = np.array([0, 1, 5, 9, 16])
rst = np.logical_and(arr_np_1 > 2, arr_np_1 < 14)
print(rst, type(rst))
[False False  True  True False] 
13. Numpy.repeat()

功能:创建一个重复值的数组

import numpy as np

rst = np.repeat(True, 10)
print(rst, type(rst))
[ True  True  True  True  True  True  True  True  True  True] 
14. Numpy.count_nonzero()

功能:返回数组中非0的个数

import numpy as np

rst = np.count_nonzero([1, 2, 3, 4, 5, 6, 0])
print(rst, type(rst))
6 
15. Numpy.copy()

功能:返回数组的深拷贝数组

import numpy as np

rst1 = np.array([1, 2, 3, 4])
rst2 = np.copy(rst1)
rst2[1] = 4
print(rst2, type(rst2))
[1 4 3 4] 
16. Numpy.bitwise_and()

功能:返回两个数组按位与的值,注意多个数组逻辑运算可以用reduce()

import numpy as np

rst1 = np.array([1, 2, 3, 4])
rst2 = np.array([3, 4, 5, 6])
rst = np.bitwise_and(rst1, rst2)
print(rst, type(rst))
[1 0 1 4] 
17. Numpy.bitwise_or()

功能:返回两个数组按位或的值

import numpy as np

rst1 = np.array([True, 2, 3, 4])
rst2 = np.array([True, 4, 5, 6])
rst = np.bitwise_or(rst1, rst2)
print(rst, type(rst))
[1 6 7 6] 
18. Numpy.equal()

功能:判断两个数组对应元素是否相同

import numpy as np

rst1 = np.array([1, 2, 3, 4])
rst2 = np.array([2, 4, 3, 6])
rst = np.equal(rst1, rst2)
print(rst, type(rst))
[False False  True False] 
19. Numpy.not_equal()

功能:判断两个数组对应元素是否不同

import numpy as np

rst1 = np.array([1, 2, 3, 4])
rst2 = np.array([2, 4, 3, 6])
rst = np.not_equal(rst1, rst2)
print(rst, type(rst))
[ True  True False  True] 
20. Numpy.zeros()

功能:返回0组成的数组

import numpy as np

rst = np.zeros(20)
print(rst, type(rst))
[0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.] 
21. Numpy.polyval()

功能:根据方程系数计算多次方程

import numpy as np

rst = np.polyval([1, 3, 4], [1, 2, 3, 4])
print(rst, type(rst))
[ 8 14 22 32] 
22. Numpy.power()

功能:返回数组元素的n次方

import numpy as np

rst = np.power([1, 3, 4], 3)
print(rst, type(rst))
[ 1 27 64] 
23. Numpy.concatenate()

功能:多维数组降成一维

import numpy as np

a = np.array([[1, 2, 3, 4, 5], [6, 7, 8, 9, 10]])
rst = np.concatenate(a)
print(rst, type(rst))
[ 1  2  3  4  5  6  7  8  9 10] 
24. Numpy.sort()

功能:数组排序

import numpy as np

a = np.array([[1, 2, 6, 4, 5], [6, 7, 12, 9, 10]])
rst = np.sort(a)
print(rst, type(rst))
[[ 1  2  4  5  6]
 [ 6  7  9 10 12]] 
25. Numpy.argsort()

功能:返回数组排序之后的索引

import numpy as np

a = np.array([[1, 2, 6, 4, 5], [6, 7, 12, 9, 10]])
rst = np.argsort(a)
print(rst, type(rst))
[[0 1 3 4 2]
 [0 1 3 4 2]] 
26. Numpy.unique()

功能:将多维数组降成一维,然后排序去重

import numpy as np

a = np.array([[1, 2, 6, 4, 5], [6, 7, 12, 9, 10]])
rst = np.unique(a)
print(rst, type(rst))
[ 1  2  4  5  6  7  9 10 12] 
27. Numpy.mat()

功能:将数组转为矩阵格式

import numpy as np

a = np.array([[1, 2, 6, 4, 5], [6, 7, 12, 9, 10]])
rst = np.mat(a)
print(rst, type(rst))
[[ 1  2  6  4  5]
 [ 6  7 12  9 10]] 
28. Numpy.reshape()

功能:修改矩阵的维度

import numpy as np

a = np.array([[1, 2, 6, 4, 5], [6, 7, 12, 9, 10]])
print(a.shape)
rst = a.reshape(5, 2)
print(rst, type(rst))
(2, 5)
[[ 1  2]
 [ 6  4]
 [ 5  6]
 [ 7 12]
 [ 9 10]] 
29. Numpy.sin()

功能:求弧度角的正弦值

import numpy as np

a = np.array([30/180*np.pi, 60/180*np.pi, 90/180*np.pi])
rst = np.sin(a)
print(rst, type(rst))
[0.5       0.8660254 1.       ] 
30. Numpy.cos()

功能:求弧度角的余弦值

import numpy as np

a = np.array([30/180*np.pi, 60/180*np.pi, 90/180*np.pi])
rst = np.cos(a)
print(rst, type(rst))
[8.66025404e-01 5.00000000e-01 6.12323400e-17] 
31. Numpy.min()

功能:求数组的最小值

import numpy as np

a = np.array([0.2, 0, 0.5, 1])
rst = np.min(a)
print(rst, type(rst))
0.0 
32. Numpy.max()

功能:求数组的最大值

import numpy as np

a = np.array([0.2, 0, 0.5, 1])
rst = np.max(a)
print(rst, type(rst))
1.0 
33. Numpy.diff()

功能:离散差值计算

import numpy as np

a = np.array([0.2, 0, 0.5, 1])
rst = np.diff(a)
print(rst, type(rst))
[-0.2  0.5  0.5] 
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/321203.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号