IT评测·应用市场-qidao123.com
标题:
科学盘算根本软件包Numpy介绍及常用法
[打印本页]
作者:
大连密封材料
时间:
2024-9-4 17:29
标题:
科学盘算根本软件包Numpy介绍及常用法
1.介绍及阐明
NumPy 是一个开源的 Python 库,专门用于科学盘算和数值处理。它提供了强大的多维数组对象和丰富的函数库,支持高效的数组运算。NumPy 是许多其他科学盘算库(如 SciPy、Pandas、Matplotlib 等)的根本。以下是对 NumPy 的详细介绍和阐明。
2.NumPy 的主要特点
1.
多维数组对象(ndarray)
:NumPy 提供了一个高效的多维数组对象,称为 ndarray,支持快速的算术运算和数组操纵。
2.
广播(Broadcasting)
:NumPy 支持广播机制,可以在不同形状的数组之间举行算术运算,而无需显式地复制数据。
3.
标准数学函数
:NumPy 提供了大量的数学函数,包括基本算术运算、统计函数、线性代数函数等。
4.
随机数天生
:NumPy 包罗一个强大的随机数天生器,支持各种概率分布的随机数天生。
5.
数组操纵
:NumPy 提供了丰富的数组操纵函数,包括数组切片、索引、连接、分割、形状操纵等。
6.
与 C/C++ 和 Fortran 代码的集成
:NumPy 可以与 C/C++ 和 Fortran 代码举行无缝集成,支持高性能盘算。
3.安装 NumPy
使用 pip 可以轻松安装 NumPy:
pip install numpy
复制代码
代码秀:
def test(): installs = [ "1. pip install numpy
", "3. 还可以试试豆瓣的源:pip install numpy
-i http://pypi.douban.com/simple/ --trusted-host pypi.douban.com", "2. 假如装不了,就试试阿里的源:pip install numpy
-i http://mirrors.aliyun.com/pypi/simple --trusted-host mirrors.aliyun.com", ] installs.sort(key=lambda i: i[0]) for step in installs: items = step.split(":") print(items[0]) if len(items) > 1: print(f" {items[1]}")if __name__ == '__main__': test()
复制代码
4.NumPy 根本示例
以下是一些 NumPy 的根本示例代码,展示了其主要功能和用法。
1.
创建数组
:
import numpy as np
# 创建一维数组
a = np.array([1, 2, 3, 4, 5])
print(a)
# 创建二维数组
b = np.array([[1, 2, 3], [4, 5, 6]])
print(b)
# 创建全零数组
c = np.zeros((2, 3))
print(c)
# 创建全一数组
d = np.ones((3, 3))
print(d)
# 创建单位矩阵
e = np.eye(3)
print(e)
# 创建等差数组
f = np.arange(0, 10, 2)
print(f)
复制代码
2.
数组运算
:
# 数组加法
g = a + 1
print(g)
# 数组乘法
h = a * 2
print(h)
# 数组间运算
i = a + np.array([5, 4, 3, 2, 1])
print(i)
# 广播机制
j = a + np.array([[1], [2], [3], [4], [5]])
print(j)
复制代码
3.
数组切片和索引
:
k = np.array([1, 2, 3, 4, 5])
print(k[1:3]) # 切片
print(k[::2]) # 步长为2的切片
l = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
print(l[1, :]) # 第二行
print(l[:, 1]) # 第二列
print(l[1, 1]) # 元素 (1,1)
复制代码
4.
数组形状操纵
:
m = np.array([[1, 2, 3], [4, 5, 6]])
print(m.shape) # 数组形状
n = m.reshape((3, 2)) # 重塑数组
print(n)
o = m.flatten() # 展平数组
print(o)
复制代码
5.
线性代数
:
p = np.array([[1, 2], [3, 4]])
q = np.array([[5, 6], [7, 8]])
# 矩阵乘法
r = np.dot(p, q)
print(r)
# 矩阵转置
s = np.transpose(p)
print(s)
# 逆矩阵
t = np.linalg.inv(p)
print(t)
# 特征值和特征向量
eigenvalues, eigenvectors = np.linalg.eig(p)
print(eigenvalues)
print(eigenvectors)
复制代码
6.
随机数天生
:
# 生成均匀分布的随机数
u = np.random.rand(3, 3)
print(u)
# 生成正态分布的随机数
v = np.random.randn(3, 3)
print(v)
# 生成整数随机数
w = np.random.randint(0, 10, (3, 3))
print(w)
复制代码
5.NumPy 功能示例
1.取列表中最小的前几位
import numpy as np
def get_top_n(array, top_n):
top_n_indexs = np.argsort(array)[0:top_n]
results = [array[index] for index in top_n_indexs]
return results
if __name__ == '__main__':
ret = get_top_n(np.array([1, 3, 34, 4, 5, 6]), 3)
print(ret)
复制代码
运行结果:
2.取列表中最大的前几位
import numpy as np
def get_top_n(array, top_n):
top_n_indexs = np.argsort(array)[:-(top_n+1):-1]
results = [array[index] for index in top_n_indexs]
return results
if __name__ == '__main__':
ret = get_top_n(np.array([1, 3, 34, 4, 5, 6]), 3)
print(ret)
复制代码
运行结果:
分析:
上面代码界说了一个函数 get_top_n,用于从一个 NumPy 数组中获取最大的 top_n 个元素。代码逻辑非常清晰,使用了 np.argsort 函数来获取排序后的索引,然后使用这些索引获取相应的数组元素。
以下是对代码的详细解读:
1.np.argsort(array) 返回的是数组元素从小到大的索引。
2.[:-(top_n+1):-1] 是一个切片操纵,用于获取从末端向前数的 top_n 个索引,对应的就是数组中最大的 top_n 个元素的索引。
3.[array[index] for index in top_n_indexs] 是一个列表推导式,用于根据这些索引获取数组中的相应元素。
代码的功能是正确的,但使用列表推导式可能会稍微影响性能,因为它会将 NumPy 数组转换为 Python 列表。你可以直接使用 NumPy 数组来进步性能和简便性。
以下是优化后的版本:
import numpy as np
def get_top_n(array, top_n):
top_n_indexs = np.argsort(array)[-top_n:][::-1]
results = array[top_n_indexs]
return results
if __name__ == '__main__':
ret = get_top_n(np.array([1, 3, 34, 4, 5, 6]), 3)
print(ret)
复制代码
在这个优化版本中,results 直接使用了 NumPy 数组的切片和索引功能,克制了将数组转换为列表的操纵。这样做不仅能进步性能,还能保持代码的简便性。
测试结果
运行优化后的代码,结果如下:
[34 6 5]
复制代码
这表明函数正确地返回了数组中最大的 3 个元素 [34, 6, 5]。
import numpy as np
def get_top_n(array, top_n):
top_n_indexs = np.argsort(array)[:-top_n:-1]
results = [array[index] for index in top_n_indexs]
return results
if __name__ == '__main__':
ret = get_top_n(np.array([1, 3, 34, 4, 5, 6]), 3)
print(ret)
复制代码
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
欢迎光临 IT评测·应用市场-qidao123.com (https://dis.qidao123.com/)
Powered by Discuz! X3.4