Arrays In Numpy

An array is simply an representation of a list in one dimension or multi dimension form. When arrays are 1-D they are called vectors and when they are 2-D they are called matrices . Numpy is very famous for its dealing with arrays and is often used only for that.

Before we start using numpy , we must import it in our code. This can be done in the following manner :

import numpy as np

Since numpy is imported as np , all the methods in numpy can be accessed through np.
Creating an array

Let us start creating array from an python list object. In the example below, i have a list and i’ll be using the array() method in numpy to convert it to an array.

import numpy as np  my_list = [1,2,3,4,5]  my_arr = np.array(my_list)  print(my_arr)

Output :
[1 2 3 4 5]

You might be wondering how is this any different from a normal python list. You’ll start noticing the diference when you actually convert multiple lists into array to really get 2-D shape. Lets do just that in the following code .

import numpy as np  my_arr = np.array([[1,2,3,4],[5,6,7,8],[9,10,11,12]])  print(my_arr)

Output :
[[ 1  2  3  4]   [ 5  6  7  8]   [ 9 10 11 12]]

Notice the two square brackets at the start and end of the array. Two square brackets represents an 2-D array , similarly three square brackets represents an 3-D array.
Methods for array’s

Let us take a look at some very important and popular numpy methods used for array creation and manipulation.

arange() :

The arange() method in numpy is very similar to our very dearly range function . It basically generates integers in the given range and stores them in form of an 1-D array. It has the following syntax:

  • np.arange( start , stop , step )

Let us take an example on this method.

import numpy as np  my_arr  = np.arange(0,500,2)  print(my_arr)

Output :
[  0   2   4   6   8  10  12  14  16  18  20  22  24  26  28  30  32  34    36  38  40  42  44  46  48  50  52  54  56  58  60  62  64  66  68  70    72  74  76  78  80  82  84  86  88  90  92  94  96  98 100 102 104 106   108 110 112 114 116 118 120 122 124 126 128 130 132 134 136 138 140 142   144 146 148 150 152 154 156 158 160 162 164 166 168 170 172 174 176 178   180 182 184 186 188 190 192 194 196 198 200 202 204 206 208 210 212 214   216 218 220 222 224 226 228 230 232 234 236 238 240 242 244 246 248 250   252 254 256 258 260 262 264 266 268 270 272 274 276 278 280 282 284 286   288 290 292 294 296 298 300 302 304 306 308 310 312 314 316 318 320 322   324 326 328 330 332 334 336 338 340 342 344 346 348 350 352 354 356 358   360 362 364 366 368 370 372 374 376 378 380 382 384 386 388 390 392 394   396 398 400 402 404 406 408 410 412 414 416 418 420 422 424 426 428 430   432 434 436 438 440 442 444 446 448 450 452 454 456 458 460 462 464 466   468 470 472 474 476 478 480 482 484 486 488 490 492 494 496 498]

zeros() and ones() :

Often it is required to create an matrices made up of zeros and ones. This is where the zeros() and ones() method are used. While using this method , the dimension of matrices must be passed in form of a tuple. Let us take an example on this.

import numpy as np  my_arr_1 = np.zeros((2,5))  my_arr_2 = np.ones((5,5))  print(my_arr_1)  print() # to give a gap of one line  print(my_arr_2)

Output :
[[0. 0. 0. 0. 0.]   [0. 0. 0. 0. 0.]]    [[1. 1. 1. 1. 1.]   [1. 1. 1. 1. 1.]   [1. 1. 1. 1. 1.]   [1. 1. 1. 1. 1.]   [1. 1. 1. 1. 1.]]

linspace() :

linspace() generates equally separated numbers in a given range . It has the following syntax :

  • np.linspace( start_value , stop_value , numbers_needed )

Note that in linspace , the stop value is also included in the range unlike the range function where the stop value is not included in the range .
import numpy as np  my_arr_1 = np.linspace(1,5,40)  my_arr_2 = np.linspace(1,20,40)  print(my_arr_1)  print()  print(my_arr_2)

