Python and Numpy

Introdution

The Python is a programming language that currently has a model for community development, open and managed by nonprofitthe principal whose main features are:

  • Interpreted Code
  • Multiplatform
  • Language and simple and usual
  • Object Oriented

In its structure has some Python statements, calls from high level they are:

  • Integers, Floats and Boolean (Basic)
  • Lists, Tuples, Strings, Dictionaries, Files and Classes and Instances. (Special)

In case of Lists and Tuples, they are very similar and we call sequences in which figures are indexed and store any value. Their only difference is that tuples once created, can not be changed.

Note: If you have any questions during the program which type of variable assumes you can use the command type(object).

Numpy

Numpy is the basic package of Python that lets you work with arrays, vectors and arrays of N dimensions. Written with an easy, he is very similar to matlab, but nevertheless effective because of its fast processing. Properties narray: ndarray.ndim, ndarray.shape, ndarray.size, ndarray.dtype, ndarray.itemsize, ndarray.data

Type of numpy.

Data type Description
bool Boolean (True or False) stored as a byte
int Platform integer (normally either int32 or int64)
int8 Byte (-128 to 127)
int16 Integer (-32768 to 32767)
int32 Integer (-2147483648 to 2147483647)
int64 Integer (9223372036854775808 to 9223372036854775807)
uint8 Unsigned integer (0 to 255)
uint16 Unsigned integer (0 to 65535)
uint32 Unsigned integer (0 to 4294967295)
uint64 Unsigned integer (0 to 18446744073709551615)
float Shorthand for float64.
float16 Half precision float: sign bit, 5 bits exponent, 10 bits mantissa
float32 Single precision float: sign bit, 8 bits exponent, 23 bits mantissa
float64 Double precision float: sign bit, 11 bits exponent, 52 bits mantissa
complex Shorthand for complex128.
complex64 Complex number, represented by two 32-bit floats (real and imaginary components)
complex128 Complex number, represented by two 64-bit floats (real and imaginary components)

Numpy types examples

  • Tipo numérico:
 1 # interger type
 2 interger_example = 3
 3 print type(interger_example)
 4 
 5 # float type
 6 float_example = 3.14
 7 print type(float_example)
 8 
 9 # tipo bool
10 bool_example = True
11 print type(bool_example)
12 
13 # tipo complex
14 complex = 3 + 1j
15 print type(complex)
<type 'int'>
<type 'float'>
<type 'bool'>
<type 'complex'>
  • Lists:
 1 # list type
 2 list_example= [3, "maria",6.7,[3.14,"luis"]]
 3 print type(list_example)
 4 
 5 # negative indices
 6 print list_example[-1]  # acess final elements from the list
 7 
 8 # slices
 9 print list_example[:2]  # list slice until element of indice 1
10 print list_example[1:]  # list slice from element of indice 1 until the last element
11 print list_example[1:3] # list slice from element of indice 1 until element of indice 2
12 
13 # lists methos examples
14 list_example.append(3)   # add an element in the end of the list
15 list_example.insert(0,3) # add an element in the indicated position
16 list_example.remove(3)   # remove the first element that possess the input parameter value
17 list_example.sort()      # sort the list elements in ascending order
18 print
19 print list_example
<type 'list'>
[3.1400000000000001, 'luis']
[3, 'maria']
['maria', 6.7000000000000002, [3.1400000000000001, 'luis']]
['maria', 6.7000000000000002]

[3, 3, 6.7000000000000002, [3.1400000000000001, 'luis'], 'maria']
  • Tuples:
1 # tuple definition
2 tuple_example = (3, "raquel",6.7)
3 print type(tuple_example)
4 # tuples nesting
5 tuple_example = tuple_example,(1,2,3,4)
6 print tuple_example
<type 'tuple'>
((3, 'raquel', 6.7000000000000002), (1, 2, 3, 4))
  • Strings:
1 # tipo string
2 string1 = "grapes and"
3 string2 = " watermelon \t"
4 print string1+string2
grapes and watermelon
  • Dictionaries:
1 # dictionary type: (key/value)
2 dictionary_example =  {1:"pink",2:"blue",6:"purple"}
3 print dictionary_example
4 print dictionary_example.keys()
5 print dictionary_example.values()
6 dictionary_example.update({6:"red"})
7 print dictionary_example
{1: 'pink', 2: 'blue', 6: 'purple'}
[1, 2, 6]
['pink', 'blue', 'purple']
{1: 'pink', 2: 'blue', 6: 'red'}

Image Visualization

1 from numpy import *
2 
3 lena = adread('lena_color.jpg')
4 adshow(lena,'Color Image')
5 lena = adreadgray('lena_color.jpg')
6 adshow(lena, title='Gray Image')

Color Image

Gray Image