Tuesday, April 20, 2021

Exploring the World of Probability Theory in ML .. derived article with own interpretations

               Exploring the World of Probability Theory in ML

 

* What is Probability and how can it be used? Probability is the likelihood of an event which means that Probability can help someone to determine the possibility of something to happen or not using the mathematical (Gannita Gyaana) where one can establish the possibility or likelihood of occurrence of an event in terms with the total number of possible events that could likely occur .

 

* The probability of an event is measured in the range from 0 (no probability that an event occurs) to the value of 1 ( a certainty that an event occurs ) which in relative terms says about the extent of any value towards the any of the extremes from the left most to the right most values .

 

* The probability of picking a certain suit from a deck of Cards (generally referred to as "Taash" in many Asian countries) is one of the most classic example on explanation of probabilities.

 

* The deck of cards contains 52 cards (joker cards excluded) which can be divided into four suits as clubs and spades which are black , and diamonds and hearts which are red in colour .

 

* Therefore , if one wants to determine whether the probability of picking the card is an ace , then one must consider that there are four aces of different suits .The probability of such an event can be calculated as p = 4/52 which is again evaluated to 0.077.

 

* Probabilities are between the values of 0 and 1 ; no probability can exceed such boundaries as everything's possibility of occurrence lies between nothing to everything and probability of not occurrence of something is always zero and the probability of occurrence of everything is always equal to 1 .

 

* If someone tries to do a Probability Possibility prediction for a given case of fraud detection in which one would like to see and find out the number of times a bank transaction related fraud has occurred over a given set of bank accounts or how many times fraud happens while conducting a banking transaction or how many times people get a certain disease in a particular country . So , after associating all the events , one can estimate the probability of occurrence of associating all the events , one can estimate the probability of occurrence of such forthcoming event with regards to the frequency of occurrence , mode of occurrence , time of occurrence , as well as the likely accounts which could be affected by the fraud and the conditions which are likely to affect the accounts .

The calculation for the estimation would take into consideration of counting the number of times a particular event occured and dividing the total number of events that could possibly occur for a set of operations and calculations.

 

* One can count the number of times the fraud happens using recorded data ( which are mostly taken from databases ) and then one would divide that figure by the total number of generic events or observations available

 

* Therefore , one should divide the total number of frauds by the number of transactions within a year or one can count the total number of people who fell ill during the year with respect to the population of a certain area . The result of this is a number ranging from 0 to 1 which one can use as baseline probability for a certain event under certain type of circumstances

 

* Counting all the occurrences of an event is not always possible for which one needs to know about the concept of sampling. Sampling is an act which is based on certain probability of expectations , which one can observe as a small part of a larger set of events or objects , yet one may not be able to infer correct probabilities for an event , as well as exact measures such as quantitative measurements or qualitative classes related to a set of objects

 

* Example - If one wants to track the sales of cars in a certain country , then one doesn't need to track all the sales that occur in that particular geography ... rather using a sample comprising of all the sales from new car sellers around the country , one can determine the quantitative measures such as average price of a car sold or qualitative measures such as the car model which were sold most often

 

Some Operating cases on Probabilities

                   Some Operating cases on Probabilities


 * It is suggested that operations on probabilities are a bit different from numeric operations because the range of occurrence of such probability values generally lies between the range of 0 & 1

 

* One must rely on some set or rules in order for the operation to make sense to the user who is conducting the experiment on probabilities. For example , if someone is conducting an experiment of tossing a coin then he/she must strictly define the rules according to which the game of tossing a coin would be played out . The instructor would declare which outcomes should be taken as valid outcomes and which should not be taken in as valid outcomes , rather must be negated the moment the norms of the game are violated .


 * For example , suppose say a case happens over where a coin does not fall over any of the sides rather falls over the floor standing erect , then the outcome is neither a heads and nor a tails , and neither a 50-50 heads-tails can be taken as consideration for the throw of the dice . Rather what would happen in such a circumstance is that the throw of the dice for this case would be nullified , the entire event of throw of such a dice would be struck off from the probable set of outputs that should happen as a result of the throw of the dice . Thats why one should also keep adhering to the rules of the experiment before conducting such an experiment which would require to know what set of events should be taken in as considerable outcomes and which should not be considered .

 

