This tutorial will explain how to use the NumPy power function, which is also called np.power.

Contents:

You can click on any of the above links and it will take you to the appropriate part of the tutorial.

So if you’re just looking for a quick answer to a question about the np.power function, just click on a link!

Having said that, if you’re new to NumPy, I recommend that you read the whole tutorial. NumPy can be a little complicated for beginners, and this tutorial is designed to give you a good overview of everything you need to know to use the np power function.

Ok …. let’s dive in.

A quick review of NumPy

Very quickly, let’s review NumPy.

If you’re new to data science in Python, you might be a little confused about what exactly NumPy is.

It’s pretty simple. NumPy is a toolkit for working with arrays of numbers in Python.

Python (as I hope you know) is a very common programming language. And increasingly, Python has become one of the most important programming languages for doing data science.

Python is excellent for data science

One of the reasons for the popularity of Python in the data science community is that it has a set of modules that are excellent for doing data science tasks:

  • Pandas enables you to manipulate dataframes
  • Matplotlib gives you tools for data manipulation
  • NumPy enables you to work with numeric data

Essentially, Python gives you a set of tools for doing almost every part of the data science workflow.

NumPy is a toolkit for working with numeric data

As I’ve mentioned, NumPy focuses on working with numbers.

Specifically, NumPy has tools that enable you to:

… and more.

There are several dozen functions in the NumPy package … it’s a little hard to list them all.

Essentially, NumPy provides a toolkit for analyzing, reshaping, and working with NumPy arrays, which are arrays of numeric data in Python.

As such, NumPy has tools that enable you to perform a variety of mathematical computations on numbers and arrays of numbers.

One of those tools is the NumPy power function.

A quick introduction to the NumPy power function

So what is the NumPy power function?

It’s pretty simple.

In its simplest form, numpy.power is just a tool for performing exponentiation in Python.

You almost certainly learned about exponentiation early in your school career. When we perform exponentiation, we raise one number b, called the base, to another number n, called the exponent. The exponent n is also sometimes called the “power.”

As explained by Wikipedia, this operation amounts to multiplying the base b by itself n times:

\[\large b^n = \underbrace{b \times \cdots \times b}_{n \text{ times}}\]

When we do this, we say that we raise b to the power of n.

The np.power function enables you to do this.

But in addition to allowing you to do this with single numbers, you can also do this with arrays of numbers.

How this works in practice can be a little complicated, but in essence, NumPy power just a tool for performing mathematical exponentiation with arrays of numbers.

np.power computes exponents in NumPy

So while the numpy.power function enables you to perform simple exponentiation like b to the power of n, it also allows you to do this with large NumPy arrays.

I want to show you some examples of how this works (to develop your intuition), but before I do that, I want to explain the syntax.

Once you understand the syntax, the examples themselves will be much easier to understand.

The syntax of numpy power

Here, let’s just take a look at the syntax at a high level.

An explanation of the syntax of the np.power function.

The syntax is really very simple, but to really “get it” you should understand exactly what the parameters are.

The parameters of np.power

In the image of the syntax above, there are two parameters:

  • array-of-bases
  • array-of-exponents

In the official documentation for the np.power function, these are called x1 and x2.

But as is often the case with the official NumPy documentation, I think those names are unintuitive. That being the case, I’m referring to x1 and x2 as array-of-bases and array-of-exponents respectively.

array-of-bases (required)

The first parameter of the np.power function is array-of-bases.

As this implies, the argument to this parameter should be an array of numbers. These numbers will be used as the “bases” of our exponents.

Note that this is required. You must provide an input here.

Also, the item that you supply can take a variety of forms. You can supply a NumPy array, but you can also supply an array-like input. The array-like inputs that will work here are things like a Python list, a Python tuple or one of the other Python objects that have array-like properties.

Keep in mind that you can also just supply a single integer!

array-of-exponents (required)

The second parameter is array-of-exponents, which enables you to specify the exponents that you will apply to the bases, array-of-bases.

Note that just like the array-of-exponents input, this input must be a NumPy array or an array-like object. So here you can supply a NumPy array, a Python list, a tuple, or another Python object with array-like properties. You can even provide a single integer!

Note: both arguments are positional arguments

Note that both arguments, array-of-bases and array-of-exponents, are positional arguments.

