• Tidak ada hasil yang ditemukan

NumPy Arrays — Indexing & Slicing

N/A
N/A
Protected

Academic year: 2023

Membagikan "NumPy Arrays — Indexing & Slicing"

Copied!
46
0
0

Teks penuh

In this tutorial, we go through how to use NumPy to analyze data on Starbucks Menu. Here we will learn how to work with NumPy, and we will try to find out the nutrition facts for Starbucks menu. And then we will divide the sum of all the elements in qualities by the total number of elements in qualities to get the average.

In this tutorial, we'll dive into the different types of multidimensional arrays, but for now we're focusing on 2-dimensional arrays. If we select the element in the third row and second column, we will get 0.1 here. So that's the basics of arrays, now we'll see how we can get from our list of lists to a NumPy array.

We'll then pass the list of Starbucks lists to the array function, which converts it to a NumPy array. Then we'll specify the dtype keyword argument to ensure that each element is converted to a float.

Alternative NumPy Array Creation Methods

Reading Text Files

Next, we specified the keyword argument delimiter as "," so that the fields are parsed properly. Here, if we read it into a list and then convert it into an array of floats, the Starbucks will look the same. Here, NumPy will automatically choose a data type for the elements in an array based on their format.

Slicing

So how we can index and slice the created NumPy arrays to retrieve results from them. Suppose if we want to select the fifth column, the index is 4, or if we want to select data with 3 rows, the index is 2, and so on. A colon when segmenting indicates that we want to select all elements from the start index to the end index, but here we do not include the end index.

And suppose we want to select the entire column, then by using only the colon (:), without a start or end index, we will get the desired result. And suppose we want to select the entire array and then use two colons to select all the rows and columns.

Multi-Dimensional NumPy Arrays

Did you notice that when we chopped up the Starbucks data, we created that one right there? Do you know that each row and column in a two-dimensional array is treated as a one-dimensional array. As for a list of lists, analog is a two-dimensional array, similarly for a single list, analog is a one-dimensional array.

Here we assume that we expect Starbucks data and get only the fifth row, then as a result we will get a 1-dimensional array. And suppose if we want to retrieve an individual element from Fifth_starbucks, we can do so using a single index. Even most of the NumPy functions, like numpy.random.rand, that we've used.

For a better understanding of this, let's take an example of a supermarket's monthly profit. The data of the month visa will be in the form of a list and if we want a quick look at it, then we can see that data in a quarter visa and year visa. But here if we add another year's profit, then it will become the third dimension.

Here we can get the earnings for the first year of the month of January by calling Yearly_Earning[0],[0],[0]. So here we need three indexes to retrieve a single element. We have the same case in a three-dimensional array in NumPy, in fact, we can convert this. Yearly_Earning to an array and then we can get the earnings for January of the first year.

Here, in a three-dimensional matrix, indexing and cutting also work exactly the same way as a two-dimensional matrix, but here we need to move to one additional axis. If we add more dimensions, we can make it much easier to query our data, since it will be organized in a.

NumPy Data Types

NumPy provides different data types, which are aligned with Python data types, such as float and str.

Converting NumPy Data Types

We even have additional data types with a suffix that indicates the bits of memory that that data type can occupy. With the output, we can observe that all the elements of the resulting array are integers. Here the array has been converted to a 32-bit integer data type, which means it will store the values ​​as 32-bit integers.

If you want more control over the way the array is stored in memory and allows for very long integer values, then we can directly create dtype NumPy objects like e.g.

NumPy Array Operations

One of the important advantages of using NumPy is that it is easy to perform the calculation.

Single Array Math

Let's say we want to multiply each Nutrition_Value by 2, which we could do like this:.

Multiple Array Math

The first array first element is added to the second array first element, the first array second element to the second array second element, and so on. Let's say we want to choose a drink that is full of fat with nutritional value, then we need to multiply Total fat, Protein and nutritional value, and later we can choose the drink with the highest score.

Broadcasting

The comparison is possible here because the array X has the trailing dimension length as 5 and the array Y has the trailing dimension length as 5. Here the last dimension of both arrays matches and Array X's first dimension has length 1. Here the last dimension of both arrays matching. , in this example neither the lengths of the dimensions are equal, nor do any of the arrays have dimension length equal to 1.

Since the two arrays do not have a matching back dimension, the example above did not work. Elements of random_Example_array are broadcast across each row of Starbucks, so the first column of Starbucks has the first value in random_Example_array added, and so on.

NumPy Array Methods

Here, as a keyword argument to the sum method, we can also pass the axis to find sums over an axis. Suppose if we call the sum over our Starbucks array and pass the axis as 0, we can find the sums over the first axis of the array. You would have understood this because the sums over the first axis would give us the sum of each column, or another way to think about this is that the specified axis is the one that 'goes away'.

Here, if we specify in axis=1, then it will find the sum over the second axis of the array.

NumPy Array Comparisons

Subsetting

Reshaping NumPy Arrays

This function helps in reshaping an array into a particular shape as per our requirements. Think of this this way that the items from the second array are added to the first array as new rows. Let's take an example where we want to combine the old Starbucks drinks nutrition dataset with ours.

Here we can see that we have attributes for 196 drinks, we have starbucks_old data, we can combine all the wine data. Now we will combine the starbucks and starbucks_old data using the vstack function, and then we will display the format of the result. So, here we can see that the result has 2084 rows which is the sum of the number of rows in starbucks and.

When we concatenate along the first axis it is similar to vstack, and when we.

Conclusion

Referensi

Dokumen terkait

Time budget calculations based on 20 scans Altmann 1974 at 5 min apart, of one to seven birds present over a 1.5 h period 21:00 - 22:55 local apparent time, interrupted by 20 min when

Tujuan Kegiatan pengabdian ini di lakukan di Wilayah Kerja Puskesmas Kebon Kopi dengan tujuan dari kegiatan ini adalah memberikan informasi dan meningkatkan pengetahuan, serta