* Again another property of Probabilities that one needs to be aware is summations between probabilities which states that summations of probabilities is possible only when all the constituting events of the sample space are mutually exclusive to each other . For example lets consider an experiment of rolling a dice over a game of ludo , in this all the possible events that could turn up as a result of throw of the dice are 1 , 2 , 3 , 4 , 5 , 6 . The probability of occurrence of each of the events is 1/6 or 1 by 6 . And here , each of the events within the given sample space are disjoint and mutually exclusive to each other which makes the individual events probability of occurrence as equal to each of the given event divided by the total number of events over the entire sample space . And in case one would like to know the probability of occurrence of all the events together in unison , then one may have to add up the probabilities of each of the individual events as a summation of each of the individual events .. which would yield an output of 1 . So in retrospect, all individual elements of an experiment of probability are disjoint and mutually exclusive and in unison lead to a summed up value of 1 .

 

* We can take another simple example to demonstrate to demonstrate the case of understanding of probability calculation ; in this case one can consider for example the case of picking a spade or a diamond from a set of cards can be calculated in the following manner . Total number of cards in the entire deck = 52 . Number of cards in the house of clubs = 13 , number of cards in the house of clubs = 13 , number of cards in the house of hearts = 13 , number of cards in the house of diamonds = 13 . If a person takes out a card from the house of diamond then the probability if picking up one of the cards is 13/52 ; the same goes for the case of picking up a random card from a house of clubs is 13/52 . So , total probability of finding a card from both the houses is 26/52 which is equals to 0.5

 

* One can take the help of subtraction operation to determine the probability of some events where probability of an event is different from the probability of an event that one would want to compare . For instance , if someone wants to determine the probability of drawing a card that does not belong to some house of card for example , say I want to draw a card which is not a diamond from the overall deck of cards , then one will approach the problem in the given manner . He will first find out the overall probability of finding any card and then he will subtract the chance of occurrence of a particular card from the total , 1 - 0.25 which happens to be as 0.75. One could get a complement of the occurrence of the card in this manner , which could be used for finding the probability of not occurrence of a particular event .

 

* Multiplication of a set of events can be helpful for finding the intersection of a set of independent events . Independent Events are those which do not influence each other . For instance , if one is playing a game of dice and one would like to throw two dices together , then the probability of getting two sixes is 1/ 36 . This can be obtained by multiplication of dices over both the cards , where first the probability of obtaining a 6 is found out to be as 1/6 and then the subsequent independent event would also produce an probability of obtaining another 6 is found out to be as 1/6 , here both the values are multiplied with each other and found that product of both the probabilities of independent events would yield a value output as 1/36 or 0.28 .

 

* Using the concepts of summation , difference and multiplication , one can obtain the probability of most of the calculations which deal with events . For instance , if one would want to compare the probability of getting atleast a six from two throws of dice which is a summation of mutually exclusive events . Probability of obtaining two sixes of dice , p = 1/6* 1/6 = 1/36

 

* In a similar manner if one would like to calculate the probability of having a six on the first dice and then something other than a six on the second throw of the dice is p = (1/6)*(1- 1/6) = 5/36 ,

 

* Probability of getting a six from two thrown dice is p = 1/6* 1/6 +2*1/6*(1- 1/6) = 11/36

 

Monday, April 19, 2021

Advanced Matrix Operations – A theoretical view

                     


  Advanced Matrix Operations – A theoretical view               ========================================

 

* One may encounter some important matrix operations using algorithmic formulations

 

* The advanced matrix operations are formulating the transpose and inverse of any given matrix form of dataset

 

* Transposition occurs when a matrix of shape n x m is transformed into a matrix in the form of m x n by exchanging the rows with the columns

 

* Most of the tests indicate the operation using the superscript T in the form of A( transpose )

 

* One can apply " matrix inversion " over matrices of shape m x m , which are square matrices that have the same number of rows and columns . In mathematical language , this form of square ordering of matrices is said that the matrix has m rows and m columns .

 

* The above operation is important for the sake of finding the immediate resolution of the various equations which involve matrix multiplication such as y = bX where one has to discover the values in the vector b . More on Matrix multiplications with more conceptual examples would be showcased in another article in which I shall try to cover how the Matrix Multiplication of different Matrices occur and how this Multiplication is used to solve more important / complex problems .

 

 