Positional arguments are a little confusing to beginners, but here’s a quick explanation.

How each input to np.power is used depends on where you put it inside of np.power().

Inside of np.power(), you must put the bases first and you must put the exponents second. The position of the argument inside of np.power() determines how it is used by the function.

Note that this is in contrast to so-called “keyword arguments.” With keyword arguments, you use a particular keyword to designate an input to a function. And as long as you use the correct keywords, they can be in any order you’d like.

So essentially, if the argument is a “positional argument,” the order maters.

And because array-of-bases and array-of-exponents are positional arguments, the bases must be specified first and the exponents must be specified second inside of the function.

Again, this is a little confusing, so be on the lookout for a future tutorial about positional arguments.

Examples of how to use NumPy power

Ok. Now that you’ve learned a little about the syntax, let’s look at some real examples of how to use numpy.power.

We’re going to work with a few examples, starting with the simplest and then progressing through examples that are more complicated.

Examples:

Run this code first

Before you fun the code in the following examples, you’ll need to run a small piece of code first.

You essentially need to import NumPy and give it an “alias.”

import numpy as np

When we do this, we essentially designate the code np as the “nickname” or alias of the numpy module.

This is a very common convention in Python, and it allows you to call a function starting with np. You’ll see what I mean in a minute.

Example 1: Raise an integer to a power with np.power

First, we’re going to work with a very simple example.

Here, we’re just going to raise an integer to a power.

To do this, we’ll call the NumPy power function with the code np.power(). Then inside of the parenthesis, we’ll provide two arguments …. the base and the exponent.

np.power(2,3)

OUT:

8

This is very simple. It just calculates 2 to the 3rd power which equals 8.

Notice how the inputs work. The first input (2) is the base and the second input (3) is the exponent.

A simple example of using numpy.power to raise an integer to a power.

This is exactly how the remaining examples will work.

Let’s take a look at a more complicated example.

Example 2: Calculate the exponents of an array of numbers

Here, we’re going to change things up a little bit.

Instead of the base being a single integer, the base will be a group of numbers organized into an array-like object (i.e., a Python list).

To be clear, we typically use np.power on NumPy arrays. However, I think that everything is easier to understand if we just use a Python list instead. By using a Python list, you’ll actually be able to see all of the numbers inside of the syntax.

In this example, the bases will be a simple list of numbers from 0 to 4: [0,1,2,3,4].

This list will be the first input to the np.power function. We’re going to raise each element of this list to the 2nd power.

np.power([0,1,2,3,4], 2)

Which produces the following output:

array([ 0, 1, 4, 9, 16])

This is pretty straight forward once you understand what’s going on here.

Essentially, the np.power function takes the elements of the list and uses those as the bases. Then, it applies the exponent (the second argument to the function) to every base.

An image that shows a simple example of np.power, where we apply apply the exponent 2 to several base values.

So np.power simply applies the exponent 2 to every single base in the first input array.

And the output of the function is a new NumPy with the computed exponential values.

Redo the example with a numpy array

Now, let’s just re-do this example with a NumPy array instead of a Python list.

As I mentioned above … everything is easier to understand and see when we use a Python list, but we’ll often need to use the np.power function on actual NumPy arrays.

So, I’ll just show you the same example with a NumPy array in place of the Python list.

First, we’ll just create a NumPy array using np.arange:

array_1d = np.arange(5)

This array just contains the numbers from 0 to 4.

A simple NumPy array of numbers from 0 to 4.

Then, we’ll use np.power to calculate the exponent:

np.power(array_1d, 2)

Which produces the following output:

array([ 0,  1,  4,  9, 16])

In terms of outputs, this example creates the same output as np.power([0,1,2,3,4], 2). So just remember that NumPy power will work with NumPy arrays, Python lists, or any array-like object.

Ok. Now that we’ve taken a look at some simple examples, let’s move on to something more complicated.

Example 3: Use np.power with two arrays

Here, we’re going to use two input arrays instead of one array and one number.

But keep in mind, we’re actually going to use Python lists instead of proper NumPy arrays, just for the sake of clarity.

The first list is going to be the list of bases, and the second list will be the list of exponents.

np.power([2,2,2,2,2], [0,1,2,3,4])

Which produces the following output:

array([ 1,  2,  4,  8, 16])

