Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

See how to master the basic knowledge points of n-dimensional array in graphic NumPy.

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article introduces the relevant knowledge of "how to master the basic knowledge points of n-dimensional array in graphic NumPy". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

NumPy is one of the most important extension libraries of Python, and it is also a necessary tool for getting started with machine learning programming. However, for beginners, a large number of operations of NumPy are very difficult to remember.

Recently, a foreign programmer said that the basic operations of NumPy were written down graphically, making the learning process easy and interesting. It received 500 likes in less than half a day after it was released in the Reddit machine learning community.

Let's follow his tutorials to study together.

The tutorial is divided into three parts: vector (one-dimensional array), matrix (two-dimensional array), three-dimensional and higher-dimensional array.

Numpy array and Python list

Before introducing the formal content, let's take a look at the difference between Numpy arrays and Python lists.

At first glance, the NumPy array is similar to the Python list. They can all be used as containers, with the ability to getting and setting elements as well as insert and remove elements.

There are many similarities between the two. Here is an example of the operation of the two:

Compared with the Python list, the Numpy array has the following characteristics:

More compact, especially in more than one-dimensional dimensions; vectorization operations are faster than Python lists, but adding elements at the end is slower than Python lists.

When △ adds elements at the end, the complexity of Python list is O (1) and the complexity of NumPy is O (N).

Vector operation vector initialization

One way to create an NumPy array is to convert directly from the Python list, where the array element is of the same type as the list element.

The NumPy array cannot be lengthened like the Python list because there is no space reserved at the end of the array.

Therefore, it is common to define a Python list, manipulate it, and then convert it to an NumPy array, or initialize the array with np.zeros and np.empty, pre-allocating the necessary space:

Sometimes we need to create an empty array of the same size and element type as the existing array:

In fact, all functions that populate the created array with constants have a _ like counterpart to create the same type of constant array:

In NumPy, you can initialize a monotone sequence array with arange or linspace:

If you need something like [0.1,2.] You can change the type of arange output: arange (3) .astype (float).

But there is a better way: the arange function is sensitive to data types, generating an array of integers if you take an integer as a parameter, or a floating-point array if you enter a floating-point number (such as arange (3.)).

But arange is not particularly good at dealing with floating-point numbers:

This is because 0.1 is a limited decimal number for us, but not for computers. In binary, 0.1 is an infinitely small number that must be truncated somewhere.

This is why adding the decimal part to step arange is usually not a good approach: we may encounter a bug that results in an array with a different number of elements than we want, which reduces the readability and maintainability of the code.

At this point, linspace will come in handy. It is not affected by rounding errors and always generates the required number of elements.

For testing purposes, you usually need to generate random arrays. NumPy provides several forms of random numbers, such as random integers, uniform distribution, normal distribution, and so on:

Vector index

Once the data is stored in an array, NumPy provides an easy way to fetch it:

The above shows a variety of indexes, such as fetching a specific interval, indexing from right to left, fetching only odd digits, and so on.

But they are all called view, that is, they do not store raw data. And if the original array changes after it is indexed, it does not reflect the change of the original array.

These indexing methods allow allocation to modify the contents of the original array, so it is important to note that only the following last method is to copy the array, and the original data may be corrupted by other methods:

Another super-useful way to get data from NumPy arrays is the Boolean index, which allows you to use a variety of logical operators to retrieve eligible elements:

Note: ternary comparison 3 in Python

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report