Adding matrices with different dimensions

In numpy and tensorflow it's possible to add matrices (or tensors) of different dimensionality if the shape of smaller matrix is a suffix of bigger matrix. This is an example: x = np.ndarray(shape=(10, 7, 5), dtype = float) y = np.ndarray(shape=(7, ...
more »

2017-02-13 22:02 (2) Answers

Does numpy provide a generalized inner product?

Most array-oriented languages such as APL or J have some form of generalized inner product, which can act like standard matrix multiplication, but supports arbitrary operations in place of the standard ones. For example, in J +/ . * is the standard ...
more »

2017-02-11 00:02 (2) Answers

How to interleave numpy.ndarrays?

I am currently looking for method in which i can interleave 2 numpy.ndarray. such that >>> a = np.random.rand(5,5) >>> print a [[ 0.83367208 0.29507876 0.41849799 0.58342521 0.81810562] [ 0.31363351 0.69468009 0.14960363 0...
more »

2017-02-10 15:02 (2) Answers

Fast scalar triple product in numpy

I have a large number of vector triples, and I would like to compute the scalar triple product for them. I can do import numpy n = 871 a = numpy.random.rand(n, 3) b = numpy.random.rand(n, 3) c = numpy.random.rand(n, 3) # <a, b x c> omega =...
more »

2017-02-10 12:02 (2) Answers

how does redundant if-else help to optimize?

I am reading the source code of Python's numpy library, and found the following snippets. It seems to perform element-wise operations on vectors (numpy.ndarray). For example, numpy.multiply([1,2,3],[4,5,6]) will get the result [4,10,18] #define BASE...
more »

2017-02-09 08:02 (2) Answers

Fast numpy roll

I have a 2d numpy array and I want to roll each row in an incremental fashion. I am using np.roll in a for loop to do so. But since I am calling this thousands of times, my code is really slow. Can you please help me out on how to make it faster. My...
more »

2017-02-07 23:02 (2) Answers

Numpy argmax - random tie breaking

In numpy.argmax function, tie breaking between multiple max elements is so that the first element is returned. Is there a functionality for randomizing tie breaking so that all maximum numbers have equal chance of being selected? Below is an exampl...
more »

2017-02-06 16:02 (1) Answers

numpy savetxt is not adding comma delimiter

numpy savetxt is not adding comma delimiter I have an array with the following contents: 3.880631596916139792e-01 6.835074831218364011e-01 4.604322858429276133e-01 3.494236368132551673e-01 7.142120448019100287e-01 2.579415438181463793e-01 8.230159...
more »

2017-02-06 13:02 (2) Answers

Removing nan values from a Python List

Can someone see why this is not working? I am trying to remove nan values from my python List/array. import math import numpy as np def clean_List_nan(List): Myarray=np.array(List) x = float('nan') for elem in Myarray: if math....
more »

2017-02-04 14:02 (2) Answers

Merge multiple rows into 1 row

Data for pd.read_csv(): Name Job Place Age John None None None None Doctor None None None None UK None None None None 50 Alex None None None None Engr None None None None US None None None None 45 ...
more »

2017-02-04 10:02 (1) Answers

Numpy: What is special about division by 0.5?

This answer of @Dunes states, that due to pipeline-ing there is (almost) no difference between floating-point multiplication and division. However, from my expience with other languages I would expect the division to be slower. My small test looks a...
more »

2017-02-03 16:02 (0) Answers

Boolean matrix form Python's dict of lists

I have a dict of lists e.g., dictionary_test = {'A': ['hello', 'byebye', 'howdy'], 'B': ['bonjour', 'hello', 'ciao'], 'C': ['ciao', 'hello', 'byebye']} I want to convert it into a boolean affiliation matrix for further analysis. Preferably, dict ...
more »

2017-01-31 19:01 (2) Answers