–tags: 3ddatarepresentation

Useful tricks:

  • Setting the subplot size:

    f, axs = plt.subplots(2,2,figsize=(15,15))

  • Permuting axes in torch tensor(Useful when torch images axes need to be changed):

    testimage = image[0].permute(1, 2, 0)

  • Adding rectangular box to images:

    ax.add_patch(rectangle)

  • patches from matplotlib.patches

  • patches will have function for a rectangle

  • Add empty numpy array
    • np.empty()

    • can be filled using matrix_name.fill(d)

  • Contiguous() call in pytorch:
    • When you call contiguous(), it actually makes a copy of tensor so the order of elements would be same as if tensor of same shape created from scratch.
  • Random permutation function in pytorch:

    rand_num_perm = torch.randperm(self.num_per_batch)

  • Expanding tensor without making a change to a dimension:
    • x = torch.tensor([[1], [2], [3]])

    • x.expand(-1, 4)

    • This means keep the first dimension intact while expanding. Here in the example, it’s 3. so the expand will return 3 by 4 tensor.

    • tensor([[ 1, 1, 1, 1], [ 2, 2, 2, 2], [ 3, 3, 3, 3]])

  • Convert 1 channel image to 3 channels:

    if len(im.shape) == 2: im = im[:,:,np.newaxis] im = np.concatenate((im,im,im), axis=2)

  • RGB to BGR image:

im = im[:,:,::-1]

Finding index of mask pixels first horizontal:

mask = np.zeros((800, 600, 4), dtype=np.int32) m = np.array(mask[:, :, 0]) np.where(np.any(m, axis=0))[0]

np.where gives the indexes of elements where the required condition meets

Get a combination of two different arrays:

scales, ratios = np.meshgrid(np.array(scales), np.array(ratios))

scales = scales.flatten() ratios = ratios.flatten()

heights = scales / np.sqrt(ratios)

In this way , we can operate over all combination of operations.

Get model’d few layers so that model can get output in between:

model = resnet50(pretrained=True)

f = torch.nn.Sequential(*list(model.children())[:4])

Itemsize for each item’s size :

x = np.array([1,2,3,4,5],dtype=np.int8) print x.itemsize # will print 1

x = np.array([1,2,3,4,5],dtype=np.int32) print x.itemsize # will print 4

Flags for numpy array:

x.flags

Numpy empty array:

import numpy as np a = np.empty([3,2],dtype=int) print a

Numpy zeros special:

x = np.zeros((2,2),dtype=[(‘x’,‘i4’),(‘y’,‘i4’)])

Numpy order of arrays:

order = “C” or “F” for c type and fortran type arrays

C means major row style array and fortran is column major array

Can create numpy arrays from buffer as well:

numpy.frombuffer(buffer,dtype=float,count=-1,offset=0)

count is the number of lines to read from the buffer

offset to change the starting position from where it is to be read

numpy linspace params:

retstep: returns the step size with the array made by linspace

endpoint = True or False will make sure endpoint is either included or not in the array

slice object to slice a numpy array:

x = np.linspace(1,10,10) s = slice(2,7,2) # takes start, end and step as params print(x[s])# will print the sliced items from the x array

But this can be done directly in numpy arrays

Use of ellepsis in array slicing:

a = np.array([[1,2,3],[3,4,5],[4,5,6]]) print a[…,1:]

Complex arrays also can be made in numpy and also can be filtered as:

a = np.array([1,2+6j,5,3.5+5j]) print a[np.iscomplex(a)]

Flatten usage temporary: x = np.array([[1,2],[3,4]]) x.flatten()[1] = 100 print(x) # still prints the same matrix

x.ravel()[1] = 100

swap axes in numpy array:

array.swapaxes(1,2) and similar implementations

move axes:

a = np.arange(24).reshape([2,3,4]) b = np.moveaxes(a, 2, 0)

expand dimensions:

x = np.ones((2,2)) y = np.expand_dims(x,axis=0) z = np.expand_dims(x,axis=1) a = np.expand_dims(x,axis=2)

y is 1,2,2 z is 2,1,2 a is 2,2,1

Squeeze to remove axes of dimension 1 anywhere they are in shape matrix

