Source: Internet
Author: User

NumPy's universal functions requires the input array shape to be consistent, and when the shape of the array is unequal, the broadcast mechanism is used, and the array is adjusted so that shape satisfies the rule, then it can be calculated, otherwise it will be wrong.

The four rules are as follows:
• Let all input arrays match the longest array in the shape, and the insufficient part of shape is padded with 1 in front
• The shape of the output array is the maximum value on each axis of the input array shape
• If an axis of an input array has the same length as the corresponding axis of an output array, or if its length is 1 o'clock, the array can be used to calculate, otherwise an error
• When an axis of an input array has a length of 1 o'clock, the first set of values on this axis are used along this axis

The following examples illustrate these issues

In general, NumPy are calculated using a one by one corresponding method (Element-by-element)

Example 1:

``from numpy import array a = array([1.0,2.0,3.0])b = array([2.0,2.0,2.0])a * barray([ 2.,  4.,  6.])当不相等时，则会采用规则对齐from numpy import arraya = array([1.0,2.0,3.0])b = 2.0a * barray([ 2.,  4.,  6.])``

A.shape Get is (3,) b is a floating-point number, if converted to an array, then B.shape is a (), a 1-axis alignment, the completion of 1,a.shape (3,1), b alignment, then the alignment is (3,1), and then calculated according to the one by one corresponding way

Perhaps the above example is not too clear, the following is a more precise example of the explanation:

``import numpy as npa = np.arange(0, 6).reshape(6, 1)a #array([[ 0], [1], [2], [3], [4], [5]])a.shape #(6, 1)b = np.arange(0, 5)b.shape #(5,)c = a + bprint (c)[[0 1 2 3 4] [1 2 3 4 5] [2 3 4 5 6] [3 4 5 6 7] [4 5 6 7 8] [5 6 7 8 9]]``

Related Keywords:

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.
{"found_math": false, "script_math_tex": 0, "script_math_asciimath": 0, "math_annotations": 0, "math_alttext": 0, "mathml": 0, "mathjax_tag": 0, "mathjax_inline_tex": 0, "mathjax_display_tex": 0, "mathjax_asciimath": 0, "img_math": 0, "codecogs_latex": 0, "wp_latex": 0, "mimetex.cgi": 0, "/images/math/codecogs": 0, "mathtex.cgi": 0, "katex": 0, "math-container": 0, "wp-katex-eq": 0, "align": 0, "equation": 0, "x-ck12": 0, "texerror": 0, "url": "https://topic.alibabacloud.com/a/broadcast-in-numpy-broadcasting_8_8_31057423.html", "fetch_time": 1725734912000000000, "content_mime_type": "text/html", "warc_filename": "crawl-data/CC-MAIN-2024-38/segments/1725700650898.24/warc/CC-MAIN-20240907162417-20240907192417-00172.warc.gz", "warc_record_offset": 576481856, "warc_record_length": 16025, "token_count": 749, "char_count": 2737, "score": 3.578125, "int_score": 4, "crawl": "CC-MAIN-2024-38", "snapshot_type": "latest", "language": "en", "language_score": 0.8280693292617798, "file_path": "/sailhome/yjruan/project/latent-lm/data/processed/finemath-4plus/finemath-4plus/train-00028-of-00064.parquet"}