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 »

2017-06-19 23:06 (1) Answers

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 »

2017-06-18 04:06 (4) Answers

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 »

2017-06-15 20:06 (3) Answers

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 »

2017-06-15 05:06 (2) Answers

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 »

2017-06-14 23:06 (1) Answers

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 »

2017-06-13 06:06 (4) Answers

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 »

2017-06-11 21:06 (1) Answers

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 »

2017-06-11 21:06 (2) Answers

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 »

2017-06-06 03:06 (1) Answers

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 »

2017-06-05 20:06 (2) Answers

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 »

2017-05-30 18:05 (2) Answers

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 »

2017-05-26 20:05 (6) Answers