The post How to Use Numpy Empty appeared first on Sharp Sight.

]]>The tutorial assumes that you have somewhat limited experience with NumPy. As such, it starts with a quick review of NumPy, then proceeds to an explanation of the NumPy empty function. Later, the tutorial shows examples of the np.empty function and answers a few common questions.

If you just need something quick, you can click on any of the following links and it will take you to the appropriate section of the tutorial.

**Contents:**

- A quick review of NumPy
- Introduction to NumPy empty
- The syntax of np.empty
- NumPy empty examples
- NumPy empty FAQ

However, if you’re still new to NumPy, I recommend that you read the whole tutorial.

Let’s get to it ….

First, let’s quickly review NumPy.

What is NumPy?

At a high level, NumPy is just a toolkit for working with arrays of numeric data.

You can think of NumPy arrays like vectors, matrices, and tensors in mathematics.

They are just row-and-column structures that store numbers.

So a simple NumPy array can look something like this 1-dimensional array:

Or you can have 2-dimensional arrays like this:

One important thing that you need to know about NumPy arrays is that NumPy arrays have a *shape*.

In simple terms, you can think of the “shape” of an array as the number of rows and columns of the array.

So let’s say you have an array that’s filled in with values. The array has 3 rows and 4 columns.

The shape of this array is `(3,4)`

. Three rows and four columns.

This is important to understand. You often need to understand how array shapes work to create new NumPy arrays, because you often need to specify the exact shape of the new array. I’ll explain more about this later in the tutorial.

One of the primary things NumPy provides is a set of tools for creating arrays.

But in addition to creating arrays, it’s important to be able to *manipulate* arrays. NumPy provides a range of tools for doing that. NumPy has data manipulation functions like the NumPy concatenate function and summary functions like NumPy sum.

But before you do any data manipulation, you actually need an array to operate on.

One of the simplest functions to create a new NumPy array is the NumPy empty function.

The NumPy empty function does one thing: it creates a new NumPy array with random values.

But, there are a few “gotchas” about the function. Let me explain more.

When you first run the NumPy empty function, you might expect it to be “empty.” You might expect that there aren’t any numbers in it … just empty positions.

No … that’s not quite right.

The np.empty function actually *does* fill the array with values. It’s just that the values are completely *arbitrary*. The values are not quite “random” like the numbers you get from the np.random.randint function or one of the other “random” functions from NumPy.

No … the values are just arbitrary. You can think of them like arbitrary, “garbage” values.

The main use of NumPy empty is that it enables you to quickly create an array with a specific size and shape.

So if you need a “holding container” for some future values, you can use the NumPy empty function to create it. In this sense, it’s very similar to the NumPy ones function and the NumPy zeros function.

The main advantage of np.empty over np.ones and np.zeros is that it’s a little bit faster, particularly for large arrays.

Having said all of that, let’s take a look at the syntax for NumPy empty so you can see how it works.

The syntax of NumPy empty (AKA, np.empty) is quite simple. It’s definitely one of the simpler functions for initializing a new array.

To run the function, we’ll typically use the code `np.empty()`

.

Now if you’re a beginner, I need you to understand that exactly how you call the function depends on how you’ve imported NumPy.

If you import NumPy with the code `import numpy as np`

, then you’ll be able to call the function as `np.empty()`

.

But if you’re imported NumPy with the code `import numpy`

, then you’ll call NumPy empty with `numpy.empty()`

.

Beginners are sometimes confused by this, so you need to understand that how specifically you call the function depends on how you import NumPy.

But let’s assume that you’ve imported NumPy as `np`

.

In that case, you’ll call the function as `np.empty()`

.

Then inside of the parenthesis, there are a group of parameters that enable you to specify the structure of the resulting array.

The parameters of np.empty are important to how it works, so let’s examine them in more detail.

There are two primary paramters for the np.empty function:

`shape`

`dtype`

There’s actually one additional parameter – `order`

– but it’s not something that’s used often, so this tutorial will not explain it.

Let’s examine each parameter individually.

`shape`

(required)The `shape`

parameter specifies the “shape” of the array that you create.

Remember in the review of NumPy earlier in this tutorial, I explained that NumPy arrays have a *shape*. The shape is essentially the number of rows and the number of columns (if you have a 2D array).

When you create an array with np.empty, you need to specify the exact shape of the output by using the `shape`

parameter.

Because you need to specify the shape, this is required …. you need to provide an argument to this parameter.

How exactly do we do this?

We provide a group of values inside of a tuple or “array like” structure.

WTF does that mean?

Essentially, the argument to `shape`

will be an integer. Or a tuple of integers. Or a list of integers.

If you only provide an integer `n`

, the output will be a NumPy array with `n`

values.

But if you provide a tuple or list of values, `[n, m]`

, NumPy empty will create an array with `n`

rows and `m`

columns.

You’ll see exactly how this works in the examples section of the tutorial.

`dtype`

Second, there’s the `dtype`

parameter.

This parameter enables you to specify the data type of the data in the new array.

Keep in mind that this parameter is *optional*. You don’t need to provide anything here. If you don’t, np.empty will use `numpy.float64`

as the default data type.

But if you want to manually set this, you have a variety of options. Check out this page to see what data types are available.

Ok. Now that we’ve looked at the syntax, let’s take a look at some examples of NumPy empty.

Here, we’re going to take a look at some examples of NumPy empty.

Since the function is fairly simple and easy to use, we only need to look at a few examples to really understand how the function works.

**Examples:**

- Create a 1-dimensional empty NumPy array
- Create a 2-dimensional empty NumPy array
- Specify the data type of the empty NumPy array

One quick note.

Before you run any of the examples, you need to run the following code to import NumPy:

import numpy as np

As I explained in the syntax section of the tutorial, this is required in order to call the function properly with the prefix `np`

.

This is the common syntax convention and we’ll be using it in the examples.

First, we’re just going to create a simple 1-dimensional NumPy array.

np.empty(shape = 3)

Which produces the following output:

array([5.e-324, 5.e-324, 5.e-324])

(Note that your output will look different!)

So what happened here?

We called NumPy empty with the syntax `np.empty()`

.

Inside of the parenthesis, we used the `shape`

parameter to specify the shape of the output.

By providing an integer, 3, to the `shape`

parameter, we’re indicating that we want to create a 1-dimensional array with three elements.

Quickly, I want to re-do example 1 *without* explicitly using the `shape`

parameter.

Let me show you what I mean:

np.empty(3)

OUT:

array([5.e-324, 5.e-324, 5.e-324])

Notice that the output is still an “empty” array with three values, but in the code, we left out the `shape =`

parameter.

Python allows you to do this. You can remove the parameter names themselves and just pass an argument by *position*.

Essentially, when you exclude the parameter name and simply pass a number (or array) to np.empty, Python understands that this number corresponds to the `shape`

parameter.

Personally, I think that excluding the parameter names makes Python code much harder to read. However, it’s a common practice, so I want you to understand what that is and how it works.

Next, let’s create a 2-dimensional array.

To do this, we’ll use the `np.empty()`

function just as before.

However this time, we’ll pass a list of values to the `shape`

parameter.

Let’s take a look:

np.empty(shape = [2,3])

OUT:

array([[3.5e-323, 3.5e-323, 3.5e-323], [3.5e-323, 3.5e-323, 3.5e-323]])

(Note: your array should contain different values.)

If you’ve read the tutorial and you understood example 1, this should make sense.

We used the `shape`

parameter to specify the output shape.

Specifically, we passed a list of numbers, `[2,3]`

to the `shape`

parameter. This indicates to np.empty that we want to create an empty NumPy array with 2 rows and 3 columns.

Finally, let’s create an array and specify the exact data type of the elements.

Remember: by default, np.empty creates arrays that contain floating point numbers (arbitrary floating point numbers).

But we can *change* the data type of the array elements by using the `dtype`

parameter.

Let’s take a look.

Here, we’re going to create a NumPy array of six *integers*, organized in 2 rows and 3 columns.

