Home  >  Article  >  Backend Development  >  Comprehensive list of commonly used functions in the Numpy library: quick start and practice guide

Comprehensive list of commonly used functions in the Numpy library: quick start and practice guide

WBOY
WBOYOriginal
2024-01-19 08:57:051242browse

Comprehensive list of commonly used functions in the Numpy library: quick start and practice guide

The Numpy library is one of the most commonly used data processing libraries in Python. It is widely loved by data analysts for its efficient and convenient operation methods. In the Numpy library, there are many commonly used functions that can help us complete data processing tasks quickly and efficiently. This article will introduce some commonly used Numpy functions, and provide code examples and practical application scenarios so that readers can get started with the Numpy library faster.

1. Create an array

  1. numpy.array

Function prototype: numpy.array(object, dtype=None, copy=True, order= 'K', subok=False, ndmin=0)

Function description: Convert objects such as lists into arrays.

Code example:

import numpy as np

a = np.array([1, 2, 3])
print(a)  # 输出 [1 2 3]
  1. numpy.zeros

Function prototype: numpy.zeros(shape, dtype=float, order='C')

Function description: Create an all-zero array of the specified shape.

Code example:

import numpy as np

a = np.zeros((2, 3))
print(a)  # 输出 [[0. 0. 0.]
          #      [0. 0. 0.]]
  1. numpy.ones

Function prototype: numpy.ones(shape, dtype=None, order='C')

Function description: Create an all-one array of the specified shape.

Code example:

import numpy as np

a = np.ones((2, 3))
print(a)  # 输出 [[1. 1. 1.]
          #      [1. 1. 1.]]
  1. numpy.arange

Function prototype: numpy.arange(start, stop, step, dtype=None)

Function description: Create an arithmetic sequence array.

Code example:

import numpy as np

a = np.arange(0, 10, 2)
print(a)  # 输出 [0 2 4 6 8]

2. Array operations

  1. numpy.reshape

Function prototype: numpy.reshape(a , newshape, order='C')

Function description: Convert array a into a new array of specified shape.

Code example:

import numpy as np

a = np.array([1, 2, 3, 4, 5, 6])
b = a.reshape((2, 3))
print(b)  # 输出 [[1 2 3]
          #      [4 5 6]]
  1. numpy.transpose

Function prototype: numpy.transpose(a, axes=None)

Function description: Transpose the array.

Code example:

import numpy as np

a = np.array([[1, 2, 3], [4, 5, 6]])
b = np.transpose(a)
print(b)  # 输出 [[1 4]
          #      [2 5]
          #      [3 6]]
  1. numpy.concatenate

Function prototype: numpy.concatenate((a1, a2, ...), axis= 0)

Function description: perform splicing operation on arrays.

Code example:

import numpy as np

a = np.array([[1, 2], [3, 4]])
b = np.array([[5, 6], [7, 8]])
c = np.concatenate((a, b), axis=0)
print(c)  # 输出 [[1 2] 
          #      [3 4] 
          #      [5 6] 
          #      [7 8]]

3. Array calculation

  1. numpy.abs

Function prototype: numpy.abs(x , args, *kwargs)

Function description: Calculate the absolute value of each element in the array.

Code example:

import numpy as np

a = np.array([-1, 2, -3])
b = np.abs(a)
print(b)  # 输出 [1 2 3]
  1. numpy.round

Function prototype: numpy.round(a, decimals=0, out=None)

Function description: Round the elements in the array.

Code example:

import numpy as np

a = np.array([1.3, 2.6, 3.2])
b = np.round(a)
print(b)  # 输出 [1. 3. 3.]
  1. numpy.sum

Function prototype: numpy.sum(a, axis=None)

Function description: Calculate the sum of each element in the array.

Code example:

import numpy as np

a = np.array([[1, 2], [3, 4]])
b = np.sum(a, axis=0)
print(b)  # 输出 [4 6]

4. Commonly used mathematical functions

  1. numpy.exp

Function prototype: numpy.exp(x , args, *kwargs)

Function description: Calculate the exponential function value of each element in the array.

Code example:

import numpy as np

a = np.array([1, 2, 3])
b = np.exp(a)
print(b)  # 输出 [ 2.71828183  7.3890561  20.08553692]
  1. numpy.log

Function prototype: numpy.log(x, args, *kwargs )

Function description: Calculate the natural logarithm of each element in the array.

Code example:

import numpy as np

a = np.array([1, 2, 3])
b = np.log(a)
print(b)  # 输出 [0.         0.69314718 1.09861229]
  1. numpy.sqrt

Function prototype: numpy.sqrt(x, args, *kwargs )

Function description: Calculate the square root of each element in the array.

Code example:

import numpy as np

a = np.array([1, 4, 9])
b = np.sqrt(a)
print(b)  # 输出 [1. 2. 3.]

5. Practical application scenarios

  1. Simulating polynomial function
import numpy as np
import matplotlib.pyplot as plt

x = np.linspace(-5, 5, num=50)
y = np.power(x, 3) - 3 * np.power(x, 2) + 2 * x + 1

plt.plot(x, y)
plt.show()
  1. Array weighted sum
import numpy as np

a = np.array([1, 2, 3, 4])
b = np.array([0.1, 0.2, 0.3, 0.4])

result = np.sum(a * b)
print(result)  # 输出 2.0
  1. Sort arrays
import numpy as np

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

print(b)  # 输出 [1 2 3 4]

Summary:

This article introduces some common functions and application scenarios of the Numpy library. Including the creation, operation, calculation of arrays, and some mathematical functions. We can use these functions flexibly according to actual application scenarios to make data processing more efficient and convenient. It is recommended that readers write the code themselves and practice it to deepen their understanding and mastery of the Numpy library.

The above is the detailed content of Comprehensive list of commonly used functions in the Numpy library: quick start and practice guide. 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