Today I’m gonna brush upon the concepts of Lambda Functions in an overview. Follow up of ipython notebook for lambda functions. Lambda functions allow us to inline the definitions inside the map functions. Lambda functions provides the flexibility to provide within the map function along with filter to extract more abstract data.
for example lets take a range of 20 numbers we wanted to return the numbers which are divisible by 2
You could do normally with an iteration like
seq = range(20) [num**2 for num in seq if num %2 == 0]
If you would like to utilize map, filter it could be like
?map #To know about the documentation ?filter map(lambda x: x**2, lambda x: x %2 == 0, seq) 
For reference checkout the ipython notebook in the link above.
This finishes the tutorial of Ipython and Raw Python Data Analysis.
I’m going to start the Numpy and will be covering following topics:
- HelpFul Methods and Shortcuts
- Vectorization
- Multi-Dimensional Arrays
- Boolean Selection
- Querying, Slicing and Splitting Arrays.
Numpy is the fundamental base for scientific computing. It is the base package for several machine learning tools like pandas, matlplot, sci-kit etc.,
In the Ipython shell if you would like to learn more about the numpy arrays. Please checkout the numpy documentaion
?numpy
In short it helps, to perform random number generation, fast calculation, Linear Algebra , Fourier transforms and a lot of complex mathematical computation. More Documentation is available in the numpy website. For more ipython notes check this link
Lets take a loot at Boolean Selection , it helps us to filter certain values just like filter in list comprehension. For example in traditional shell, if we wanted to get the list of numbers divisible by 2 out of 20. we give like:
seq = arange(20) [x for x in seq if x % 2 == 0]
But in a numpy array you could simple apply the condition
import sys import numpy as np npa = np.arange(20) npa[npa % 2 == 0]
Also the cool thing about ipython shell is you could majic functions to display inline graphs, benchmarking the time execution of certain values.
%timeit #Above is a majic function to measure the time of the execution.
Lets take the sample divisibility example to take a look at the timeit
npa = np.arange(20) %timeit [x for x in npa if x % 2 == 0] <p class="p1"><span class="s1">#Result :The slowest run took 8.50 times longer than the fastest. This could mean that an #intermediate result is being cached</span></p> <p class="p1"><span class="s1">100000 loops, best of 3: 10.2 µs per loop</span></p> <p class="p1"># Result HAHA This is because I didn't turn off my machine form a week almost and lots of load</p> <p class="p1">#on processor.</p>
The sample implementation if you use it with boolean selection results are way #too faster.
%timeit npa[npa % 2 == 0] <p class="p1"><span class="s1">#Result: 1000 loops, best of 3: 405 µs per loop</span></p>