Numpy Sum And Prod

Description

Similar to Python lists, NumPy has sum() and prod commands for ndarray objects.

Sage Cell

Code

import numpy as np
a = np.array([[1, 2, 3], [4, 5, 6]])
print a.sum()
print a.prod()

Options

Operating on Axes

By setting the axis parameter, we can operate on specific parts of the array. For a 2-dimensional array, setting axis to 0 adds or multiplies columns, and setting axis to 1 adds or multiplies rows.

Code

import numpy as np
a = np.array([[1, 2, 3], [4, 5, 6]])
print a.sum(axis=0)
print a.sum(axis=1)

Tags

Primary Tags:

Secondary Tags:

A list of possible tags can be found at The WeBWorK Open Problem Library. For linear algebra tags see the Curated Courses Project.

Related Cells

Any related cells go here. Provide a link to the page containing the information about the cell.

Attribute

Permalink:

Author:

Date: 06 Apr 2019 18:27

Submitted by: Zane Corbiere

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License