大连密封材料 发表于 2024-9-4 17:29:01

科学盘算根本软件包Numpy介绍及常用法

1.介绍及阐明

        NumPy 是一个开源的 Python 库,专门用于科学盘算和数值处理。它提供了强大的多维数组对象和丰富的函数库,支持高效的数组运算。NumPy 是许多其他科学盘算库(如 SciPy、Pandas、Matplotlib 等)的根本。以下是对 NumPy 的详细介绍和阐明。
https://i-blog.csdnimg.cn/direct/4f5dc3d31b644d56ae3ea69b065c54af.png
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)    for step in installs:                                                                                                                                          items = step.split(":")      print(items)      if len(items) > 1:            print(f"{items}")if __name__ == '__main__':    test() https://i-blog.csdnimg.cn/direct/7baaa9bc5bdd4ec4ad2d37f6e084e32d.png
4.NumPy 根本示例

以下是一些 NumPy 的根本示例代码,展示了其主要功能和用法。
1.创建数组:
import numpy as np

# 创建一维数组
a = np.array()
print(a)

# 创建二维数组
b = np.array([, ])
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()
print(i)

# 广播机制
j = a + np.array([, , , , ])
print(j)
3.数组切片和索引:
k = np.array()
print(k)# 切片
print(k[::2])# 步长为2的切片

l = np.array([, , ])
print(l)# 第二行
print(l[:, 1])# 第二列
print(l)# 元素 (1,1)
4.数组形状操纵:
m = np.array([, ])
print(m.shape)# 数组形状

n = m.reshape((3, 2))# 重塑数组
print(n)

o = m.flatten()# 展平数组
print(o)
5.线性代数:
p = np.array([, ])
q = np.array([, ])

# 矩阵乘法
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)
    results = for index in top_n_indexs]
    return results

if __name__ == '__main__':
    ret = get_top_n(np.array(), 3)
    print(ret) 运行结果:
https://i-blog.csdnimg.cn/direct/ef7e66a992a04defa9de5c93801c1f64.png
2.取列表中最大的前几位 

import numpy as np

def get_top_n(array, top_n):
    top_n_indexs = np.argsort(array)[:-(top_n+1):-1]
    results = for index in top_n_indexs]
    return results

if __name__ == '__main__':
    ret = get_top_n(np.array(), 3)
    print(ret) 运行结果:
https://i-blog.csdnimg.cn/direct/150e0562ec2f455fa546b1bd5774b4a3.png
分析:
        上面代码界说了一个函数 get_top_n,用于从一个 NumPy 数组中获取最大的 top_n 个元素。代码逻辑非常清晰,使用了 np.argsort 函数来获取排序后的索引,然后使用这些索引获取相应的数组元素。
以下是对代码的详细解读:
1.np.argsort(array) 返回的是数组元素从小到大的索引。
2.[:-(top_n+1):-1] 是一个切片操纵,用于获取从末端向前数的 top_n 个索引,对应的就是数组中最大的 top_n 个元素的索引。
3. 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
    return results

if __name__ == '__main__':
    ret = get_top_n(np.array(), 3)
    print(ret)
        在这个优化版本中,results 直接使用了 NumPy 数组的切片和索引功能,克制了将数组转换为列表的操纵。这样做不仅能进步性能,还能保持代码的简便性。
测试结果
运行优化后的代码,结果如下:

这表明函数正确地返回了数组中最大的 3 个元素 。

import numpy as np


def get_top_n(array, top_n):
    top_n_indexs = np.argsort(array)[:-top_n:-1]
    results = for index in top_n_indexs]
    return results


if __name__ == '__main__':
    ret = get_top_n(np.array(), 3)
    print(ret) https://i-blog.csdnimg.cn/direct/9ac8b7d039e143b2be6a1029337186d3.png





免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
页: [1]
查看完整版本: 科学盘算根本软件包Numpy介绍及常用法