Updated April 19, 2023
Introduction to NumPy square
The following article provides an outline for NumPy square. This function is also from the NumPy library which is used for the computation on the array and single elements. As the name suggests this function is used when we want to calculate the square of the array elements or a single element value can also be calculated. This function will return the new array containing the square value of array element it does not modify the original array, it will be an unchanged array only.
Syntax:
By the use of the square function, we can calculate square of Float, Integer, and complex number type in python. This function takes array as the input parameter. In order to use this we need to have NumPy package as the import in our program.
This is the syntax as per the python doc:
numpy.square(x[, out])
This above function takes array as the input and returns a new array without being modified the original array.
How square Function work in NumPy?
As now we know that the NumPy square function is used to calculate the square of the value and NumPy is used to calculate the numeric value of array element in python, this is the library that makes the computation easy and clean. This function works in the same way like we have learnt in school.
This function takes 4 argument:
- arr (first param): This parameter is array that contains various elements that we want to calculate. This is required parameter.
- dType (second param): This parameter is optional and this parameter specifies the type of array we want to return.
- out (third param): This parameter is also optional. This parameter is used when we want to place our resultant output into an array. In short it represent an resultant array with result.
- where (fourth Parma as condition on elements): This parameter specifies that which element needs to be taken into consideration while calculating the square of the array elements.
Return Type (array as the return type):
- This function return an array which is an Newley formed array only. Square function does not modify the original array.
In order to use this function we have to have the NumPy package should be imported in our program otherwise we will not be able to use this function.
Start using the function by following the below steps:
1. Import the package and specify the alias name for more convenient way to access and call the computation functions.
Example:
import NumPy as name
2. After this step we can directly call any function which is available in the NumPy library. For now we will look how to use square function.
Example:
myNum.square(array)
In the above line of code we are using the alias name to call the square function and inside we are passing the array which need to be calculate.
Now we will see one example and its working:
Example:
import numpy as myNum
arr = [2, 5, 6, 8, 9]
result = myNum.square(arr)
print (result)
In the first line you can see the import statement with name ‘myNum’. After that we have declared one array with various elements. In the next line we are calling the square function and try to passing the array inside it. Then this function will return us the new array which is going to be hold the ‘result’ variable and on the next line we are just printing our result. We can also calculate the square of the single element by passing the single variable inside the square function.
np.sqaure(20)
Like this it will calculate the square of the single element.
Points to remember while working with square function in NumPy:
- This function takes 4 parameters out of which three are optional.
- This will always return as new array and the source array will not be changed anyway round.
- Square function can take single param also and part of NumPy library.
Examples of NumPy square
Given below are the examples of NumPy square:
Example #1
In this example we are trying to get the square of the single element using square function.
Code:
import numpy as myNum
val1 = 20
print("value is :", val1)
#calling numpy function here.
result = myNum.square(val1)
print("result is ::")
print (result)
Output:
Example #2
In this example we are calculating the square of integer array elements by using the square function.
Code:
import numpy as myNum
#defining the array
arr = [1, 2, 3,3, 4, 5, 6, 7, 8]
print("array value is :", arr)
#calling numpy function here.
result = myNum.square(arr)
print("result is ::")
print (result)
Output:
Example #3
In this example we are calculating the square of the float array elements by using the square function.
Code:
import numpy as myNum
#defining the array
arr = [2.4, 3.5, 1.5, 3.2, 9.1]
print("array value is :", arr)
#calling numpy function here.
result = myNum.square(arr)
print("result is ::")
print (result)
Output:
Example #4
In this example we are trying to calculate the square of the complex number type. Which is an array nu using the square function.
Code:
import numpy as myNum
#defining the array
arr = [-2, -3, -4, -5]
print("array value is :", arr)
#calling numpy function here.
result = myNum.square(arr)
print("result is ::")
print (result)
Output:
Example #5
In this example we are calculating square of multiple array with different values by using square function from NumPy library.
Code:
import numpy as myNum
#defining the array
#first array
arr1 = [-2, -3, -4, -5]
#second array
arr2 = [-6, -7, -8 , -9]
print("array value is :", arr1)
print("array value is :", arr2)
#calling numpy function here.
result1 = myNum.square(arr1)
result2 = myNum.square(arr2)
print("result is ::")
print (result1)
print (result2)
Output:
Conclusion
By using the square function, we can calculate the square of array elements without being iterating the elements this also reduced the line of code and optimized it. This function is also available inside the NumPy library. We can also specify the optional parameter of this function if needed.
Recommended Articles
This is a guide to NumPy square. Here we discuss the introduction, how square function work in NumPy? and examples. You may also have a look at the following articles to learn more –