Numpy Square Root post featured image
Numpy Square Root post featured image

Numpy Square Root : Calculate the Square Root of a Numpy Array

Numpy has many useful functions that allows you to do array manipulation. The function numpy.sqrt is one of them. In this entire tutorial I will show you how to calculate Numpy array square root for the both one dimension and Multi-Dimension.

 

What Numpy Square Root Do and its Synatx ?

The numpy.sqrt() method calculate the square root of a each element of a Numpy Array. It returns array of the square root for each element. Below is  the syntax for it.

numpy.sqrt(x, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True[, signature, extobj])

The most general used parameters are :

x : Your input array.

out: A location into which the result is stored. It should be samed as the shape of the input array.

You can learn more about the other parameters on Numpy Square Root Offical Documentation.

Calculate the Square Root of a 1D Numpy Array

In this section You will know how to do numpy square root opeartion on 1D array. To do lets create a Single Dimensional array and apply numpy.sqrt() method over it.

array_1d = np.array([1,2,3,4,9,12])

When you apply the square root function, you will get the output as follow.

np.sqrt(array_1d)

Output

Square root of 1D Array
Square root of 1D Array

 

Find the Square Root of a 2-D Array

The same method can be used to calculate the Square Root of a  2D Numpy Array. Lets create two-dimensional array and find root of each element.

2D Numpy array

array_2d = np.array([[1,2,3,4],[5,6,7,8],[9,10,11,12]])

Apply the numpy.sqrt() method to the above created array.

array_2d = np.array([[1,2,3,4],[5,6,7,8],[9,10,11,12]])

You will get the following output.

Square root of 2D Array
Square root of 2D Array

Find the Square Root and Output the results to another Array

In the above examples the square root results of array was on the memory. But you can store it into another array also. To do so you have to pass extra argument inside the np.sqrt() method. And that is out parameter.

I will use the same two-dimensioanl array I, have created above. But you have to first create a empty or zero array of the same dimension as the input aaray. To do so run the below code.

out = np.zeros((array_2d.shape[0],array_2d.shape[1]))

Here array_2d.shape[0] is the row size and array_2d.shape[1] is the number of columns.

Output

Zero array of 2D Numpy Array of Size 3x4
Zero array of 2D Numpy Array of Size 3×4

Execute the following code and you will get the square root results in another array.

np.sqrt(array_2d,out=out)

Output

Getting Square Root results on another array
Getting Square Root results on another array

These are the examples for implementing the numpy.sqrt() method. Thats all for now.

I hope you have fully understand this article. You can also contact us for more information.

Thanks 

Data Science Learner Team

 

Join our list

Subscribe to our mailing list and get interesting stuff and updates to your email inbox.

Thank you for signup. A Confirmation Email has been sent to your Email Address.

Something went wrong.

 
Thank you For sharing.We appreciate your support. Don't Forget to LIKE and FOLLOW our SITE to keep UPDATED with Data Science Learner