* Since most scalar numbers (exceptions including zero) have a number whose multiplication results in a value of 1 , the idea is to find a matrix inverse whose multiplication would result in a special matrix called the identity matrix whose elements are zero , except the diagonal elements

 ( the elements in positions where the index 1 is equal to the index j)

* Now , if one wants to find the inverse of a scalar quantity , then one can do so by finding the inverse of a scalar . (The scalar number n has an inverse value that is n to the power minus 1 which can be represented by 1/n that is 1 upon n )

 

* Sometimes, finding the inverse of a matrix is impossible and hence the inverse of a matrix A is indicated as A to the power minus 1

 

* When a matrix cannot be inverted, it is referred to "singular matrix" or a "degenerate matrix" . Singular matrices are usually not found in isolation, rather are quite rare to occur and generalise .

Using Vectorisation Effectively in Python and R – a revision example

 


    ========================================================

     Using Vectorisation Effectively in Python and R – instance example

    ========================================================


·        While performing Matrix Operations, such as Vector Multiplication , its very hard to consider that the computer does all the forms of hard work .  What does one need to do while working on numbers in a Vectorised form?

 ·        So , while one is working upon numbers , one needs to put the numbers in the form of matrices , vectors and constants and then one can sum , subtract , divide and then multiply the numbers .

 ·        These operations can be performed upon any number which is in the form of Vectors .

 ·        So , when one deals with these vectorised form of numbers in Machine Learning , then one should also have a good ( good is a relative value that lies in between novice and expert ) understanding and know how on how to feed the vectorised data to the algorithm over which the encoded function is going to work upon , which would help the programmer to know and understand the results obtained after the processing of the data is done .

 ·        In order to obtain a correct result , one may need to feed the right data to the right algorihtm .

 ·        In Python, Numpy package offers all the functionality needed to create and manipulate the matrices . The "ndarray" objects allow fast creation of an array , such as a multidimensional matrix by starting with data which are queued into the lists

 ·        The term "ndarray" means "n-dimensional array" which implies that one can create arrays of multiple dimensions like one-dimensional array , 2 dimensional array , 3 dimensional array and so on .

 

 

=========================================

Using a simple Vector for creation of a Numpy List

=========================================

 

import numpy as np

y = np.array([43, 45,47,49,51])

print(y)

print(y.shape)

 

Output

[43,45,47,49,51]

(5,)

 

==================================

==================================

 

* The method "shape" can promptly inform someone about the shape of a matrix . In the above given example , one can see that the shape of the matrix is just a one-dimensional entity which reports only three rows and no columns which means that the object is a vector

 * In order to create matrices made of rows and columns , one can make use of lists of list items .

 * The contents of the list inside the main list are the rows of one's matrix

 

==================================

==================================

import numpy as np

X = np.array([1.1,1,545,1],[4.6,0,345,2],[7.2,1,754,3])

print(X)

 

==================================

==================================

 * The output of the following operation would yield a vector which one can reshape into any desired form of rows and columns .

 * Here , the numbers are filled into the new matrix stacked into it in the form of row data which is occupied from the top most to the lowermost elements of the array which are stored in the output object "X" which would store in it all the elements in an iterative manner containing all the elements in i,j manner . Elements in an iterative manner containing all the elements in i,j manner .

 * Operations with addition and subtractions with scalars using the numpy "ndarray" is the best method of performing any operation .

 

* One can do the following operation - sum , subtract , multiply or divide using the severall standard operators applicable over Python language in the given manner . Lets take two data-array objects .. a and b

 ==================================

a = np.array([[1,1],[1,0]])

b = np.array([[1,0],[0,1]])

print(a - b)

[[ 0 1] [1 -1]]

==================================

a = np.array([[0,1],[1,-1]])

print(a * -2)

[[ 0 -2 ]

[ 2 -2 ]]

==================================

 * In order to perform multiplication on vectors and matrices , one needs to use the "np.dot" function . The input for this function is two arrays which are of compatible sizes to multiply according to the given order

 ==================================

X = np.array([[4,5],[2,4],[3,3]])

b = np.array([3,-2])

print(np.dot(X,b))

[ 2 -2  3 ]

B = np.array([3,-2],[-2,5])

print(np.dot(X,B))

 ==================================


 * The same form of multi-dimensional array output can be also produced by an array which one can find in the below set of code . One does not need any additional set of libraries while using R , R performs the tasks using standard functions as given in the below set of code .

 

