[Python data analysis] use of numpy library - Part 1

Introduction to numpy

NumPy is a Python library for scientific computing that provides high-performance multidimensional array objects and various tools for manipulating these arrays. The name NumPy comes from Numerical Pythonthe abbreviation of "".

The main functions of NumPy include:

  • Multidimensional array object: NumPy provides a multidimensional array object, called ndarray, which is a table composed of data of the same type. ndarrayIt can contain multiple data types such as integers and floating point numbers, and supports basic mathematical operations and array operations.

  • Array operation: NumPy provides many array operation functions, including array indexing, slicing, splicing, splitting, etc., which can easily operate and process arrays.

  • Linear Algebra: NumPy provides functions related to linear algebra, such as matrix multiplication, matrix decomposition, solving linear equations, etc.

  • Random number generation: NumPy provides various random number generation functions, such as normal distribution, uniform distribution, etc., which can easily generate random number sequences.

  • Fourier transform: NumPy provides functions related to Fourier transform, which can easily perform tasks such as signal processing and image processing.

Because NumPy provides high-performance array operations and mathematical functions, it is widely used in scientific computing, data analysis, machine learning and other fields.

Before using numpy, make sure that the numpy library has been downloaded and installed. If it is not installed, please enter the following command in the console.

pip install numpy

numpy create array

The following introduces several functions of NumPy to create arrays, and you can choose the appropriate function to create arrays according to your needs.

  1. array(): Creates an array that can accept a sequence or nested sequences as input.
import numpy as np

arr = np.array([1, 2, 3])
print(arr)  # 输出 [1 2 3]
  1. zeros(): Creates an array whose elements are all 0.
import numpy as np

arr = np.zeros((3, 4))
print(arr)  # 输出 [[0. 0. 0. 0.]
            #      [0. 0. 0. 0.]
            #      [0. 0. 0. 0.]]

  1. ones(): Creates an array with all 1 elements.
import numpy as np

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

  1. empty(): Create an uninitialized array, the value of the elements is uncertain.
import numpy as np

arr = np.empty((2, 3))
print(arr)  # 输出 [[4.67296746e-307 6.90583476e-310 1.69759663e-307]
            #      [1.69109977e-306 7.56595733e-307 1.37961302e-306]]

  1. arange(): Creates a one-dimensional array whose elements are integers within the specified range.
import numpy as np

arr = np.arange(10)
print(arr)  # 输出 [0 1 2 3 4 5 6 7 8 9]

  1. linspace(): Creates a one-dimensional array whose elements are uniformly distributed values ​​within the specified range.
import numpy as np

arr = np.linspace(0, 1, 5)
print(arr)  # 输出 [0.   0.25 0.5  0.75 1.  ]

numpy creates attributes of arrays

The following are several common attributes of NumPy arrays, which can be used to obtain information such as the shape, dimension, number of elements, and data type of the array.

  1. shape: Returns a tuple representing the shape of the array, that is, the number of elements in each dimension.
import numpy as np

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

  1. ndim: Returns an integer representing the number of dimensions of the array.
import numpy as np

arr = np.array([[1, 2, 3], [4, 5, 6]])
print(arr.ndim)  # 输出 2

  1. size: returns an integer representing the total number of elements in the array.
import numpy as np

arr = np.array([[1, 2, 3], [4, 5, 6]])
print(arr.size)  # 输出 6

  1. dtype: Returns an object describing the type of elements in the array.
import numpy as np

arr = np.array([1, 2, 3], dtype=float)
print(arr.dtype)  # 输出 float64

  1. itemsize: Returns an integer representing the byte size of each element in the array.
import numpy as np

arr = np.array([1, 2, 3], dtype=float)
print(arr.itemsize)  # 输出 8

  1. nbytes: Returns an integer representing the number of bytes occupied by all elements in the array.
import numpy as np

arr = np.array([1, 2, 3], dtype=float)
print(arr.nbytes)  # 输出 24

numpy array operations

  1. Operations on arrays and scalars: Operations such as addition, subtraction, multiplication, and division can be performed on each element in the array.
import numpy as np