So what happened here?

The first input list – [2,2,2,2,2] – is the list of bases.

The second input list – [0,1,2,3,4] – is the list of exponents.

Recall the previous examples.

In an example like np.power(2,3), the first argument is the base and the second argument is the exponent.

The code np.power([2,2,2,2,2], [0,1,2,3,4]) is essentially the same! The first list is the bases, and the second list is the exponents.

The only major difference is how these inputs are put together to produce the output.

NumPy applies the exponents to the bases element wise. This means that it applies the first exponent to the first base, the second exponent to the second base, and so on.

An example of using np.power with two arrays to compute the exponents, element wise.

Considering that our input lists are fairly simple, this is still a fairly simple example. But it shows you how powerful (heh heh) the np.power function is when you start working with arrays and array like objects.

Ok. Now let’s move on to a more complicated example where we have a multi dimensional array of bases.

Example 4: Calculating exponents with multi-dimensional arrays (broadcasting)

Here, we’re going to use the NumPy power function to compute exponents for a 2-dimensional array of bases.

Moreover, the exponents will also be in an array-like structure.

Let’s take a look at the example and then I’ll explain exactly how NumPy handles this.

First, let’s create the input array. This is actually going to be a NumPy array, instead of a Python list.

To create this array, we’ll simply use the np.array function.

array_2d = np.array([[0,0,0,0,0],[1,1,1,1,1],[2,2,2,2,2],[3,3,3,3,3]])

Let’s can print it out, so you can see it:

print(array_2d)

OUT:

[[0 0 0 0 0]
 [1 1 1 1 1]
 [2 2 2 2 2]
 [3 3 3 3 3]]

This is a fairly simple 2-dimensional NumPy array. As you can see, the first row has all 0s, the second row has all 1s, and so on.

Now, let’s run the np.power function on this and apply some exponents to these bases.

np.power(array_2d, [0,1,2,3,4])

Which produces the following output:

array([[ 1,  0,  0,  0,  0],
       [ 1,  1,  1,  1,  1],
       [ 1,  2,  4,  8, 16],
       [ 1,  3,  9, 27, 81]])

What happened here?

You have to think about the structure of the inputs.

The first argument – the array of bases – is a 2-dimensional array. The second argument – the exponents – is a 1-dimensional array. And notice that they both have the same number of columns.

In a case like this, the NumPy power function is sort of smart. It applies the exponents to every row.

An example of using broadcasting to apply exponents to every row of an array using np.power.

It still does this element-wise, meaning that the exponent of column 0 is applied to the base of column 0 …. the exponent of column 1 is applied to the base of column 1, and so on.

But essentially, it applies the exponents to every row.

This behavior is called broadcasting, and it appears quite a bit in NumPy when you’re working with arrays that have different dimensions. A full explanation of broadcasting is beyond the scope of this post. Just understand that this is common in NumPy, and you’ll probably need to understand how it works.

NumPy power FAQ

Before I end the tutorial, let’s quickly discuss any frequently asked questions.

Frequently asked questions:

Can I use a negative exponent with NumPy power?

No, NumPy power doesn’t work with negative exponents.

So for example, if you try, you’ll get an error:

np.power(2, -1)
ValueError: Integers to negative integer powers are not allowed.

If you want to use negative exponents, use numpy.float_power() instead.

Leave your other questions in the comments below

Do you have any other questions about the NumPy power function?

Leave your question in the comments below.

Learn more about NumPy

NumPy is extremely powerful, and very important for data science in Python.

That being the case, if you’re interested in data science in Python, you should really learn more about Python.

You can learn more by checking out our other tutorials:

… and more.

I also suggest that you check out our tutorials about matplotlib and Pandas. For example, we have tutorials about the Pandas dataframe, the Pandas iloc method, and more.

For FREE Python data science tutorials, sign up for our email list

Ultimately, if you’re interested in data science in Python, there’s a lot to learn.

The good news is that we provide FREE data science tutorials here at Sharp Sight.

If you sign up for our email list, you’ll get our tutorials delivered FOR FREE right to your inbox.

When you sign up, you’ll get free tutorials on:

  • NumPy
  • Pandas
  • Base Python
  • Scikit learn
  • Machine learning
  • Deep learning
  • … and more.

Want to learn data science?

Sign up right now …