科学计算基础软件包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)

  • 7
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

图灵追慕者

您的支持是我写作分享最大动力!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值