Intro to NumPy
Last updated
Was this helpful?
Last updated
Was this helpful?
NumPy is a commonly used package that we will use to manipulate a new data type called arrays. This data type is very similar to lists. One of the reasons we use the NumPy package is that it can be faster and more efficient to perform operations on NumPy arrays than lists. To use arrays, we must first import NumPy.
A common way to import NumPy is to import the whole package, and to give it the abbreviation np
.
Now that you can access the NumPy package, we will use it to create arrays.
Arrays are a data structure similar to lists. To create an array, you must input your data into a list, then you can convert it into an array by calling np.array(list)
. If you only want to input one data piece into the array function, you can skip the list step.
Arrays can only hold data of the same type. If you enter different data types into an array, then it will convert them all to the same type if it can. For example, if you input an int and a float into an array, then it will convert them all to floats, so that they have the same data type.
The output of the array showsdtype='<U21'
this means that the data type (dtype) within your array is string.
The items of the array must all be of the same type, because it makes storing the information simpler. In comparison to lists, arrays require less space / memory to save them.
To access the data within an array, we use a method similar to lists, but we use a function called .item()
which must be called on an array. We call .item()
on our array, and we input the index of the data we want to access within the parentheses.
Arrays are indexed starting at zero just like lists, meaning that the first item of our array is numbered 0, not 1. If you input an index larger than the size of the array, then it will throw an error.
Another difference between arrays and lists is that arrays have a fixed size. This means that it takes longer to add an item to an array than to a list.
To add into an array, there are two ways we can add. We can add elements into a specific index within an array or we can add elements to the end of the array. If you only want to add to the end of an array, then the easiest way is to use the function called np.append(...)
.
np.append()
The append method lets us add on to the end of an array. It has 2 arguments. The first is the array you want to add to, and the second argument is the item(s) you want to add to the existing array.
Here are some examples of adding to the ends of arrays.
When we append to an array, the changes do not affect the original array. We have to save the changes we make by assigning the result of appending to a new array. If we assign the result to the same array, we will not have access to the original array anymore.
The part that you add on to your array when using np.append()
can be an array or list. It does not have to be a single item.
Be careful what you add on to your array, because the type of data you enter may change all of the pieces in your array.
If you are not adding to the end of an array, it is best to use np.insert(...)
which will allow you to modify the array at any index.
np.insert()
The insert method allows us to add items into any index within an array. It has 3 arguments. The first argument is array you want to add to. The second argument is the index you want to add your data to. The index you input is the index that the value you are adding will have. If you are adding multiple values, then the first will have that index and the rest will follow. The third argument is the value(s) you want to add.
Here are some examples of adding a single item into an array.
Just likenp.append(),
when we append to an array, the changes do not affect the original array, so we must define a variable.
We can add to any index in our array. In this case we can choose any number from 0 to 8. In this case, if we use 8, it is the same as ending to the end of the array.
The benefit to using np.append()
instead of np.insert()
when you are adding to the end of an array is that you do not need to know the exact size of your array with np.append()
, while you will need the size each time you use np.insert()
.
Let's insert multiple items into our array.
Be careful what you insert into an array, because the type of data you enter may be changed to fit the types of the data already in the array. Or it will error.
We mentioned earlier that arrays can only hold elements of the same data type. As the example above shows, if you try to insert an integer into an existing array containing string, it will error - the data type of the element you are adding has to be the same as the data type of elements in the array.
To delete items within an array we will use np.delete()
. It takes in 2 arguments. The first argument is the array you want to change, and the second is the index of the item you want to remove.
Let's look at some examples.
Just likenp.append()
andnp.insert()
when we modify our array it does not affect original array, so we must define a variable to save the changed array.
Now that we understand how to remove a single item, we approach removing multiple items.
Now, you can create and manipulate arrays!