Numpy basics and generation of random data in python #Feed2

 


Photo by Kim Greenhalgh on Unsplash


Numpy built-in methods to generate random data:

Please go through the previous post for continuation...

https://spacewidget.blogspot.com/2021/09/numpy-basics-for-beginners-python-lists.html


Let's see built-in methods to generate random data:


a) rand( )


rand( ) function returns random values within the interval [0,1). These values were uniformly distributed. Notice the values will be between 0 and 1 including 0 and excluding 1.


Syntax:
a) For one dimensional array:


np.random.rand(number of elements)


b) For two dimensional array:


  np.random.rand(rows, columns)  

Ex1: Generate an array of 6 random samples or values.


    >>np.random.rand(6)

  Output:
array([0.76902525, 0.7350518 , 0.88309016, 0.78865659, 0.66939996,
       0.66937215])

Ex2: Generate an 3x2 matrix array of random values that are uniformly distributed.


   >>np.random.rand(3,2)

  Output:
    array([[0.5263897 , 0.26520951],
       [0.91524442, 0.33315039],
       [0.69569297, 0.05059593]])

Note: The random values generated after each run be will different. 

b) randn( )

randn( ) function returns random values that follows guassian or normal duistribution. The values that follow standard guassian distribution have a mean of 0 and a variance of 1.


Syntax:
a) For one dimensional array:


np.random.randn(number of elements)


b) For two dimensional array:


  np.random.randn(rows, columns)  


Ex1: Generate an array of 6 random samples that follow guassian distribution.


    >>np.random.randn(6)

  Output:
array([ 1.24712829, -2.35714528,  1.51187872, -1.24711601,  0.38634127,
        1.54541885])

Ex2: Generate an 3x2 matrix array of random values that are normally distributed.

   >>np.random.randn(3,2)

  Output:
   array([[ 0.19673461, -0.2620097 ],
       [-0.83845025,  0.03753841],
       [ 0.32662613,  0.05662677]])

c) randint( )

    randint( ) function returns random values between the specified interval with inclusion of start number and exclusion of stop_number.


Syntax:
a) For one dimensional array:

np.random.randint(start_num, stop_num, number of elements)


b) For two dimensional array:


  np.random.randint(start_num, stop_num, (rows, columns) )

Ex1: Generate an array of 10 random samples tin the interval 1 to 100.

    >>np.random.randint(1, 100, 10)

  Output:
    array([76, 64, 46, 91, 17, 11,  5, 69, 39, 16])

Ex2: Generate an 3x2 matrix array of random values in the interval 30 to 50.

   >>np.random.randint(30,50,(3,2))

  Output:
    array([[49, 40],
           [35, 37],
           [48, 41]])

Properties/ Attributes of the arrays:


a) ndim
    
    ndim gives the dimensions of an array. In general, the arrays were one dimensional (1-D), two dimensional (2-D) or multidimensional (n-D).
   Ex1: Finding the dimensions of an array discussed in previous section (randint( ))

>> sd = np.random.randint(1, 100, 10)
>> sd.ndim

   Output:

                1

Since the output of np.random.randint(1, 100, 10) is a one-dimensional array, the output returned was 1.

   Ex2: 

>> md = np.random.randint(30,50,(3,2))

>> md.ndim

   Output:

                2

In this example, we created a 3x2 matrix that was two-dimensional, so the output is 2 here.

b) dtype
    
   dtype gives the data type of the elements in the array. All the elements in the array should be of same data type. Commonly used data type in numpy python are int32, float 32, U32, int64,float64.

    Ex1: Finding the data types of elements of an array discussed in previous section (randint( ))

>> sd = np.random.randint(1, 100, 10)
>> sd.dtype

   Output:

             
        dtype('int32')

   Ex2: 

>> md = np.random.randint(30,50,(3,2))

>> md.dtype

   Output:

              dtype('int32')


c) itemsize
    
itemsize specifies the memory size occupied by each element in terms of bytes.
We know 1byte = 8 bits.

    Ex1: Finding the memory occupied by an array discussed in previous section (randint( ))

>> sd = np.random.randint(1, 100, 10)
>> sd.itemsize

   Output:

             
        4

Here we know these elements are integer type (int 32) by its data type. So there are 32 bits, and we know 8bits =1byte. Therefore it was 4 bytes (32/8=4)

  Ex2: 

>> md = np.random.randint(30,50,(3,2))

>> md.itemsize

   Output:

            4

d) size
    
   size gives the total number of elements in the array.

    Ex1: Finding the number of elements of an array.

>> sd = np.random.randint(1, 100, 10)
>> sd.size

   Output:

             
        10

   Ex2: Finding the number of elements in an matrix array.

>> md = np.random.randint(30,50,(3,2))

>> md.size

   Output:

            6

e) shape
   shape defines the number of elements in each dimension of an array.

   Ex1: Finding the shape of an array.

>> sd = np.random.randint(1, 100, 10)
>> sd.shape

   Output:

             
        (10,)

 Ex2: Finding the shape of an matrix array.

>> md = np.random.randint(30,50,(3,2))

>> md.shape

   Output:
            (3, 2)

  f) reshape()
    
   reshape function was used to change the shape of the existing array with the same existing array elements. Rememeber the total number of the elements in the existing array and the reshaped array shuld be same.
   Ex1: Generate an 1dimensional array of 10 elements and convert into ina matrix.

>> elements=np.arange(1, 100, 10)
>> elements

   Output:

array([ 1, 11, 21, 31, 41, 51, 61, 71, 81, 91])


>> elements.reshape(2,5)


    Output:


        array([[ 1, 11, 21, 31, 41],
             [51, 61, 71, 81, 91]])

      In this example, we created an 1-Dimensional (1-D) array of 10 numbers with stepsize of 10 and named the array as "elements". We then reshaped the 1-D array into 2-D array using reshape function. Here we reshaped the aray named "elements" into a 2x 5 matrix with 2 rows and 5 columns.
Notice the reshaped array have 10 elements (5 x 2 = 10).



Continue learning new topics and keep going...
          


     




Comments