Hitchhiker Tips on Effectively using Python NumPy Arrays | by Deepak K [Daksh] Gupta | Towards Data Science
Clip source: Hitchhiker Tips on Effectively using Python NumPy Arrays | by Deepak K [Daksh] Gupta | Towards Data Science
If you’re using python for data science, either you have used NumPy or must have heard about it. Most of the statistical analysis which needs data to be stored in memory uses NumPy.If you’re new to NumPy, I’ll highly recommend reading my earlier post A hitchhiker guide to python NumPy Arrays to get a basic idea about the usage of NumPy.In this post, I’ll talk about some of the specific tips, tricks and techniques which will be help you in getting most of the NumPy arrays
1. Modifying existing NumPy Arrays
Unlike Python lists, NumPy doesn’t have a append(...) function which effectively means that we can’t append data or change the size of NumPy Arrays.For changing the size and / or dimension, we need to create new NumPy arrays by applying utility functions on the old array.Syntactically, NumPy arrays are similar to python lists where we can use subscript operators to insert or change data of the NumPy arrays. As an example, for a NumPy array of size 5, we can use loops like while and for to access / change / update the contentsChanging individual events of NumPy arrayUnderstandably, this is very old fashioned and non functional way of accessing / manipulating the data. Fortunately, NumPy provides bulk access functions for the same which does the same things effortlessly in one line. One such function is numpy.put(...) .To depict the usage of numpy.put(...) , we’ll create a python list and copy the contents of that list to the NumPy arrayUsing numpy.put(…) functionThe numpy.put(...) function takes a range and then use the same to copy the source from the destination. The copy will be limited to the number provided in range(..) function.In the example above, if we change the np.put(...) line asnp.put(np_arr, range(3), py_arr)It will change just the first three elements of np_arr and leave everything else untouchedThe applicability of np.put(...) function is not limited to copying 1-D arrays, but can also be used to copy n-D arrays as depicted in the code belownumpy.put(…) for multi dimensional arrayWith np.put(...) function, we can provide a list of numbers, however, there are some conditions, where we want to copy same numbers to all the elements of NumPy. This is similar to calling np.zeros(...) or np.ones(...) , with a difference that along with zero and one, other numbers can also be provided
This is achieved by calling np.fill(...) function.Whatever number we provide inside the fill(..), it be applied to all elements of NumPy array irrespective of whether that the array is a single dimension array or a multi dimension arrayUsing numpy.fill(…) functionThe .fill(..) function takes only scalar values.The example depicted here is of dtype=np.int . It’s also applicable for all other data types like np.float, np.str, np.object etc
2. Slicing NumPy Arrays
If you’ve experienced slicing python list, you’ll feel at home over here.
Slicing NumPy arrays come with additional functionalities and becomes interesting in case of multi dimension arrays. However, to properly understand how slicing works, we need to first look into single dimension arrays before looking into multi dimension arrays.
The : is called the separator and works with Inclusive — Exclusive principal, which means that the left of the separator shall be included while right of the separator will be excluded.For example, a statement like np_arr[1:4] means it will include the Index → 1, but exclude the Index → 4. So this will effectively display elements from Index → 1,2 & 3Here are some examples of slicing single dimension arrays and their expected outputsSlicing 1-D arraysOne unique functionality of slicing present with NumPy arrays, but can’t be used with python list is the ability to change multiple elements of the array in-place with a value.To be honest, this is one of the extremely valuable functionality and helps in both maths and machine learning. Here is how you use this functionalitySlicing 1-D arrayNow let’s look in slicing the multi dimensional arrays. When we slice multi dimension arrays, the meaning of left and right of the separator remains same (i.e Index) but we extend it by using two sets of comma separated separator which represents Rows and Columns i.e[rows → Start_Index : End_Index, Column → Start_Index : End_Index ]Here are some examples on what slicing means in case of 2D arraysSlicing a 2 — D ArraySimilarly with a 3D array, we’ll have to add one more comma separated value for a dimension. Here are some examples of the sameSlicing a 3-D Array
3. Splitting the NumPy Arrays
At some point of time, it’s become necessary to split n-d NumPy array in rows and columns. There are multiple functions and ways of splitting the numpy arrays, but two specific functions which help in splitting the NumPy arrays row wise and column wise are split and hsplit .
splitfunction is used for Row wise splittinghsplitfunction is used for Column wise splitting
The simplest way of splitting NumPy arrays can be done on their dimension. A 3 * 4 array can easily be broken into 3 sets of lists for Rows and 4 set of lists using Columns. Here is how we can do the sameNumPy Split Row and Column wiseWe can provide a different number than the available dimension for splitting, provided the multiplicity factor allows the division. For example, we can divide the array column wise into 2 sets as
Multiple uneven Splits
In the example above, we provide a number to equally split the NumPy array row wise or column wise.
However, it’s possible to do uneven splits on different rows and columns by providing the exact index on which it needs to be split.
It works just like slicing as we’ve seen above. Based on the index provided in the list, these split functions will slice either Rows or Columns accordingly.Let’s assume that we have the same 2D array as displayed above and I want to do custom splitting.Case — 1: Divide 3 Rows into [1] and [2,3]In here, I want to divide the rows such that it divides it into two where we extract the first row and then the combination of second and third row as one. Here is how we’ll do thatSplitting RowsCase — 2: Divide 4 Columns into [1], [2,3], and [4]In here, I want to divide the columns such that it divides into three where we extract first column, followed by a second + third column, and finally with a fourth column. Here is how we’ll do thatSplitting ColumnsSo these were few tips of NumPy which may help you in using it in better ways.
That’s all for this particular story. More on next set of stories.
Thanks for Reading…!!!Daksh