Friday 29 June 2018

Defining CSS files

In an html file we can add style using CSS i.e Cascading style sheets.
CSS files can be defined in 3 ways:
Inline
Internal
External

An inline CSS file the styling code is used to apply style to single html element. A shown in this example the whole paragraph element is styled with red font.

<html>
<head>
<title>Inline Css example</title>
</head>
<body>
<img src="tanush.jpg" width="100" height="100">
<p style="color: red">My name is Tanush Agarwal. </p>
</body>
</html>
The inline style is added to the element directly.

An Internal CSS is used if the style property is to be applied to whole page.Internal styles are defined within the <style >element on the <head> section of html page. In the following example if inline style is to be applied then we have to write the style code with each html element or we can apply the property in head section within <style> element.

<html>
<head>
<title>Internal Css example</title>
<style>
p {
color: red;
}
</style>
</head>
<body>
<p> My name is Tanush Agarwal. <br></p>
<p>another paragraph</p>
</body>
</html>

The most used way of adding styles is External CSS . In it we create a separate file with extension .css and in it write the code for styling. The two file are:
/external.html
<html>
<head>
<title>External Css example</title>
<link href="stylesheets/external.css" rel="stylesheet">
<body>
<p> My name is Tanush Agarwal. <br></p>
<p>another paragraph</p>
</body>
</html>

../stylesheets/external.css
p{
color:red;


}








Monday 4 June 2018

Python lambda expression

The lambda expression are used to create small anonymous one-line functions. They are basically created at run-time and are not bound to the name of the functions. So they are also called throw-away functions.i.e they are just needed where they have been created.They return definition of the function on the fly. Lambda functions don't have  a return statement , they always return an expression.
They are mostly use along with another function or inside a function definition.
Syntax :

lambda [arg1 [,arg2,.....argn]]:expression 
The lambda operator is mostly used in combination with functions map, filter and reduce. We can put a lambda definition anywhere a function is expected.

For example :
1. use with filter() function

multiple_3 = filter(lambda x: x % 3 == 0, [1, 2, 3, 4, 5, 6, 7, 8, 9])
This function returns array of multiples of 3. Lambda expression has filtered out all the elements that are divisible by 3 from the given array. 

2. use with sorted()  function 

>>> sorted([1, 2, 3, 4, 5, 6, 7, 8, 9], key=lambda x: abs(5-x))
[5, 4, 6, 3, 7, 2, 8, 1, 9]
3. Defining with in another function

def make_incrementor(n):
  return lambda x: x + n
f = make_incrementor(42)
f(0)
>>> 42
f(1)
>>> 43

4. Use with reduce() function 
>>> reduce(lambda a,b: '{}',"{}".format(a,b),[1,2,3,4,5,6,7,8,9])
'1,2,3,4,5,6,7,8,'
5. Making a flatten list out of list of lists 

l = [[1,2,3],[4,5,6],[7,8,9]]
flatten = lambda l: [item for sublist in l for item in sublist]
print flatten(l)
will give output :
[1,2,3,4,5,6,7,8,9]

Sunday 3 June 2018

File handling

Python provide many facilities for creating and accessing files. Here we shall learn about some of the basic ones. Python achieves operating system independence by accessing files through something called a file handle.

For example the following code :

nameHandle = open('new_file' , 'w')
for i in range(2):
    name = raw_input('Enter Name :  ') 
    nameHandle.write(name + '\n ')
nameHandle.close()

opens a file 'new_file ' for writing, then it asks for two inputs which are separated by new line. After you run the code always remember to close the file.

We can now open the file for reading(using the argument 'r' instead of 'w'), and print its contents.
nameHandle = open('new_file' , 'r')
for line in namehandle:
    print line
nameHandle.close()

This  code will print the input given earlier for writing.

Now,  if we write something else to this 'new_file', our previous content will get overwrite. So to avoid such condition , we open the file for appending (denoted by 'a' ) rather than writing.
nameHandle = open('new_file' , 'a')
namehandle.write('Tanush')
namehandle.write('Kanishka')
nameHandle.close()
nameHandle = open('new_file' , 'r')
for line in namehandle:
    print line
nameHandle.close()
This  code will print the input given earlier along with the appended strings.







Time complexity

Time complexity is the computational complexity that measures the  time taken for running an algorithm.

The big O notation is used to express time complexity which is typically  is commonly expressed











where n is the input size measured by the number of bits needed for representing it.


Time complexity is estimated by counting the number of elementary operations performed by the algorithm. The amount of time taken and the number of elementary operations performed by the algorithm differ by atmost a constant factor.

  • Constant time Complexity :  O(1)  The number of operations for the algorithm doesn't actually change as the problem size increases. it is mostly a program that contains no loops or recursive calls. it can also be a for loop that runs a constant number of times.
         For example:
         a for loop with a constant range:
        >>> for i in range(0,c):
       print i
        This loop will run in constant time for  c = constant integer



  • Linear Complexity: O(n) .The algorithm is linear, doubling the problem size also doubles the number of operations required. Algorithms that deal with lists or similar kinds in which each element has to be accessed are linear because they touch each element of the sequence a constant (>0) number of times.




  • Quadratic Complexity O(n^2) :Doubling the problem size multiplies the operation count by four. Time complexity of 2 nested loops will give Quadratic Complexity.
  •  For example:
      >>> for i in range(0,c):      
                   for  j in range(0,c):
                            print i, j         
          Selection sort and Insertion Sort have O(n^2) time complexity


  • Logarithmic Complexity :   O (log(n)) Such functions have a complexity that grows as the log of at least one of the inputs. In the loop the constant factor is multiplied or divided by another constant factor. Binary search , is logarithmic in the length of the list being searched. The base used to take the logarithm makes no difference, since it just  multiplies the operation count by a constant factor. 
            >>> for i in range(0,c, /c):
       print i


  • Log linear Complexity:O(nlog(n)) Merge sort algorithm has log-linear time complexity

Python Debugger

Debugging is the process of looking for and resolving the issues in the code, that prevent the software from running correctly.

The Python debugger provides a debugging environment for Python programs. It supports setting conditional breakpoints, stepping through the source code on line at a time, stack inspection and show code as it executes.

At the top of your code import pdb module:
import pdb   

Now whereever you want to check the code insert break point :
pdb.set_trace() 

Following are the commands used by debugger at command line :

n                execute next line
c                 complete execution
l                  list 3 lines before and after execution
s                 step into function call
b                 show list of all break points
b[int]          set break point at line number
b [func]      break at function name
cl                clear all break points
cl[int]         clear all break points at line number
p                 print


For example  :

import pdb

def say_hello():
    pdb.set_trace()
    return "Hello pals"

print(say_hello())

A we run this program , we will run into a debugger :

> c:\python27\say_hello.py(5)say_hello()
-> return "Hello pals"
(Pdb) 


Now from the above commands we can run the debugger and locate the bug, if any.

Loan Prediction Analysis

In this post I will share my visuals ,learning and understanding of a dataset and using various algorithms of Machine Learning to analyse i...