How do I convert matrices from Matlab to Python?

I have the following code in Matlab which I'm not familiar with: function segments = segmentEnergy(data, th) mag = sqrt(sum(data(:, 1:3) .^ 2, 2)); mag = mag - mean(mag); above = find(mag>=th*std(mag)); indicator = zeros(size(mag...
more »

Searching only a portion of a matrix in python

I have a symmetric matrix and I am curious if it is possible to only search the upper triangle portion of the matrix using np.where. That is, is there a way to either delete the lower triangular portion of the matrix using a loop or a function so I c...
more »

Rounding elements in numpy array to the number of the same index in another array

For example: a=[[ 2.22323422 3.34342 ] [ 24.324 97.56464 ]] round_to= [[2 1] [1 3]] My expected output would be: a_rounded= [[ 2.2 3. ] [ 2. 97.6]] I would like to do this without slicing out each elem...
more »

Why would numpy.count_nonzero(arr) not work with assigned axis value?

I have a two dimensional array and would like to count values by row. Even though I assign an axis value, axis = 1, method still returns single integer. x = rng.randint(10, size=(6,3)) x Out[120]: array([[3, 3, 8], [8, 8, 2], [3, 2, ...
more »

Save Columns with Header and Line names in np.savetxt python

I have an array: array([[ 1., 5., 9., 13., 17., 21., 25.], [ 2., 6., 10., 14., 18., 22., 26.], [ 3., 7., 11., 15., 19., 23., 27.], [ 4., 8., 12., 16., 20., 24., 28.]]) ...
more »

Numpy, apply a list of functions along array dimension

I have a list of functions of the type: func_list = [lambda x: function1(input), lambda x: function2(input), lambda x: function3(input), lambda x: x] and an array of shape [4, 200, 200, 1] (a batch of images)...
more »

Pandas: Count the first consecutive True values

I am trying to implement a function that identifies the first consecutive occurrences in a Pandas Series, which has already been masked with the condition I wanted: (e.g.) [True, True, True, False, True, False, True, True, True, True] I want the ab...
more »

How does numpy's fancy indexing work?

I was doing a little experimentation with 2D lists and numpy arrays. From this, I've raised 3 questions I'm quite curious to know the answer for. First, I initialised a 2D python list. >>> my_list = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] I th...
more »

Python: Justifying NumPy array

Please I am a bit new to Python and it has been nice, I could comment that python is very sexy till I needed to shift content of a 4x4 matrix which I want to use in building a 2048 game demo of the game is here I have this function def cover_left(...
more »

How to do a cumulative "all"

Setup Consider the numpy array a >>> np.random.seed([3,1415]) >>> a = np.random.choice([True, False], (4, 8)) >>> a array([[ True, False, True, False, True, True, False, True], [False, False, False, False, Tru...
more »

Python - easy way to "comparison" map one array to another

I have an array a = [1, 2, 3, 4, 5, 6] and b = [1, 3, 5] and I'd like to map a such that for every element in a that's between an element in b it will get mapped to the index of b that is the upper range that a is contained in. Not the best explanati...
more »

How to get an integer array from numpy.bincount when the weights parameter are integers

Consider the numpy array a a = np.array([1, 0, 2, 1, 1]) If I do a bin count, I get integers np.bincount(a) array([1, 3, 1]) But if I add weights to perform the equivalent bin count np.bincount(a, np.ones_like(a)) array([ 1., 3., 1.]) Sa...
more »

append some value to integer array in python

import numpy as np c=[4,8,2,3........] a=np.array([2,3,5],np.int16) #data type=np.int16=Integer(-32768 +32767)) for i in range(len(c)): a.append(c[i]) # This line getting error how can I add some value to this integer array "a" using append...
more »

Python: Create pandas dataframe with columns based off of unique values in nestled list

I have a nestled list containing various regions for each sample. I would like to make a dataframe such that each row (sample) has the presence or absence of the corresponding region (column). For example, the data might look like this: region_list ...
more »

Performance of zeros function in Numpy

I just noticed that the zeros function of numpy has a strange behavior : %timeit np.zeros((1000, 1000)) 1.06 ms ± 29.8 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each) %timeit np.zeros((5000, 5000)) 4 µs ± 66 ns per loop (mean ± std...
more »

Why is np.compress faster than boolean indexing?

What is np.compress doing internally that makes it faster than boolean indexing? In this example, compress is ~20% faster, but the time savings varies on the size of a and the number of True values in the boolean array b, but on my machine compres...
more »

Create a string with a variable definition in Python (of a nympy.arange object)

I'm working on a Python script, and I would like to generate a string with the definition of a variable, which is a numpy array. I want this to write it to an external file or to screen instead of printing/writing the whole array. To make it clear, ...
more »

What is a simple framework for time testing pandas and numpy related solutions

Motivation I answer a few questions here and there. I'm often testing the how fast mine and other peoples solutions are. Question Is there a simple framework to get a fairly comprehensive view into how fast various solutions are? Example Consid...
more »

Why are some numpy datatypes JSON serializable and others not?

Numpy has a lot of different basic types, all of which are listed here. I've tracked down an issue in my program to float32s not being JSON-serializable, so I've started testing all datatypes from the list above: >>> import numpy as np >...
more »

How can I vectorize a function that uses lagged values of its own output?

I'm sorry for the poor phrasing of the question, but it was the best I could do. I know exactly what I want, but not exactly how to ask for it. Here is the logic demonstrated by an example: Two conditions that take on the values 1 or 0 trigger a s...
more »

How to have a list of random numbers by ensuring all of them at least once used?

I am trying to have a list of numbers from 1 to 10 with many occurrences, therefore I began with using the following code: list(range(1,11)) However, it only gives every number only once. I need to have an output in double size, half of it having ...
more »

Count the number of likert scale results from multiple column questions in pandas

I have the following dataframe: Question1 Question2 Question3 Question4 User1 Agree Agree Disagree Strongly Disagree User2 Disagree Agree Agree Disagree User3 ...
more »

Pandas rolling regression: alternatives to looping

I got good use out of pandas' MovingOLS class (source here) within the deprecated stats/ols module. Unfortunately, it was gutted completely with pandas 0.20. The question of how to run rolling OLS regression in an efficient and pythonic manner has ...
more »

Python sum elements between two numbers

I'm sure this is very simple. I've searched google and here and not found the specific answer a = rnd.randn(100) print np.sum(a) gives sum of elements in a np.sum(a[a>0.]) gives sum of elements greater than 0 print np.sum((a < 2.0) &...
more »

If you multiply the smallest positive floating point value by a non-zero number, are you guaranteed a non-zero result?

If I take epsilon to be the smallest positive non-zero floating point number (be it 16, 32, or 64 bit) and multiply epsilon by a non-zero floating point value of the same size: Am I guaranteed a non-zero result of the same sign as the original ...
more »

Why does assert np.nan == np.nan cause an error?

If assert 1 == 1 is fine, then why does: assert np.nan == np.nan cause an assertion error? What's even more confusing, this is OK: assert np.nan != np.nan What's the best way to test for nan? ...
more »

itertools does not recognize numpy ints as valid inputs on Python 3.6

Take this code: import itertools as it import numpy as np data = ['a','b','c','d'] dw = np.array([1, 3], dtype=np.int64) print(list(it.islice(data,dw[0],dw[1],1))) On Python 2.7 it prints ['b', 'c',] as expected. On Python 3.6 it throws an except...
more »

Numpy Array Division - unsupported operand type(s) for /: 'list' and 'float'

I defined the following numpy array: import numpy as np numpy_array = np.array([[-1,-1,-1,-1,-1], [-1,2,2,2,-1], [-1,2,8,2,-1], [-1,2,2,2,-1], [-1,-1,-1,-1,-1,-1]]) Now I wanted to divide the whole array by 8: numpy_array /= np.float(8.0) I get...
more »

Can I use lambdify to evaluate the derivative of a python function?

I asked a question yesterday regarding differentiating a python function and then when I found that none of the answers posted were satisfactory for my need of evaluating (in some variables) and then plotting the derivative, I was able to figure out ...
more »

Fast way to calculate conditional function

What is the most fast way to calculate function like # here x is just a number def f(x): if x >= 0: return np.log(x+1) else: return -np.log(-x+1) One possible way is: # here x is an array def loga(x) cond = [x >=...
more »

reverse flatten numpy array?

I have an array [[0, 1, 0, 0] [0, 1, 0, 0] [1, 0, 0, 0] ..., [0, 1, 0, 0] [0, 1, 0, 0] [1, 0, 0, 0]] of Shape(38485,) i want to reshape to (38485,4) like [[0, 1, 0, 0] [0, 1, 0, 0] [1, 0, 0, 0] . . . [0, 1, 0, 0] [0, 1, 0, 0] [1, 0, 0, 0]] but...
more »

Scipy.optimize.curve_fit won't fit cosine power law

For several hours now, I have been trying to fit a model to a (generated) dataset as a casus for a problem I've been struggling with. I generated datapoints for the function f(x) = A*cos^n(x)+b, and added some noise. When I try to fit the dataset wit...
more »

Is there a method in numpy to multiply every element in an array?

I want to multiply all elements in a numpy array. If there's an array like [1,2,3,4,5], I want to get value of 1*2*3*4*5. I tried this by making my own method, but size of array is very large, it takes very longs time to calculate because I'm using ...
more »

How to I factorize a list of tuples?

definition factorize: Map each unique object into a unique integer. Typically, the range of integers mapped to is from zero to the n - 1 where n is the number of unique objects. Two variations are typical as well. Type 1 is where the numbering occu...
more »