# Numpy.sqrt () in Python

| | |

This tutorial will cover the square root function NumPy, also known as numpy.sqrt or np.sqrt.
This is a fairly easy NumPy function to understand and use, but in order to help real beginners, this tutorial will break everything down.

``````import numpy as np

np.sqrt(4)

# Expected result
# 2.0

np.sqrt([4, 16, 64])

# Expected result
# array([2., 4., 8.])``````

If you are a real beginner or have some time, I recommend that you check out the entire tutorial. This way, everything will make more sense.
But if you are in a hurry, click one of the links in the Contents list and it will take you directly to the appropriate section of the tutorial.
Ok… before we get into Numpy’s square root function itself, let’s start with a quick overview of NumPy.

We’ll go through NumPy quickly, walk you through the syntax, and show you some examples.
That being said, if you’re in a hurry, you can click any of the links to the following sections and get what you’re looking for.

The input is “array-like”, meaning it can be a number, a Python sequence or a NumPy array (with any number of dimensions). The result will be a NumPy scalar (if you pass in a scalar) or a NumPy array (if you pass in a sequence or an array).

np.sqrt() is an element-wise function, meaning it operates on each element of an array independently. The output will be the same shape as the input and each of the output elements will be the square root of its corresponding input element. Easy!

### A few facts about np.sqrt():

• Negative numbers will be np.nan (you will also see a RuntimeWarning)
• The square root of np.inf is np.inf
• If you pass in integers, NumPy will coerce the result to one of its float data types. As of version 1.19.4, it seems to convert int types to float with double the precision up to float64. So int8 becomes float16, int16 becomes float32 and int32 and above become float64.
• Complex numbers work as expected (e.g. np.sqrt(8 - 6j)). The result will be one of the complex NumPy types.
• By the way, other popular tensor libraries like PyTorch and TensorFlow follow roughly the same API. See the documentation for torch.sqrt() and tf.sqrt().

## So what does numpy.sqrt do?

NUMPY.SQRT CALCULATES A SQUARE ROOT IN PYTHON
In simple terms, the NumPy square root function calculates the square root of the input values.

So if you give it an input x, numpy.sqrt () sqrt {x} computes:

```   begin {equation *}  mbox { Huge  sqrt {x}}  end {equation *}
```

NUMPY.SQRT ALSO WORKS ON ARRAY
What’s crucial, however, is that the numpy square root function also works on numpy arrays.

So if you give it a numpy array as input, square root numpy calculates the square root of each value in the array.

I’ll show you examples of both cases in the Examples section, but let’s first take a closer look at the syntax.

### THE SYNTAX OF NP.SQRT

NumPy square root syntax is extremely simple. It is probably one of the simplest functions in the NumPy module.

To call the function, call it by name (although exactly what you call it depends on how you imported NumPy).

### AN IMPORTANT NOTE ABOUT IMPORTING NUMPY

The first time you import NumPy into your environment, you can easily do it using the import numpy code. This allows you to call NumPy functions with the prefix "numpy". followed by the name of the function.

However, it is very common to give NumPy a "nickname" when importing. It is very common to import NumPy as np using the import numpy code. This essentially gives NumPy the np alias in your code, which allows you to use "np". instead of "boring". when you call functions.

This is important because how you import numpy determines how you name it in your code.

Since it is common to import NumPy as np with the import numpy code, we follow this convention in our syntax.

Note, however, that np.sqrt () and numpy.sqrt () are essentially interchangeable as long as you’ve imported NumPy appropriately.

### NP.SQRT-SYNTAX

Assuming we have imported NumPy as np using the import numpy code, we call the square root function NumPy using the code np.sqrt ().

An explanation of the syntax of the NumPy square root function.

Then there is a parameter inside the function with which you can specify the input of the function.

Let’s discuss it quickly. ## NUMPY.SQRT PARAMETERS

The NumPy square root function actually has two parameters, but we’ll only talk about one of them.
Let me briefly explain each of them individually.

With the x parameter you can specify the input for the np.sqrt function.

Technically, you can specify the rooting with the x parameter. The radical is the value below the radical when calculating the square root.

Note that the function is relatively flexible in terms of the types of input it accepts as arguments for the x parameter. You can provide a single number, but you can also provide a NumPy array or any array-like input. The array-like inputs that work are things like Python lists and tuples.

When you provide a NumPy or array-type input, numpy.sqrt calculates the square root of each value in the array.

I’ll show you examples of all these cases in the Examples section.

Note that you have to provide an argument to the x parameter, which means you have to provide some kind of input to the function ... an integer, an array, a list. You have to provide something as input.

You can use the out parameter to specify an array in which to store the output.

This parameter isn’t used much by beginners, so we won’t really be working on it in this tutorial.

Now that I’ve explained the syntax and parameters, let’s take a look at some examples. ## EXAMPLES OF USING NUMPY SQUARE ROOT

As I said earlier, the numpy square root is very easy to use.

Even so, I’ll start with the simplest example possible and then add complexity as we move on to other examples.
As mentioned above, you need to import NumPy into your environment for the code to work correctly.

You can do this by running this code:

```import numpy as np
```

This will import the NumPy module with the alias "np". As I said earlier, this is a common convention among NumPy users. Ultimately, it allows us to refer to NumPy as np in our code, which is the prefix "np". will come before the function name when we call a function.

```>>> np.sqrt([1,4,9])
array([ 1.,  2.,  3.])
```
```>>> np.sqrt([4, -1, -3+4J])
array([ 2.+0.j,  0.+1.j,  1.+2.j])
```
```>>> np.sqrt([4, -1, numpy.inf])
array([  2.,  NaN,  Inf])
```

Numpy.Sqrt has – consistent with common convention – as its branch cut the real “interval” [-inf, 0), and is continuous from above on it. A branch cut is a curve in the complex plane across which a given complex function fails to be continuous.

` `

## Shop Learn programming in R: courses

\$ Best Python online courses for 2022

\$ Best laptop for Fortnite

\$ Best laptop for Excel

\$ Best laptop for Solidworks

\$ Best laptop for Roblox

\$ Best computer for crypto mining

\$ Best laptop for Sims 4

\$

Latest questions

NUMPYNUMPY

psycopg2: insert multiple rows with one query

NUMPYNUMPY

How to convert Nonetype to int or string?

NUMPYNUMPY

How to specify multiple return types using type-hints

NUMPYNUMPY

Javascript Error: IPython is not defined in JupyterLab

## Wiki

Python OpenCV | cv2.putText () method

numpy.arctan2 () in Python

Python | os.path.realpath () method

Python OpenCV | cv2.circle () method

Python OpenCV cv2.cvtColor () method

Python - Move item to the end of the list

time.perf_counter () function in Python

Check if one list is a subset of another in Python

Python os.path.join () method