How To Create An Empty Numpy Array
This tutorial will explain the NumPy empty function (AKA np.empty) and will show you how to create an empty array in NumPy.
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 ….
A quick review of NumPy
First, let's quickly review NumPy.
What is NumPy?
NumPy is a toolkit for working with arrays of numbers
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:
NumPy arrays have a shape
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.
NumPy provides many tools for creating numeric arrays
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.
A quick introduction to NumPy empty
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.
NumPy empty produces arrays with arbitrary values
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.
NumPy empty enables you to create arrays of a specific shape
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
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
.
np.empty syntax
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.
The parameters of np.empty
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.
Examples of how to create an empty numpy array
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
Run this code first
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.
EXAMPLE 1: Create a 1-dimensional empty NumPy array
First, we're just going to create a simple 1-dimensional NumPy array.
Code
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!)
Explanation
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.
Redo example without explicit parameter
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.
EXAMPLE 2: Create a 2-dimensional empty NumPy array
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.)
Explanation
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.
EXAMPLE 3: Specify the data type of the empty NumPy array
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.
Code
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.)
Explanation
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.
Frequently asked questions about NumPy empty
Now that you've seen some solid examples, I want to address some frequently asked questions.
Frequently asked questions:
- How do I create an empty numpy array?
- Why isn't my array "empty"?
How do I create an empty numpy array?
Respectfully: read the f^*king tutorial.
Why isn't my array "empty"?
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.
Leave your other questions in the comments below
Do you have more questions about NumPy empty?
Leave your question in the comments section below.
Learn more about NumPy
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.
For more tutorials, 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.
Check your email inbox to confirm your subscription ...
How To Create An Empty Numpy Array
Source: https://www.sharpsightlabs.com/blog/numpy-empty/
Posted by: terrellsuaing.blogspot.com
0 Response to "How To Create An Empty Numpy Array"
Post a Comment