Home  >  Article  >  Backend Development  >  Explore commonly used numpy functions in Python: Understanding numpy functions

Explore commonly used numpy functions in Python: Understanding numpy functions

PHPz
PHPzOriginal
2024-01-26 09:16:16416browse

Explore commonly used numpy functions in Python: Understanding numpy functions

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

Use the arange function to create a one-dimensional array

arr1 = np.arange(10)
print ("One-dimensional array created by arange function:", arr1)

Use linspace function to create one-dimensional array

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)

Use logspace function to create one-dimensional array

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

Use the array function to create a two-dimensional array

arr4 = np.array([[1, 2, 3],

             [4, 5, 6]])

print("Two-dimensional array created by array function:
", arr4)

Use array function to create three-dimensional array

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])

Calculate the square of the array

print("The square of the array:", np.square(arr6))

Calculate the square root of the array

print("The square root of the array:", np.sqrt(arr6))

Calculate the exponential function of the array

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])

Find the sum of the arrays

print("The sum of the array:", np.sum(arr7))

Find the average of the array

print("The average of the array:", np .mean(arr7))

Find the maximum value of the array

print("The maximum value of the array:", np.max(arr7))

Find the minimum value of the array Value

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] )

Judge whether the elements in the array are greater than 2

print("Whether the elements in the array are greater than 2:", np.greater(arr8, 2))

Judge whether the elements in the array are greater than 2 Whether the elements of the array are less than or equal to 3

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)

Convert a one-dimensional array to three rows and three columns Two-dimensional array

arr10 = np.reshape(arr9, (3, 3))
print("Convert one-dimensional array to two-dimensional array:
", arr10)

Convert a multi-dimensional array into a one-dimensional array

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]])

Vertical splicing array

arr14 = np.vstack((arr12, arr13))
print("Vertical splicing array:
", arr14)

Horizontal splicing array

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!

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