NumPy Sum of Squares in Python [6 Methods]

In this Python NumPy tutorial, I will explain how to find the NumPy sum of squares in Python using different methods with some illustrative examples.

To find the sum of squares in NumPy Python, we can use a for loop to go through each element and find the square and then add them, square() with + operator or sum() function, power() function, einsum() function, linalg() function or we can find the dot product of it using dot() function.

NumPy sum of squares in Python Methods

Let’s list all the methods present in Python to find the NumPy sum of squares in Python.

  • Using nested for loop
  • Using square()
  • Using power()
  • Using dot()
  • Using einsum()
  • Using linalg()

Let’s see them one by one using some illustrative examples:

Method 1: NumPy squared sum of array in Python using nested for loops

  • This method uses nested for loops to iterate through the elements of the NumPy array in Python.
  • It calculates the square of each element and adds it to the Python variable.
  • This process continues until all elements of the NumPy array in Python are processed.
READ:  Python Django concatenate string

Case 1: NumPy squared sum of the 1D array in Python using nested for loops

A for loop is initiated to iterate over each number stored in the 1D array. And, each num from the array is squared (num**2). Then, the squared number is added using the += operator.

import numpy as np

data_1d = np.array([1, 2, 3, 4, 5])
sum_of_squares = 0
for num in data_1d:
    sum_of_squares += num**2
print("Sum of squares:", sum_of_squares)

Output:

Sum of squares: 55

The screenshot of the output is:

numpy sum of squares in Python

Case 2: NumPy sum of squares for 2D array in Python

import numpy as np
data_2d = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
sum_of_squares = 0
for row in data_2d:
    for num in row:
        sum_of_squares += num**2
print("Sum of squares:", sum_of_squares)

Output: The implementation of the above code is given below.

Sum of squares: 285
numpy sum Python

Case 3: Python NumPy sum of squares for 3D array

import numpy as np
data_3d = np.array([[[1, 2, 3], [4, 5, 6], [7, 8, 9]],
                   [[10, 11, 12], [13, 14, 15], [16, 17, 18]],
                   [[19, 20, 21], [22, 23, 24], [25, 26, 27]]])
sum_of_squares = 0
for matrix in data_3d:
    for row in matrix:
        for num in row:
            sum_of_squares += num**2
print("Sum of squares:", sum_of_squares)

Output: The implementation of the above code is given below in Pycharm editor:

Sum of squares: 6930
Python numpy sum 3d array

This way we can use nested for loops with the ** operator and += operator for the NumPy sum of squares in Python.

Method 2: NumPy squared sum in Python using square() and sum()

  • In this method, we first use np.square() to square all elements in the NumPy array, creating a new array with squared values in Python.
  • Then, we use np.sum() to compute the sum of all the squared values in the new array in the Python NumPy library.
  • The result is stored in the variable.

Case 1: NumPy square sum for 1D array in Python

import numpy as np
data_1d = np.array([1, 2, 3, 4, 5])
sum_of_squares = np.sum(np.square(data_1d))
print("Sum of squares:", sum_of_squares)

Output: The implementation of the code for NumPy sum of squares in Python for 1D array.

Sum of squares: 55
numpy square sum in Python for 1d array

Case 2: NumPy sum of squares in Python for 2D array

import numpy as np
data_2d = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
sum_of_squares = np.sum(np.square(data_2d))
print("Sum of squares:", sum_of_squares)

Output: The implementation of the code with screenshot:

Sum of squares: 285
numpy sum square in Python for 2D array

Case 3: Python NumPy sum of squares for 3D array

import numpy as np
data_3d = np.array([[[1, 2, 3], [4, 5, 6], [7, 8, 9]],
                   [[10, 11, 12], [13, 14, 15], [16, 17, 18]],
                   [[19, 20, 21], [22, 23, 24], [25, 26, 27]]])
sum_of_squares = np.sum(np.square(data_3d))
print("Sum of squares:", sum_of_squares)

Output: The implementation with the screenshot of the code:

Sum of squares: 6930
Python numpy square sum

We can use square() with the sum() function can find the NumPy sum of squares in Python.

READ:  PyTorch fully connected layer

Method 3: Pyhton np square sum using power() with sum() function

  • Here, we will use the numpy.power() function to raise each element of the array in Python to the power of 2, effectively squaring each number.
  • This operation transforms the original array into a new NumPy array in Python where each element is the square of its original value.
  • Next, we take this new NumPy array of squared values and calculate its sum using the numpy.sum() function that adds up all the values in the array, giving the total sum of squares of the original array in Python.

Case 1: 1D array in NumPy sum of squares in Python

import numpy as np

array = np.array([1, 2, 3, 4])
sum_of_squares = np.sum(np.power(array, 2))
print("Sum of Squares:", sum_of_squares)

Output: The output of the Python code is:

Sum of Squares: 30
Python find sum of squares in 1d numy array

Case 2: 2D array in NumPy sum of squares in Python

import numpy as np
array_2d = np.array([[1, 2, 3], [4, 5, 6]])
sum_of_squares_2d = np.sum(np.power(array_2d, 2))
print("Sum of Squares of 2D Array:", sum_of_squares_2d)

Output: The implementation of the above code in Python is:

Sum of Squares of 2D Array: 91
Python sum of squares numpy

Case 3: 3D array in NumPy sum of squares in Python

import numpy as np
array_3d = np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]]])
sum_of_squares = np.sum(np.power(array_3d, 2))
print("Sum of Squares:", sum_of_squares)

