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) Out[10]:array([[1,2,3], [4,5,6], [7,8,9]])

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) Out[12]:array[0,1,2,3,4,5,6,7,8,9]

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.

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 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])

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], [2,3]])