np.empty(shape = [2,3], dtype = int)

Which produces the following output:

array([[7, 7, 7], [7, 7, 7]])

(Note: your output will probably contain different numbers.)

This example is almost exactly the same as example 2.

The only major difference is that we’ve set `dtype = int`

.

When we set `dtype = int`

, we’re telling np.empty to create a new NumPy array filled with *integers*.

Alternatively, we could also use the `dtype`

parameter to create an array with some *different* type of data. Consult the NumPy documentation to find out what other data types are available.

Now that you’ve seen some solid examples, I want to address some frequently asked questions.

**Frequently asked questions:**

Respectfully: read the f^*king tutorial.

This is a little confusing to people.

The name “numpy.empty” implies that the output will be “empty.”

But, it’s not!

The output of np.empty is actually a NumPy array with numeric values.

It’s just that the values of the array will be completely arbitrary.

They have no significance. They are “empty” of any meaning.

We just use this function to create the general shape and structure (the size, the number of elements, the number of rows and columns, etc).

Once we have this “empty” array, we need to fill it in with values that actually mean something.

Do you have more questions about NumPy empty?

Leave your question in the comments section below.

This tutorial should explain how to use NumPy empty.

But if you’re serious about NumPy, there is a lot more to learn.

For starters, you can check out a few of our other NumPy tutorials.

- How to create a NumPy array filled with zeros
- How to create a NumPy array filled with ones
- Combining NumPy arrays together with np.concatenate
- Summarizing numpy arrays with np.sum

Those are just a few tutorials though.

… if you’re really serious about learning more NumPy you should sign up for our email list.

Here at Sharp Sight, we regularly publish FREE data science tutorials.

That includes free tutorials about NumPy …

But also tutorials about a *lot* of other topics.

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

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

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

Ready to learn more about data science in Python?

Sign up for our email list now.

The post How to Use Numpy Empty appeared first on Sharp Sight.

]]>The post How to Use Numpy Full appeared first on Sharp Sight.

]]>At a high level, the Numpy full function creates a Numpy array that’s filled with the same value.

It’s a fairly easy function to understand, but you need to know some details to really use it properly.

Having said that, this tutorial will give you a quick introduction to Numpy arrays. Then it will explain the Numpy full function, including the syntax. After explaining the syntax, it will show you some examples and answer some questions.

The following links will take you to the appropriate part of the tutorial. So if you’re in a hurry, you can just click on a link.

**Contents:**

- A quick review of Numpy and Numpy arrays
- Introduction to Numpy full
- The syntax of np.full
- Numpy full examples
- Numpy full FAQ

However, it’s probably better to read the whole tutorial, especially if you’re a beginner.

Quickly, let’s review Numpy and Numpy arrays.

So what is Numpy?

To put it simply, Numpy is a toolkit for working with numeric data in Python. Said differently, it’s a set of tools for doing data manipulation with numbers.

More specifically, Numpy operates on special *arrays* of numbers, called Numpy arrays.

What is a Numpy array?

You can think of a Numpy array like a vector or a matrix in mathematics.

Like a matrix, a Numpy array is just a grid of numbers.

These Numpy arrays can be 1-dimensional … like a vector:

And Numpy arrays can be 2-dimensional:

They can also have *more than two dimensions*. These higher-dimensional Numpy arrays are like tensors in mathematics (and they are often used in advanced machine learning processes like Python’s Keras and TensorFlow).

One thing to remember about Numpy arrays is that they have a *shape*.

The shape of a Numpy array is essentially the number of rows and columns. (Or more technically, the number of units along each axis of the array.)

So let’s say that you have a 2-dimensional Numpy array.

This array has a shape of `(2, 4)`

because it has two rows and four columns.

You need to know about Numpy array shapes because when we create new arrays using the numpy.full function, we will need to specify a shape for the new array with the `shape =`

parameter.

You’ll read more about this in the syntax section of this tutorial.

Essentially, Numpy just provides functions for creating these numeric arrays and manipulating them.

For example, we can use Numpy to perform summary calculations. We can use Numpy functions to calculate the mean of an array or calculate the median of an array.

And Numpy has functions to change the shape of existing arrays. So we use Numpy to combine arrays together or reshape a Numpy array.

But before we do any of those things, we need an array of numbers in the first place.

Numpy has a variety of ways to create Numpy arrays, like Numpy arrange and Numpy zeroes.

One of the other ways to create an array though is the Numpy full function.

The Numpy full function is fairly easy to understand.

It essentially just creates a Numpy array that is “full” of the same value.

So for example, you could use it to create a Numpy array that is filled with all 7s:

It can get a little more complicated though, because you can specify quite a few of the details of the output array. For example, you can specify how many rows and columns. You can also specify the data type (e.g., `integer`

, `float`

, etc).

That being said, to really understand how to use the Numpy full function, you need to know more about the syntax.

The syntax of the Numpy full function is fairly straight forward.

Moreover, if you’ve learned about other Numpy functions, some of the details might look familiar (like the `dtype`

parameter).

But on the assumption that you might need some extra help understanding this, I want to carefully break the syntax down.

To call the Numpy full function, you’ll typically use the code `np.full()`

.

Having said that, you need to remember that how exactly you call the function depends on how you’ve imported numpy. If you’ve imported Numpy with the code `import numpy as np`

then you’ll call the function as `np.full()`

. But if you’ve imported numpy differently, for example with the code `import numpy`

, you’ll call the function differently.

For the most part here, I’ll refer to the function as np.full.

Ok. So you call the function with the code `np.full()`

. Then inside of the function there are a set of parameters that enable you to control exactly how the function behaves.

Let’s take a closer look at those parameters.

The three main parameters of np.full are:

`shape`

`fill_value`

`dtype`

There’s actually a fourth parameter as well, called `order`

. However, we don’t use the `order`

parameter very often, so I’m not going to cover it in this tutorial.

Let’s examine each of the three main parameters in turn.

`shape`

(required)The `shape`

parameter specifies the shape of the output array.

Remember, the output of the Numpy full function is a Numpy array.

Also remember that all Numpy arrays have a *shape*. The shape of a Numpy array is the number of rows and columns.

When we specify a shape with the `shape`

parameter, we’re essentially specifying the number of rows and columns we want in the output array. That’s it.

To do this, we need to provide a number or a list of numbers as the argument to `shape`

.

If we provide a single integer `n`

as the argument, the output will be a 1-dimensional Numpy array with `n`

observations.

But if we provide a list of numbers as the argument, the first number in the list will denote the number of rows and the second number will denote the number of columns of the output. (And if we provide more than two numbers in the list, np.full will create a higher-dimensional array.)

This might not make a lot of sense yet, but sit tight. I’ll show you examples in the examples section of this tutorial.

`fill_value`

The `fill_value`

parameter is easy to understand.

It’s the value that you want to use as the individual elements of the array.

So if you set `fill_value = 7`

, the output will contain all 7s. If you set `fill_value = 102`

, then every single element of the output array will be 102.

`dtype`

Finally, there’s the dtype parameter.

This just enables you to specify the data type of the elements of the output array.

By default, Numpy will use the data type of the `fill_value`

. So if your fill value is an integer, the output data type will be an integer, etc.

But you can manually specify the output data type here.

In the simplest cases, you’ll use data types like `int`

(integer) or `float`

, but there are more complicated options since Numpy recognizes a large variety of data types.

For the sake of simplicity, I’m not going to work with any of the more exotic data types … we’ll stick to floats and ints.

Ok … now that you’ve learned about the syntax, let’s look at some working examples.

Using Numpy full is fairly easy once you understand how the syntax works.

Still, I want to start things off simple. We’ll start with simple examples and increase the complexity as we go.

**Examples:**

- Create a 1-dimensional array filled with the same number
- Create a 2-dimensional array filled with the same number
- Modify the data type of the output array

One quick note before you run any code …

You need to make sure to import Numpy properly.

To import Numpy run this code:

import numpy as np

This will enable us to call functions from the Numpy package.

