Python NumPy Sum + Examples

In this Python NumPy tutorial, we will discuss Python numpy sum with a few examples like below:

  • Python NumPy summary statistics
  • Python NumPy sum of columns
  • Python NumPy sum each row
  • Python NumPy sum function
  • Python NumPy sum ignore nan
  • Python NumPy sum two arrays
  • Python NumPy sum of squares
  • Python NumPy sum 3d array

Python NumPy sum

  • In this section, we will learn about the python numpy sum.
  • Numpy.sum() function is available in the numpy libraries of Python.
  • This function is used to sum all elements, the sum of each row, and the sum of each column of a given array.
  • We can also specify dtype to specify the returned output datatype.

Syntax:

Here is the syntax of numpy.sum()

numpy.sum(
          arr,
          axis,
          dtype,
          out
          )
  1. arr: Input array
  2. axis: axis along which we want to calculate the sum value.
  3. dtype: We can specify the returned output datatype.
  4. out: We want to place the result.

Example

Let’s take an example to check how to sum an elements in an array.

import numpy as np

arr = np.array([[3, 6], [9, 2]])
sum_arr = np.sum(arr)
print(sum_arr)

Here is the Screenshot of following given code.

Python numpy sum
Python numpy sum

This is an example of Sum in Python NumPy.

Read: Python NumPy zeros

Python numpy summary statistics

Summary statistics describe a variable value and its spread. For example, imagine you work for a company that monitors parent’s heath in real-time and you need to build a script that detects dangerous anomalies.

  • You could generate summary statistics in micro batch and then calculate the mean the max and standard deviation of incoming data that is generated from the monitoring device with there summary statistics.
  • You generate automatic alerts when you unusually high or low data points are generated by the device.
  • Numpy has quite a few useful statistical function for calculating
    • Sum
    • Median
    • Mean
    • Max

Example

Let’s take an example to check summary statistics

import numpy as np
import sys

arr = np.array([[3, 6], [9, 2]])
a=np.sum(arr) # sum of elements
print(a)
b=np.mean(arr) # mean of array
print(b)
c=np.min(arr) #minimum number in array
print(c)
d= np.max(arr) # maximum number in array
print(d)
e = np.std(arr) # standard deviation
print(e)

Here is the screenshot of following given code

Python numpy summary statistics
Python numpy summary statistics

Read: Check if NumPy Array is Empty in Python

Python numpy sum of columns

  • In this section, we will learn about the python numpy sum of columns.
  • Use the numpy.sum() function to find the sum of columns of a matrix.
  • This function is used to the sum of each column of a given array.

Syntax

Here is the syntax of numpy.sum()

numpy.sum(
          arr,
          axis,
          out
         )
         
  1. arr: Input array
  2. axis: axis along which we want to calculate the sum value.
  3. out: We want to place the result.

Example

Let’s take an example to check how to sum of columns in an array

import numpy as np
a =np.array([[2,3,4],[4,5,7],[8,9,2]])
new_column = a.sum(axis=0) #column sums
print(new_column)

Here is the Screenshot of following given code

Python numpy sum of columns
Python numpy sum of columns

This is how to do sum of columns in Python NumPy.

Read: Python NumPy to list

Python numpy sum each row

  • In this section, we will learn about the python numpy sum of rows.
  • Use the numpy.sum() function to find the sum of rows of a matrix.
  • This function is used to the sum of each row of a given array.

Syntax:

Here is the syntax of numpy.sum()

numpy.sum(
          arr,
          axis,
          out
         )
         
  1. arr: Input array
  2. axis: axis along which we want to calculate the sum value.
  3. out: We want to place the result.

Example

Let’s take an example to check how to sum of rows in an array

import numpy as np
a =np.array([[4,6,7],[7,5,7],[8,9,2]])
new_row = a.sum(axis=1) #rows sums
print(new_row)

Here is the Screenshot of following given code

Python numpy sum of rows
Python numpy sum of rows

This is how to do sum each row in Python NumPy.

Read: Python NumPy Random + Examples

Python numpy sum function

  • In this section, we will learn about the python numpy sum.
  • Numpy.sum() function is available in the numpy libraries of Python.
  • This function is used to sum all elements, the sum of each row, and the sum of each column of a given array.
  • We can also specify dtype to specify the returned output datatype.

