Methods on NumPy.add() - GeeksforGeeks (2024)

Last Updated : 10 Jun, 2020

Comments

Improve

The ufunc expect a set of scalars as input and produce a set of scalars as output. Universal
functions can be related to mathematical aspects such as, add, subtract, divide, multiply, and likewise.

Methods on numpy.add

Actually, universal functions are not functions instead, these are objects representing functions. Here we are taking the function – add, they have two input parameters and return one output parameter (signature mismatch of ufunc will results in a ValueError. Hence this will work only for binary universal functions).

The four methods on add are:

  • reduce
  • accumulate
  • outer
  • reduceat

In this tutorial we are going through each of the above function in detail.

numpy.ufunc.reduce()

The given input array is reduced by applying the universal function recursively along a specified axis on consecutive elements.
Note: add.reduce() is equivalent to sum()

Syntax: ufunc.reduce(a, axis=0, dtype=None, out=None, keepdims=False, initial=, where=True)

Parameters:
a(array_like): The array upon which processing occurs
axis(None or int or tuple of ints, optional): Axis or axes along which a reduction is performed. The default is (axis = 0). Axis may be negative, whenever it counts backwards.None, a reduction is performed over all the axes.Tuple of ints, a reduction is performed on multiple axes.
dtype(data-type code, optional): The type used to represent the intermediate results.
out(ndarray, None, or tuple of ndarray and None, optional): Location into which the result is stored. If not provided or None, a freshly-allocated array is returned.
keepdims(bool, optional): If this is set to True, the axes which are reduced are left in the result as dimensions with size one.
initial(scalar, optional): The value with which to start the reduction.
where(array_like of bool, optional): A boolean array which is broadcasted to match the dimensions of a, and selects elements to include in the reduction.

Returns:
r : ndarray

Example:

# Reducing an array using np.add.reduce()

import numpy as np

# Array formation

a = np.arange(10)

# Reduction occurs column-wise with

# 'int' datatype

b = np.add.reduce(a, dtype = int, axis = 0)

print("The array {0} gets reduced to {1}".format(a, b))

OUTPUT

The array [0 1 2 3 4 5 6 7 8 9] gets reduced to 45

numpy.ufunc.accumulate()

It stores the intermediate results in an array and returns that. The result, in the case of the add function, is equivalent to calling the c*msum function.

Syntax: ufunc.accumulate(array, axis=0, dtype=None, out=None)

Parameters:
array(array_like): The array to act on.
axis(int, optional): The axis along which to apply the accumulation; default is zero.
dtype(data-type code, optional): The data-type used to represent the intermediate results. Defaults to the data-type of the output array if such is provided, or the data-type of the input array if no output array is provided.
out(ndarray, optional): A location into which the result is stored. If not provided a freshly-allocated array is returned.

Returns:
r : ndarray

Example:

import numpy as np

# Array formation

a = np.arange(10)

# Cumulative sum of array, column wise,

# float datatype

c = np.add.accumulate(a, axis = 0, dtype = float)

print("The array {0} gets added cumulatively to {1}".format(a, c))

OUTPUT

The array [0 1 2 3 4 5 6 7 8 9] gets added cumulatively to [ 0. 1. 3. 6. 10. 15. 21. 28. 36. 45.]

numpy.ufunc.outer()

The ‘outer’ method returns an array that has a rank, which is the sum of the ranks of its two input arrays. The method is applied to all possible pairs of the input array elements.

Syntax: ufunc.outer(A, B, **kwargs)

Parameters:
A(array_like): First array
B(array_like): Second array
kwargs(any): Arguments to pass on to the ufunc.

Returns:
r : ndarray

Example:

# To find the outer of two input arrays

import numpy as np

# Initialization of a 2x2 matrix

# as first input

a = np.arange(4).reshape(2,2)

# Initialization of an array as

# second input

b = np.arange(3)

# Outer of a & b

z = np.add.outer(b, a)

print("The outer of {0} & {1} is {2}".format(b,a,z))

OUTPUT

The outer of [0 1 2] & [[0 1] [2 3]] is [[[0 1] [2 3]] [[1 2] [3 4]] [[2 3] [4 5]]]

numpy.ufunc.reduceat()

The ‘reduceat()‘ method requires as arguments, an input array, and a list of indices. The reduceat() method goes through step-by-step procedures to perform its operation. We will look up its action by four steps.

Example:

# Reduceat method example

import numpy as np

a = np.arange(9)

z = np.add.reduceat(a, [1, 4, 2, 8])

