Showing posts with label Python. Show all posts
Showing posts with label Python. Show all posts

Saturday 30 January 2016

Quick Python Pandas Basics


Lets Learn Pandas

In [1]:
import pandas as pd

Pandas series


pandas series is similar to numpy array, But it support lots of extra functionality like Pandaseries.describe()
Basic access is similar to numpy array, it support access by index( s[5] ) or slicing ( s[5:10] ).
It also support vectorise operation and looping like numpy array.
Implemented in C so it works very fast.

Note : Get Code for offline testing  Github_Link or nbviewer.jupyter link

Learn Basic of Numpy

Benfits of Pandas series

In [8]:
s=pd.Series([2,3,4,5,6])
print s.describe()
count    5.000000
mean     4.000000
std      1.581139
min      2.000000
25%      3.000000
50%      4.000000
75%      5.000000
max      6.000000
dtype: float64

Friday 29 January 2016

Basics of Numpy & Pandas


Numpy and pandas are very popular packages used for data analysis. this post provide you quick guide to learn the basics of these. We assume you are familiar with vector operation . 

Numpy is mostly written in C & pandas are written over numpy & also uses C programming language as core implementation So these two are fast as compare to general data structure provided with python.

Numpy array is similar to python list but it contain all data of same data-type & it is much faster.
we can also treat numpy array as a vector.

Pandas uses scalar instead of list or numpy array but we can also visualize it as numpy array with some advance functionalities. 

Example

Numpy uses array whereas pandas used scaler
In [2]:
import numpy as np

Array are similar to python list , but it all element must be of same data type, and it faster than list

In [13]:
num = np.array([3,4,2,5,7,23,56,23,7,23,89,43,676,43])
num
Out[13]:
array([  3,   4,   2,   5,   7,  23,  56,  23,   7,  23,  89,  43, 676,  43])

Lets see some of functionality

Importing CSV File in Python


CSV file is one of most common used format we encounter. Let see few technique to efficiently import these data to python for better & efficient use.
CSV is also known as comma separated format. you can easily write a code to import CSV data .
But today we will see some of very efficient way to do so.

Python provide few package like CSV, unicodecsv, pandas Thease packages help you to do your job.

  • CSV : package follow basic approach & slow , it reads data line by line & split.
  • unicodecsv : Import data & create a list of dictionaries each data is associated with its attribute as key. So it  coud be very usefull in many cases but this process is also slow
  • pandas : This technique is very fast as compare to  above two methods.
You can download & test in your computer using Ipython notebook [Github_link]

Example

In [2]:
import unicodecsv
import pprint
import csv
In [3]:
csv_file_name="enrollments.csv"

Json Import of csv file

Import data in json formate, All data are imported as string

In [16]:
enrolment = []

f=open(csv_file_name,'rb')

reader =unicodecsv.DictReader(f)
#reader is a itterater so loop is possible only once
print "type(reader) =",reader

#for each_row in reader:
#    enrolment.append(each_row)
enrolment=list(reader) #shorthand for above two line

#close file
f.close()

print "Total no of row : ",len(enrolment),"\n\n"

#print demo data
pprint.pprint(enrolment[1])
type(reader) = <unicodecsv.py2.DictReader instance at 0x04A10A08>
Total no of row :  1640 


{u'account_key': u'448',
 u'cancel_date': u'2014-11-10',
 u'days_to_cancel': u'5',
 u'is_canceled': u'True',
 u'is_udacity': u'True',
 u'join_date': u'2014-11-05',
 u'status': u'canceled'}

## Simple import


Wednesday 20 January 2016

Pattern Search using Regular Expression in Python

In our daily life we found lots of problem of pattern search. like find all mobile numbers, or emails etc  from  given web page or from any file.

Writing manual code for that is not efficient and also very messy. Regular Expression is very popular technique used for pattern search (All compiler & interpreter use it ) & it is very easy to implement & it is very efficient .

I suggest you to write a code for extracting all emails from a webpage without using regular expression & test it .
Emails found on a webpage may follow some of these pattern like

 abc_7@gmail.com
 abcd(at)hotmail.in
 abcd@@stanford.edu.org
 abcd@mnp(dot)com

Tuesday 10 November 2015

Pong Game In python

This is very easy if you are intermediate in python and good in basic physics and basic maths.
You need to know basics of reflection, velocity, acceleration, and basics mathematics in order to calculate collision .When boundary of pong just touches wall or paddle are situation of collision. rest of thing is just basic python.

We need to increase velocity of pong with time so we add some constant small  acceleration that is being triggered after some small time interval . why small because if we use large acceleration or time then change in velocity will be sudden, so speed of pong increase suddenly.

Thursday 24 September 2015

Python In Sort

Python is one of most famous Programming Language. This is summary of python programming language for Quick Start to Python Programming .

{ Zoom or download picture}

Saturday 29 August 2015

Vigenere Cipher

A vigenere Cipher uses a different strategy to create the key stream. The key stream is repetition of initial key stream of length m  where we have  1<=key<=26 .


Auto Key Cipher