Output: The implementation of Python code with a proper screenshot is:

Sum of Squares: 204
sum of array in python with square of the numbers

The power() with sum() function in the NumPy library to find the sum of squares in Python.

Method 4: Sum of squares NumPy in Python using dot()

  • Here, we flatten the array in NumPy Python using array.flatten() to create an array in Python.
  • Then, we use np.dot() to calculate the dot product of the flattened array in Python with itself.
  • The dot product essentially computes the sum of squares of the flattened array’s elements in Python.

Case 1: Sum of squares Python NumPy for 1D array

import numpy as np

data_1d = np.array([1, 2, 3, 4, 5])
sum_of_squares = np.dot(data_1d, data_1d)
print("Sum of squares:", sum_of_squares)

Output: The above code output is:

Sum of squares: 55
sum of squares numpy Python for 1d array

Case 2: Sum of squares Python NumPy for 2D array

import numpy as np
data_2d = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
sum_of_squares = np.dot(data_2d.flatten(), data_2d.flatten())
print("Sum of squares:", sum_of_squares)

Output: The implementation of the code is:

Sum of squares: 285
np sum of squares in Python 2d array

Case 3: Sum of squares Python NumPy for 3D array

import numpy as np
data_3d = np.array([[[1, 2, 3], [4, 5, 6], [7, 8, 9]],
                   [[10, 11, 12], [13, 14, 15], [16, 17, 18]],
                   [[19, 20, 21], [22, 23, 24], [25, 26, 27]]])
sum_of_squares = np.dot(data_3d.flatten(), data_3d.flatten())
print("Sum of squares:", sum_of_squares)

Output: The code output with a screenshot is:

Sum of squares: 6930
np square sum Python

This is how the dot() function directly calculates the NumPy sum of squares in Python.

READ:  Matplotlib scatter plot legend

Method 5: The np sum of squares in Python with einsum() function

  • The np.einsum() is a powerful function that can perform various array operations in Python.
  • In this method, we use np.einsum() with the notation ‘ijk,ijk‘ to calculate the sum of squares in NumPy Python.
  • The ‘ijk‘ notation specifies that we multiply corresponding elements in the NumPy array and then sum them up through Python.

Case 1: The NumPy sum squares in Python for 1D array.

import numpy as np

data_1d = np.array([1, 2, 3, 4, 5])
sum_of_squares = np.einsum('i,i', data_1d, data_1d)
print("Sum of squares:", sum_of_squares)

Output: The implementation of the Python code is:

Sum of squares: 55
sum of squares python numpy for 1d array

Case 2: The NumPy sum squares in Python for 2D array.

import numpy as np
data_2d = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
sum_of_squares = np.einsum('ij,ij', data_2d, data_2d)
print("Sum of squares:", sum_of_squares)

Output: The output of the above code is:

Sum of squares: 285
Python numpy sum squares

Case 3: The NumPy sum squares in Python for 3D array.

import numpy as np
data_3d = np.array([[[1, 2, 3], [4, 5, 6], [7, 8, 9]],
                   [[10, 11, 12], [13, 14, 15], [16, 17, 18]],
                   [[19, 20, 21], [22, 23, 24], [25, 26, 27]]])
sum_of_squares = np.einsum('ijk,ijk', data_3d, data_3d)
print("Sum of squares:", sum_of_squares)

Output: The implementation of the code in the Python Pycharm editor is:

Sum of squares: 6930
sum of squares numpy in Python

The einsum() function calculates the NumPy sum of squares in Python.

Method 6: Square Python NumPy array sum using the linalg() function

  • We first flatten the NumPy array using array.flatten() to create an array in Python.
  • Then, we use np.linalg.norm() in the NumPy library to calculate the L2 norm (Euclidean norm) of the flattened array in Python.
  • Squaring the L2 norm gives us the sum of squares of the flattened array’s elements.

Case 1: NumPy sum squares for 1D array in Python

import numpy as np

data_1d = np.array([1, 2, 3, 4, 5])
sum_of_squares = np.linalg.norm(data_1d)**2
print("Sum of squares:", sum_of_squares)

Output: The result of the Python code is:

Sum of squares: 55.0
numpy sum squares in Python for 1d array

Case 2: NumPy sum 2D array numbers square in Python.

import numpy as np
data_2d = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
sum_of_squares = np.linalg.norm(data_2d.flatten())**2
print("Sum of squares:", sum_of_squares)

Output: The output of the code in Python is:

Sum of squares: 285.00000000000006
Python numpy sum of squares

Case 3: NumPy sum of squares in 3D Python array

import numpy as np
data_3d = np.array([[[1, 2, 3], [4, 5, 6], [7, 8, 9]],
                   [[10, 11, 12], [13, 14, 15], [16, 17, 18]],
                   [[19, 20, 21], [22, 23, 24], [25, 26, 27]]])
sum_of_squares = np.linalg.norm(data_3d.flatten())**2
print("Sum of squares:", sum_of_squares)

Output: This is the implementation of the Python code with a screenshot:

Sum of squares: 6929.999999999999
numpy sum squares Python

The linalg.norm() function calculates the NumPy sum of squares in Python.

Conclusion

Understanding how to calculate the NumPy sum of squares in Python of different dimensional arrays using six methods such as using for loop, square() function with sum() function, power() with sum() function, using dot() function, einsum() function, and linalg.norm() function. I have explained each of the methods in detail with multiple examples.

Now the choice of method depends on the requirement of the problem in Python.

You may also like to read: