Posts

Showing posts with the label NumPy

Array Slicing using NumPy

When I started working on a new classification project, I had to slice data array to get the desired result. That’s when I realized I have not understood array slicing completely. That prompted me to get my hands dirty on array slicing and I’ll share my learning in this post. We need to use numpy library to play around arrays. So I’ll first import numpy. import numpy as np Next, I’ll create a 2-d array with random numbers. arr2d = np.random.randint(10, size=(4, 5)) This is how the array looks like array ( [[3, 7, 3, 2, 0] ,       [8, 1, 6, 1, 9] ,       [3, 3, 3, 8, 2] ,       [5, 9, 5, 1, 3] ]) Essentially, there are two parts while describing this array. arr2d [rowFrom:rowTo-1, columnFrom:columnTo-1] The first section tells numpy how many (or which) rows we are interested in, and the second section tells numpy how many (or which) columns we are interested in. If I don’t mention any values in those sections like this: arr2d [:,:] It sho...