Extract patches from 3D Matrix

I have a 3D matrix A of dimensions h x w x c. I want to extract patches of dimensions ph x pw from each "channel" c. ph divides h and pw divides w. In this example, h x w x c = 4 x 4 x 3 ph x pw = 2 x 2 I know how to do this in tensorflow using ...
more »

2017-01-11 09:01 (1) Answers

Emulating fixed precision in python

For a university course in numerical analysis we are transitioning from Maple to a combination of Numpy and Sympy for various illustrations of the course material. This is because the students already learn Python the semester before. One of the dif...
more »

2017-01-10 10:01 (1) Answers

Speed up Pandas cummin/cummax

Pandas cummin and cummax functions seem to be really slow for my use case with many groups. How can I speed them up? Update import pandas as pd import numpy as np from collections import defaultdict def cummax(g, v): df1 = pd.DataFrame(g, col...
more »

2017-01-07 21:01 (2) Answers

Average face - algorithm

Quesion and code is at the end I hope this question belongs here and not to the TCS's stack. I am trying to go through algorithms in Turk and Pentland's "Eigenfaces for Recognition". On page 74 one can read (last paragraph of left column): Let t...
more »

2017-01-03 22:01 (1) Answers

Checking if Adjacent Values are in a Numpy Matrix

So, I am currently trying to figure out a more optimal solution to determine connected components in an image. Currently, I have an array with coordinates that have certain values. I want to create groups of these coordinates based on if they are tou...
more »

2017-01-01 21:01 (2) Answers

saving numpy matrix in text file with indices

I am trying to save a 2D matrix A to text file for each non-zero entry. I would like to save it in the below format, For the (i,j)-th entry, one line as row[i]::column[j]::A[i,j] where row and column are 1D numpy array corresponding to rows an...
more »

2016-12-27 23:12 (2) Answers

numpy ufuncs speed vs for loop speed

I've read a lot "avoid for loops with numpy". So, I tried. I was using this code (simplified version). Some auxiliary data: In[1]: import numpy as np resolution = 1000 # this parameter varies tim = np.lin...
more »

2016-12-26 01:12 (1) Answers

Mask out specific values from an array

Example: I have an array: array([[1, 2, 0, 3, 4], [0, 4, 2, 1, 3], [4, 3, 2, 0, 1], [4, 2, 3, 0, 1], [1, 0, 2, 3, 4], [4, 3, 2, 0, 1]], dtype=int64) I have a set (variable length, order doesn't matter) of "bad"...
more »

2016-12-25 06:12 (3) Answers

Can this cython code be optimized?

I am using cython for the first time to get some speed for a function. The function takes a square matrix A floating point numbers and outputs a single floating point number. The function it is computing is the permanent of a matrix When A is 30 ...
more »

2016-12-24 17:12 (2) Answers

Finding the index of a numpy array in a list

import numpy as np foo = [1, "hello", np.array([[1,2,3]]) ] I would expect foo.index( np.array([[1,2,3]]) ) to return 2 but instead I get ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or ...
more »

2016-12-21 22:12 (4) Answers

Convert 0s and 1s from text file to a 2-D array

I am given a text file that only has 0 and 1 values. One example is: 010 111 010 I need to take these values and turn them into a 2-D array. So for the example, I should get an array like this: [[0,1,0] , [1,1,1], [0,1,0]] Each text file has a ...
more »

2016-12-19 20:12 (3) Answers

Strange copy construction of numpy array

The example below is odd to me. Arrays a and c are different, but at modification of the first element of a, the first element of c changes as well. Why is the numpy array implemented like this? If a is assigned as a list, changing the first element ...
more »

2016-12-18 21:12 (1) Answers