SciPy – 4 – indicizzare e suddividere arrays

With indexing, you basically use square brackets [] to index the array values. In other words, you can use indexing if you want to gain access to selected elements -a subset- of an array. Slicing your data is very similar to subsetting it, but you can consider it to be a little bit more advanced. When you slice an array, you don’t consider just particular values, but you work with “regions” of data instead of pure “locations”.

Preparo gli arrays

ed ecco

Now that your mind is fresh with slicing and indexing, you might also be interested in some index tricks that can make your work more efficient when you’re going into scientific computing together with SciPy. There are four functions that will definitely come up and these are np.mgrid(), np.ogrid(), np.r_ and np.c_.

You might already know the two last functions if you already have some experience with NumPy. np.r and np.c are often used when you need to stack arrays row-wise or column-wise, respectively. With these functions, you can quickly construct arrays instead of using the np.concatenate() function.

e

By looking at the two first of these four functions, you might ask yourself why you would need a meshgrid. You can use meshgrids to generate two arrays containing the x- and y-coordinates at each position in a rectilinear grid. The np.meshgrid() function takes two 1D arrays and produces two 2D matrices corresponding to all pairs of (x, y) in the two arrays.

The np.mgrid() function is an implementation of MATLAB’s meshgrid and returns arrays that have the same shape. That means that the dimensions and number of the output arrays are equal to the number of indexing dimensions.

The np.ogrid() function, on the other hand, gives an open meshgrid and isn’t as dense as the result that the np.mgrid() function gives. You can see the visual difference between the two in the code chunk above.

You can also read up on the differences between these two functionshere.

Another function that you might be able to use for indexing/slicing purposes is the np.select() function. You can use it to return values from a list of arrays depending on conditions, which you can specify yourself in the first argument of the function. In the second argument, you pass the array that you want to consider for this selection process.

Awesome! Now that you have selected the right values of your original array, you can still select the shape and manipulate your new array.

Selezione di forma e manipolazione NumPy offers a lot of ways to select and manipulate the shape of your arrays and you’ll probably already know a lot of them. The following section will only give a short overview of the functions that might come in handy, so the post won’t cover all of them.

Now, is there such a thing as functions that are handy when you’re working with SciPy?

Well, the ones that are most useful are the ones that can help you to flatten arrays, stack and split arrays. You have already seen the np.c_ and np.r_ functions that you’ll often prefer instead of np.concatenate(), but there are many more that you want to know!

Like np.hstack() to horizontally stack your arrays or np.vstack() to vertically stack your arrays. Similarly, you can use np.vsplit() and np.hsplit() to split your arrays vertically and horizontally. But you’ll probably know all of this already.

Remember that the np.eye() function creates a 2X2 identity array, which is perfect to stack with the 2-D array that has been loaded in for you in the code chunk above.

If you want to know more about the conditions that you need to take into account if you want to stack arrays, gohere. However, you can also just look at the arrays and what the functions to do gather an intuition of which ‘rules’ you need to respect if you want to join the two arrays by row or column.

The most important thing that you need to take into account when splitting arrays is probably the shape of your array, because you want to select the correct index at which you want the split to occur.

Besides functions to stack and split arrays, you will also want to keep in mind that you have functions that help to ensure that you’re working with arrays of a certain dimension are indispensable when you’re diving deeper into scientific computing.

Note the difference between reshaping and resizing your array. With the first, you change the shape of the data but you don’t change the data itself. When you resize, there is the possibility that the data that is contained within the array will change, depending on the shape that you select, of course.

Besides the splitting and stacking routines and the functions that allow you to further manipulate your arrays, there is also something like “vectorization” that you need to consider. When you apply a function to an array, you usually apply it to each element of the array. Consider, for example, applying np.cos(), np.sin() or np.tan() to an array. You’ll see that it works on all array elements. Now, when you see this, you know that the function is vectorized.

But, when you define functions by yourself, as you will most likely do when you’re getting into scientific computing, you might also want to vectorize them. In those cases, you can call np.vectorize():

When it comes to other vectorized mathematical functions that you might want to know, you should consider np.angle() to provide the angle of the elements of complex array elements, but also basic trigonometric, exponential or logarithmic functions will come in handy.