How do I do an einsum that mimics 'keepdims'?

a python question: I've got a np.einsum operation that I'm doing on a pair of 3d arrays: return np.einsum('ijk, ijk -> ik', input_array, self._beta_array) Problem I'm having is the result is 2d; the operation collapses the 'j' dimension. What I...
more »

2017-03-23 18:03 (1) Answers

Weighted Cumulative Sum in Python

So I'm trying to figure out a good way of vectorizing a calculation and I'm a bit stuck. | A | B (Calculation) | B (Value) | |---|----------------------|-----------| | 1 | | | | 2 | | ...
more »

2017-03-23 18:03 (1) Answers

Installing python packages without dependencies

I am trying to install a specific version of a python package into a pre-installed python environment. The package is laspy and the version is an old one (1.2.5). The package is supposed to work with Python version 2.7, but I am trying to install it ...
more »

2017-03-23 13:03 (1) Answers

How to vectorize loss in SVM

I'd like to calculate the loss of SVM without loop. But I cannot get it right. Need some enlightment.           and           def svm_loss_vector...
more »

2017-03-23 09:03 (1) Answers

Quickly access sorted subset

Let's say I have an n by d matrix X, with n samples and d features. Let's assume that I can presort X as much as I realistically want (for example, have a matrix of indices for X sorted by each individual feature). The algorithm I am using requires m...
more »

2017-03-23 08:03 (1) Answers

How to effectively slice numpy array

I want to implement the following code in a matrix-operation way instead of with a for loop. a = np.random.randint(0, 7, (4,3)) b = np.random.randint(0, 6, (4,3,2)) c = None for idx in xrange(a.shape[0]): max_idx = np.argmax(a[idx]) ex_b =...
more »

2017-03-23 04:03 (1) Answers

plotting matplotlib from nump ndarray

This is embarrassing, but I am unable to plot this: import numpy as np import matplotlib.pyplot as plt datf=np.loadtxt(filename, dtype=float,delimiter=" ") print((datf)) plt.plot(datf[:0], datf[:1]) This is datf: [[ 1. ...
more »

2017-03-22 19:03 (2) Answers

Efficient repeated numpy.where

I have a code in which I want to check whether pairs of coordinates fall into certain rectangles. However, there are many rectangles and i am not sure how to generalize the following code to many rectangles. I only can do it using eval in a loop but ...
more »

2017-03-22 17:03 (3) Answers

What is the use/purpose of pandas?

I have some specific questions about whether to use Pandas or alternative tools. What is the reason to use Pandas rather than other tools or data structures? When memory is a concern, how heavy is the cost of Pandas and what are the cheaper alterna...
more »

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

Convert 2D numpy.ndarray to nested dictionary

Suppose I have the following numpy.ndarray: array([[50, 0, 0], [ 0, 3, 47], [ 0, 36, 14]]) How is is possible to convert it to a dictionary of this form: {0: {0: 50}, 1: {1: 3, 2: 47}, 2: {1: 36, 2: 14}} The question is similar t...
more »

2017-03-22 11:03 (3) Answers

How to change names of a list of numpy files?

I have list of numbpy files, I need to change their names, In fact, let's assume that I have this list of files: AES_Trace=1_key=hexaNumber_Plaintext=hexaNumber_Ciphertext=hexaNumber.npy AES_Trace=2_key=hexaNumber_Plaintext=hexaNumber_Ciphertext=hex...
more »

2017-03-22 11:03 (2) Answers

Removing subarray from array

I have a numpy array A and B. A = [ 1, 2, 5, 9.8, 55, 3] B = [ 3, 4] Now, how to remove A[3] & A[4] that is whatever indices array B is having and then put them at the start of array A. So, I want my output to be A = [9.8, 55, 1, 2, 5,...
more »

2017-03-22 08:03 (2) Answers

Find last value in numpy array

I have a 1d array and want to find the last value like this. a = np.array([1,2,3,4,5,6,7,8,9,8,7,6,5,4,3,2,1]) # find the index that value(7) last appear. np.argwhere(a >= 7).max() # output 10 But it's suit for 1d array and how about 3d array. ...
more »

2017-03-22 07:03 (2) Answers

Difficulty determining the type of a numpy int64

question I'm having trouble identifying numpy.int64 objects, in order to convert them to base python int for json serialisation. isinstance usually works but does not in the following example, and I would love to understand why this is. >>>...
more »

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

Column name and index of max value

I currently have a pandas dataframe where values between 0 and 1 are saved. I am looking for a function which can provide me the top 5 values of a column, together with the name of the column and the associated index of the values. Sample Input: ...
more »

2017-03-21 16:03 (3) Answers

Read complex numbers from a csv file using python

I am having problem reading complex number from a csv file. The format of the file is the following: ( -353.10438 +j1.72317617 ),( -23.16000 +j0.72512251 ) I tried importing the data using numpy.genfromtxt: data=genfromtxt(fname, dtype=complex...
more »

2017-03-21 13:03 (2) Answers

Scipy NNLS using mask

I am performing non-negative least squares using scipy. A trivial example would be as follows: import numpy as np from scipy.optimize import nnls A = np.array([[60, 70, 120, 60],[60, 90, 120, 70]], dtype='float32') b = np.array([6, 5]) x, res = nnl...
more »

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