1 Can lists create multidimensional arrays?
list_three = [[0 for i in range(3)] for j in range(3)] print(list_three) list_three[1][1] = 3 print(list_three)
[[0, 0, 0], [0, 0, 0], [0, 0, 0]] [[0, 0, 0], [0, 3, 0], [0, 0, 0]]
02 Advantage of NumPy array over List
NumPy, all called Numerical Python, is one of Python's matrix Basic software package for scientific computing. NumPy is often used in conjunction with Padaas and Matpotlib, so it is known as the Data Analysis Three Swordsmen. Numpy has a powerful ndarray object that can create N-dimensional arrays. Numpy also provides a number of general functions that support manipulating elements of arrays, performing arithmetic operations on arrays, and providing commonly used statistical functions.
NumPy arrays have the following advantages over List objects:
- This is because the elements of the list are stored separately in system memory, and the NumPy array is stored in a uniformly contiguous block of memory. This saves computing resources by traversing all elements in an array calculation, unlike a list in which memory addresses need to be looked up.
- Numpy arrays can use vectorization to process the entire array, which is faster. Python lists, on the other hand, typically require looping statements to iterate through them, which is relatively inefficient.
- Matrix calculations in NumPy can take Multithreaded By using the multi-core CPU computing resources, the computing efficiency is greatly improved.
- Numpy uses an optimized C API and is faster.
03 Create Array
As mentioned earlier, the main opposite of NumPy is the ndarray object, which is actually a collection of data of the same type. Because ndarray supports the creation of multidimensional arrays, there are two concepts of rows and columns.
The first way to create ndarray is by using array.
import numpy as np # Create a one-dimensional array nd_one = np.array([1, 2, 3]) # Create a two-dimensional array nd_two = np.array([[1, 2, 3], [4, 5, 6]]) print(nd_one) print(nd_two) print('nd_two.shape =', nd_one.shape) print('nd_two.shape =', nd_two.shape) >>> Run result: [1 2 3] [[1 2 3] [4 5 6]] nd_two.shape = (3,) nd_two.shape = (2, 3)
Where shape is an attribute of an array, it gets the size of the array (how many rows, how many columns) and, if it is a one-dimensional array, only displays (rows,). Print out nd_in code The shape of the two, output as (2,3), indicates that there are two rows and three columns in the array.
The second option uses Numpy's built-in function
- 1 Use arange or linspace to create a continuous array.
```python import numpy as np # arange() range() similar to Python's built-in function # Arange (initial value, final value, step) Does not contain final values x0 = np.arange(1, 11, 2) print(x0) # Create a 5x3 array x1 = np.arange(15).reshape((5, 3)) print(x1) # linspace() linear equal vector # Linspace (initial, final, number of elements) contains the final value x2 = np.linspace(1, 11, 6) print(x2) >>> Run result: [1 3 5 7 9] [[ 0 1 2] [ 3 4 5] [ 6 7 8] [ 9 10 11] [12 13 14]] [ 1. 3. 5. 7. 9. 11.]
although np.arange and np.linspace It works the same way, it creates Equal difference array But they are created in different ways.
- 2 Use zeros(), ones(), full() to create arrays
import numpy as np # Create a 3x4 array with all values zero x3 = np.zeros((3, 4), dtype=int) print(x3) # Create a 3x4 array with all element values of 1 x4 = np.ones((3, 4), dtype=int) print(x4) # Create a 3x4 array and fill all elements with values of 2 x5 = np.full((3, 4), 2, dtype=int) print(x5) >>> Run result: [[0 0 0 0] [0 0 0 0] [0 0 0 0]] [[1 1 1 1] [1 1 1 1] [1 1 1 1]] [[2 2 2 2] [2 2 2 2] [2 2 2 2]]
- 3 Use eye() to create the unit matrix
The array created by eye() is characterized by the number of rows and Number of columns All the same. Because it creates a unit matrix, which is a regular matrix. diagonal The values are all 1, and the values at other locations are 0.
import numpy as np # Create a 3x3 unit matrix x6 = np.eye(3, dtype=int) print(x6) >>> Run result: [[1 0 0] [0 1 0] [0 0 1]]
- 4 Use diag() to create a diagonal matrix
diag() is a diagonal matrix of NxN, which is a matrix with 0 elements outside the main diagonal of the diagonal.
import numpy as np x7 = np.diag([1, 2, 3]) print(x7) >>> Run result: [[1 0 0] [0 2 0] [0 0 3]]
- 5 Use random to create random arrays
There are many built-in functions in random in numpy, and a few of them can be introduced briefly.
import numpy as np # Create a 2x2 array with all values randomly filled x9 = np.random.random((2, 2)) print(x9) # Create a random integer with a value of 3x3 in the [0, 10] interval x10 = np.random.randint(0, 10, (3, 3)) print(x10) >>> Run result: [[ 0.77233522 0.41516417] [ 0.22350126 0.31611254]] [[0 6 5] [7 6 4] [5 5 9]]