Posts

Python Tokens

Tokens  or Lexical unit are the smallest buidling blocks of a program. Tokens are the equivalent of alphabets , grammer and tenses of a English language. Python has the following tokens:- Keywords Identifiers(Names) Literals Operators Punctuators a. Keywords - are the reserved words that has a special meaning and should be used only where its meant to be. Example:- False, True, for, while, None, break, if, elif, else b. Identifiers(Names) - The names given to different parts of the program like variables, function, objects, classes etc. eg:- a = 2, add(), here a and add are identifier names Rules for framing identifier names. Identifier name should be a combination of letters(a-z, A-Z) and digits(0-9). eg:- Valid names - abc123, abc, xy_123, xy_ Invalid names - abc#12, age$  First character must be a letter or underscore. eg:- Valid names- _123, a_123    Invalid names - 1abc Upper and lower case are different. eg:- ABC and abc are both different i...

Python class 11 'IP' Basics

Python is a Programming language used to create software, websites and for scientific computing. Programming language is a language that can be used to write programs to give instructions to computer to perform a specific task. Other popular programming language are C, C++, Java, VB, C# and JavaScript. Popularity of python can be attributed to its huge library(collection of modules, functions) that can be used by the programmer to build any program in less time and effort than any other programming language. Data Science and Machine learning are the two buzzwords that's been around quite some time and to implement techniques of data science and machine learning python is the tool of choice for most programmers. Advantages of Python Language are:- Easy to use - Programmers find it very easy to use it because the rules(syntax) for writing instructions are very much similar to other high level language(C, C++, Java). Expressive Language - Requires fewer lines of code to...

Descriptive Statistics

A number of statistical operations can be performed on the Dataframe and Series objects. This operations are useful in data science to evaluate your data from different perspective. Some common operations are:- a. abs() -   Return a Series/DataFrame with absolute numeric value of each element. Only works on numeric elements. Example:- import pandas as pd import numpy as np s = pd.Series([-1.2,-2.2,3.2]) print(s.abs()) Output:- 0 1.2 1 2.2 2 3.2 Explanation:- Converts negative value to positive value. Example:- import pandas as pd import numpy as np s = pd.Series([-1.2,-2.2,1+1j]) print(s.abs()) Output:- 0 1.200000 1 2.200000 2 1.414214 Explanation:- Converts negative to positive and complex numbers to absolute number as √ a 2 +b 2 Example:- import pandas as pd import numpy as np #s = pd.Series([-1.2,-2.2,3.2]) df = pd.DataFrame({'Age':[-23,-44,24],'Name':['tim','henry','jerry']})...

Groupby - Transformations & Filtrations

b. Transformations -  This function lets you change the data elements into some other value. Example:- import pandas as pd import numpy as np weather_data = {'Weather': ['Rainy', 'Stormy', 'Sunny', 'Cloudy', 'Rainy', 'Sunny', 'Cloudy', 'Rainy', 'Stormy', 'Cloudy', 'Sunny', 'Sunny'], 'State': ['CG', 'AP', 'HP', 'MP', 'HY','DH' ,'CG' ,'HP','AP' , 'MP','CG','AP'], 'Year': [2014,2015,2014,2015,2014,2015,2016,2017,2016,2014,2015,2017], 'Humidity':[3.4,2.3,3.2,4.7,5.8,8.1,3.2,3.5,7.3,1.1,1.2,2.3]} df = pd.DataFrame(weather_data) gp = df.groupby('Year') print(gp['Humidity']. transform ( lambda x: x*100 )) Output:- 0 340.0 1 230.0 2 320.0 3 470.0 4 580.0 5 810.0 6 320.0 7 350.0 8 730.0 9 1...

Groupby- Applying a Function

2. Applying a function Three types of operation can be performed with groupby function: a. Aggregation b. Transformation c. Filteration a. Aggregation An aggregated function returns a single aggregated value for each group.  Example:- import pandas as pd import numpy as np weather_data = {'Weather': ['Rainy', 'Stormy', 'Sunny', 'Cloudy', 'Rainy', 'Sunny', 'Cloudy', 'Rainy', 'Stormy', 'Cloudy', 'Sunny', 'Sunny'], 'State': ['CG', 'AP', 'HP', 'MP', 'HY','DH' ,'CG' ,'HP','AP' , 'MP','CG','AP'], 'Year': [2014,2015,2014,2015,2014,2015,2016,2017,2016,2014,2015,2017], 'Humidity':[3.4,2.3,3.2,4.7,5.8,8.1,3.2,3.5,7.3,1.1,1.2,2.3]} df = pd.DataFrame(weather_data) grouped = df.groupby('Year') print(grouped['Humidity'].agg(np.sum)) Output:- ...

Function Application Continued Aggregation Groupby...

Aggregation(group by) -  A groupby operation involves some combination of splitting the object, applying a function, and combining the results.  This can be used to group large amounts of data and compute operations on these groups. Groupby function works when you have categorical data as we have in the weather data below. Here 'weather', 'Year'and 'State'are all categorical. Groupby operation involves one of the following operations. Splitting the Object Applying a function    Combining the results gp = df.groupby(['Weather','State']) for name,group in gp: print(name) print(group) Output:- 1. Splitting the object -  import pandas as pd weather_data = {'Weather': ['Rainy', 'Stormy', 'Sunny', 'Cloudy', 'Rainy', 'Sunny', 'Cloudy', 'Rainy', 'Stormy', 'Cloudy', 'Sunny', 'Sunny'], 'State': ['CG...

Function Application Continued(transform)...

Transform - This function is used to change the data elements, lets say some data are not how you wannt them to be to process it, usually used during the data preparation step in machine learning. Example1:- import pandas as pd import numpy as np df = pd.DataFrame({'A':range(3),'B':range(1,4)}) print(df.transform(lambda x:x+1)) Output:- A B 0 1 2 1 2 3 2 3 4 Explanation:- range(3) gives a series of number from 0 to 2 range(1,4) gives a series of number from 1 to 3 lambda operator takes each element x and adds 1 to it. Example2:- import pandas as pd import numpy as np s = pd.Series(range(3)) print(s.transform([np.sqrt,np.exp])) Output:- sqrt exp 0 0.000 1.000 1 1.000 2.718 2 1.414 7.389 Explanation:- range(3) gives a series of number from 0 to 2 s.transform function applies two operation(square root and exponential) on each element of series s.