Posts

Showing posts from June, 2019

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 identifiers Xyz and

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.

Function Application Continued(applymap)...

Applymap -  Apply a function to a Dataframe element wise.  This method applies a function that accepts and returns a scalar to every element of a DataFrame. Example:- import pandas as pd import numpy as np df=pd.DataFrame([[1, 2.12], [3.356, 4.567]]) print(df.applymap( lambda x : len(str(x)))) Output:-       0   1 0  3    4 1  5    5 Explanation:- applymap applies particular function to each element of the dataframe. lambda operator is used to create functions without a name. x represent each element of dataframe and len(str(x)) is the operation applied on elements str function is used to convert integer into string len is a function used to calculate the length of the string Quiz:- 1.  import pandas as pd import numpy as np df=pd.DataFrame([[1, 2.12], [3.356, 4.567]])print(df.applymap(lambda x: x**2)) #Post your answer in comment 2.   import pandas as pd import numpy as np def ff(x):     if x>4:        x=x*2        return x     else:    

Function Application Continued(apply)...

Apply -  Apply a function along an axis(rowwise or columnwise) of the DataFrame. Example:- import pandas as pd import numpy as np df = pd.DataFrame({'one':pd.Series([1,2,3,4]),'two':pd.Series([3,5,6,7])}) print(df) print(df.apply(np.mean)) Output:- one two 0 1 3 1 2 5 2 3 6 3 4 7 one 2.50 two 5.25 Explanation:- df is a dataframe object with some float/number data. df.apply(np.mean) applies mean(average) on the index(columnwise/axis=0) by default thats why the output above. df.apply(np.mean,axis=1) applies mean on the columns(Rowwise) and gives output as below one    2.50           two    5.25 Quiz:- 1. import pandas as pd import numpy as np df = pd.DataFrame({'one':pd.Series([1,2,3,4]),'two':pd.Series([3,5,6,7])}) print( df . apply ( np . sum , axis = 0 )) #Post your answer in comment

Function Application CBSE IP 12

To apply any built in library functions such as mean, sum etc. to the pandas object(dataframe) or your own functions their are different types of functions Pipe - applies function to the dataframe objects each element. Example:- import pandas as pd import numpy as np def func1(ele1,ele2): return ele1+ele2 df = pd.DataFrame(np.random.randn(5,3),columns=['col1','col2','col3']) print(df) print(df.pipe(func1,10)) Output:- col1 col2 col3 0 0.994857 -0.137329 0.992666 1 0.115403 -1.273267 -0.030973 2 -0.865839 -0.575995 1.629540 3 -0.649246 1.127552 0.750935 4 -0.369847 0.100534 -0.214271 col1 col2 col3 0 10.994857 9.862671 10.992666 1 10.115403 8.726733 9.969027 2 9.134161 9.424005 11.629540 3 9.350754 11.127552 10.750935 4 9.630153 10.100534 9.785729 Explanation:- random.randn(5,3) is a library function found inside numpy module, it creates 5x3 i.e 5 rows and 3 colu

Running Python code in Visual Studio Code

Image
1.       Download and install  python  latest version from  https://www.python.org/downloads/ . 2.      Download  VS code  a software from Microsoft from the site  https://code.visualstudio.com/ . 3.      Make sure you download the right software depending on the operating system(Windows, MAC, Linux) you have in your system. 4.      Install it by double clicking on the downloaded  VSCodeUserSetup-x64-1.34.0.exe  file.  5.      Double click to open  VS code , Welcome screen should look like below.              Welcome Screen        6. Click on the  Extensions  icon on the left hand side marked red in image below.                   7. Click on the option or search for  "Python Extension Pack"  in the search field marked red and click on install button marked red to install it on your system.      8.Next goto  File menu  and select  "Open Folder"  Option to select the folder where you want to save all your python scripts.