Home > Backend Development > Python Tutorial > A complete guide to parsing NumPy functions

A complete guide to parsing NumPy functions

王林
Release: 2024-01-26 10:35:06
Original
1144 people have browsed it

A complete guide to parsing NumPy functions

NumPy (Numerical Python) is an open source Python scientific computing library that provides multi-dimensional array objects and tools for operating on arrays. It is one of the core libraries of the Python data science ecosystem and is widely used in fields such as scientific computing, data analysis, and machine learning. This article will analyze the commonly used functions in the NumPy library one by one, including array creation, array operations, mathematical functions, statistical functions, linear algebra, etc., and provide specific code examples.

  1. Array Creation
    NumPy provides a variety of methods to create arrays. Arrays can be created by specifying dimensions, data types, and initialization values. Commonly used functions are:

1.1 numpy.array(): Create an array from a list or tuple.

import numpy as np

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

# 输出:[1 2 3 4 5]
Copy after login

1.2 numpy.zeros(): Creates an all-zero array of specified dimensions.

import numpy as np

arr = np.zeros((3, 4))
print(arr)

"""
输出:
[[0. 0. 0. 0.]
 [0. 0. 0. 0.]
 [0. 0. 0. 0.]]
"""
Copy after login

1.3 numpy.ones(): Create an all-one array of specified dimensions.

import numpy as np

arr = np.ones((2, 3))
print(arr)

"""
输出:
[[1. 1. 1.]
 [1. 1. 1.]]
"""
Copy after login

1.4 numpy.arange(): Create an arithmetic array.

import numpy as np

arr = np.arange(0, 10, 2)
print(arr)

# 输出:[0 2 4 6 8]
Copy after login
  1. Array operations
    NumPy provides many functions for array operations, including shape operations, indexing and slicing, expansion and stacking, and array transposition. Commonly used functions are:

2.1 reshape(): Change the shape of the array.

import numpy as np

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

"""
输出:
[[1 2]
 [3 4]
 [5 6]]
"""
Copy after login

2.2 indexing and slicing: operate arrays through indexing and slicing.

import numpy as np

arr = np.array([1, 2, 3, 4, 5])
print(arr[2])       # 输出:3
print(arr[1:4])     # 输出:[2 3 4]
print(arr[:3])      # 输出:[1 2 3]
print(arr[-3:])     # 输出:[3 4 5]
Copy after login

2.3 concatenate(): Concatenate two or more arrays.

import numpy as np

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

# 输出:[1 2 3 4 5 6]
Copy after login

2.4 transpose(): Transpose the array.

import numpy as np

arr = np.array([[1, 2], [3, 4]])
new_arr = np.transpose(arr)
print(new_arr)

"""
输出:
[[1 3]
 [2 4]]
"""
Copy after login
  1. Mathematical functions
    NumPy provides a wealth of mathematical functions, such as numerical operations, trigonometric functions, logarithmic functions, exponential functions, etc. Commonly used functions are:

3.1 np.mean(): Calculate the average of an array.

import numpy as np

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

# 输出:3.0
Copy after login

3.2 np.sin(): Calculate the sine value of the array element.

import numpy as np

arr = np.array([0, np.pi/2, np.pi])
sin = np.sin(arr)
print(sin)

# 输出:[0.         1.         1.2246468e-16]
Copy after login

3.3 np.exp(): Perform exponential operation on array elements.

import numpy as np

arr = np.array([1, 2, 3])
exp = np.exp(arr)
print(exp)

# 输出:[ 2.71828183  7.3890561  20.08553692]
Copy after login
  1. Statistical functions
    NumPy provides commonly used statistical functions, including maximum, minimum, median, variance and standard deviation. Commonly used functions are:

4.1 np.max(): Calculate the maximum value of the array.

import numpy as np

arr = np.array([1, 2, 3, 4, 5])
max_value = np.max(arr)
print(max_value)

# 输出:5
Copy after login

4.2 np.min(): Calculate the minimum value of the array.

import numpy as np

arr = np.array([1, 2, 3, 4, 5])
min_value = np.min(arr)
print(min_value)

# 输出:1
Copy after login

4.3 np.median(): Calculate the median of the array.

import numpy as np

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

# 输出:3.0
Copy after login

4.4 np.var(): Calculate the variance of the array.

import numpy as np

arr = np.array([1, 2, 3, 4, 5])
variance = np.var(arr)
print(variance)

# 输出:2.0
Copy after login
  1. Linear Algebra
    NumPy provides basic linear algebra operation functions, such as matrix multiplication, matrix inversion, matrix determinant, etc. Commonly used functions are:

5.1 np.dot(): Calculate the dot product of two arrays.

import numpy as np

arr1 = np.array([[1, 2], [3, 4]])
arr2 = np.array([[5, 6], [7, 8]])
dot_product = np.dot(arr1, arr2)
print(dot_product)

"""
输出:
[[19 22]
 [43 50]]
"""
Copy after login

5.2 np.linalg.inv(): Calculate the inverse of a matrix.

import numpy as np

arr = np.array([[1, 2], [3, 4]])
inverse = np.linalg.inv(arr)
print(inverse)

"""
输出:
[[-2.   1. ]
 [ 1.5 -0.5]]
"""
Copy after login

The above are only part of the functions in the NumPy library. By understanding how to use these common functions, we can use NumPy more efficiently to perform computing tasks such as array operations, mathematical operations, statistical analysis, and linear algebra. At the same time, by in-depth study of the relevant documents of the NumPy library, we can discover more powerful functions and functions to provide strong support for our scientific computing work.

The above is the detailed content of A complete guide to parsing NumPy functions. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template