* One can define the dimensions of one's vector using the "length()" function . But one can use the dis() function instead for the matrices , because applying the length() function to a matrix shows the output to carry only some number of elements in it .

 


Ref : Machine Learning in R and Python - 

Friday, April 16, 2021

Python Program to create a Static Method - with sample code

 

Python Program to create a Static Method

 

# A Python Program can be used to create a static method which can be used to calculate the square root value of a given number

static method for finding square root value

 

import math

class Sample:

@staticmethod

def calculate(x):

result = math.sqrt(x)

return result

 

# accept a number from keyboard

num = float(input('Enter a number:'))

# call the static method and pass the number

res = Sample.calculate(num)

print(' The square root of {} is {:.2f}'.format(num,res))

 

Output

Enter a number : 49

The square root of 49 is 7

 

Static Methods in Python - Example of a Static Method in a Class in Python

 


    Static Methods in Python


* One can use Static Methods while the class level but one may not involve the class or the constituting instances .

 

* Static Methods are used when one wants to process an element in relation to a class but does not need the class or its instance to perform any work .

 

* Example :

writing the environmental variables that go inside creation of a class , counting the number of instances of the class or changing an attribute in another class are the tasks related to a class .. such tasks can be handled by Static Methods

 

* Static Methods can be used to accept some values , process the values and then return the result .

 

* Also one could use Static Methods to accept some values , process the values and then return the result

 

* In this case , the involvement of neither the class nor the objects is of paramount importance .

 

* Static methods are written with a decorator @staticmethod above the methods

 

* Static Methods are called in the form of classname,method()

 

* In the following method , one is creating a static method "noObjects()" that counts the number of objects or instances created in MyClass . In MyClass , one can write a constructor that increments the class variable 'n' everytime an instance of the class is created . This incremented value of 'n' gets displayed by the "noObject()" method

  


 

Class Methods in Python - An example of creation of a Class Method in Python along with sample code

 


    Class Methods in Python


* These are the set of Methods are act on class level . Class Methods are the methods which act on the class variables or static variables .

 

* The Class Methods can be written using @classmethod decorator above them .

 

* For example , 'cls.var' is the format to refer to the class variable which includes methods which can be generally called using the classname.method()

 

* The process which is commonly needed by all the instances of the class is handled by the class methods

 

* In the given example below, one can see the instance of the class which is handled by the class methods . The same program can be developed using an example class which can be used in the following manner .

 

* In the example , one can refer to a sample Bird Class for more insight into the description and elaboration of a Method Class . All the birds in nature have only 2 wings (as we mostly see , but there are abberations ofcourse ). Here , one can take an instance of a Bird Class . All the Birds in Nature have 2 wings , therefore one can take 'wings' as a class variable and a copy of this class variable is

available to all the instances of the Bird Class . In this Bird class , we will create a hypothetical method which applies to the functions that a Bird can operate upon and thus will make use of this method that is "fly" method (... to fly above the sky ... fly rhymes good with sky .. please accept from me a satirical High of Hi ... I am poetic too you know :P)

 

* So where was I .. ya I was at the forefront of creation of a class which would take into its ambit a Bird which would have some generic applicable class variables all applicable to the organism class of Birds like all Birds have a pair of wings .. that makes the count to 2 . And birds fly .. which is a method attributed to birds . These two class variables and class methods would be made use of to instantiate a generic sample class of a Bird

 

* So lets create a Bird with two wings and this flies too (I am sorry to know that when God created a Bird like Penguin , God forgot to add the the instance function "fly" to its class genus ... therefore I shall also keep this off the charts for penguins,kiwis and take only those birds which can fly ... up above the sky )

 

* Without further ado .. lets get to this class creation which would take into effect all the common features of all the instances of a Bird Class

  

==================================

==================================

# understanding class methods

class Bird:

     # calling a class variable

     wings = 2

     # creating a class method @classmethod

     def fly(cls,name):

     print('{} flies with {} wings'.format(name,cls.wings))

 

#display information of 2 birds

Bird.fly('Garuda')

Bird.fly('Pigeon')

Bird.fly('Crow')

Bird.fly('HummingBird')

Bird.fly('Eagle')

 

==================================

==================================

Output

Garuda flies with 2 wings

Pigeon flies with 2 wings

Crow flies with 2 wings

HummingBird flies with 2 wings

Eagle flies with 2 wings

==================================

==================================