print("Reduceat of matrix {} is {}".format(a,z))

OUTPUT

Reduceat of matrix [0 1 2 3 4 5 6 7 8] is [ 6 4 27 8]

STEP-1
It concerns with indices 1 and 4. This step results in a reduced operation of the array elements between indices 1 and 4.

import numpy as np

a = np.arange(9)

print("Result of STEP-I is", np.add.reduce(a[0:4]))

OUTPUT

Result of STEP-I is 6

STEP-2
The second step concerns indices 4 and 2. Since 2 is less than 4, the array element at index 4 is returned:

import numpy as np

a = np.arange(9)

print("Result of STEP-II is", a[4])

OUTPUT

Result of STEP-II is 4

STEP-3
The third step concerns indices 2 and 8. This step results in a reduce operation of the array elements between indices 2 and 8:

import numpy as np

a = np.arange(9)

print("Result of STEP-III is", np.add.reduce(a[2:8]))

OUTPUT

Result of STEP-III is 27

STEP-4
The fourth step concerns index 8. This step results in a reduce operation of the array elements from index 8 to the end of the array:

import numpy as np

a = np.arange(9)

print("Result of step IV is", np.add.reduce(a[8:]))

OUTPUT

Result of step IV is 8

By going through all this step we get the output of ‘numpy.add.reduceat’.



amalchandranmv

Improve

Next Article

Python | Numpy np.lagadd() method

Please Login to comment...

Methods on NumPy.add() - GeeksforGeeks (2024)

References

Top Articles
The top 12 free movie streaming sites
Unbiased Heart and Soil Supplements Review - My Honest Opinion
Golden Abyss - Chapter 5 - Lunar_Angel
1970 Chevrolet Chevelle SS - Skyway Classics
Needle Nose Peterbilt For Sale Craigslist
Midway Antique Mall Consignor Access
Bbc 5Live Schedule
Things To Do In Atlanta Tomorrow Night
Turning the System On or Off
How Much Is Tj Maxx Starting Pay
Tracking Your Shipments with Maher Terminal
Craigslist Edmond Oklahoma
Directions To 401 East Chestnut Street Louisville Kentucky
Illinois Gun Shows 2022
The Largest Banks - ​​How to Transfer Money With Only Card Number and CVV (2024)
Free Online Games on CrazyGames | Play Now!
Betaalbaar naar The Big Apple: 9 x tips voor New York City
LCS Saturday: Both Phillies and Astros one game from World Series
Wnem Tv5 Obituaries
Living Shard Calamity
Craigslist Wilkes Barre Pa Pets
Discord Nuker Bot Invite
Publix Near 12401 International Drive
King Soopers Cashiers Check
Lincoln Financial Field, section 110, row 4, home of Philadelphia Eagles, Temple Owls, page 1
Math Minor Umn
Ravens 24X7 Forum
60 Second Burger Run Unblocked
Edict Of Force Poe
Acadis Portal Missouri
Boggle BrainBusters: Find 7 States | BOOMER Magazine
Case Funeral Home Obituaries
The disadvantages of patient portals
8 Ball Pool Unblocked Cool Math Games
Dee Dee Blanchard Crime Scene Photos
Sukihana Backshots
Jack In The Box Menu 2022
Who Is Responsible for Writing Obituaries After Death? | Pottstown Funeral Home & Crematory
Nail Salon Open On Monday Near Me
Sofia With An F Mugshot
COVID-19/Coronavirus Assistance Programs | FindHelp.org
How Big Is 776 000 Acres On A Map
Mynord
M&T Bank
Dobratz Hantge Funeral Chapel Obituaries
UNC Charlotte Admission Requirements
Shannon Sharpe Pointing Gif
Superecchll
Chitterlings (Chitlins)
Gainswave Review Forum
Latest Posts
Article information

Author: Kerri Lueilwitz

Last Updated:

Views: 5999

Rating: 4.7 / 5 (67 voted)

Reviews: 82% of readers found this page helpful

Author information

Name: Kerri Lueilwitz

Birthday: 1992-10-31

Address: Suite 878 3699 Chantelle Roads, Colebury, NC 68599

Phone: +6111989609516

Job: Chief Farming Manager

Hobby: Mycology, Stone skipping, Dowsing, Whittling, Taxidermy, Sand art, Roller skating

Introduction: My name is Kerri Lueilwitz, I am a courageous, gentle, quaint, thankful, outstanding, brave, vast person who loves writing and wants to share my knowledge and understanding with you.