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

Wednesday, January 29, 2020

pyplot

Provides a MATLAB-like plotting framework.
pylab combines pyplot with numpy into a single namespace. This is convenient for interactive work, but for programming it is recommended that the namespaces be kept separate, e.g.:
import numpy as np
import matplotlib.pyplot as plt

x = np.arange(0, 5, 0.1);
y = np.sin(x)
plt.plot(x, y)

Thursday, January 23, 2020

Glob module in python


Usually, the programmers require to traverse through a list of files at some location, mostly having a specific pattern. Python’s glob module has several functions that can help in listing files under a specified folder. We may filter them based on extensions, or with a particular string as a portion of the filename.
All the methods of Glob module follow the Unix-style pattern matching mechanism and rules. However, it doesn’t allow expanding the tilde (~) and environment variables.

Wednesday, January 22, 2020

MoviePy




MoviePy is a Python module for video editing, which can be used for basic operations (like cuts, concatenations, title insertions), video compositing (a.k.a. non-linear editing), video processing, or to create advanced effects. It can read and write the most common video formats, including GIF.
Here it is in action (run in an IPython Notebook):

Geometric Image Transformation




Geometric Image Transformations

The functions in this section perform various geometrical transformations of 2D images. They do not change the image content but deform the pixel grid and map this deformed grid to the destination image. In fact, to avoid sampling artifacts, the mapping is done in the reverse order, from destination to the source. That is, for each pixel (x, y) of the destination image, the functions compute coordinates of the corresponding “donor” pixel in the source image and copy the pixel value:
\texttt{dst} (x,y)= \texttt{src} (f_x(x,y), f_y(x,y))
In case when you specify the forward mapping \left<g_x, g_y\right>: \texttt{src} \rightarrow \texttt{dst} , the OpenCV functions first compute the corresponding inverse mapping \left<f_x, f_y\right>: \texttt{dst} \rightarrow \texttt{src} and then use the above formula.

Tuesday, January 21, 2020

matplotlib and pyplot

Startup commands

First, let’s start IPython. It is a most excellent enhancement to the standard Python prompt, and it ties in especially well with Matplotlib. Start IPython either at a shell, or the IPython Notebook now.
With IPython started, we now need to connect to a GUI event loop. This tells IPython where (and how) to display plots. To connect to a GUI loop, execute the %matplotlib magic at your IPython prompt. There’s more detail on exactly what this does at IPython’s documentation on GUI event loops.
If you’re using IPython Notebook, the same commands are available, but people commonly use a specific argument to the %matplotlib magic:

Tuesday, April 24, 2018

Mean, Median and Mode

Mean
The "average" number; found by adding all data points and dividing by the number of data points.


Sunday, April 22, 2018

Markdown Cheat Sheet (Jupyter Notebook)

Headers

# H1
## H2
### H3
#### H4
##### H5
###### H6

Alternatively, for H1 and H2, an underline-ish style:

Alt-H1
======

Alt-H2
------

Thursday, January 18, 2018

Using filters in Python



Creates a list of elements for which a function returns true. Here is a short and concise example:

number_list = range(-5, 5)
less_than_zero = list(filter(lambda x: x < 0, number_list))
print(less_than_zero)


List comprehension in Python

  • List comprehension is an elegant way to define and create list in Python. 
  • These lists have often the qualities of sets, but are not in all cases sets. 
  •  List comprehension is a complete substitute for the lambda function as well as the functions map(), filter() and reduce(). 
  •  Syntax of list comprehension is easier to be grasped.

Logical operators in Python


There are following logical operators supported by Python language
OperatorDescriptionExample
and Logical ANDIf both the operands are true then condition becomes true.(a and b) is true.
or Logical ORIf any of the two operands are non-zero then condition becomes true.(a or b) is true.
not Logical NOTUsed to reverse the logical state of its operand.Not(a and b) is false.

Conditional statements in Python


The if-then construct (sometimes called if-then-else) is common across many programming languages, but the syntax varies from language to language.

The general form of the if statement in Python looks like this:

if condition_1:
    statement_block_1
elif condition_2:
    statement_block_2
else:
    statement_block_3

Comparison operators in Python


Python Comparison Operators These operators compare the values on either sides of them and decide the relation among them. They are also called Relational operators.

Loops in Python



  • There are two types of loops in Python, for and while. 
  •  For loops iterate over a given sequence. 

Note: For loops can iterate over a sequence of numbers using the "range"
Here is an example:

Wednesday, January 17, 2018

Sets in Python



  • Unique set of collections
  • Looks the same as dictionary with the curly braces { }. It does not have { ' ',' ' }
  • If  duplicates are discarded and not added to the collections

Tuples in Python


  • Tuples are sequence of immutable objects
  • They do not support item assignment
  • They are created using ()
Example
tup1 = ('physics', 'chemistry', 1997, 2000)
tup2 = (1, 2, 3, 4, 5 )
tup3 = "a", "b", "c", "d"
tup1 =(); //Empty tuples

Dictionary in Python


  • Created by using bracket
  • Key value pairs
  • Each key is separated from its value by a colon (:), the items are separated by commas, and the whole thing is enclosed in curly braces
  • Do not have any  order
Accessing Values in Dictionary
To access dictionary elements, you can use the familiar square brackets along with the key to obtain its value. 
#!/usr/bin/python3

dict = {'Name': 'Zara', 'Age': 7, 'Class': 'First'}
print ("dict['Name']: ", dict['Name'])
print ("dict['Age']: ", dict['Age'])

Lists in Python



Python Lists

  • The list is the most versatile datatype available in Python, which can be written as a list of comma-separated values (items) between square brackets. 
  • The items in a list need not be of the same type.
List is a collection which is ordered and changeable. Allows duplicate members.
Tuple is a collection which is ordered and unchangeable. Allows duplicate members.
Set is a collection which is unordered and unindexed. No duplicate members.
Dictionary is a collection which is unordered, changeable and indexed. No duplicate members.

Creating a list is as simple as putting different comma-separated values between square brackets. For example −
list1 = ['physics', 'chemistry', 1997, 2000];
list2 = [1, 2, 3, 4, 5 ];
list3 = ["a", "b", "c", "d"];