Showing posts with label technique. Show all posts
Showing posts with label technique. Show all posts

Wednesday, March 31, 2021

Rotating Data Tables / Matrices in R

 


        Rotating Data Tables / Matrices in R

 

* One can do a rotation operation over a T table / dataframe / matrix object so that the rows become the columns and the columns become the rows .

 

* For doing the rotation operation over any data table object / data frame object or a matrix object in R , one can use the t() command .

 

* One can think of such a program as short for transpose operation

 

* The given example begins with a dataframe that contains two columns of numeric data with its rows also named .

  



* The final object is transposed ( means - the rows are changed to columns and the columns are changed to rows / reversal of attributes )

 

* Also , the new object is in fact a matrix rather than a dataframe .

  

* One can see this much more clearly if one tries the same t() command over a simple vector .




* Vectors are treated like columns , but when they are displayed they look like rows


* In any event , when you apply a t() command one would get a matrix object as a result .


* One can easily convert the objects to a dataframe using the "as.dataframe()" command .



 

Monday, March 15, 2021

Self Variable in _init_ constructor

 


Self Variable in _init_ constructor


* 'Self' is a default variable that contains the memory address of the instance of the current class which is under current usage .

 

* So , one can use 'self' instance variable to refer to all the instance variables and Instance Methods .

 

* When an instance of the class is created , the

instance name contains the memory location of the instance . The memory location is internally passed to the 'self' . For example , one can create an instance of the Student Class in the given manner :

 

S1 = Student()

 

* Here , 's1' contains the memory address of the instance . This memory address is internally and by default passed to the 'self' variable .

 

 

* Since the 'self' knows the memory address of the instances , it can refer to all the members of the instance .

 

* One can use 'self' in the following ways :

1) The 'self' variable is used as a first parameter within the constructor as :

 

def _init_(self):

In this case , 'self' can be used to refer to the instance variables inside the constructor .

 

2) 'self' can be used as a first parameter in the instance method as :

 

def talk(self):

 

* Here , talk() is an instance method as it acts on the instance variables present within the Class and defined under the _init_(self): method .

 

* If the method wants to act on the instance variables , then the method should know the memory location of the instance variables . That memory location is by default available to the talk() method through the 'self' method .

Types of Variables in Python ( related to Classes and Objects in OOPS )

 


Types of Variables in Python

 

* The variables which are written inside a class are of primarily two types :

1) Instance Variables

2) Class Variables / Static Variables

 

* Instance variables are the variables whose separate copy is created in every instance ( or object )

 

* For example , if 'x' is an instance variable and if we create 2 instance variables then there will be 2 copies of 'x' in the instances .

 

* When we modify the copy of 'x' in any instance, then it will not modify the other two copies .

 

==========================================
Python Program to understand Instance Variables
==========================================

 

# instance variable example

Class Sample:

    # this is a constructor

    def __init__(self):

    self.x = 10

 

    # this is an instance method

   def modify(self):

   self.x += 1

 

 

# Create 2 instances

    s1 = Sample()

    s2 = Sample()

    print(' x in s1 = ', s1.x)

    print(' x in s2 = ', s2.x)

 

# modify x in s1

     s1.modify()

    print(' x in s1 = ' , s1.x)

    print(' x in s2 = ' , s2.x)

 

# modify x in s1

    s1.modify()

    print(' x in s1 = ', s1.x)

    print(' x in s2 = ', s2.x)

 

Output

x in s1 = 10

x in s2 = 10

x in s1 = 11

x in s2 = 10

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

 

* Instance Variables are defined and initialised using a constructor with 'self' parameter . In the above program it can be realised in the line just after the usage of the docstring over where the code for declaration and definition of the Instance Variables is mentioned and in the succeeding line the __init__(self) is defined .

 

* In order to access the instance variables one needs Instance methods with 'self' as the first parameter which can be observed from the above code as well

 

* It is possible that the instance methods may have other parameters in addition to the 'self' parameter .

 

* In order to access the instance variables , one can use a self.variable in a program .

 

* It is also possible to access the instance variables from outside the class as : instancename.variable e.g. s1.x .

 

* Unlike instance variables , class variables are the variables whose single copy is available to all the instances of the class .

 

Thursday, March 11, 2021

Introduction to OOPS using Python - ( important points to remember )

 


Introduction to OOPS using Python

 

* OOPS is one of the most revolutionary concepts in Computer Science , and its full form is called as Object Oriented Programming System ( OOPS ) which is based on languages like Smalltalk , Simula-67 , C++ , Java , Python etc .

 

* The languages like C , Pascal , Fortran etc are called as Procedure Oriented Programming which perform a given set of tasks .

 