Syntax:

Here is the syntax of numpy.sum()

numpy.sum(
          arr,
          axis,
          dtype,
          out
          )
  1. arr: Input array
  2. axis: axis along which we want to calculate the sum value.
  3. dtype: We can specify the returned output datatype.
  4. out: We want to place the result.

Example

Let’s take an example to check how to sum an elements in an array.

import numpy as np

arr = np.array([[2, 3], [6, 7]])
sum_arr = np.sum(arr)
print(sum_arr)

Here is the Screenshot of following given code

Python numpy sum function
Python numpy sum function

This is how to use Python NumPy Sum() function.

Python numpy sum ignore nan

  • In this section, we will learn about the python numpy sum ignore nan.
  • nansum() function can be used to calculate the sum of the array ignoring the nan value.
  • This function is used to sum all elements, the sum of each row, and the sum of each column of a given array.

Syntax:

Here is the syntax of sum ignore nan

numpy.nansum(
             arr,
             dtype=None,
             )

Example

import numpy as np

a = np.nansum([1, np.nan])
a = np.array([[2, 3], [4, np.nan]])
b = np.nansum(a)
print(b)

Here is the Screenshot of following given code

Python numpy sum ignore nan
Python numpy sum ignore nan

Read: Python NumPy Array

Python numpy sum two arrays

  • In this section, we will learn about the python numpy sum of two arrays.
  • Numpy.sum() function is available in the NumPy libraries of Python.
  • This function is used to sum all elements, the sum of each row, and the sum of each column of a given array.
  • We can also specify dtype to specify the returned output datatype.

Syntax:

Here is the syntax of numpy.sum()

numpy.sum(
          arr,
          axis,
          dtype,
          out
          )

Example

Let’s take an example to check how to sum an elements in an array.

import numpy as np

arr = np.array([[2, 3], [4, 5]])
sum_arr = np.sum(arr)
print(sum_arr)

Here is the Screenshot of following given code

Python numpy sum two arrays
Python numpy sum two arrays

This is how to sum two arrays using Python NumPy.

Python numpy sum of squares

  • In this section, we will learn about the python numpy sum of squares.
  • Numpy. square() function helps the user to calculate the square value of each element in the array.
  • This function is used to sum all elements, the sum of each row, and the sum of each column of a given array.

Syntax:

Here is the syntax of numpy.square()

numpy.square(
          arr,
          axis,
          dtype,
          out
          )

Example

Let’s take an example to check sum of square

import numpy as np

arr = np.array([[2, 3], [4, 5]])
squa_arr = np.square(arr)
print(squa_arr)

Here is the Screenshot of following given code

Python numpy sum of squares
Python numpy sum of squares

Python numpy sum 3d array

  • In this section, we will learn about the python NumPy sum 3d array.
  • Numpy. sum() function is available in the NumPy libraries of Python.
  • This function is used to sum all elements, the sum of each row, and the sum of each column of a given array.
  • We can also specify dtype to specify the returned output datatype.

Syntax:

Here is the syntax of numpy.sum()

numpy.sum(
          arr,
          axis,
          dtype,
          out
          )
  1. arr: Input array
  2. axis: axis along which we want to calculate the sum value.
  3. dtype: We can specify the returned output datatype.
  4. out: We want to place the result.

Example

Let’s take an example to check how to sum an elements in an 3d array.

import numpy as np

a = np.arange(0,27,1) #3 matrix layer 3 rows and columns
a = a.reshape(3,3,3)
b = a.sum(axis=1)
print(b)
 

Here is the screenshot of following given code

Python numpy sum 3d array
Python numpy sum 3d array

This is how to sum 3d array using Python NumPy.

You may like the following Python tutorials:

In this Python tutorial, we will discuss Python numpy sum and also cover the below examples:

  • Python numpy summary statistics
  • Python numpy sum of columns
  • Python numpy sum each row
  • Python numpy sum function
  • Python numpy sum ignore nan
  • Python numpy sum two arrays
  • Python numpy sum of squares
  • Python numpy sum 3d array