arr = np.array([1, 2, 3])
arr = arr + 1
print(arr)  # 输出 [2 3 4]

  1. Operations between arrays: operations such as addition, subtraction, multiplication, and division can be performed on two arrays, and the shapes of the two arrays are required to be the same.
import numpy as np

arr1 = np.array([1, 2, 3])
arr2 = np.array([4, 5, 6])
arr3 = arr1 + arr2
print(arr3)  # 输出 [5 7 9]

  1. Logical operations on arrays: Logical operations can be performed on each element in the array and return an array of Boolean type.
import numpy as np

arr = np.array([1, 2, 3])
arr2 = arr > 2
print(arr2)  # 输出 [False False True]

  1. Array broadcast operation: operations can be performed on arrays of different shapes, and the system will automatically expand the array to meet the operation requirements.
import numpy as np

arr1 = np.array([1, 2, 3])
arr2 = np.array([[4], [5], [6]])
arr3 = arr1 * arr2
print(arr3)  # 输出 [[ 4  8 12]
             #      [ 5 10 15]
             #      [ 6 12 18]]

  1. Statistical operations of arrays: Statistical calculations can be performed on the elements in the array, such as sum, mean, standard deviation, etc.
import numpy as np

arr = np.array([1, 2, 3])
sum = arr.sum()
mean = arr.mean()
std = arr.std()
print(sum, mean, std)  # 输出 6 2.0 0.816496580927726

numpy array indexing and slicing

The following are some common indexing and slicing operations of NumPy arrays, and you can choose the appropriate way to access the elements in the array according to your needs.

  1. Indexing of arrays: An index can be used to access a single element in an array.
import numpy as np

arr = np.array([1, 2, 3])
print(arr[0])  # 输出 1

  1. Indexing of Multidimensional Arrays: Multiple indexes can be used to access a single element in a multidimensional array.
import numpy as np

arr = np.array([[1, 2, 3], [4, 5, 6]])
print(arr[0, 1])  # 输出 2

  1. Slicing of arrays: You can use slices to access multiple elements in an array.
import numpy as np

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

  1. Slicing of multidimensional arrays: Multiple slices can be used to access multiple elements in a multidimensional array.
import numpy as np

arr = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
print(arr[:2, 1:])  # 输出 [[2 3]
                    #      [5 6]]

  1. Fancy indexing of arrays: You can use array indexing to access multiple elements in an array.
import numpy as np

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

Shape transformation of numpy arrays

NumPy provides reshape()functions to change the shape of an array, often used to convert an array from one shape to another.
Let's see how to use it together.

  1. Converting a one-dimensional array to a two-dimensional array
    You can use the reshape() function to convert a one-dimensional array to a two-dimensional array, where the first parameter indicates the number of rows of the new array, and the second parameter indicates the number of columns of the new array.
import numpy as np

arr = np.array([1, 2, 3, 4, 5, 6])
new_arr = arr.reshape(2, 3)
print(new_arr)
# 输出
# [[1 2 3]
#  [4 5 6]]
  1. Convert a multidimensional array to a one-dimensional array
    You can use the reshape() function to convert a multidimensional array into a one-dimensional array, where you can only specify the size of one dimension, and use -1 for the remaining dimensions, so that NumPy will automatically calculate the new array. size.
import numpy as np

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

  1. Flattening an array into a one-dimensional array
    You can use the ravel() function to flatten a multidimensional array into a one-dimensional array. This function returns a new one-dimensional array without modifying the original array.
import numpy as np

arr = np.array([[1, 2, 3], [4, 5, 6]])
new_arr = arr.ravel()
print(new_arr)
# 输出 [1 2 3 4 5 6]

  1. Changing the dimension of an array
    You can use the reshape() function to change the dimension of an array, where -1 can be specified to indicate that NumPy needs to automatically calculate the size of the new array.
import numpy as np

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

The above is a basic use of the numpy library in python, and the next article will introduce other usage methods of numpy. If you are interested in numpy, you may wish to like and bookmark to support it.

At the same time, everyone is welcome to subscribe to the column python-data analysis , which contains specific usage methods of the numpy library.

Guess you like

Origin blog.csdn.net/weixin_46277553/article/details/130639363