The Python library is designed to be used for mathematical computation, manipulation, and visualization. The library is designed to be used for mathematical computation, manipulation, and visualization. Today’s Python tutorial starts with Numpy!

In this paper, the target

  • I met Numpy
  • Ndarray add delete change check
  • Ndarray slicing and screening
  • Ndarray arithmetic and sorting

NumPy profile

NumPy, short for Numerical Python, is the basic software package for scientific computation in Python. NumPy provides Python with a number of mathematical libraries that allow us to do numerical calculations efficiently. More can be found on Numpy’s website.

Numpy is a Python tutorial.

A few things to know about Numpy:

  • NumPy arrays are created with a fixed size, unlike Python lists, which can grow dynamically. Changing the size of ndarray creates a new array and deletes the original data.
  • The elements in a NumPy array all need to have the same data type and therefore will have the same size in memory. Arrays whose elements are also arrays (either Python’s native array or NDARray) form multidimensional arrays.
  • NumPy arrays facilitate advanced mathematical and other types of manipulation of large amounts of data. In general, such operations may be more efficient and require less code execution than using Python’s built-in sequences.

So, at the heart of Numpy is the NDARray object, which encapsulates an N-dimensional array of homogeneous data types. And the reason they call it NDARray is because it’s short for N-Dimensional-array. Next, all the lessons in this section are about NDARray. There is less theoretical knowledge and more code, so when you learn, you should try to run the code by yourself.

Create ndarray

  • Created by the Python list
  • Created by the numpy built-in function

Access, delete, and add elements in NdarRay

The basic methods for accessing, changing, or adding an element in NdarRay are provided here.

Access & Change

Similar to how elements in a Python List are accessed or changed by their index.

delete

You can use the np.delete(ndarray, elements, Axis) function for deletion.

What needs to be noted here is the parameter axis. In 2-dimensional data, axis = 0 means selecting rows and axis = 1 means selecting columns, but 0 cannot be mechanically considered as representing rows and 1 as representing columns, as noted in the previous 2-dimensional data.

In 3d data, axis = 0 represents groups, 1 represents rows, and 2 represents columns. Why is that? How are groups, rows, and columns sorted in a three-digit array shape?

Therefore, axis assignments must take into account the array shape.

One more thing to note is that if you want to keep the deleted data, you need to reassign it.

increase

The way to add elements to ndarRay is similar to the way to add elements to python lists. There are two common methods:

  • One is append, which is to add the new element to the end of ndarRay
  • Syntax: np.append(ndarray, elements, axis)
  • The arguments are the same as the delete function, and the usage is the same
  • One is insert, which allows you to insert new elements into a specified location
  • Insert (ndarray, index, elements, axis)
  • An index is added to the argument, indicating where to insert the new element.

It is important to note that when inserting elements into a multidimensional array, whether append or INSERT, it is important to ensure that the corresponding Axis shape is consistent. Another is that, like delete, if you want to change the original data, you need to reassign.

Sectioning and screening

Ndarray slice

Now that we’ve learned how to select an element in AN NDARRay, we’ll learn how to get a subset of nDARRay — slicing.

We are not unfamiliar with slicing, in the list we have also touched slicing, one-dimensional NDARRay slicing is the same as list. Note that it is necessary to understand 2d and multidimensional NDARray slices.

  • 2 dimensional matrix slice

So you can see here that we filter all the rows in the first three columns of the A matrix, how does that work?

The first element of the slice: selects all rows, and the second element :-1 indicates from column 0 to the last (not included), so the result is shown above.

Here’s another example:

All columns in rows 2-3 are filtered.

  • A commonly used slice

Get the last column of data as a column:

Get the last column as a one-dimensional array:

The above two methods are often used. Shape of the former is (4,1), and shape of the latter is (4,).

Ndarray screening

  • Select the diagonal of NDARRay

The function used is np.diag(ndarray, k=N), where the value of parameter k determines which diagonal to select data according to.

Default k = 0, take the main diagonal;

When k = 1, take the element in the first row above the main diagonal;

When k is equal to negative 1, take the element in the row 1 below the main diagonal.

Consider: this function can only select elements on the main diagonal, but what if you want to get elements on the secondary diagonal?

Try searching for numpy Opposite Diagonal yourself.

Getting the opposite Diagonal of a NUMpy array is not recommended.

  • Extract unique values in Ndarray

Np.unique (ndarray) is used as the function np.unique(ndarray). Note that unique can also be added to the parameter axis to control the direction of the evaluation of the unique value.

  • Filter by Boolean operation

The filter condition is added here in brackets, and is returned when the result of the condition is True (that is, when the condition is satisfied).

Here it is important to note that when the input multiple filters, & said with, or | said, not ~.

Operation and sorting

Ndarray operation

  • Set operations
  • Arithmetic operations

We can add, subtract, multiply and divide the two matrices by +, -, *, / or Np. add, NP. substract, NP. multiply and Np. divide at the element level. Since it is an element-level operation, the shape of the two matrices must be consistent or broadcastable.

The so-called broadcastable here means that although the shape of the two matrices A and B are different, A can be split into integer matrices with the same shape as B. In this way, when performing element-level operations, A will be split first, and then it will be calculated with B, and the results will be combined. Here A is the “broadcastable” matrix.

So the product that we’re talking about is the product of the elements, the dot product, what about the cross product of the matrix? The numpy. Matmul function.

Ndarray sorting

We use np.sort() and ndarray.sort() to sort Ndarray.

The same is true:

In both cases, you can use the axis parameter axis to determine which axis to sort by, either column when axis = 0 or row when axis = 1.

The difference is:

Np.sort () does not change the original array; Ndarray.sort () changes the original array.

More Python tutorials will continue to be updated! Everybody where have not clear place can leave a message or private letter!