Home  >  Article  >  Backend Development  >  A quick introduction to commonly used functions in numpy

A quick introduction to commonly used functions in numpy

PHPz
PHPzOriginal
2024-01-03 09:26:53572browse

A quick introduction to commonly used functions in numpy

Quickly understand the commonly used function sets in numpy, specific code examples are needed

With the rise of data science and machine learning, numpy has become the most commonly used science in Python One of the computing libraries. Numpy not only provides powerful multi-dimensional array objects, but also provides a rich set of functions that can perform mathematical operations, array operations, statistical analysis, linear algebra and other operations.

In order to quickly understand the collection of commonly used functions in numpy, some commonly used functions will be introduced below and specific code examples will be provided.

  1. Create Array

numpy provides different functions to create arrays, including converting lists to arrays, generating arithmetic sequences or random numbers, etc.

import numpy as np

array1 = np.array([1, 2, 3, 4, 5])  # 将列表转换为数组
print(array1)

array2 = np.arange(1, 10, 2)  # 生成等差数列,起始值为1,结束值为10,步长为2
print(array2)

array3 = np.random.random((2, 3))  # 生成2行3列的随机数数组
print(array3)
  1. Array operations

numpy provides a variety of mathematical operation functions, including addition, subtraction, multiplication and division, exponents, logarithms, trigonometric functions, etc.

import numpy as np

array1 = np.array([1, 2, 3, 4, 5])
array2 = np.array([6, 7, 8, 9, 10])

array_sum = np.add(array1, array2)  # 数组相加
print(array_sum)

array_diff = np.subtract(array1, array2)  # 数组相减
print(array_diff)

array_mult = np.multiply(array1, array2)  # 数组相乘
print(array_mult)

array_div = np.divide(array1, array2)  # 数组相除
print(array_div)

array_exp = np.exp(array1)  # 数组指数
print(array_exp)

array_log = np.log(array1)  # 数组对数
print(array_log)

array_sin = np.sin(array1)  # 数组正弦值
print(array_sin)
  1. Array operations

numpy provides a variety of functions for operating on arrays, including array shape transformation, array splicing, array slicing, etc.

import numpy as np

array1 = np.array([[1, 2, 3], [4, 5, 6]])
array2 = np.array([[7, 8, 9], [10, 11, 12]])

array_transpose = np.transpose(array1)  # 数组转置
print(array_transpose)

array_concatenate = np.concatenate((array1, array2), axis=0)  # 数组垂直拼接
print(array_concatenate)

array_slice = array1[0:2, 1:3]  # 数组切片
print(array_slice)
  1. Statistical analysis

numpy provides a variety of functions for statistical analysis, including sum, average, variance and standard deviation, etc.

import numpy as np

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

array_sum = np.sum(array1)  # 数组元素求和
print(array_sum)

array_mean = np.mean(array1)  # 数组元素求平均值
print(array_mean)

array_std = np.std(array1)  # 数组元素求标准差
print(array_std)
  1. Linear algebra

numpy provides a wealth of linear algebra functions that can perform operations such as matrix multiplication, matrix inversion, and matrix eigenvalues.

import numpy as np

matrix1 = np.array([[1, 2], [3, 4]])
matrix2 = np.array([[5, 6], [7, 8]])

matrix_dot = np.dot(matrix1, matrix2)  # 矩阵相乘
print(matrix_dot)

matrix_inv = np.linalg.inv(matrix1)  # 求矩阵的逆
print(matrix_inv)

matrix_eigen = np.linalg.eig(matrix1)  # 求矩阵的特征值
print(matrix_eigen)

The above are code examples of commonly used function collections in numpy. By understanding these functions, we can perform calculations such as array operations, mathematical operations, statistical analysis, and linear algebra more flexibly. I hope this article can help readers better grasp the common functions in numpy.

The above is the detailed content of A quick introduction to commonly used functions in numpy. 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