* While developing software , the main task is divided into several sub tasks and each of the sub task can be represented as a procedure or a function .

 

* The main task is therefore composed of several procedures and functions and the approach for this is called as Procedure Oriented Approach .

 

* Procedure Oriented Approach for OOPS based programs . Languages like C++ , Java and Python use classes and Objects in their programs and are called as + , Java and Python use classes and Objects in their programs and are called as Object Oriented Programming Languages .

 

* A Class is a module which itself contains data and methods (functions) to achieve the task.

 

* The main task is divided into several sub tasks and they are represented as classes. Each class can perform several inter-related tasks for which several methods are written in a class . This approach is called as Object Oriented Approach .

 

* Programmers follow a Procedure Oriented Approach for looking into problems that associate with problems related to Procedure Oriented Programming .

 

Important Points to Remember : Dictionary Objects in Python ( syntax , usage , methods , behaviour etc )

 

  Points to Remember: Dictionary Objects in Python




 

1) A dictionary represents a group of elements arranged in the form of key-value pairs. In the dictionary object , the first element is considered as the 'key' and the immediate next element is considered as its associated 'value' .

 

2) The Key and Value Pairs should be written inside a dictionary by separating them with the help of a colon (:) operator. Each pair should be separated with the help of a comma sign. All the key-value pairs of the dictionary should be written inside curly braces { }

 

3) Indexing and Slicing are not useful to access the elements of a dictionary

 

4) While inserting a new element or modifying the existing element, it is preferable to use the given format:

 

dict(key) = Value

 

5) The keys of a dictionary should be unique and must be Immutable which means that once the data for the Keys is assigned, one cannot change the data type of the elements inside the dictionary.

 

6) The keys of a dictionary should be unique and belong to immutable datatype . The value associated with the key should be unique and should be immutable in nature.

 

7) The get(k,v) method returns the value upon taking the key 'k' . If the key is not found in the dictionary , then it will return a default value 'v'

 

8) The update({k:v}) method stores the key 'k' and its value 'v' pair into an existing dictionary

 

9) The dict() method converts a list or tuple or a zip object into a dictionary

 

10) The zip() method is useful to convert the sequences like lists into a zip class object

 

11) An ordered dictionary is a dictionary but it will keep the order of the elements

 

12) Ordered Dictionaries are created using the OrderedDict() method of collections module .

 

Python program to pass Dictionary Objects into Functions ( with sample code and pictorial representation of implementation in Jupyter Notebook )

 




Wednesday, March 10, 2021

Converting Lists into Dictionary in Python ( sample code )

 

Converting Lists into Dictionary




 

* When we have two lists , it is possible to convert the lists into a dictionary

 

* For example , we have two lists containing the names of the countries and names of their capital cities

 

countries = [ "USA" , "India" , "Russia","China","Germany" ]

 

cities=["Washington","Delhi","Moscow","Beijing","Berlin"]

 

* If the user wants to create a dictionary out of these two lists by taking the elements of 'countries' list as keys and 'cities' list as values , then the dictionary would look like this :

 

d = { "USA":"Washington" , "India":"Delhi" ,

"Russia":"Moscow","China":"Beijing","Germany":"Berlin"}

 

* There are two steps involved to convert the lists into a dictionary

 

Step - 01 :

Create a "ZIP" class object by passing the two lists to the zip() function as :

 

z = zip(countries,cities)

 

The zip() function is useful to convert the sequences into a zip class object . There may be 1 or more sequences that can be passed to the zip() function . Therefore , we passed only 2 lists to the zip() function in the above statement . The resultant zip object is 'z' .

 

Step - 02 :

The second step is to convert the zip object into a dictionary by using the dict() function .

 

d = dict(z)

 

Here , the 0th element of the z object is taken as the 'key' and 1st element is converted into the 'value' . On a similar note , 2nd element is taken as the 'key' and '3rd' element is considered as the 'value' . All the values are stored in the dictionary object 'd' . If the user wants to display 'd' object , then the user can do this by using the following dictionary :

 

d = { "USA":"Washington" , "India":"Delhi" , "Russia":"Moscow" , "China":"Beijing", "Germany":"Berlin" }

 

Python program to sort the elements of a dictionary based on a key or a Value

 


Python program to create a dictionary from keyboard and display the elements


 

Friday, March 5, 2021

Keys and Values over Dictionary Objects in Python

 

Keys and Values over Dictionary Objects in Python



* Important points to remember while dealing with Dictionary Objects in Python are the following :

 

1) One can use any type of datatype while dealing with any value for a Dictionary Object in Python

2) A value can be a number , string , list , tuple or any other dictionary object in Python

3) Keys in Python should obey the following rules

 

*** point-1 ***

