Computer Vision Notes (1)

Python generator and ‘yield’ statement

1
2
3
4
5
6
7
8
9
10
import numpy as np

def numbers(n):
for i in range(n):
num = np.random.randint(1, 40)
print('generated the {}-th number = {}'.format(i+1, num))
yield num

num_gen = numbers(10000000)
first = next(num_gen)

Map, reduce and filter (lambda function)

  • Python map() function

    • map() function returns a list of the results after applying the given function to each item of a given iterable (list, tuple etc.)

    • Syntax:

      1
      map(func, iter)
    • Parameters:

      func: It is a function to which map passes each element of given iterable.
      iter: It is a iterable which is to be mapped.

    • NOTE: You can pass one or more iterable to the map() function.

  • Python reduce() function

    • The reduce(fun,seq) function is used to apply a particular function passed in its argument to all of the list elements mentioned in the sequence passed along.This function is defined in “functools” module.
    • Working:
      • At first step, first two elements of sequence are picked and the result is obtained.
      • Next step is to apply the same function to the previously attained result and the number just succeeding the second element and the result is again stored.
      • This process continues till no more elements are left in the container.
      • The final returned result is returned and printed on console.
  • Python filter() function

    • The filter() method filters the given sequence with the help of a function that tests each element in the sequence to be true or not.

    • syntax:

      1
      filter(func, seq)
    • Parameters:

      func: function that tests if each element of a sequence true or not.

      seq: sequence which needs to be filtered, it can be sets, lists, tuples, or containers of any iterators.

Linear algebra with Numpy

Basic

1
2
3
4
5
6
7
8
9
10
import numpy as np

A = np.diag((1, 0))
print('A=\n', A)

B = np.identity((2), dtype=int)
print('B=\n', B)

C = np.ones((3,2), dtype=int)
print('C=\n', C)

References