It is a very simple poly alphabetic cipher. In this cipher previous character of plain text used as key for encryption of next character using shift ( Caesar )cipher. And  for encryption of first character of plain text we use predetermine value secretly agreed upon by both communicator.

Tuesday 21 July 2015

Check Panagram

 Pangrams are sentences constructed by using every letter of the alphabet at least once.
Example : "The quick brown fox jumps over the lazy dog"
 Write a program to check given sentence to paragraph is pangrams or not.

Steps:

  1. create a list of all char a...z  in small letters say list is ls.
  2. convert given string to small letters.
  3. Now chose char from given string one by one if that is found in ls then remove it from ls.
  4. Repeat step 3 until ls is empty of given string ends.
  5. if  ls is empty after step 4 then it is pangrams else not pangrams.

Thursday 8 January 2015

Play With Prime

Prime numbers :- Numbers that are only divisible by 1 and by itself .

Now think how to check number is prime or not

Basic Approach

 Check Divisibility of number form 2 to n-1 is it is not divisible then prime .

Friday 26 September 2014

Number Of Occurence

One Of very commonly asked problem in Job Interviews is To find number of occurrence of given word in given paragraph . This very easy question but people still make mistakes .

How to do it  :-

  • Find first occurrence of first character of word in paragraph 
  • If found >>  from that point check next character of word match with next character of paragraph
  • Check until last character of word match with consecutive next character of paragraph
  • If all character match increment  count by 1.
  • Repeat all above statement but now search perform on paragraph started at index of end of last character until which our search is completed 

Reverse Integer

This Is one of Very Basic Programming Problem  for beginner . But If you never Did it , Try it once.

Reverse Given Number :- 

Example >> Let Number is 12345 then after reversing Number Should be 54321

There is two way to do it

Way 1:-

Pop Last Digit then push it to another variable and repeat procedure until       number == 0
  • temp = num%10
  • num = num/10
  • numr = numr*10 + temp
  • repeat above step until num==0

Thursday 25 September 2014

Length of string using Recursion

Finding Out Length  of  a String Is very easy task . you can directly use inbuilt function like strlen() or len() . 
But if there is restriction to not use these function then how find length of a string .

Length Using Loop : count number of element using for loop or while loop

  1. # Using For LOOP
  2. count=0
  3. st='http://beginer2cs.blogspot.com'
  4. for ch in st:
  5.     count +=1
  6. print count

Friday 12 September 2014

Even or Odd

Checking Number is Even Or Odd Is very Easy Task .

  1. num=int(raw_input())
  2. if num%2==0:
  3.     print 'Even'
  4. else:
  5.     print 'Odd'


But If Someone say I gave you lots of Numbers to check Until I satisfied that your Program is correct .
And number is in range 0 to 10^10000

Sunday 13 July 2014

Floyd's Triangle

Floyd's Triangle :

1
2  3
4  5  6
7  8  9  10
..
and so on ..


Armstrong Number

Armstrong Number : Numbers who's sum of cube of digits of number is equal to same given number. Then number is called Armstrong Number .

Algorithm for Armstrong Number :


Concept of  Armstrong Number is basically Algorithm to find it . Find sum of cube of digit ,way of finding this may differ according to programming language you choose .

I use Python 2.7 So I can directly convert number into string and accessing character by character and hence find sum of cube of digits

Monday 7 July 2014

Palindrome Number

Palindrome number are very common question we face in programming field .

Palindrome number :

Numbers that remain the same when digit is reversed .
Example : 2, 202 , 101 ,191, 99599, 76567 these are palindrome number

Not palindrome : 123, 3452, 6789, 23434 4561, 34546

How check Number Is Palindrome Or Not :

There are many ways to  check number is palindrome :

Thursday 3 July 2014

How to Find prime factor of a number

Find Prime factor of a number is one of most common question  encounter in basic mathematics . But it has very high uses not only in mathematics problems but also in high level algorithmic programming problems. Mostly problems that uses properties of numbers .
If you wan't to become a programmer ,you will face many problems that uses properties of numbers , and we know a good programmer always try to find best way to solve problem otherwise there is always a straight way (Brute force way ) to solve almost any problem .
But we know it will ruin speed of  programming  result  that's why we always try to find fastest and effective way to solve problems .

Algorithm to Find prime factor of a number :

It uses very basic concept of division

Friday 27 June 2014

Karatsuba Multiplication

This is a Non-traditional way of multiplication .But due to its better time efficiency in computer application It can use as better way of multiplication in computer program.

Note : let we have to find X*Y then Karatsuba Multiplication is applicable if and only if both X &Y have same number of digits .
Ex : x=2345  & y =4535 both have 4 digits


Friday 2 May 2014

Insertion Sort , In Python

Insertion Sort is one of easiest & efficient way to sort small group of numbers . Using Python this algorithm is very very simple to implement .
But in other languages like C it is not so easy ..

Read actual algorithm & its implementation in C .
Insertion sort Algorithm & implementation in C

Python 2.7.6 Implementation of Insertion sort Algorithm

Algorithm is well explain in program comment

Blogger Widgets