(Note: this assumes that you already have Numpy *installed*. If you don’t have Numpy installed, the import statement won’t work! If you don’t have Numpy installed, I recommend using Anaconda.)

Ok, with that out of the way, let’s look at the first example.

This first example is as simple as it gets.

We’re going to create a Numpy array filled with all 7s.

np.full(shape = 3, fill_value = 7)

And here’s the output:

array([7, 7, 7])

This is really simple.

To specify that we want the array to be filled with the number ‘7’, we set `fill_value = 7`

.

By setting `shape = 3`

, we’re indicating that we want the output to have three elements.

So the code `np.full(shape = 3, fill_value = 7)`

produces a Numpy array filled with three 7s.

Quickly, I want to redo that example *without* the explicit parameter names.

Among Python programmers, it’s *extremely* common to remove the actual parameters and to only use the arguments to those parameters.

Let me show you:

np.full(3, 7)

OUT:

array([7, 7, 7])

In terms of output, this the code `np.full(3, 7)`

is equivalent to `np.full(shape = 3, fill_value = 7)`

. The output is exactly the same. Numpy knows that the “3” is the argument to the `shape`

parameter and the “7” is the argument to the `fill_value`

parameter.

Unfortunately, I think `np.full(3, 7)`

is harder to read, particularly if you’re a beginner and you haven’t memorized the syntax yet.

Having said that, I think it’s much better as a best practice to explicitly type out the parameter names.

Next, let’s create a 2-dimensional array filled with the same number.

Here, we’re going to create a 2 by 3 Numpy array filled with 7s.

To do this, we’re going to call the np.full function with `fill_value = 7`

(just like in example 1). This will fill the array with 7s.

But to specify the *shape* of the array, we will set `shape = (2,3)`

.

Let’s take a look:

np.full(shape = (2,3), fill_value = 7)

Which creates the following output:

array([[7, 7, 7], [7, 7, 7]])

This is very straight forward.

By setting `shape = (2,3)`

, we’re indicating that we want the output to have 2 rows and and 3 columns. The code `fill_value = 7`

fills that 2×3 array with 7s.

In the example above, I’ve created a relatively small array. Two rows and three columns.

But understand that we can create arrays that are much larger.

Here’s an example of a 5×20 array:

np.full(shape = (5,20), fill_value = 7)

OUT:

array([[7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7], [7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7], [7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7], [7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7], [7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7]])

You could even go a step further and create an array with thousands of rows or columns (or more).

Like almost all of the Numpy functions, np.full is flexible in terms of the sizes and shapes that you can create with it.

Now, let’s build on example 2 and increase the complexity just a little.

Here, we’re going to create a Numpy array that’s filled with floating point numbers instead of integers.

Just like in example 2, we’re going to create a 2×3 array filled with 7s.

Now remember, in example 2, we set `fill_value = 7`

. But notice that the value “7” is an *integer*. Because of this, np.full just produced an output array filled with integers. That’s the default. By default, the output data type matches the data type of `fill_value`

.

It’s possible to override that default though and manually set the data type by using the `dtype`

parameter.

Let’s take a look:

np.full(shape = (2,3), fill_value = 7, dtype = float)

OUT:

array([[7., 7., 7.], [7., 7., 7.]])

Here, we have a 2×3 array filled with 7s, as expected.

But notice that the array contains floating point numbers. You can tell, because there is a decimal point after each number. You could also check the dtype attribute of the array with the code `np.full(shape = (2,3), fill_value = 7, dtype = float).dtype`

, which would show you that the data type is `dtype('float64')`

.

This is a simple example with a fairly familiar data type. Just keep in mind that Numpy supports a wide range of data types, including a few “exotic” options for Numpy (try some cases with `dtype = np.bool`

).

For the final example, let’s create a 3-dimensional array.

So far, we’ve been creating 1-dimensional and 2-dimensional arrays. We’ve been sticking to smaller sizes and shapes just to keep the examples simple (when you’re learning something new, start simple!).

But you need to realize that Numpy in general, and np.full in particular can work with very large arrays with a large number of dimensions.

So let’s look at the slightly more complicated example of a 3D array.

To do this, we’re going to provide more arguments to the `shape`

parameter.

Remember from the syntax section and the earlier examples that we can specify the shape of the array with the `shape`

parameter. If we provide a single number as the argument to `shape`

, it creates a 1D array. If we provide a list of *two* numbers (i.e., `shape = [2,3]`

), it creates a 2D array.

So how do you think we create a 3D array?

Pass an extra number to `shape`

.

Let’s take a look.

np.full(shape = (2,3,4), fill_value = 7)

OUT:

array([[[7, 7, 7, 7], [7, 7, 7, 7], [7, 7, 7, 7]], [[7, 7, 7, 7], [7, 7, 7, 7], [7, 7, 7, 7]]])

As you can see, this produces a Numpy array with 2 units along axis-0, 3 units along axis-1, and 4 units along axis-2.

As a side note, 3-dimensional Numpy arrays are a little counter-intuitive for most people. I hesitate to use the terms ‘rows’ and ‘columns’ because it would confuse people. I’ll probably do a separate blog post to explain 3D arrays in another place.

Having said that, just be aware that you *can* use Numpy full to create 3-dimensional and higher dimensional Numpy arrays.

Now that you’ve seen some examples and how Numpy full works, let’s take a look at some common questions about the function.

**Frequently asked questions:**

- Can you fill a Numpy array with True or False?
- Is Numpy full slower than Numpy zeros and Numpy empty?

Yes, actually you can.

np.full(shape = [2,2], fill_value = True, dtype = bool)

And here is the output:

array([[ True, True], [ True, True]])

As you can see, the code creates a 2 by 2 Numpy array filled with the value `True`

.

Note that there are actually a few other ways to do this with np.full, but using this method (where we explicitly set `fill_value = True`

and `dtype = bool`

) is probably the best.

If you’re just filling an array with the value zero (0), then the Numpy zeros function is faster.

You can check it out by timing them.

%timeit np.zeros(shape = 100000000) %timeit np.full(shape = 100000000, fill_value = 0)

You can learn more about Numpy zeros in our tutorial about the np.zeros function.

Having said that, if your goal is simply to initialize an empty Numpy array (or an array with an arbitrary value), the Numpy empty function is faster.

You can learn more about Numpy empty in our tutorial about the np.empty function.

If you have questions about the Numpy full function, leave them in the comments.

This tutorial should tell you almost everything you need to know about the Numpy full function.

But if you’re new to using Numpy, there’s a *lot* more to learn about Numpy more generally.

For example, there are several other ways to create simple arrays. You can create an empty array with the Numpy empty function. Or you can create an array filled with zeros with the Numpy zeros function. And obviously there are functions like np.array and np.arange.

Moreover, there are quite a few functions for *manipulating* Numpy arrays, like np.concatenate, which concatenates Numpy arrays together.

There’s also a variety of Numpy functions for performing summary calculations (like np.sum, np.mean, etc).

My point is that if you’re learning Numpy, there’s a lot to learn.

And it doesn’t stop there … if you’re interested in data science more generally, you will need to learn about matplotlib and Pandas.

If you want to learn more about Numpy, matplotlib, and Pandas …

… if you want to learn about data science …

Then sign up for our email list.

Here at Sharp Sight, we teach data science.

And on a regular basis, we publish FREE data science tutorials.

If you sign up for our email list you’ll get our free tutorials delivered directly to your inbox.

You’ll learn about:

- Numpy
- Pandas
- Base Python
- Scikit learn
- Machine learning
- Deep learning
- … and more.

If you want to learn more about data science, then sign up now:

The post How to Use Numpy Full appeared first on Sharp Sight.

]]>The post How To Use Numpy Tile appeared first on Sharp Sight.

]]>Without getting into any of the details yet, let me explain what NumPy tile does.

At a very high level, the NumPy tile function creates a new NumPy array by repeating the input array.

However, the exact details of how the function works and what it does are a little more complicated.

Having said that, this tutorial will cover several different topics to help you learn how to use np.tile properly. It will provide you with a quick review of NumPy arrays. It will explain the syntax of np.tile. After showing you the syntax, it will show you step-by-step examples.

**Contents:**

- A quick review of NumPy arrays
- Introduction to NumPy tile
- The syntax of np.tile
- NumPy tile examples
- NumPy tile FAQ

If you’re in a hurry, you can click on any of the links above and it will take you to the appropriate location in the tutorial.

Ok … let’s start the tutorial with a quick review of NumPy.

NumPy is a very commonly used tool for doing data manipulation in Python.

More specifically, NumPy provides a toolkit for working with numbers in Python.

The NumPy package essentially provides tools for organizing and manipulating numbers.

The heart of NumPy is what we call a NumPy array. A NumPy array is just a special Python object for storing numbers in a row-and-column format.

The best way to mentally visualize a NumPy array is thinking about a grid of numbers:

Again, NumPy provides a *lot* of tools for creating these arrays. There’s the NumPy zeros function, which creates NumPy arrays that contain all zeros. There’s the NumPy ones function, which creates NumPy arrays that contain all ones. And there’s the NumPy arange function which creates arrays that contain ranges of numbers. Those are just a few examples though.

Many of the other NumPy functions are used for *manipulating* NumPy arrays that already exist.

There are many examples of this, like NumPy reshape, which changes the shape of a NumPy array. In addition to Numpy reshape, NumPy concatenate, NumPy vstack, and NumPy hstack all combine NumPy arrays together, in one way or another.

And then there’s NumPy tile.

We can use the NumPy tile function to create a new NumPy array based on an existing NumPy array. It’s somewhat similar to NumPy repeat, but definitely different.

Let’s talk about exactly what NumPy tile is and why we use it.

Let’s take a closer look at NumPy tile and how it works.

Put simply, the NumPy tile function creates new NumPy arrays that contain little copies of an input array.

The input array is treated like a “tile” …. a little unit that can be copied and repeated into a grid format.

So when we use the NumPy tile function, it’s not repeating the individual elements in the way that NumPy repeat does.

Again, Numpy tile takes the whole input array and copies everything (including the order of the elements) and repeats that whole structure within the output array.

This enables you to do things like copy an entire 2-dimensional NumPy array and repeat it in the output.

It’s possible to copy the input array once (like in the above image).

But you can also copy the array multiple times, like this:

Exactly *how* we tile the array though is determined by how we use the syntax.

That being the case, let’s take a look at how the code for np.tile works.

The syntax of the NumPy tile function is fairly simple, but at the same time, it’s not immediately easy to understand.

Part of that is because you need to “wrap your mind” around how the function works ….

… and the other reason is that the parameter names are often not used.

In any case let’s take a close look at everything.

To call the NumPy tile function, you’ll commonly use the syntax `np.tile()`

.

Keep in mind that this assumes that you’ve imported NumPy with the code `import numpy as np`

. If you instead import NumPy with the code `import numpy`

, you’ll have to refer to the function as `numpy.tile()`

instead. Just remember that how you call the function depends on how you’ve imported NumPy!

(Throughout this explanation of the syntax, I’ll mostly refer to the function as np.tile, but I might alternatively call it numpy.tile. They are both essentially the same.)

So we call the function with the function name `np.tile()`

.

Then inside of the function are two parameters that enable you to control how the function works: `A`

and `reps`

.

Let’s talk about those parameters in more detail.

The np.tile function really only has two parameters:

`A`

`reps`

Let’s talk about them individually.

`A`

(required)The `A`

parameter enables you to specify the input array.

This array will essentially function as the “tile” that gets copied and repeated inside of the output array.

Technically, NumPy will allow you to provide any “array like input” to the `A`

parameter.

In Python, “array like inputs” include:

- NumPy arrays
- Python lists
- Python tuples

No matter what, the output will still be a NumPy array, but np.tile is fairly flexible in terms of the types of inputs that it will operate on.

Note that NumPy requires you to pass something to this parameter. So you must provide an argument to the `A`

parameter.

However, like many parameters in Python functions, you do not explicitly need to type the “A” parameter. This means that you can pass an argument to the `A`

parameter by position. If you do this, the first argument to the function will be treated as the argument to the A parameter. (If this is confusing, look at the examples to see how this works.)

`reps`

The `reps`

parameter enables you to indicate how to repeat the input array.

That includes how many times to repeat it, but also the structure of the tiled output.

To do this, you will pass an integer or an “array like” object to `reps`

.

It will be easiest to understand by looking at examples, but let me quickly explain.

If you provide an integer `n`

to `reps`

, then it will simply copy the array `n`

times, horizontally. So if you set `reps = 2`

, it will produce an output with two instances of the input array, tiled horizontally.

But remember: you can also pass an “array like” object to `reps`

.

That means that you can pass a list or a tuple to `reps`

. When you do, the first number in the tuple is the number of repeats *downwards* and the second number in the tuple is the number of repeats *across*.

And if you have more than two numbers, it will actually repeat into a greater number of dimensions! So you can actually use np.tile to create 3-dimensional or multi dimensional NumPy arrays.

That’s a little more complicated, so we’re not going to cover that …. we’ll stick to 1D and 2D arrays here, for the sake of simplicity.

Ok. Now that you’ve seen how the syntax works, let’s take a look at some proper examples of NumPy tile.

NumPy tile isn’t terribly hard to understand, but it can be a little bit confusing in the beginning.

That’s why we’re going to start with very simple examples and then slowly increase the complexity. In the later examples, you’ll start to get more of a sense of how np.tile works.

**Examples:**

- Tile a list of numbers horizontally
- Tile a 1D NumPy array horizontally
- How to tile an array vertically
- Tile an 1-d input both vertically and horizontally
- Tile a 2-dimensional array vertically and horizontally

One quick note … make sure that you import NumPy properly.

In order for the following examples to work properly, you need to import NumPy with the following code:

import numpy as np

Ok … let’s start with a very simple example.

First, we’re going to repeat a simple Python *list* horizontally.

I’m going to use a list here because I think that it makes the syntax more intuitive if you can actually see the numbers in the input. It makes it easier to directly compare the input to the output.

The input list will contain three numbers: `[6, 7, 8]`

.

We’ll repeat this two times, horizontally. To do this, we’ll set `reps = 2`

.

Ok. Let’s take a look at the code.

np.tile(A = [6,7,8], reps = 2)

This creates the following NumPy array as an output:

array([6, 7, 8, 6, 7, 8])

As you can see, the output array contains the original input numbers – 6, 7, and 8 – repeated two times.

But notice *how* they are repeated.

The elements of the input are __not__ repeated individually, such that the first element is repeated two times and *then* the second element is repeated two times (i.e., `[6, 6, 7, 7, 8, 8]`

). That’s how the NumPy repeat would repeat the input.

No. NumPy tile repeats the input as if it is copying the original input as a coherent unit, including the original order of the elements.

The input array (in this case, a list) is treated like a small ’tile’ that can be copied and repeated.

Here, because we’ve set `reps = 2`

, it only copied the array one extra time … meaning the total number of repeated ’tiles’ is 2.

Notice one thing.

NumPy tile copied the array *horizontally*. This is the default behavior if you only provide an integer as the argument to the `reps`

parameter. It just tiles the input horizontally.

That will change though when we look at more complex examples, with an array of numbers as the argument to reps. We’ll see that in future examples.

Very quickly, I want to re-do example 1 with a proper NumPy array as an input instead of a list.

Recall that in example 1, we ran the code with a *list* as the input.

Also remember that NumPy tile will accept any *array like input* as the argument to the `A =`

parameter. So it will accept a list, a tuple, or a NumPy array.

To reinforce this point, let’s redo the example with a NumPy array.

First, let’s just create a simple NumPy array:

simple_array = np.array([6,7,8])

If you print this out, with `print(simple_array)`

, you’ll see that it contains the same elements as the list in example 1: `[6 7 8]`

. However, if you check the data type with the code `type(simple_array)`

, you’ll see that `simple_array`

is an `numpy.ndarray`

… i.e., a NumPy array.

Ok. We have our NumPy array. Let’s use np.tile.

np.tile(A = simple_array, reps = 2)

OUT:

array([6, 7, 8, 6, 7, 8])

As you can see, the array of input numbers – 6, 7, and 8 – have been tiled across.

This code is effectively the same as `np.tile(A = [6,7,8], reps = 2)`

. The only difference in this case is that we’re using a proper NumPy array as the input instead of a list.

Again, I’m showing you this to reinforce the point that the np.tile function will operate on a variety of array like inputs.

In this example, we’re going to take a simple 1-dimensional input and tile it *downwards*.

Essentially, we’re going to treat the input like a row of data, and *copy the row*.

To keep things simple, we’re going to use a Python list here as the input, but remember that this could also be a NumPy array.

Ok … let’s take a look at the code.

np.tile(A = [6,7,8], reps = (2,1))

OUT:

array([[6, 7, 8], [6, 7, 8]])

So what happened here?

Effectively, np.tile just cloned the row. But why?

Look carefully at the code we used to execute this: `np.tile(A = [6,7,8], reps = (2,1))`

.

Specifically, look at the `reps`

parameter.

We used the code `reps = (2,1))`

.

This tells NumPy how many times to “repeat” the input “tile” downwards and across.

So when we type `reps = (2,1))`

, we’re indicating that in the output, we want 2 tiles going downward and 1 tile going across (including the original tile). This will essentially just duplicate the original input downward.

The argument to reps – the tuple of numbers – tells np.tile how many times to tile the input array.

Ok … now we’re going to make things a little more complicated.

We’re going to tile a 1-dimensional input both vertically and horizontally.

To keep things simple, we’ll use a Python list as the input, but remember again that we could use use a NumPy array as an input.

Here, we’re going to use a *very* simple input. Only two numbers in a list: `[6, 7]`

.

And we’re going to tile this list of numbers three times downwards and two times across.

To do this, we’ll set `reps = [3,2]`

.

If we’re treating the input list `[6,7]`

as a “tile”, then the output will be like a 3×2 grid of those tiles.

Let’s look at the full code:

np.tile(A = [6,7], reps = [3,2])

Which produces the following NumPy array as an output:

array([[6, 7, 6, 7], [6, 7, 6, 7], [6, 7, 6, 7]])

If you carefully read example 1, example 2, and example 3, then you should probably understand this, but let me explain.

We’re treating the input list `[6,7]`

as a “tile” … as a unit that can be copied and repeated.

The `reps`

parameter indicates how many times to repeat it. By setting `reps = [3,2]`

, we’re indicating that we want to repeat the input “tile” three times vertically and two times horizontally.

Ok. One more example.

Here, we’re going to use a 2-dimensional array as an input.

First, let’s just create our 2-dimensional NumPy array using the NumPy array function.

np_array_2d = np.array([[6,7],[8,9]])

And let’s print out the array:

print(np_array_2d)

OUT:

[[6 7] [8 9]]

Essentially, `np_array_2d`

is a 2×2 NumPy array.

Next, we’re going to tile the array with np.tile.

np.tile(np_array_2d, reps = [2,2])

OUT:

array([[6, 7, 6, 7], [8, 9, 8, 9], [6, 7, 6, 7], [8, 9, 8, 9]])

The output here is a NumPy array with 4 rows and 4 columns.

But take a careful look at the contents of the output array. It essentially contains four little copies of the original input: two vertically and two horizontally.

That’s really all np.tile does …. it takes an input array and uses that input as a “tile” that can be copied and repeated multiple times, in a grid format, inside of the output array.

Let’s quickly address some frequently asked questions about NumPy tile.

**Frequently asked questions:**

This is fairly straight forward.

We essentially covered this in example 3.

Let’s assume that you have a 1-dimensional input (a 1D NumPy array or a list).

To duplicate the row, you’ll use np.tile with `reps = [2,1]`

.

np_array_1d = np.array([6,7,8,9]) np.tile(A = np_array_1d, reps = [2,1])

OUT:

array([[6, 7, 8, 9], [6, 7, 8, 9]])

Obviously, you could copy the row more than twice …. to do that you’d just have to change the argument to the `reps`

parameter.

Yep.

In the examples section of this tutorial, we stuck to 1D inputs and 2D inputs, for the sake of simplicity.

Also, we only tiled those inputs into 1 or 2 dimensions.

However, you can actually use np.tile in higher dimensions.

For example:

np.tile(A = [6,7], reps = [2,2,2])

OUT:

array([[[6, 7, 6, 7], [6, 7, 6, 7]], [[6, 7, 6, 7], [6, 7, 6, 7]]])

To be honest, it’s a little more complicated to visualize and think about higher dimensions, so for the sake of simplicity, I’m not going to explain much about this.

Just understand that np.tile *does* work in more than 2 dimensions.

In this tutorial, I’ve explained how np.tile works in a fair amount of detail.

But np.tile is just one function in the larger NumPy system.

To really be able to use NumPy fluently, you’ll need to learn:

- How to use the NumPy array function
- What NumPy axes are, and how NumPy axes work
- How to summarize NumPy arrays
- How to reshape NumPy arrays

There’s quite a bit to learn!

Moreover, there’s a lot more to learn about other parts of the Python data science stack, like matplotlib, Pandas, and sci-kit learn.

If you want to learn the Python data science stack, then sign up for our email list.

Here at Sharp Sight, we regularly publish tutorials about:

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

When you sign up for our email list, you’ll get our tutorials delivered directly to your inbox.

Ready to learn more data science?

Sign up now.

The post How To Use Numpy Tile appeared first on Sharp Sight.

]]>The post How to use numpy repeat appeared first on Sharp Sight.

]]>The NumPy repeat function essentially *repeats* the numbers inside of an array. It repeats the individual *elements* of an array.

Having said that, the behavior of NumPy repeat is a little hard to understand sometimes. It’s a little un-intuitive.

To help explain how this function works, I’m going to explain everything step by step. I’ll review the basics of NumPy arrays and how they are structured. Then I’ll explain the NumPy repeat function. Next, I’ll explain the actual syntax of np.repeat, before moving on to some examples and questions.

**Table of Contents:**

- A quick review of NumPy arrays
- Introduction to NumPy repeat
- The syntax of np.repeat
- NumPy repeat examples
- NumPy repeat FAQ

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

But if you really want to understand how this works, I recommend that you read the full tutorial.

Ok. Onward.

First, let’s just review NumPy arrays.

A NumPy array is just an array of numbers that we create with the NumPy package. It’s basically a special kind of object in Python that we use to store and manipulate numeric data.

NumPy arrays can be 1-dimensional:

… 2-dimensional:

Or even have 3 or more dimensions.

The real constraint with NumPy arrays is that they essentially can only hold numbers.

You can think of NumPy arrays like vectors, matrices, and tensors in mathematics.

Importantly, NumPy arrays have “axes.”

What are axes?

You can think of axes like directions.

A 1-dimensional array only has one axis, called axis-0:

And 2-dimensional arrays have two axes, called axis-0 and axis-1:

As it turns out, NumPy axes are important because they are often used when we manipulate arrays.

We’ll actually use NumPy axes when we manipulate our NumPy arrays with NumPy repeat.

Now that we’ve reviewed NumPy arrays, let’s talk more specifically about the NumPy repeat function.

Essentially, the NumPy repeat function repeats the elements of an array.

That’s basically all it does!

It can get a little more complicated though in more complicated cases.

One important thing to understand is that by default, NumPy repeat will flatten out an array.

That might not make sense, so let me give you an example. Let’s say that you have a 2-dimensional array with some numbers.

If you apply the np.repeat function on the 2-dimensional input array, then by default, it will repeat the values and put those repeated values into a 1-dimensional array.

By default, the output will be 1-dimensional.

It’s possible to change that behavior, but to do so, you need to understand the syntax of np.repeat.

That being said, let’s take a look at the syntax.

The syntax of the np.repeat function is fairly straightforward.

First of all, you will typically call the function with the name `np.repeat()`

.

However, it’s important to note that how exactly you call the function depends on how you’ve imported NumPy.

Remember that before you *use* NumPy in a Python program, you need to *import* NumPy.

The common way to do this is with the code `import numpy as np`

. That’s a very common convention used by many programmers.

When you import NumPy with the alias “`np`

,” it allows you to refer to the NumPy package with the prefix “`np.`

” in your syntax.

So if you import NumPy with the code `import numpy as np`

, then you will call the repeat function as `np.repeat()`

.

On the other hand, if you instead import NumPy with the code `import numpy`

, you’ll have to use the prefix “`numpy.`

” in your syntax, which means that you’d have to call the function as `numpy.repeat()`

.

Essentially, how you call the function depends on how you import NumPy.

Having said that, I’ll mostly refer to the function as `np.repeat()`

, but you might see me use the term `numpy.repeat()`

as well.

Let’s finally take a close look at the syntax of np.repeat.

First, you’ll typically call the function with the name `np.repeat()`

.

Then inside of the function are a set of parameters that you can use to specify exactly how the function will work.

The parameters are important, so let’s take a look at them.

The np.repeat function has 3 parameters:

`a`

`repeats`

`axis`

Let’s examine each of these individually.

`a`

(required)The `a`

parameter enables you to specify the “array” that you will use as the input to np.repeat.

Keep in mind that although the np.repeat function is primarily designed to operate on NumPy arrays, it will also work on any array-like object. That includes Python lists and Python tuples. Technically, it will even operate on a string (although it will treat it like a Python list). The important thing to understand is that np.repeat is flexible concerning what inputs it will work on.

Also keep in mind that the `a`

parameter is required. This means that you *must* provide some sort of input to the function.

`repeats`

(required)The `repeats`

parameter enables you to specify how many times to repeat the items in the input array.

The argument to this parameter will be an integer …

… so if you want to repeat each item twice, you’ll type ‘`2`

‘.

… if you want to repeat each item three times, you’ll type ‘`3`

‘.

… etcetera.

This is fairly self explanatory, but you’ll be able to see how this works in practice in the examples section of the tutorial.

`axis`

Finally, the `axis`

parameter enables you to specify the axis along which you will repeat the elements.

This is a little confusing to NumPy beginners, because most people don’t really understand what axes are.

Remember, NumPy arrays have axes. A 1D NumPy array has one axis, and a 2D array has two axes.

These “axes” are essentially like *directions*.

When we use the `axis`

parameter, we are specifying the direction along which we will repeat the items.

So for example, if we have a 2-dimensional NumPy array and we want to repeat the items downwards down the columns, we will set `axis = 0`

.

If we want to repeat the items horizontally across the rows, we will set `axis = 1`

.

(Although, keep in mind that the axis specifications will be different for a 1-dimensional array.)

Again, this might not make a lot of sense. If you’re confused about NumPy axes, make sure to read our tutorial that explains what a NumPy axis is.

You should also pay close attention to example 4 and example 5 in this tutorial which explain how to repeat elements down the columns or across the rows of a 2D array.

Finally, let’s talk about the output of np.repeat.

This is simple.

The output of NumPy repeat is a NumPy array.

But keep in mind that the size and exact shape of the output will depend on how we use the np.repeat function.

Now that I’ve explained the syntax, let’s look at some examples of how to use NumPy repeat.

**Examples:**

- Repeat a single number
- How to repeat a 1-d list of numbers multiple times
- How to repeat the values of a 2-d array
- Repeat elements down the columns
- Repeat elements across the rows

One quick note before you run any code:

You need to import NumPy properly.

I’m assuming here that you have NumPy already *installed* on your machine (if you don’t, you should install Anaconda, which will include NumPy).

But even if you have NumPy installed on your computer, you will need to import it into your working environment by using the following code:

import numpy as np

This essentially makes the NumPy functions available in our code by typing the prefix “np.” followed by the name of the function.

Ok. With that out of the way, let’s look at some examples.

First, let’s start veeeery simple.

Here, we’re just going to repeat a single number several times.

To do this, we’ll call the np.repeat function. Inside of the parenthesis, we’ll provide the value that we want to repeat with the `a =`

parameter, and specify the number of repeats with the `repeats =`

parameter.

Essentially, we’re going to repeat the number “7” four times.

Here’s the code:

np.repeat(a = 7, repeats = 4)

Which produces the following output:

array([7, 7, 7, 7])

This is really easy to understand.

We indicate that we will repeat the number ‘7’ with the `a`

parameter, and indicate that we will repeat it four times with the `repeats`

parameter.

Notice also that the output is a NumPy array. If you want, you could check that with the code `type(np.repeat(a = 7, repeats = 4))`

.

Very quickly, let’s redo the above examples *without* explicitly referencing the parameter names.

For better or for worse, many Python programmers write their code without explicitly showing the parameter names.

Personally, I think this is a bad habit (one that I’ve been guilty of myself). It’s much more clear to explicitly show the parameter names.

But just so you can see it, let’s run the code without the parameter names. We’ll just pass arguments to the function by position.

np.repeat(7, 4)

OUT:

array([7, 7, 7, 7])

The output is still the same.

The only difference is that we’ve removed the actual parameters. Python knows that the argument in the first position refers to the “`a`

” parameter and the item in the second position refers to the “`repeats`

” parameter.

The code looks a little different, but it’s essentially the same and the output is the same.

Again, I want you to see this so you understand what’s going on when the parameter names are removed.

Now, we’re going to move on to a slightly more complicated example.

Instead of repeating a single number, we’re going to repeat *multiple* numbers.

This example is almost exactly the same as the first example, except we’re going to make one small modification to our code.

… we’re going to provide a *list* of numbers to the `a`

parameter.

Let’s take a look.

Here, we’re going to repeat the numbers 6 and 7 two times each. We specify the two numbers to repeat with the code `a = [6,7]`

. Then the parameter `repeats = 2`

indicates that we’ll repeat those numbers two times.

np.repeat(a = [6,7], repeats = 2)

OUT:

array([6, 6, 7, 7])

Notice what happened here.

Our input numbers, 6 and 7, are repeated two times each.

And notice *how* it repeats the numbers. It repeats the first number two times and then repeats the second number two times.

The output is a NumPy array, as we should expect, but it’s a 1-dimensional NumPy array.

This is important!

By default, the output of numpy.repeat is a flat, 1-dimensional array that simply repeats all of the elements of the input.

We’ll see this behavior in the next example as well, and we’ll only be able to change it when we finally start to use the `axis`

parameter.

In this example, we’re going to use a 2-d array as the input to NumPy repeat.

First, let’s create a NumPy array.

Here, we’re going to use the NumPy array function to create a 2-dimensional array with four numbers.

np_array_2d = np.array([[6,7],[8,9]])

We can print this out with the code `print(np_array_2d)`

which produces the following output:

[[6 7] [8 9]]

The resulting NumPy array, `np_array_2d`

, is basically an array with 2 rows and two columns.

Now, we’re going to use np.repeat to repeat the elements of the array.

Here’s the code:

np.repeat(a = np_array_2d, repeats = 2)

OUT:

array([6, 6, 7, 7, 8, 8, 9, 9])

This example is actually easy to understand if you understood example 1 and example 2.

The array `np_array_2d`

is the input to the function. We’re using the `repeats`

parameter to specify that we want to repeat every element of the input two times.

When it executes, the np.array function simply takes every individual element of the input, repeats it 2 times, and puts it into a 1-dimensional output array.

Now, let’s make things more complicated.

We’re going to work with a 2-dimensional NumPy array again.

But we’re now going to use numpy.repeat to repeat the values downward down each column.

This is a little more complicated and more difficult to understand, so let’s take it step by step.

First let’s just create a 2-d NumPy array.

This is the same as the array in the previous example, so if you already ran that code, you don’t need to run this again.

np_array_2d = np.array([[6,7],[8,9]])

If we print this out with the code `print(np_array_2d)`

, you can see that it’s a 2×2 array with four values:

[[6 7] [8 9]]

Now, we’re going to repeat the values of each column *downward* down each column.

Let’s first run the code and then let me explain.

Here, we’re going to run np.repeat with 2 repeats. But we’re now going to use the `axis`

parameter.

np.repeat(a = np_array_2d, repeats = 2, axis = 0)

OUT:

array([[6, 7], [6, 7], [8, 9], [8, 9]])

So look at what happened …

The np.repeat function repeated the values of each column *downward*.

Why?

To understand this, you need to understand NumPy axes.

Remember that NumPy arrays have axes. The axes of an array are like *directions*. For a 2-dimensional array, axis-0 is the axis that points downwards down the columns and axis-1 is the axis that points horizontally across the rows.

When we use numpy.repeat with `axis = 0`

, we’re telling numpy.repeat to operate in the axis-0 direction.

So when we set `axis = 0`

, it takes the values of the array and it repeats them *in that direction*. Remember, axis-0 points downwards down the columns. So in this case, it repeats the values downwards down the columns.

The behavior of np.repeat actually makes sense, but to really understand this, you really need to understand NumPy array axes first. So if you’re a little confused by this example, you might want to read our tutorial about NumPy axes.

Next, let’s do something similar to the last example. In the previous example, we repeated elements down the columns.

In this example, we will repeat elements *horizontally across the rows*.

This is very easy to do and it will make sense if you read the previous example.

We’re essentially going to set `axis = 1`

which indicates that we want np.repeat to operate in the axis-1 direction.

Again, let’s create a 2-d NumPy array.

This is the same as the array in the last example, so if you already ran that code, you don’t need to run this again.

np_array_2d = np.array([[6,7],[8,9]])

If we print this out with the code `print(np_array_2d)`

, you can see the contents. It’s a 2-dimensional NumPy array:

[[6 7] [8 9]]

Now, let’s repeat those values horizontally.

To do this, we’ll use np.repeat with `axis = 1`

.

np.repeat(a = np_array_2d, repeats = 2, axis = 1)

And here’s the output:

array([[6, 6, 7, 7], [8, 8, 9, 9]])

This is very similar to the previous example where we repeated values downward.

But here, by setting `axis = 1`

, we’re telling np.repeat that we want to repeat the values in the axis-1 direction. Remember, for a 2-dimensional array, axis-1 is the direction that points horizontally across the rows.

So essentially, this code will take each element in the input array and repeat it horizontally across axis-1.

Again, this is fairly easy to understand once you really understand NumPy axes. To learn more about axes, read our tutorial that explains what a NumPy axis is and how we use them.

Now let’s address some frequently asked questions about NumPy repeat.

**Frequently asked questions:**

- How can I duplicate the rows of a 2-D array?
- How can I duplicate a 1-dimensional array?
- What is the difference between np.repeat and np.tile?

If you already have a 2-dimensional NumPy array, then it’s relatively easy to duplicate all of the rows.

To do this, we’re simply going to use np.repeat with `axis = 0`

.

Let me show you.

First, we’ll just create a 2D numpy array:

np_array_2d = np.array([[1,2,3],[4,5,6]])

Now, let’s duplicate the rows with NumPy repeat:

np.repeat(a = np_array_2d, repeats = 2, axis = 0)

OUT:

array([[1, 2, 3], [1, 2, 3], [4, 5, 6], [4, 5, 6]])

As you can see, np.repeat has produced an output array that repeats every row of the input.

Note that this is essentially the same as example 4 in the examples section of this tutorial.

Duplicating a 1D array is a little harder because a 1-dimensional array only has one axis (axis 0).

This is a problem, because when we duplicate the rows, there needs to be another dimension in which to copy the rows.

So essentially, to do this, we actually need to increase the number of dimensions of the array first, and then copy the row.

Let’s take a look.

First, let’s just create a 1-dimensional NumPy array.

np_array_1d = np.array([1,2,3,4])

If you were to use np.repeat on this with `axis = 1`

, then you’d get an error.

So before we run that code, we need to expand the number of dimensions.

np_array_2d = np.expand_dims(np_array_1d, axis = 0)

And let’s print this out so you can see it.

print(np_array_2d)

OUT:

[[1 2 3 4]]

Now, let’s duplicate the row.

np.repeat(a = np_array_2d, repeats = 2, axis = 0)

OUT:

array([[1, 2, 3, 4], [1, 2, 3, 4]])

As you can see, the output of np.repeat in this case is a new array with the row duplicated two times.

The np.repeat function repeats the individual *elements* of an input array.

But np.tile will take the entire array – including the order of the individual elements – and copy it in a particular direction.

So the difference is between copying the individual numbers verses copying the whole array all at once.

To learn more about np.tile, check out our tutorial about NumPy tile.

In this tutorial, we’ve strictly focused on the NumPy repeat function.

But if you’re serious about doing data manipulation in Python (i.e., for machine learning and deep learning), you really need to know more.

I recommend that you learn about:

- How to create NumPy arrays with np.arange
- How to change the shape of a NumPy arrays
- Appending new elements to a NumPy array
- How to concatenate NumPy arrays together

You’ll also want to learn about summary functions like np.sum, np.median, etc.

Moreover, even after you learn NumPy, you will probably need to learn a little about Pandas and matplotlib.

If you’re serious about learning NumPy (and Pandas, and matplotlib), then we can help.

Here at Sharp Sight, we teach data science.

We can show you how to do data manipulation, data visualization, data analysis, and machine learning in Python.

Want to learn more?

Sign up right now for our email list.

Members of our email list get immediate access to our free tutorials.

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

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

You’ll also get exclusive notifications when we open our premium data science courses for enrollment.

Want to learn data science?

Then sign up right now for our email list.

The post How to use numpy repeat appeared first on Sharp Sight.

]]>The post Numpy square root explained appeared first on Sharp Sight.

]]>This is a fairly easy NumPy function to understand and use, but for the sake of helping true beginners, this tutorial will break everything down. I’ll quickly review NumPy, I’ll explain the syntax, and I’ll show you some examples.

That being said, if you’re in a hurry, you can click on one of the links to the following sections and get what you’re looking for.

**Contents:**

- A quick review of NumPy
- Introduction to NumPy square root
- The syntax of np.sqrt
- NumPy square root examples
- NumPy square root FAQ

If you’re a real beginner or you have some time on your hands, I recommend that you read the whole tutorial. Everything will make more sense that way.

But if you’re in a hurry, click on 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 the Numpy square root function itself, let’s start with a quick review of NumPy.

The NumPy square root function is part of the NumPy module.

So what is NumPy?

NumPy is a toolkit for performing computing with numeric data in Python.

Base Python itself has many functions for working with numeric data, but Numpy has been carefully designed to work with large *arrays* of numbers.

What is a Numpy array?

A Numpy array is just a special data structure for storing numbers. You can think of a Numpy array as a grid of numbers.

In simple cases, these arrays can be 1-dimensional:

In more complicated cases, Numpy arrays can be 2-dimensional:

Numpy also supports 3D arrays, and higher dimensional arrays.

So what makes NumPy important is that it provides a variety of tools for creating arrays of numbers, manipulating those arrays, and performing mathematical computations on those arrays.

One tool for performing calculations on a NumPy array is the NumPy square root function.

So what does numpy.sqrt do?

To put it simply, the NumPy square root function calculates the square root of input values.

So if you give it an input , `numpy.sqrt()`

will calculate :

Critically though, the Numpy square root function also works on Numpy arrays.

So if you give it a Numpy array as an input, Numpy square root will calculate the square root of every value in the array.

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

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

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

When you initially import NumPy into your environment, you can simply do this with the code `import numpy`

. This will enable 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 it’s imported. It’s very common to import NumPy with the code `import numpy as np`

. This essentially gives NumPy the alias `np`

in your code, which enables you to use “`np.`

” instead of “`numpy.`

” when you call functions.

This is important, because how you import numpy will determine how you call it in your code.

Since it’s common to import NumPy with the `code import numpy as np`

, we’ll follow that convention in our syntax.

Just be aware though that `np.sqrt()`

and `numpy.sqrt()`

are essentially interchangeable, as long as you’ve imported NumPy the correct way, respectively.

Assuming that we’ve imported NumPy with the code `import numpy as np`

, we call call the NumPy square root function with the code `np.sqrt()`

.

Then inside of the function, there is a parameter that enables you to specify the input of the function.

Let’s quickly discuss this.

The NumPy square root function actually has two parameters, but we’re really only going to talk about one of them.

**Parameters:**

`x`

`out`

Let me quickly explain each of these separately.

`x`

(required)The `x`

parameter enables you to specify the input to the np.sqrt function.

Technically, the `x`

parameter enables you to specify the radicand. The radicand is the value under the radical when you compute the square root.

Keep in mind that the function is somewhat flexible in what types of inputs it will accept as arguments to 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 will work are things like Python lists and tuples.

If you provide a NumPy array or array-like input, numpy.sqrt will compute the square root of *each value in the array*.

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

Note that you *need* to provide an argument to the `x`

parameter, meaning that you need to provide *some* sort of input to the function … an integer, an array, a list. You need to provide something as an input.

`out`

The `out`

parameter enables you to specify an array where the output will be stored.

This parameter is not commonly used by beginners, so we’re not really going to work with it in this tutorial.

Ok, now that I’ve explained the syntax and the parameters, let’s take a look at some examples.

As I mentioned earlier, Numpy square root is very easy to use.

Having said that, I’m still going to start with the simplest possible example, and then increase the complexity as we move on to other examples.

**Examples:**

- Use np.sqrt on a single number
- Use np.sqrt with a 1-dimensional array
- Using np.sqrt with a 2-dimensional array

One quick note.

As I mentioned earlier, you’ll need to import NumPy into your environment in order for the code to work properly.

You can do that by running this code:

import numpy as np

This imports the NumPy module with the alias “`np`

“. As I mentioned previously, this is a common convention among NumPy users. Ultimately, it enables us to refer to NumPy as `np`

in our code, so the prefix “`np.`

” will be in front of the function name when we call a function.

First, let’s start simple.

Here, we’re going to apply the NumPy square root function to a single numeric input.

np.sqrt(9)

OUT:

3.0

So what happened here?

We called the function with the code `np.sqrt(9)`

. This is essentially the same as `np.sqrt(x = 9)`

, but we can chose not to explicitly type the `x =`

. The code works the same way in either case.

And what did it compute?

This is very simple.

Here, np.sqrt computed the square root:

As I’m sure you know, , so it gave us the output of 3.0.

Next, let’s use the function on a 1-dimensional “array” of numbers.

Actually, for the sake of clarity, I’m going to show you the example with a Python list.

Remember: numpy.sqrt will operate on any array-like structure.

I think that Python lists are a little easier to understand, because they make the syntax easier to understand. For the sake of completeness though, I’ll re-do this example later with a proper NumPy array.

Ok, let’s run the code. Here, we’re going to apply the np.sqrt function to the Python list `[0,1,2,3,4]`

.

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

Which produces the following output:

array([0. , 1. , 1.41421356 , 1.73205081 , 2. ])

So what happened here?

This is really simple.

Remember: when we apply np.sqrt to a list, it computes the square root for every element of the list.

So it takes the input values `[0,1,2,3,4]`

, computes the square root for each value, and produces the output in the form of a NumPy array.

That’s it!

Very quickly, let’s redo this example with a proper NumPy array as the input.

So, instead of using the list `[0,1,2,3,4]`

, we’re going to use a NumPy array that contains those values instead.

Let’s first create the array:

np_array_1d = np.arange(5)

If you print this out, you’ll see the contents:

print(np_array_1d)

OUT:

[0 1 2 3 4]

So `np_array_1d`

contains the same values as the list that we worked with earlier, but it’s in the form of a NumPy array. Note that you can check the object type with the code `type(np_array_1d)`

… which will show you that it’s a `numpy.ndarray`

.

Ok. So we now have our NumPy array, `np_array_1d`

.

Now, let’s use that as the input to `np.sqrt()`

:

np.sqrt(np_array_1d)

Which produces the output:

array([0. , 1. , 1.41421356 , 1.73205081 , 2. ])

Notice that this is the same output that we got for `np.sqrt([0,1,2,3,4])`

.

The point that I’m trying to make here is that np.sqrt will operate on either a list or a NumPy array … in fact, it will work on any array-like object.

Next, we’re going to use np.sqrt on a 2-dimensional array.

First, let’s just create the array.

np_array_2d = np.arange(10).reshape([2,5])

This new array, `np_array_2d`

, is a simple 2-dimensional array. It contains the numbers from 0 to 9, arranged in an array with 2 rows and 5 columns. We’re creating this array with the np.arange function, in conjunction with the NumPy reshape method.

You could print it out with `print(np_array_2d)`

, but visually, it looks like this:

Now, let’s use this array as the input to np.sqrt:

np.sqrt(np_array_2d)

Which produces the following output:

array([[0. , 1. , 1.41421356, 1.73205081, 2. ], [2.23606798, 2.44948974, 2.64575131, 2.82842712, 3. ]])

This is really easy to understand, if you’ve been paying attention to the previous examples.

When we use a 2D NumPy array as the input, the np.sqrt function simply calculates the square root of every element of the array.

The output of the function is simply an array of those calculated square roots, arranged in exactly the same shape as the input array. So if the input array has 2 rows and 5 columns … then the output array will have 2 rows and 5 columns.

So far, I’ve shown you examples of how the NumPy square root function operates on scalars, 1D arrays, and 2D arrays.

But keep in mind that it will also work on arrays of different sizes and shapes.

So you can apply this function to 3D arrays or higher-dimensional arrays and it works in essentially the same way: it computes the square root of each element.

Let’s quickly take a look at one commonly asked question about NumPy square root.

**Frequently asked questions:**

Not exactly.

Taking the square root of -1 with np.sqrt causes an warning message:

np.sqrt(-1)

RuntimeWarning: invalid value encountered in sqrt Out: nan

Essentially, NumPy is saying that the input is invalid. The code still runs, but the output is “nan” … meaning “not a number.”

Having said that, NumPy supports complex numbers, and the np sqrt function *does* operate on complex numbers.

np.sqrt(-1+0j)

OUT:

1j

Explaining this example is a little complex (heh heh), and it’s outside the scope of this tutorial.

Ultimately, you need to know that NumPy sqrt does not natively operate on negative numbers, and to get it to do so, you’ll need to use complex numbers.

Do you have other questions about NumPy square root?

Leave your question in the comments section below.

If you’re interested in data science in Python, you’ll have a lot to learn … a lot more than just the NumPy square root function.

You’ll need to learn more about NumPy, but also Pandas, matplotlib, and several other packages.

We have plenty of free tutorials here that you can check out.

**NumPy tutorials:**

- NumPy random seed explained
- An explanation of NumPy axes
- How to create a NumPy array filled with zeros
- How to use NumPy linspace
- Creating normally distributed data with np.random.normal

Those are just a few of our NumPy tutorials.

We also have tutorials about Pandas and matplotlib.

Ultimately though, if you want to regularly get our FREE tutorials, you should sign up for our email list.

Members of our email list are notified *immediately* when we publish new data science tutorials … we basically send the tutorials to your inbox.

If you sign up, you’ll get free tutorials on a variety of data science topics:

- NumPy
- Pandas
- Base Python
- Scikit learn
- Machine learning
- Deep learning
- data science in Python
- data science in R
- … and more.

So if you want access to our free tutorials, sign up right now:

The post Numpy square root explained appeared first on Sharp Sight.

]]>