Numpy easy start

summary

Numpy is a basic package for high-performance scientific computing and data analysis. It provides the function of matrix operation and is widely used in the field of deep learning and data analysis..

use

Create array

vector = np.asarray([1,2,3,4]);

Create zero array

a = np.zeros(10) # Create vector
 a1 = np.zeros(shape=(5,3)) # Create a 0 matrix with 5 rows and 3 columns

Create one matrix

one = np.ones((3,4));

Create a matrix of custom values

user_define = np.full((3, 5), 110)  # Create a matrix with 3 rows and 5 columns, and the filling value is 110

Create an incremental matrix

np.arange(0,20,2) # Three parameters, the first parameter represents the first value of the vector, the second parameter represents the last value, and the third parameter is the step size

Use linspace to create an equally divided matrix

np.linspace(0, 10, 5) # Three parameters. The first parameter represents the starting value of the vector, the second parameter represents the last value of the vector, and the third parameter represents the total number of vectors

Create a matrix with random numbers

np.random.randint(0,10,size = 10) # Generate a vector with a length of 10 with random numbers generated in the range of 0 to 10
np.random.randint(4,9,size = (5,3)) # Generate a matrix of 5 rows and 3 columns with random numbers in the range of 4 to 9

Create a matrix with a positive distribution

np.random.normal(loc = 1.0, scale = 1.0, size = (3,2)) # Create a matrix with 3 rows and 2 columns with a positive distribution with a mean loc of 1.0 and a parameter scale of 1

Properties in Numpy

Array dimension

ndim represents the number of array dimensions (or axes).

arr_1_d = np.asarray([1])
arr_2_d = np.asarray([[1, 2], [3, 4]])
print(arr_1_d.ndim) #The result is 1, representing arr_1_d is a 1-dimensional matrix, that is, a vector
print(arr_2_d.ndim) # It ends with 2

shape

Shape represents the dimension or shape of the array. It is an integer tuple type, and the length of the tuple is equal to ndim.

print(arr_1_d.shape) # The result is
print(arr_2_d.shape) # The result is [2,2]

shpe is very useful. We can even change the array shape with the help of reshape() function, but we need to ensure that the number of matrix elements after transformation is consistent with that before transformation

arr_2_d.reshape((4,1)) # Set arr_2_d reshape is an array of (4, 1)

In addition, reshape also has a * * order parameter. * * refers to the order in which elements are read. There are several parameters

  • 'C': the default parameter, which is read and written using an index method similar to that in C-like language (row first).
  • 'F': read and write using the index method similar to that in FORTRAN like language (column first).
  • 'A': if the original array is stored in 'C', reshape the array with the index of 'C', otherwise use the index of 'F'.
np.arange(6).reshape(2,3)
# The result is
# array([[0, 1, 2],
#       [3, 4, 5]])
np.arange(6).reshape(2,3,order = 'F')
# The result is
# array([[0, 2, 4],
#       [1, 3, 5]])

size

size, that is, the total number of array elements, is equal to the product of the elements in the shape attribute.

arr_2_d.size # The result is 2

Matrix type

dtype, which is an object that describes the type of elements in the array.

Use the dtype property to view the data type to which the array belongs. Most common data types in NumPy are supported, such as int8, int16, int32, float32, float64, etc. Dtype is a common attribute, which can be seen when creating arrays and converting data types.

arr_2_d.dtype # The result is dtype('int64 ')

Through the astype() method, we can output a new matrix from the old matrix according to the specified type.

vector = np.arange(1,3)
print(vector.dtype) # The result is int32
vector = vector.astype(float) # The result is float64

Matrix operation

Dot multiplication between matrices

Matrix multiplication requires the number of columns of the first matrix to be equal to the number of rows of the second matrix. The specific point multiplication function is dot()

a = np.arange(1, 6).reshape(2,3) 
b = np.arange(1,7).reshape(3,2)
print(a.shape[1] == b.shape[0])
print(a.dot(b)) 
# result
# [[22 28]
# [49 64]]

Transpose of matrix

Turn the rows of the original matrix into columns and columns into rows

a = np.arange(1,7),reshape(2,3)
print(a.T)
# result
[[1 4]
 [2 5]
 [3 6]]

Inverse of matrix

If the inverse of the matrix is required, you first need to import numpy Linalg, and then find the inverse through linalg's inv function. The condition of matrix inversion is that the number of rows and columns of the matrix must be the same.

import numpy.linalg as lg
a = np.array([[0,1],[2,3]])
invA = lg.inv(a)
# result
print(A.dot(invA))
# result
array([[-1.5,  0.5],
       [ 1. ,  0. ]])

arg operation

The argmax(a, axis = None, out = None) function is mainly used to find the subscript of the maximum value in an array, that is, the index position corresponding to the maximum number.

index = np.argmax([1,2,9,3,2]) # The result is 2

Axis stands for which axis to find the largest index value

summary

After coming back from tx internship, I have been busy with autumn recruitment and design for a long time. I haven't seriously blogged for a long time. Today, I'll set up a flag. In the future, I will write a blog every week to maintain the original habit of blogging.

Keywords: Python Algorithm Machine Learning AI

Added by Kia on Tue, 04 Jan 2022 13:15:42 +0200