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.
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:
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
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
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
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
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
We can use square() with the sum() function can find the NumPy sum of squares in Python.
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
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
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
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
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
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
This is how the dot() function directly calculates the NumPy sum of squares in Python.
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
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
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
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
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
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
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:
I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.