Concatenate two arrays: by default concat occurs with axis = 0 , if other axis you need to specify.

Hstack, vstack are simply concat done axis = 0 and axis = 1

simply hsplit, vsplit occurs to split the arrays horizontally and so on.

Splitting ndarrays:

x = np.split(a, subsection [2,7] or no of splits to be made 3, axis=0)

Numpy character functions:

np.char.add([‘hello’],[’ xyz’])

np.char.multiply(‘Hello ‘,3)

numpy.char.center(arr,width,fillchar)

e.g. np.char.center(‘hello’,20,fillchar=’*’)

np.char.capitalize(‘hello world’)

np.char.title(‘hello how are you?’)

lower and upper functions

split with sep option

strip and choose which to strip . can do it in arrays too.

np.char.join(’:’,‘dmy’)

replace, and also encode functions are available

Rounding to decimal places:

np.around(a,decimals=1)

np.around(a,decimals=-1) # rounds to nearest 10s places: 0, 10, 120, and so on.

Reciprocal function to do 1/sth

mod function to mod on two arrays

Power function to do power on two arrays: one will be the base and the other will be the power

amax and amin to get the max or min in the array or along certain axes

ptp to get the range of values in an array or along the axes

percentile function to get percentile values along the whole array or along specific axes

mean, median, average with support of weights matrix

std and var to calculate deviation and variance resp.

Sorting searching and stuffs:

np.sort(ndarray) – > will sort the individual rows i.e. axis = 0

or you can just give the axis to sort about to sort by column and so on.

numpy has dtype function to specifically give the pararmeters like definition for specific arrays

say for person’s array , we can have dtype as:

person_dtype = np.dtype([(’name’,‘S10’),(‘age’,int)])

a = np.array([(“raju”,21),(“anil”,25),(“ravi”, 17), (“amar”,27)], dtype = dt)

np.sort(a,order = ’name’)

argsort to get the index of sorted array so that it can be used to order other related arrays as well.

nonzero function : gives the index of non zero elements in ndarray. The indexes will be such that x indices in first array and y indices in second array.

np.where(condition) also gives index in row, column format so passing such value to ndarray as ndarray[index_of_rows_and_cols] will give the resp elements

extract elements satisfyig a condition:

cond = np.mod(x,2) == 0

res = np.extract(cond,x)

byte swap function:

a.byteswap(True) # will be helpful to convert big endian to little and so on.

id function to get the id of a matrix

just assigning one matrix to other will not change the id. So , any change in one will result in the change of the other.

So we can use b = a.view() to make a different view for b which will have OWNDATA false. Now the shape may be different in showing and using them but the underlying data they use is the same.

so any changes in b will change the data of a though they are not similarly showing.

so make a matrix as b = a.copy() –> this will have rights over the data as own data and they will be different from each oter.

byteswap function is very risky if you don’t properly care how to use it.

  • zeros

  • empty

  • ones

  • eye: identity matrix: option to specify no of rows, columns, index of diagonal, dtype: can make variants of row echleon matrices

  • identity: normal square identity matrix

  • rand

    this type of matrix can be casted to ndarray when required using np.asndarray(matrix)

Some useful Functions

  • vdot: flattens multidim arrays first and then dots
  • dot: normal dot product of matrices
  • inner function to perform dot product of both real as well as complex numbers
  • multi_dot : function to calculate dot products of mutliple arrays by efficiently choosing best scheme for dot product
  • tensordot: dot along specific axes
  • linalg
  • eigvals: gives eigen values of matrix
  • eigenvalsh: eigen vals of complex hermitian or real symmetric matrix
  • det: find the determinant of a matrix
  • matrix_rank: matrix using SVD method
  • inv: multiplicative inverse of the matrix
  • pinv: moorse penrose psedudo inverse of a matrix

Broadcasting Arrays

  • Broadcast happens when the sizes of matrix operation doesn’t meet. e.g. adding 3 by 3 matrix with 1 by 3 makes the 1 by 3 repeat 3 times and add with all rows.

  • Shapes need to be compatible in order for broadcast to occur.

  • When operating on two arrays, NumPy compares their shapes element-wise. It starts with the trailing dimensions, and works its way forward.

  • Two dimensions are compatible when they are equal, or one of them is 1