Home >Backend Development >Python Tutorial >Comprehensive collection of commonly used functions in the Numpy library: unlocking efficient scientific computing techniques

Comprehensive collection of commonly used functions in the Numpy library: unlocking efficient scientific computing techniques

PHPz
PHPzOriginal
2024-01-19 10:36:05480browse

Comprehensive collection of commonly used functions in the Numpy library: unlocking efficient scientific computing techniques

Complete list of commonly used functions in the Numpy library: unlocking efficient scientific computing skills

For scientific computing in Python, the Numpy library is an essential tool, which provides a variety of functions Powerful functions and other useful tools. In order to help readers make better use of the Numpy library, this article will introduce some commonly used Numpy functions, as well as their usage and examples.

  1. Creating arrays
  • np.array(): Create arrays from Python lists and tuples.
  • np.zeros(): Create an array of all zeros.
  • np.ones(): Create an array of all ones.
  • np.full(): Create an array of arbitrary values.
  • np.arange(): Creates an array similar to the Python range function.
  • np.linspace(): Create an array within the specified interval.

Example:

import numpy as np

# 从 Python 列表和元组创建数组
arr1 = np.array([1, 2, 3, 4, 5])
arr2 = np.array((1, 2, 3))

# 创建全 0 数组
zeros_arr = np.zeros((2, 3))

# 创建全 1 数组
ones_arr = np.ones((2, 3))

# 创建一个任意值的数组
full_arr = np.full((2, 3), 4)

# 创建一个类似于 Python range 函数的数组
range_arr = np.arange(0, 10, 2)

# 创建一个在指定间隔内的数组
linspace_arr = np.linspace(0, 1, 5)
  1. Basic mathematical functions
    ##np.add(): Addition of two arrays.
  • np.subtract(): Subtraction of two arrays.
  • np.multiply(): Multiplication of two arrays.
  • np.divide(): Division of two arrays.
  • np.power(): Calculate a set of numbers raised to the specified power.
  • np.sqrt(): Find the square root of each element.
Example:

import numpy as np

arr1 = np.array([1, 2, 3])
arr2 = np.array([4, 5, 6])

# 两个数组的加法
add_arr = np.add(arr1, arr2)

# 两个数组的减法
sub_arr = np.subtract(arr1, arr2)

# 两个数组的乘法
mul_arr = np.multiply(arr1, arr2)

# 两个数组的除法
div_arr = np.divide(arr1, arr2)

# 将一组数按照指定的次数幂进行计算
power_arr = np.power(arr1, 2)

# 对每个元素求开方
sqrt_arr = np.sqrt(arr2)

    Array operations
##np.transpose(): Swap the axes of the array.
  • np.reshape(): Redefine the array shape.
  • np.concatenate(): Concatenate multiple arrays.
  • np.split(): Split an array into multiple arrays.
  • np.sort(): Sort the array according to the specified requirements.
  • Example:
import numpy as np

arr = np.array([[1, 2], [3, 4], [5, 6]])

# 交换数组的轴
transpose_arr = np.transpose(arr)

# 重新定义数组形状
reshape_arr = np.reshape(arr, (2, 3))

# 对多个数组进行拼接
arr1 = np.array([1, 2, 3])
arr2 = np.array([4, 5, 6])
concat_arr = np.concatenate((arr1, arr2), axis=0)

# 将一个数组分裂成多个数组
split_arr = np.split(concat_arr, 2)

# 按照指定要求进行数组排序
sort_arr = np.sort(arr1)

Statistical function
  1. ##np.sum(): Calculate the sum of arrays.
    np.mean(): Calculate the average of the array.
  • np.std(): Calculate the standard deviation of the array.
  • np.var(): Calculate the variance of the array.
  • np.max(): Calculate the maximum value of the array.
  • np.min(): Calculate the minimum value of the array.
  • Example:
  • import numpy as np
    
    arr = np.array([1, 2, 3, 4, 5, 6])
    
    # 对数组进行求和计算
    sum_arr = np.sum(arr)
    
    # 对数组进行求平均数计算
    mean_arr = np.mean(arr)
    
    # 对数组进行求标准差计算
    std_arr = np.std(arr)
    
    # 对数组进行求方差计算
    var_arr = np.var(arr)
    
    # 对数组进行求最大值计算
    max_arr = np.max(arr)
    
    # 对数组进行求最小值计算
    min_arr = np.min(arr)
Summary

The Numpy library provides very powerful tool functions to help complete various scientific computing tasks quickly and efficiently. This article introduces some commonly used functions and how to use them, including creating arrays, basic mathematical functions, array operations, statistical functions, etc. It is hoped that readers can master the use of these functions and improve work efficiency in the actual scientific calculation process.

The above is the detailed content of Comprehensive collection of commonly used functions in the Numpy library: unlocking efficient scientific computing techniques. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn