Python NumPy square() Function

Python numpy.square() function is used to return the element-wise square values of the input array. In other words, this mathematical function helps to user calculate the square values of every element in the given array. The NumPy square() function takes four parameters arr, out, where, and dtype, and returns a new array with an argument value as the square of the source array elements.

In this article, I will explain how to use Numpy square() function on single and multi-dimension arrays.

1. Quick Examples of Python NumPy square() Function

If you are in a hurry, below are some quick examples of how to use Python NumPy square() function.


# Below are the quick examples

# Example 1: Use numpy.square() function to get single element
arr = 25
arr2 = np.square(arr)

# Example 2: Use numpy.square() function
arr = [2, 3, 5, 8, 9, 4]
arr2 = np.square(arr)

# Example 3: Use numpy.square() function to floating-point array
arr = [2.3, 3.5, 5.1, 8.7, 9.2, 4.8]
arr2 = np.square(arr)

# Example 4: Use numpy.square() function with negative numbers
arr = [-6, -13, -25, -8, -15, -17]
arr2 = np.square(arr)

# Example 5: Use numpy.square() function to square from multiple array
arr = np.array([[5, 8, 3, 7], [9, 4, 2, 6]])
arr2 = np.square(arr)

# Example 6: Use numpy.square() function with complex numbers
arr = [2+3j, -4-5j, 3-4j, -3+4j]
arr2 = np.square(arr)

# Example 7: Use complex numbers
arr = 6 + 5j
arr2 = np.square(arr)

# Example 8: Use numpy.square() function to graphical representation  
arr = np.linspace(start = -12, stop = 15,num = 6, endpoint = True)
arr2 = np.square(arr)                 
plt.plot(arr, np.square(arr)) 
plt.plot(arr, arr, color = 'yellow')
plt.show()

# Example 9: Create an empty array
arr = np.array([])
# Use numpy.square() to get the empty array
arr2 = np.square(arr)

2. Syntax of NumPy square()

Following is the syntax of the numpy.square() function.


# Syntax of numpy.square() 
numpy.square(arr, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None)

2.1 Parameters of square()

  • arr – Input data.
  • out – It is ndarray, None, or tuple of ndarray and None, optional. Out will be the location where the result is to be stored. If provided, it must have a shape that the inputs broadcast to. If not provided or None, a freshly-allocated array is returned.
  • where – This condition is broadcast over the input.
  • dtype – It is an optional parameter that specifies the type of the returned array.

2.2 Return Value of square()

Return : It returns an array with square() value of each input array

3. Usage of NumPy square() Function

This mathematical Python NumPy in-built function square() takes the input array as param and returns a the square value of each element in the input array. Moreover, you can get the square root values of an array using numpy.sqrt().

3.1 Get the Square Value of Single Element

Use numpy.square() function to get the square value of a single element of an input NumPy array.


import numpy as np
arr = np.array([25])
# Use numpy.square() to get the square value
arr2 = np.square(arr)
print(arr2)

# Output :
# [625]

3.2 Get the Square Values of Multiple Elements of 1-D Array

To calculate the square values of integer array elements by using the numpy.square() function. For example,


# Create an 1D input array
arr = [2, 3, 5, 8, 9, 4]

# Use numpy.square() function to get square values
arr2 = np.square(arr)
print(arr2)

# Output:
# [ 4  9 25 64 81 16]

4. Get the Square Values of Float Elements of 1-D NumPy Array

You can calculate the square of the float elements of an array by using the numpy.square() function. Let’s take an example,


# creating an 1D input array
arr = [2.3, 3.5, 5.1, 8.7, 9.2, 4.8]

# Use numpy.square() function io get 
# the square values of floating point
arr2 = np.square(arr)
print(arr2)

# Output:
# [ 5.29 12.25 26.01 75.69 84.64 23.04]

5. Get the Square Values of Negative Elements

You can also calculate the square values of the negative numbers using the numpy.square() function.


import numpy as np

# creating an 1D input array
arr = [-6, -13, -25, -8, -15, -17]

# Use numpy.square() function with negative numbers
arr2 = np.square(arr)
print(arr2)

# Output:
# [ 36 169 625  64 225 289]

6. Get the Square Values of 2-D NumPy Array Elements

If you want to get square values of 2- Dimensional array elements you will use square() function of NumPy array. It will give the square values of 2-D array elements. Let’s create a 2-D NumPy array using numpy.array().


import numpy as np

# creating an 2D input array
arr = np.array([[5, 8, 3, 7], [9, 4, 2, 6]])

# Use numpy.square() get the square values 
# of 2-D array elements
arr2 = np.square(arr)
print(arr2)

# Output :
# [[25 64  9 49]
# [81 16  4 36]]

7. Get the Square Values of Complex Numbers

We can also get the square value of complex elements of an array. For example, before snippet uses the complex type.


# Create an array with complex values
arr = [2+3j, -4-5j, 3-4j, -3+4j]

# Use numpy.square() function with complex numbers
arr2 = np.square(arr)
print(arr2)

# Output :
# [-5.+12.j -9.+40.j -7.-24.j -7.-24.j]

8. Get the Square Values on Graphical Representation

We can also use NumPy square() function for graphical representation using MatLab library.


import numpy as np
import matplotlib.pyplot as plt
 
# Use numpy.square() function to graphical representation  
arr = np.linspace(start = -12, stop = 15,
                num = 6, endpoint = True)

arr2 = np.square(arr)                 
print(arr2)

plt.plot(arr, np.square(arr)) 
plt.plot(arr, arr, color = 'yellow')
plt.show()

# Output:
# [144.    43.56   1.44  17.64  92.16 225.  ]
python numpy square

You can see the Parabolic graph of the square() method in Numpy.

9. Get The Empty Array

When we pass an empty array to the numpy.square() function, it will return the empty array.


# Create an empty array
arr = np.array([])
# Use numpy.square() to get the empty array
arr2 = np.square(arr)
print(arr2)

# Output : []

10. Conclusion

In this article, I have explained how to use Python NumPy.square() function using how to calculate the square value of every element in the given array with examples.

Happy Learning!!

Related Articles

References

Leave a Reply

You are currently viewing Python NumPy square() Function