Wednesday, March 22, 2017

numPy Python module - Revisit

I know that there is a module called 'numPy' in python  from long time before, but I did not care that one much, thinking it is just library for math functions.

If you try to learn Machine learning, then it is important to learn the numPy module as a prerequisite for ML. The calculation in Machine learning functions are build with matrix or column vector manipulations.

I remember that how difficult is it to do matrix manipulations in C programming. Usually it will come as a lab exam in college :-) ( that skill is needed),but if you are really focusing on applications or functionalities , the calculations part should be minimal and easy. Numpy module gives that freedom to focus on the applications part.


from numPy import *

matrix = array([[1,2,3],[3,4,5]])

the above single statement give you a matrix 2 columns and 3 raws.  how cool it is !!!

you can get the matrix dimension with following python statement

matrix.shape

Matrix addition - looks so simple as shown below






if you check the data type for 'matrix' you will get the below -

>>> type(matrix)
"<"class numpy.ndarray="">

Tuesday, March 21, 2017

5 things I find very useful and new in Python programming

1.  List - (a list of values. Each one of them is numbered, starting from zero )

I find it very handy in Python programming language , because looping through list and reading each values are very easy in List.


num = [3,4,5,6]

items = ['desk','pen','pencil']

you can read through List very easily as below -























2. split function


You can split a string with any seperator and put into a list very easily with this function.











3.Map function


Map applies a function to all the items in an input_list.



Output:

{1,4,9,16]






4. Reduce Function:

Reduce is a really useful function for performing some computation on a list and returning the result.

Below example we are passing the 'sum' function to inbuild 'reduce' function and getting the total sum for the list of numbers.




Output:
14





5.Some thing you find very strange in Python ( Mutable and Immutable objects)

I thought it was bug, but its not ..

eg:
First Lets me create a list as below.

a = [1,2]


then simply create another list 'b' and assign  'a' + [3]

b= a;
b+=[3]

what you will expect on 'b' .. obviously .. [1,2,3]

But what you expect on 'a'... I thought that i didn't change anything in 'a' , so it should be same, but what actually 'print a'  become [1,2,3]

read below link for more details - http://book.pythontips.com/en/latest/mutation.html

https://codehabitude.com/2013/12/24/python-objects-mutable-vs-immutable/

 










Is ML(Machine Learning) a Programming Style ?

I remember that I started programming with declaring variables and doing multiplication, addition , 'for/while' loops etc.. Suddenly one day my Plus1 computer teacher started writing functions, those days I didn't know that I was moving to a new programming style . Functions were looks very natural and nothing surprising to me. But when I came to know that some of programming languages only running with functions(but it took years to realize that :-))  ..check this link Functional Programming
I came to know that this can be adopted as a programming style.

When I moved to Plus 2 standard,  I think that is time when I started hearing about about objects oriented programming. I felt so surprising that "Why the teacher is taking me to different different words/concepts like inheritance,polymorphism ,abtraction etc.." ok I just bi-hearted them to pass the exams. But I can remember that my teacher was trying to understand me that 'object oriented programming' is new programming style.

Now I started going through some of the ML(machine learning) tutorials, I am getting same doubt like 'Is it going to be a new programming style or Is it the future programming method ?'