Share EncyclopediaHome EncyclopediaCategories Switch Channel

Numpy, it's a lesson to understand the best image of the array

2026-06-18 06:041330NameNetworking

All of the above examples deal with vectors at one dimension. The key to numpy's beauty is its ability to apply all of the above methods to any number of dimensions。

Create matrix

We can pass the python list of the following shapes so that numpy creates a matrix to express it:

Np. Array()

We can also use the method mentioned aboveNes(), zeros() and randall. Random() just write down a group of elements describing the array dimensions we created:

Rare matrix structure

Matrix operations

If the two arrays are the same size, we can add and multiply the arrays using an algorithm operator (+-*/). Numpy considers them as position-wise operations:

Rare matrix structure

We can also perform these algorithms for two arrays of different sizes, provided that one dimension is 1 (e. G., only one column or one row of the matrix), in which case numpy uses the broadcast rules to perform the algorithm:

Point multiplication

A key difference between arithmetical and matrix operations is the use of point multipliers for matrix multiplication. Numpy assigns dot() to each matrix, which we can multiply with other matrix implementation points:

Rare matrix structure

I added matrix dimensions to the lower right corner of the top figure to emphasize that the two matrices must have the same dimensions on the adjacent side. You can see the above as:

Rare matrix structure

Matrix index

When we process the matrix, indexing and slice operations become more useful:

Rare matrix structure

Matrix aggregation

We can aggregate the matrix like a polymer vector:

Rare matrix structure

We can not only aggregate all values in the matrix, but also use axis parameters to perform cross-line or cross-column aggregation:

Rare matrix structure

Converting and reshaping

A common requirement when processing the matrix is the rotation matrix. When two matrices need to be implemented by multiplying the operational points and aligning the dimensions they share, conversion is usually required. Numpy array has a convenient way to use t for matrix conversion:

Rare matrix structure

In more advanced cases, you may need to change the dimensions of a particular matrix. In machine learning applications, this is often the case: a model requires different shapes than your data set. In these circumstances, numpy's reshape() method can work. Just give it the new dimensions that the matrix needs. Numpy can extrapolate the right dimensions from your matrix:

Rare matrix structure

More dimensions

Numpy can achieve all the above at any level. Its central data structure is called ndarray (n dimensional array) and is not unreasonable。

Rare matrix structure

In many cases, the processing of a new dimension requires only the addition of a comma in the parameters of the numpy function:

Rare matrix structure

Practical use

Here is a demonstration of the useful functions that numpy can achieve。

Formula

Achieving mathematical formulas that can be used for matrices and vectors is a key example for numpy. That's why numpy is python community pet. For example, the equation, it's oversight

Machine learning

At the heart of the model approach to regression:

Rare matrix structure

Achieving this formula in numpy is easy:

The advantage of this is that numpy does not care whether the preditions and labels contain a value or a value of 1,000 (as long as they are the same size). We can do four of the above code lines in one instance:

Rare matrix structure

Both the projection and the label vector contain three values, i. E. The value of n is 3. After deduction, the following values were obtained:

Rare matrix structure

Then you get the square:

Rare matrix structure

Now for these values:

Rare matrix structure

The result is the error value and model quality rating of the projection。

Data representation

Consider all data types (e. G. Spreadsheets, images, audio, etc.) required to process and construct models, many of which are suitable for representation in n-dimensional arrays:

Tables and spreadsheets

A spreadsheet or value sheet is a two-dimensional matrix. Each sheet in a spreadsheet can be its own variable. The most popular abstract in python is the pandas data frame, which actually uses numpy and builds on it。

Rare matrix structure

Audio and time series

Audio files are a 1-dimensional array of samples. Each sample is a number representing a small fraction of the audio signal. Cd quality audio contains 44,100 samples per second, each of which is an integer number between 65535 and 65536. This means that if you have a 10-second cd mass wave file, you can load it into a numpy array with a length of 10*44,100 = 441,000. If you want to extract the first second audio, you simply load the file into the numpy array of the audio and then get the audio。

The following is an audio file:

Rare matrix structure

The same applies to time series data (e. G. Stock prices over time)。

Image

The image is a pixel matrix of size (higher x width)。

If the image is black and white (i. E. Greyscale), each pixel can be expressed in a single number (usually between 0 (black) and 255 (white)). Want to crop the pixels of the top left 10 x 10 of the image? Writing in numpy

It's okay。

The following is a clip of an image file:

Rare matrix structure

If the image is coloured, each pixel is expressed in three numbers - red, green and blue. In this case we need a three-dimensional array (because each cell can contain only one number). So the color image is expressed by ndarray, which is size (high x width x 3):

Rare matrix structure

Languages

If we deal with the text, the situation is different. The figures for the text indicate the need for a step to construct a glossary (the only word list known to the model) and an embedded step. Let's look at the steps taken to use numbers to express the following text:

The model would need to look at a large number of texts before numeric expressions of the poet. We can make it handle a small data set and use it to build a vocabulary (71,290 words):

Rare matrix structure

This sentence can be divided into a token array (part of a word or word based on general rules):

Then we replace each word with an id in the vocabulary:

These ids still do not provide much information value for models. Therefore, before entering this group of words into the model, we need to replace token/ word with embedded ones (50d word2vec embedded in this example):

Rare matrix structure

You can see the dimensions of the numpy array as

I'm sorry, i'm sorry

I don't know. For performance reasons, an in-depth learning model tends to retain the first dimension of batch size (because model training is faster if there are multiple examples of parallel training). In this case, reshape() becomes very useful. Models like bert expect input in the form of:

Watch the size, security the left, embedding the size

Rare matrix structure

Now it's in the form of a numeric volume, and the model can process and perform the corresponding operation. Other lines, although left blank, are filled with other examples for model training (or projections)。

Like 0
Report
Favorite 0
Tip 0
Comment 0
Share 8
MoreRelated Comments
No comments yet, be the first to comment