Python : NumPy Arrays

Let me show you how to create a NumPy array from the python object such as the list.

In [1]: my_list = [1,2,3]
In [2]: my_list
Out[3]: [1,2,3]

The above is a simple list output which I have created several times throughout these blog series. In order to work with NumPy arrays, you will need to import the NumPy library in order to proceed forward.

In [4]: import numpy as np
In [5]: arr = np.array(my_list)
In [6]: arr
Out[7]: array ( [1,2,3])

Now, we have created a one dimensional array by casting a normal python list into an array using NumPy. In order to create a two dimensional array we will have to cast a list of list within the NumPy array.

In [8]: my_mat = [[1,2,3],[4,5,6],[7,8,9]]
In [9]:np.array(my_mat)

If I want to generate a range of values I can use the np.arange method ( a range and not arrange ) which is a builtin value generation method within the NumPy library.

In [11]:np.arange(0,10)

In the above example 0 is the starting point and 10 is the ending point. So the values are 0 to 9 which accounts for 10 digits. If the starting point is 1 and ending point is 10 – how many digits are retrieved ? Leave your answers in the comment section below.

If we want to create a one dimensional array of numbers between 0 and 5 with 10 evenly spaced points between 0 and 5, we can make use of the np.linspace method.

In [13]:np.linspace(0,5,10)
Out[14]:array([ 0. , 0.55555556, 1.11111111, 1.66666667, 
        2.22222222, 2.77777778, 3.33333333, 3.88888889, 4.44444444, 5. ])
Though the above example looks like a 2 dimensional array, its actual one dimensional.There is only one square bracket in the output array.
Note : The difference between the third parameter in arange and linspace is that in arange the third parameter will be the step size and in linspace it will be the number of points you want between the start ( first parameter ) & end ( second parameter ).
Lets now create some random numbers using the NumPy arrays, this is the most useful and widely used feature to generate random numbers for working with or testing data science driven problems.

In [15]:np.random.rand(5,5)
Out[16]:array([[ 0.77137502,  0.28137699,  0.06032922,  0.86411406,  0.18949888],
       [ 0.82909673,  0.49042171,  0.80491907,  0.0319554 ,  0.96599774],
       [ 0.37228731,  0.77284343,  0.99227889,  0.29140901,  0.69968362],
       [ 0.51595908,  0.55724643,  0.11668673,  0.80466184,  0.53639437],
       [ 0.03324634,  0.31724955,  0.88069452,  0.78311061,  0.00182403]])

The distribution above is uniform, if in case we need normal distribution centered around 0 of randomly generated numbers we can make use of np.random.randn(3,3).

In [17]:np.random.randn(3,3)
Out[18]:array([[-1.73848932,  1.05867812, -0.18784838],
       [ 1.09207573,  0.59035732, -0.29759153],
       [ 0.50540417,  0.21646846, -0.40956973]])

In the above examples we generated random integers using a distrubution logic ( Uniform / Normal / Gaussian ). If we need 10 random integers between 0 to 100 we can make use of np.random.randint()

In [19]: np.random.randint(0,100,10)
Out[20]: array([69,35,34,43,19,18,79,93,71,82])
So we get 10 random integers between 0 ( included ) and 100 ( excluded ). If we want to reshape the array, we can use the reshape method as shown below :
In [21]: arr=np.arange(4)
In [22]: arr
Out[23]: array([0,1,2,3])
In [24]: arr.reshape(2,2)
Out[25]: array([[0,1],
Make sure that the reshape size fits the number of elements in your array and no element is missed out based on the size you choose. A reshape of (2,1) will not hold good as the 4th element will be left out in reshape.
If we want to find out the data type of the elements in our array, what would be the line of code ? Leave the answer in the comment section below.


Python : Introduction To NumPy

Num Pi or Num P as it is commonly referred to is a Linear Algebra library for python. Almost all of the libraries in the PyData ecosystem reply on NumPy as one of their main building blocks. NumPy is incredibly fast.

The NumPy library can be installed using pip install command. In case you are new to installing libraries in python, I would recommend you to visit this link to get a deeper understanding on how to install libraries in python.

  • NumPy arrays will be widely used while working with data.
  • NumPy arrays essential come in two flavors : vectors and matrices.
  • NumPy Vectors are strictly 1 dimensional arrays and matrices 2 dimensional.
  • A matrix however can have just one row or just one column.

This should be sufficient for you to get started with NumPy. Let’s get started with using NumPy arrays in python using jupyter notebook.

Python : Installing The NumPy Package

How to install the numpy package within the python shell on a windows machine in less than 10 steps !

Unlike ‘ R ‘ the installation of packages in Python is slightly different.

Step1 : Open a command line window ( START > RUN > CMD )

Microsoft Windows [Version 10.0.14393]
(c) 2016 Microsoft Corporation. All rights reserved.


Step 2 : Navigate to the ” Python Scripts ” directory as shown below

C:\Users\harsh> CD C:\
C:\> CD Python27
C:\Python27> CD Scripts

Step 3 : make sure you are connected to the internet and then type pip install numpy

C:\Python27\Scripts> pip install numpy
Collecting numpy
 Downloading numpy-1.13.3-2-cp27-none-win32.whl (6.7MB)
 100% |################################| 6.7MB 85kB/s
Installing collected packages: numpy
Successfully installed numpy-1.13.3

Step 4 : Import numpy like you would import any other installed packages in Python on the Python Shell ! make sure numpy is all lower case else the package will not load and the shell will return an error.

Hope you found this post easy enough to get started with numpy.