Keys should be always unique . Duplicate Keys are not allowed in Python . And If the user/programmer enters the same key again over the existing dictionary , then the old key would be overwritten and only the new key would be available . One can consider the following example for considering the case of uniqueness of a Key in Python

 

emp =

{

  'Data1':100,

  'Data2':200,

  'Data1':300

}

 

If someone wants to print the contents of the dictionary object after the object has been created , then one would find that the values within the dictionary for the data item which has been entered twice that is "Data1" which is entered twice in the case would be printed only once as a single data item can only exist once , and this data item would have the higher value of the object .

  

print(emp)

Result :

{

  'Data1':300,

  'Data2':200

}

 

Here , one would be noticing that the values for the data item are replaced with the one which has a higher value over the other data item object which means that the dictionary object gives precedence to that sub-data item within it to exist within it which has a higher value for the same / duplicate key .


The screenshot for the above code is as given below :



 

*** point-2 ***

Keys should be immutable in nature . For example , one can use a number , a string or a tuple as key since the keys are immutable in nature . One cannot use lists or dictionaries as Keys . If as such , Keys are used then one will get 'TypeError' . One can consider the following example in case of a Dictionary Object :

 

emp =

{

  [ 'Data1']:100,

    'Data2':200,

    'Data1':300

}

 

# Here , upon compilation and execution of the code one would get to see that for the list element 'Data1' we get an error in the output section . This is because, as mentioned above, the key item should only be a number , string or a tuple in nature .

Saturday, February 27, 2021

Chi-Square Tests of Independence testing using R ( Chi - Square Tests for two-way table )

 

             Chi-Square Tests of Independence

   ( Chi - Square Tests for two-way table )

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


For effective execution and analysis of the Chi-square test in R language we are going to do the following steps :



1) Step no.1
- We will create a vector in the following format

 data <- c(40,25,19,37,39)

 This "data" vector holds the data for all the students who are registered for the different classes in a school . These are 40 students in class 1 , 25 students for class 2 , 19 students for class 3 , 37 students for class 4 and 39 students for the class 5 .

 

2) Step no. 2 - In R we are going to evaluate whether the values as have been specified in the data variable object "data" are same in value and this equality condition is going to be tested in the NULLHypothesis for the test for evaluation of the data object .

 The Null Hypothesis and the Alternative Hypothesis for checking the validity of both the conditions can be stated and represented in the given manner with the null hypothesis being represented by the symbol (H0) and Null Hypothesis being represented by the symbol (H1) .

 

# H0 is the null hypothesis

H0 : p1 = p2 = p3 = p4 = p5

 

The alternative Hypothesis can be given in the alternate manner i.e,

 

# H1 is the alternative hypothesis

H1 : p1 != p2 != p3 != p4 !=p5

 

3) In the 3rd step , we will run the chi - square test over the function by calling the appropriate function for the evaluation of the same

 

·         chisq.test(data)

 

On the console one can see that the entire data item which had been fed to the prompt has been registed as a memory object . And when one wants to run the Chi-squared test upon the data item object , the values that come as a result of execution of the test are in the given format :

 

X-squared = 11.125, df = 4 , p-value - 0.02519

 

# from the given evaluation one can analyse with the help of the Chi-square table that the critical value which is also acronymised as (T-crit) is found to be as 9.488 with a degree of freedom for the data as 4 and alpha ( degree of significance is found out to be as 0.05 )

which can be found out from the p-value obtained after running the test is that we are basically not wrong in our assumption while framing the null and alternative hypothesis which means that the p-value for the evaluation is less than 0.025 which is less than the alpha value set for the test which is 0.05 then we can reject the null hypothesis as again stating .. we found out the P-value to be lesser than that of the Alpha set for the test.

 


 But had it been the case that the value of the alpha level had also been set at 0.25 then we would not have been at a position to reject the null hypothesis .

 

Conclusion and Verification

 

For the sake of analysing the obtained values if we are again running the chi-square test upon another pair of data in the given format .

 

data1 <- c(35,31,38,27,29)


 And again , we are running the chi-square test on these numbers in the "data1" vector , then the following results would be obtained .

 

> chisq.test(data1)

 

X-squared statistic is 2.5 , degrees of freedom is 4 , p-value is obtained as 0.644 .This means that since one is looking at the alpha value of 0.05 and the p-value is lying towards the left of the normal distribution line then we can say that the Null Hypothesis for the given set of values within the vector "data1" cannot be rejected .. I repeat , for the data values presented in the second vector item , the null hypothesis cannot be rejected .

 



 Again drawing a general conclusion from the experiment .. if we obtain the value of the p-value to be greater than that of the alpha value , then the null hypothesis cannot be rejected .