Home > Backend Development > Python Tutorial > Summary of numpy functions: List of commonly used functions and functions

Summary of numpy functions: List of commonly used functions and functions

PHPz
Release: 2024-01-26 09:52:06
Original
1416 people have browsed it

Summary of numpy functions: List of commonly used functions and functions

numpy function guide: an overview of commonly used functions and their functions in the numpy library, specific code examples are required

Introduction:
NumPy is a Python library for science The core library of computing provides a large number of efficient array operation functions and tools. It has been widely used in fields such as data processing, numerical computing and machine learning. This article will introduce some commonly used NumPy functions, as well as their specific functions and usage, and provide corresponding code examples.

1. Function to create array

  1. numpy.array()
    numpy.array() function is used to create an array. Can take a list, tuple, number, or other array and create an array of specified shape and data type.

Code example:
import numpy as np

Create a 1-dimensional array

a = np.array([1, 2, 3])
print(a) # Output: [1 2 3]

Create a 2-dimensional array

b = np.array([[1, 2, 3], [4, 5, 6]])
print(b)
'''
Output:
[[1 2 3]
[4 5 6]]
'''

  1. numpy.zeros()
    The numpy.zeros() function is used to create an array of a specified size and initialize the array elements to 0.

Code example:
import numpy as np

Create a 3x3 array of all 0s

a = np.zeros((3, 3))
print(a)
'''
Output:
[[0. 0. 0.]
[0. 0. 0.]
[0. 0. 0 .]]
'''

  1. numpy.ones()
    numpy.ones() function is used to create an array of a specified size and initialize the array elements to 1.

Code example:
import numpy as np

Create a 2x2 array of all 1s

a = np.ones((2, 2))
print(a)
'''
Output:
[[1. 1.]
[1. 1.]]
'''

2. Array operation functions

  1. numpy.shape()
    numpy.shape() function is used to obtain the shape of the array.

Code example:
import numpy as np

arr = np.array([[1, 2, 3], [4, 5, 6]])
print(arr.shape) # Output: (2, 3)

  1. numpy.reshape()
    numpy.reshape() function is used to change the shape of the array.

Code example:
import numpy as np

arr = np.array([1, 2, 3, 4, 5, 6])
res = arr.reshape((2, 3))
print(res)
'''
Output:
[[1 2 3]
[4 5 6]]
' ''

  1. numpy.concatenate()
    numpy.concatenate() function is used to join two or more arrays together along the specified axis.

Code example:
import numpy as np

a = np.array([[1, 2], [3, 4]])
b = np.array([[5, 6]])
res = np.concatenate((a, b), axis=0)
print(res)
'''
Output:
[[1 2]
[3 4]
[5 6]]
'''

3. Functions for mathematical operations

  1. numpy .add()
    numpy.add() function is used to perform element-wise addition of two arrays.

Code example:
import numpy as np

a = np.array([1, 2, 3])
b = np.array([4 , 5, 6])
res = np.add(a, b)
print(res) # Output: [5 7 9]

  1. numpy.subtract()## The #numpy.subtract() function is used to perform element-wise subtraction of two arrays.
Code example:

import numpy as np

a = np.array([4, 5, 6])

b = np.array([1 , 2, 3])
res = np.subtract(a, b)
print(res) # Output: [3 3 3]

    numpy.dot()## The #numpy.dot() function is used to calculate the dot product of two arrays.

  1. Code example:
import numpy as np


a = np.array([1, 2, 3])

b = np.array([4 , 5, 6])

res = np.dot(a, b)
print(res) # Output: 32

Conclusion:

This article introduces some commonly used NumPy functions and Its functions and usage, and corresponding code examples are provided. By using these functions, we can easily create arrays, perform array operations and perform mathematical operations. NumPy plays an important role in scientific computing. I hope this article can help readers learn and use NumPy.


Reference materials:

1. "NumPy Official Documentation", https://numpy.org/doc/

2. "Usage of Python Scientific Computing Library NumPy", https://www .runoob.com/numpy/numpy-tutorial.html

The above is the detailed content of Summary of numpy functions: List of commonly used functions and 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