Output :
[1.         1.1025641  1.20512821 1.30769231 1.41025641 1.51282051   1.61538462 1.71794872 1.82051282 1.92307692 2.02564103 2.12820513   2.23076923 2.33333333 2.43589744 2.53846154 2.64102564 2.74358974   2.84615385 2.94871795 3.05128205 3.15384615 3.25641026 3.35897436   3.46153846 3.56410256 3.66666667 3.76923077 3.87179487 3.97435897   4.07692308 4.17948718 4.28205128 4.38461538 4.48717949 4.58974359   4.69230769 4.79487179 4.8974359  5.        ]    [ 1.          1.48717949  1.97435897  2.46153846  2.94871795  3.43589744    3.92307692  4.41025641  4.8974359   5.38461538  5.87179487  6.35897436    6.84615385  7.33333333  7.82051282  8.30769231  8.79487179  9.28205128    9.76923077 10.25641026 10.74358974 11.23076923 11.71794872 12.20512821   12.69230769 13.17948718 13.66666667 14.15384615 14.64102564 15.12820513   15.61538462 16.1025641  16.58974359 17.07692308 17.56410256 18.05128205   18.53846154 19.02564103 19.51282051 20.        ]

eye() :

When dealing with matrices , identity matrix is created all the times and this is where eye() method will be used.

Note that all identity matrices are square matrix , meaning that the number of rows is equal to number of columns. So the eye() method takes only one integer( say n ) as argument and creates an n*n identity matrix.
import numpy as np  my_arr_1 = np.eye(5)  my_arr_2 = np.eye(3)  print(my_arr_1)  print()  print(my_arr_2)

Output :
[[1. 0. 0. 0. 0.]   [0. 1. 0. 0. 0.]   [0. 0. 1. 0. 0.]   [0. 0. 0. 1. 0.]   [0. 0. 0. 0. 1.]]    [[1. 0. 0.]   [0. 1. 0.]   [0. 0. 1.]]
Random object in numpy

The random object in numpy has many useful methods associated with it and all these methods generate random number in one way or the other.

Let us go ahead and look at two most common methods in random object

rand() :

The rand() method generates numbers between 0 and 1. To use this method , simply pass the number of random numbers needed ( between 0 and 1 ) in parenthesis.

import numpy as np  my_arr_1 = np.random.rand(5)  print(my_arr_1)

Output :
[0.13121796 0.36993056 0.13394632 0.26465573 0.72176372]

Random numbers can also be generated in form of a 2-D array. The example below is based on this.

import numpy as np  my_arr_1 = np.random.rand(5,5)  print(my_arr_1)

Output :
[[0.64979343 0.77667001 0.94943148 0.43175576 0.66127934]   [0.74525955 0.29651389 0.85767725 0.90955509 0.18378935]   [0.40996232 0.7667591  0.38131645 0.24151369 0.32410859]   [0.12718593 0.86700833 0.8008471  0.87645771 0.29691512]   [0.42558936 0.33859692 0.1310974  0.62114823 0.5305895 ]]

randint() :

randint() generates integers between a given range. It has the following syntax :

  • np.random.randint(start , end , integers_needed)

Let us take an example on this.

import numpy as np  my_arr_1 = np.random.randint(5,10,5)  print(my_arr_1)

Output :
[8 9 8 6 6]
reshape() method

The reshape() method in numpy is used to reshape an array into different dimensions. it has the following syntax :

  • my_arr.reshape( row , column )

In order for this method to work , the product of row and column should be equal to the number of elements in the array.
import numpy as np  my_arr_1 = np.random.randint(5,100,30).reshape(5,6)  my_arr_2 = np.arange(0,100).reshape(10,10)  print(my_arr_1)  print()  print(my_arr_2)

Output :
[[83 18 51 24 62 85]   [86 71 13 35 98 97]   [38 32 41 98 55 43]   [13 35 15 48 50 70]   [25 16 43 31 32 47]]    [[ 0  1  2  3  4  5  6  7  8  9]   [10 11 12 13 14 15 16 17 18 19]   [20 21 22 23 24 25 26 27 28 29]   [30 31 32 33 34 35 36 37 38 39]   [40 41 42 43 44 45 46 47 48 49]   [50 51 52 53 54 55 56 57 58 59]   [60 61 62 63 64 65 66 67 68 69]   [70 71 72 73 74 75 76 77 78 79]   [80 81 82 83 84 85 86 87 88 89]   [90 91 92 93 94 95 96 97 98 99]]

min() and max() :

The min() and max() method returns the minimum and maximum value of an array.

import numpy as np  my_arr = np.array([[1,2,3,4],[5,6,7,8]])  print(my_arr.max())

Output :
8