NumPy - The very basics!

Sujan Shirol
Analytics Vidhya
Published in
5 min readAug 12, 2020

This article is for people who have zero knowledge of NumPy so that they can get a little hang of it to kick start.

NumPy is the package for scientific and mathematical computing in Python. While NumPy is widely used in an assortment of routines for fast operations on arrays, including mathematical, logical, shape manipulation, sorting, selecting, I/O, discrete Fourier transforms basic linear algebra, basic statistical operations, random simulation and much more we will, however, today scratch the very basics of NumPy.

Numpy basics
Source-https://numpy.org

Entire article I will use code from Jupyter Notebook and assuming you already know basics of Python and installed NumPy.

We will start by importing NumPy.

In[1]: import numpy as np

Dimensions of NumPy array

Dimensions of NumPy array
Source-https://fgnt.github.io/python_crashkurs_doc/include/numpy.html

One dimensional array

Let us create an array using a python list. ( Not a recommended way )

In[2]: np.array([1,2,3,4,5])
Out[2]: array([1,2,3,4,5])

One very important property of NumPy which more like a constraint is, NumPy array can have only a single data type. Meaning, one cannot have an array of mixed data types. The moment you try to achieve that NumPy will implicitly try to upcast where possible. Below code, we can see that even though we have integers they are automatically upcasted to string my NumPy.

In[3]: print(np.array([1,2,’Hello’,3,4])Out[3]: ‘1’,’2’,’Hello’,’3’,’4’

Two-dimensional array

NumPy array can be multidimensional also. Here is an example of a 2x5 matrix.

In[4]: np.array([[1,2,3,4,5], [1,2,3,4,5]])Out[4]: array([[1,2,3,4,5],
[1,2,3,4,5]])

Creating arrays using built-in routines

Numpy basics

NumPy Attributes

  • 1. ndim - Number of dimensions.
  • 2. shape - The size of each dimension.
  • 3. size - The total size of the array.
  • 4. dtype - The data type of array elements.
  • 5. itemsize - Byte size of each array element.
  • 6. nbytes - Total size of the array. It is equal to itemsize times size.

Let’s create a 3-dimensional array of shape (3,4,5) using random variables and examine each attribute.

In[5]: a=np.random.randint(10, (3,4,5))       print(a.ndim)       print(a.nshape)       print(a.size) #Multiplication of shape values       print(a.dtype)       print(a.itemsize)       print(a.nbyte)Out[5]: 3       (3,4,5)       60       Int64       8 bytes       480 bytes

Array Indexing

If you have come this far, I’m sure you are well aware of lists and it’s indexing. Array indexing is quite familiar. Let’s dive into examples.

In[6]: a=np.array([4,8,3,0,1,5])       a[0]Out[6]: 5In[7]: a[4]Out[7]: 1

We use negative indices to index from the end of the array.

In[8]: a[-1]Out[8]: 5In[8]: a[-2]Out[8]: 1

Negative indexing might be confusing some times. So, I have a visual representation below to give a clear idea. Another point to be noted while negative indexing is it starts from 1 rather than 0.

In a multidimensional array, you access items using comma-separated indices.

In[9]: a=np.array([[2,3,5,8,7],                   [4,1,0,9,6],                   [6,3,4,0,6]])       # 1st element of 1st list or element at position 0,0       a[0 , 0]Out[9]: 2In[10]: a[2, -1]   #Last element of last listOut[10]: 6

Array slicing

As we use of square brackets for extracting elements of the array we can use it for extracting subarray also with a combination of the colon(:). The syntax is similar to range function except we use square brackets here.

array_variable[start : stop: step]

The default values for start is 0, stop is size of dimension and step is 1

In[11]: x = np.arange(10)        xOut[11]: array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
In[12]: x[:5] #first five elementsOut[12]: array([0, 1, 2, 3, 4])
In[13]: x[5:] # elements from index 5Out[13]: array([5, 6, 7, 8, 9])
In[14]: x[4:7] # subarrayOut[14]: array([4, 5, 6])
In[15]: x[::2] # every other elementOut[15]: array([0, 2, 4, 6, 8])
In[16]: x[1::2] # every other element, starting at index 1Out[16]: array([1, 3, 5, 7, 9])
In[17]: x[::-1] # all elements, reversedOut[17]: array([9, 8, 7, 6, 5, 4, 3, 2, 1, 0])
In[18]: x[5::-2] # reversed every other from index 5Out[18]: array([5, 3, 1])

Multidimensional array slicing also works the same way the only difference is we need to use comma(,) to separate rows and columns.

In[19]: a=np.array( [[2,3,5,8,7],                    [4,1,0,9,6],                    [6,3,4,0,6]] )       # 2 rows 3 columns       a[ :2, :3]Out[19]: array( [[2,3,5,8,7],                 [4,1,0,9,6]] )In[20]: a[ :3, :2] # 3 rows 2 columnsOut[20]: array( [[2,3],                [4,1],                [6,3]] )

The next one is a bit tricky. We will reverse the element from both rows and columns. Compare the original array with the below output.

In[21]: a[ ::-1, ::-1]Out[21]: array([[6, 0, 4, 3, 6],                [6, 9, 0, 1, 4],                [7, 8, 5, 3, 2]] )

One way where array slicing is different from list slicing is, the array slicing returns a view of the original array unlike in the list it returns the copy. So, any modification in the sliced subarray also reflects in the original array.

Array concatenation and splitting

Joining or concatenating array in NumPy is done by using np.concatenate, np.vstack, np.hstack, np.dstack. Don’t be worried by the sophisticated names “stack”, it's very simple operations here.

In[22]: a= np.array([ 1,2,3,4 ])        b=np.arrar([5,6,7,8])        c=np.array([[9,8,7,6],                   [5,4,3,2]])        d=np.array[([5,6],                    [4,7]])        np.concatenate([a,b])Out[22]: array([1,2,3,4,5,6,7,8])
In[23]: np.vstack([a,c])Out[23]: array([[1,2,3,4 ], [9,8,7,6], [5,4,3,2]])
In[24]: np.hstack([c,d])Out[24]: array([[9,8,7,6,5,6], [5,4,3,2,4,7]])
#np.dstack is used on three-dimensional array

The same goes for array splitting. We use np.split, np.vsplit and np.hsplit. All these work exactly opposite of concatenate.

In[25]: x = [1,2,3,99,99,3,2,1]       x1, x2, x3 = np.split(x,[3,5])       print(x1, x2, x3)Out[25]: [1 2 3] [99 99] [3 2 1]
In[26]: a=np.arange(16).reshape((4,4)) aOut[26]: array([[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11],
[12, 13, 14, 15]])
In[27]: upper,lower = np.vsplit(a,[2]) print(upper) print(lower)Out[27]: [0 1 2 3] [4 5 6 7]
In[28]: left, right= np.hsplit(a,[2]) print(left) print(right)Out[28]: [[0 1]
[4 5]
[8 9]
[12 13]]
[[2 3]
[6 7]
[10 11]
[14 15]]

So that's it for basics of NumPy. Again this is for people with zero knowledge of NumPy. I will be posting more advanced concepts of NumPy.

--

--