Advanced Python: 8. NumPy Library

1. Introduction to NumPy LibraryNumPy is the core library for scientific computing in Python, primarily providing high-performance multidimensional array objects and accompanying mathematical functions. It is a fundamental tool in fields such as data analysis, machine learning, and scientific computing. Here, to complement the study of data visualization (using the Matplotlib library), we will only introduce some commonly used functions for creating arrays.2. Installation of NumPy LibraryEnter the following command in the command line:

pip install numpy

3. Common Functions for Creating Arrays(1) array() function: Used to convert iterable objects such as lists and tuples in Python into NumPy’s unique array object (ndarray).

import numpy as np
lst=[1,2,3,4,5,6]   # List of numbers
print(lst)
arr=np.array(lst)   # Convert to array form, elements remain unchanged
print(arr)
lst1=["a","b","c","d"]  # List of strings
print(np.array(lst1)) 

Advanced Python: 8. NumPy Library(2) empty(shape, dtype) function: Creates an uninitialized array of a specified shape (n rows and m columns) with random data.· shape: A tuple or list of two elements· dtype: Any type recognized by NumPy, default is float64

import numpy as np
arr=np.empty((3,5),dtype='int8')  # Generate a 3x5 array, data type is integer
print(arr)

Advanced Python: 8. NumPy Library(3) zeros() function: Creates an array of a specified shape with all elements initialized to 0.

import numpy as np
arr=np.zeros((3,3),dtype='int')  # Generate a 3x3 array, data type is integer
print(arr)

Advanced Python: 8. NumPy Library

import numpy as np
arr=np.zeros((3,3),dtype='float')  # Generate a 3x3 array, data type is float
print(arr)

Advanced Python: 8. NumPy Library(4) ones() function: Creates an array of a specified shape with all elements initialized to 1.

import numpy as np
arr=np.ones((3,3),dtype='int')  # Generate a 3x3 array
print(arr)

Advanced Python: 8. NumPy Library

import numpy as np
arr=np.ones((3,3),dtype='float')  # Generate a 3x3 array
print(arr)

Advanced Python: 8. NumPy Library(5) zeros_like() function: Creates an array with the same shape as a given array, filled with zeros.

import numpy as np
arr1=np.ones((2,3),dtype="int")  # Array with shape 2x3 and all elements are 1
print(arr1)
arr2=np.zeros_like(arr1)         # Generate a 2x3 array, all elements are 0
print(arr2)

(6) ones_like() function: Creates an array with the same shape as a given array, filled with ones.

import numpy as np
arr1=np.zeros((3,2),dtype="int")  # Array with shape 3x2 and all elements are 0
print(arr1)
arr2=np.ones_like(arr1)          # Generate a 3x2 array, all elements are 1
print(arr2)

(7) full() function: Creates an array of a specified shape with specified element values.

import numpy as np
arr=np.full((3,3),100)  # 3x3 array, all elements are 100
print(arr)

Advanced Python: 8. NumPy Library(8) arange() function: Creates a one-dimensional array of integers or floating-point numbers with a fixed step size. The syntax and usage are similar to Python’s built-in range() function, but it supports floating-point steps and returns a NumPy array.

import numpy as np
arr=np.arange(1,5,0.5) # In the interval [1,5), increase by 0.5 each time
print(arr)

Advanced Python: 8. NumPy Library(9) linspace() function: Creates a specified number of evenly distributed elements within a specified interval.

import numpy as np
arr=np.linspace(1,10,19)  # Generate 19 evenly distributed elements in the interval [1,10]
print(arr)

Advanced Python: 8. NumPy Library(10) eye(n, m=None, k=0, dtype=”float”) function: Used to generate an n-dimensional identity matrix, characterized by having 1s on the main diagonal and 0s elsewhere.· n: The number of rows in the identity matrix, must be specified.· m: The number of columns in the identity matrix, default is equal to n (i.e., generates a square matrix).· k: The position of the diagonal, default is 0 (main diagonal); k>0 indicates diagonals above the main diagonal, k<0 indicates diagonals below the main diagonal.· dtype: The data type of the array, default is float, can be specified as int, bool, etc.

import numpy as np
arr=np.eye(3)  # Only row parameter, defaults to generate a square matrix (3x3)
print(arr)

Advanced Python: 8. NumPy Library

import numpy as np
arr=np.eye(2,4)  # Generate a 2x4 identity matrix (main diagonal is 1)
print(arr)

Advanced Python: 8. NumPy Library

import numpy as np
arr=np.eye(2,4,1)  # Generate a 2x4 identity matrix (main diagonal shifted up by 1 unit)
print(arr)

Advanced Python: 8. NumPy Library

Leave a Comment