Python's NumPy Advanced app!

Source: Internet
Author: User
Tags instance method

Although NumPy users are rarely interested in the span information of arrays, they are an important factor in building a non-replicated array view. The span can even be negative, which makes the array move back in memory, such as in the slice obj[::-1] or obj[:,::-1].

Advanced Array Operations

In addition to the fancy index, slice, Boolean conditional take subset and other operations, the array has many ways to operate. While the advanced functions in pandas can handle many of the heavy tasks in data analysis, sometimes you still need to write some data algorithms that are not found in the existing libraries.

Operations with reshape to convert one-dimensional arrays to multidimensional arrays are often referred to as flattening (flattening) or diffuse (raveling):

For common connection operations, NumPy provides some more convenient methods, such as Vstack and Hstack. Therefore, the above operation can also be expressed as:

Stacking auxiliary classes: R_ and C_

The NumPy namespace has two special objects, one r_ and c_, that make stacking operations of arrays more concise:

Repeated operations of elements: Title and repeat

The tools that repeat an array to produce larger arrays are mainly the repeat and tile functions. Repeat will repeat the elements in the array for a certain number of times, resulting in a larger array:

The tile feature is a copy of the stacked array along the specified axis. You can visualize it as a "paving tile":

Equivalent functions for fancy indexes: take and put

In the 4th chapter we have said that one way to get and set a subset of arrays is to use a fancy index with an array of integers:

The put does not accept the axis parameter, it only indexes on the flattened version of the array (one-dimensional, C-order) (which should be improved in the future). Therefore, it is a good idea to use a fancy index when you need to set elements with other inline indexes.

Broadcasting

Broadcast (broadcasting) refers to how arithmetic operations are performed between arrays of different shapes. It's a very powerful feature, but it's also easy to misunderstand, even for seasoned veterans. The simplest broadcast occurs when a scalar value is merged with an array:

The principle of broadcasting: If the trailing edge dimension of two arrays (trailing dimension, which is the dimension from the end of the calculation), corresponds to the length of the axis or the length of one of the parties is 1, they are considered broadcast-compatible. Broadcasts are performed on a dimension that is missing and/or length 1.

Ufunc Advanced Applications

While many numpy users will only use the fast element-level operations provided by common functions, there are actually some high-level usages of general functions that allow us to throw out loops and write more concise code.

Ufunc instance method

Each of the NumPy's $ two ufunc has some special methods for performing specific vectorization operations. Reduce accepts an array parameter and aggregates its values through a series of two-tuple operations (which can indicate axial). For example, we can sum each element in an array with np.add.reduce:

Custom Ufunc

There are two tools for you to use your custom functions like Ufunc. Numpy.frompyfunc accepts a python function and two integers representing the number of input and output parameters, respectively. For example, here is a simple function that implements element-level addition:

You are welcome to follow my blog or public number: Https://home.cnblogs.com/u/Python1234/Python Learning Communication

If you need some video material or source code can add group: 125240963

Python's NumPy Advanced app!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.