Understand numpy functions: Explore commonly used numpy functions in Python, specific code examples are required
Introduction:
In Python, NumPy (short for Numerical Python) It is a powerful scientific computing library that provides efficient multi-dimensional array objects and a large number of mathematical function libraries for Python. NumPy is one of the core libraries for scientific computing using Python and is widely used in data analysis, machine learning, image processing and other fields. This article will introduce some commonly used NumPy functions and provide specific code examples.
1. Functions for creating arrays
(1) Creating one-dimensional arrays
We can create one-dimensional arrays by using numpy's arange, linspace, logspace and other functions.
Code example:
import numpy as np
arr1 = np.arange(10)
print ("One-dimensional array created by arange function:", arr1)
arr2 = np.linspace(0, 1, 10) # Generate from 0 to 10 equally spaced numbers of 1
print("One-dimensional array created by linspace function:", arr2)
arr3 = np.logspace (0, 2, 10) # Generate 10 equally spaced logarithmic numbers from 10^0 to 10^2
print("One-dimensional array created by the logspace function:", arr3)
(2) Creating multi-dimensional arrays
In addition to one-dimensional arrays, we can also create multi-dimensional arrays by using numpy's array function.
Code example:
import numpy as np
arr4 = np.array([[1, 2, 3],
[4, 5, 6]])
print("Two-dimensional array created by array function:
", arr4)
arr5 = np. array([[[1, 2, 3],
[4, 5, 6]], [[7, 8, 9], [10, 11, 12]]])
print("Three-dimensional array created by array function:
", arr5)
2. Array operation function
NumPy provides a wealth of array operation functions, including mathematical functions, statistical functions, logical functions, etc.
(1) Mathematical functions
The mathematical functions in NumPy can perform operations on elements in the array Some calculations, such as logarithmic functions, trigonometric functions, exponential functions, etc.
Code example:
import numpy as np
arr6 = np.array([1, 2 , 3, 4])
print("The square of the array:", np.square(arr6))
print("The square root of the array:", np.sqrt(arr6))
print("The exponential function of the array:", np.exp (arr6))
(2) Statistical functions
By using NumPy’s statistical functions, we can perform statistical analysis on arrays, such as sum, average, maximum, minimum, etc.
Code example:
import numpy as np
arr7 = np.array([1, 2, 3, 4, 5])
print("The sum of the array:", np.sum(arr7))
print("The average of the array:", np .mean(arr7))
print("The maximum value of the array:", np.max(arr7))
print("Minimum value of array:", np.min(arr7))
(3) Logical function
Logical function performs logical operations on the elements in the array, such as Determine whether an element meets a certain condition.
Code example:
import numpy as np
arr8 = np.array([1, 2, 3, 4, 5] )
print("Whether the elements in the array are greater than 2:", np.greater(arr8, 2))
print("whether the elements of the array are less than or equal to 3:", np.less_equal(arr8, 3))
3. Shape function of the array
NumPy provides many functions for array shape operations, such as changing array shape, splicing arrays, etc.
(1) Change the shape of the array
You can change the shape of the array by using the reshape function, such as changing a one-dimensional array into a two-dimensional array or changing a multi-dimensional array into a one-dimensional array.
Code example:
import numpy as np
arr9 = np.arange(9)
arr10 = np.reshape(arr9, (3, 3))
print("Convert one-dimensional array to two-dimensional array:
", arr10)
arr11 = np.ravel(arr10)
print("Convert a multi-dimensional array into a one-dimensional array:", arr11)
( 2) Splicing arrays
NumPy provides functions such as vstack, hstack and concatenate for splicing arrays.
Code example:
import numpy as np
arr12 = np.array([[1, 2, 3],
[4, 5, 6]])
arr13 = np .array([[7, 8, 9],
[10, 11, 12]])
arr14 = np.vstack((arr12, arr13))
print("Vertical splicing array:
", arr14)
arr15 = np.hstack((arr12, arr13))
print("Horizontal splicing array:
", arr15 )
Summary:
Through the introduction of this article, we have learned about some commonly used functions in NumPy, including functions to create arrays, array operation functions and array shape functions. These functions can help us be more convenient Easily perform array operations and mathematical calculations to improve programming efficiency. We hope that through the study of this article, readers will become more familiar with the commonly used functions in NumPy and be able to flexibly apply them to actual data processing and scientific calculations.
The above is the detailed content of Explore commonly used numpy functions in Python: Understanding numpy functions. For more information, please follow other related articles on the PHP Chinese website!