## Intersection of sorted numpy arrays

I have a list of sorted numpy arrays. What is the most efficient way to compute the sorted intersection of these arrays? In my application, I expect the number of arrays to be less than 10^4, I expect the individual arrays to be of length less than ...
more »

## Using python, how to count area of objects with specified dimensions within an image

I am working on data looking at the degeneration of neurites in Neurons derived from human stem cells. The output data is a pair of images, one "before" picture of long, healthy neurites, and one "after" picture of broken, dystrophic neurites. We hav...
more »

## Multiply two numerical columns on conditional using Pandas

I have pd dataframe (data) with three columns, X, Y and Z. I need to run the following: X * Y where Z = 'value' I'm working along the lines of: data[data['Z'] == 'value',[data['X']*data['Y']]] Now I know that this isn't correct, but I can sm...
more »

## python: loop through txt files and delete first few rows of strings

I have text files where each file has 90 columns of timeseries data that vary in length. Before these 90 columns, there are 6 rows of junk string data I want to delete. From row 7 onward, the data is all of the type float. I have tried the followin...
more »

## find intersection point of two lines drawn using houghlines opencv

How can I get the intersection points of lines down using opencv Hough lines algorithm? Here is my code: import cv2 import numpy as np import imutils im = cv2.imread('../data/test1.jpg') gray = cv2.cvtColor(im,cv2.COLOR_BGR2GRAY) edges = cv2.Canny...
more »

## Random Numbers in a range around a median

I have a median and a standard deviation, what i want is to generate random numbers between the median-std and the median+std. I know how to do it like this: import numpy as np import random as rnd median=30 std=15 random_nr=rnd.randint(median-std...
more »

## How to do boolean slicing on array of tuples in numpy

I have a .csv file like this vehicle,speed,datetime,x,y 61C22276,0.0,1.4926212E9,106.33695,11.12652 60C28912,0.0,1.4926212E9,106.84327166666667,10.90424 51D06538,0.0,1.4926212E9,106.7806,10.765768333333334 50LD08650,0.0,1.4926212E9,106.91705,10.7461...
more »

## python , opencv, image array to binary

I have a large image , using cv2 module in python and some coordinates i cropped the image: img = cv.imread(image_path) crop_img = img[y1:y2,x1:x2] cv.imwrite(cropPath, crop_img) now the crop_img is a numpy.ndarray type. then I save this ima...
more »

## Python Pandas Data frame creation

I tried to create a data frame df using the below code : import numpy as np import pandas as pd index = [0,1,2,3,4,5] s = pd.Series([1,2,3,4,5,6],index= index) t = pd.Series([2,4,6,8,10,12],index= index) df = pd.DataFrame(s,columns = ["MUL1"]) df["M...
more »

## How to sort a list of tuples that contain numpy array?

First, this is the code that does not work: ls = [(1.0,np.array([3.0, 4.0])), (1.0,np.array([3.0, 4.1])), (3.0,np.array([2.0, 1.0]))] ls.sort() As you can see, I have a list of tuples (ls). The first element of each tuple is a float number. I try ...
more »

## How to conditionally combine two numpy arrays of the same shape

This sounds simple, and I think I'm overcomplicating this in my mind. I want to make an array whose elements are generated from two source arrays of the same shape, depending on which element in the source arrays is greater. to illustrate: import ...
more »

## Enlarge numpy image and fill extra space with zeros

I'm working with a list of images image_list. Each item in image_list is a numpy array with shape (X, Y, 3). X is the height, Y is the width, and each image has 3 colors channels. I want to get the max width and height of all the images, and resize ...
more »

## Print rows where all columns meet a certain criteria in a numpy array?

I have an array with 500 rows and 5 columns. I need to find all the rows where the value in each of the last 4 columns is greater than 100. I found a way to check each column individually but I'd like to be able to check them all at once. I tried ins...
more »

## Vectorize numpy code

So I am trying to vectorize this code in numpy and im having trouble Heres is my for loop version which works as desired: B is a 3x3 Matrix for i in range(b.shape[0]): for j in range(b.shape[0]): Z[i,j] = np.sqrt(np.dot((b[i,:].T - b[...
more »

## Returning the N largest values' indices in a multidimensional array (can find solutions for one dimension but not multi-dimension)

I have a numpy array X, and I'd like to return another array Y whose entries are the indices of the n largest values of X i.e. suppose I have: a =np.array[[1, 3, 5], [4, 5 ,6], [9, 1, 7]] then say, if I want the first 5 "maxs"'s indices-here 9, 7 ...
more »

## Comparing two pandas series for floating point near-equality?

I can compare two Pandas series for exact equality using pandas.Series.equals. Is there a corresponding function or parameter that will check if the elements are equal to some ε of precision? ...
more »

## np.dot output incorrect in Jupyter Notebook

I am getting a different result when I run this code in my terminal vs. a Jupyter Notebook. Both are using Python 3.6 and numpy 1.13.1. Terminal (correct solution): >>> import numpy as np >>> vector_one = np.asarray([ ... [1], .....
more »

## Flask to Numpy Image Conversion

I have been trying to send an image over javascript to a Flask server to draw bounding boxes on coordinates I received from my own API. How might I convert this to a numpy array? I was thinking of using the cv2.imdecode feature, but I don't want to ...
more »

## Count the most frequent value and manipulate it

I have a dataframe as follow: User Bought 0 U296 PC 1 U300 Table 2 U296 PC 3 U296 Chair I would like to create 2 columns, one displays th...
more »

## How to convert sympy function to plot with matplotlib?

I have certain function, for example sin(b*x), with sympy I get derivative and antiderivative expressions, but I need to plot these 3 functions in matplotlib. My problem is I can't convert correctly functions to numpy in order to plot in matplotlib. ...
more »

I am new to python and have a requirement to load dataframes from various CSV files. It turns out that there is a business logic depending on the number of rows in csv. Can i know this beforehand if i can know CSV total row numbers without writing re...
more »

## Create specific array using numpy

I wanted to create this kind of array using numpy: [[[0,0,0], [1,0,0], ..., [1919,0,0]], [[0,1,0], [1,1,0], ..., [1919,1,0]], ..., [[0,1019,0], [1,1019,0], ..., [1919,1019,0]]] To which I can access via: >>> data[25][37] array([25, 37...
more »

## keras - TypeError: 'int' object is not iterable

I was trying to test a network, but seem to get an annoying error, which I am not quite sure I understand. import keras from keras.models import Sequential from keras.optimizers import SGD from keras.layers.core import Dense, Activation, Lambda, Re...
more »

## Map discrete columns to the index of their unique values

I have a dataframe with an int columns: df=pd.DataFrame(data=2*np.random.randint(0,high=10,size=5),columns=['N']) N 0 8 1 4 2 8 3 14 4 2 5 18 I would like to generate another dataframe as: df2= N ID 0 8 2 1 4 1 2 8 ...
more »

## np.random.choice: probabilities do not sum to 1

how can I use np.random.choice here? there is p that calculate by some opertation, like : p=[ 1.42836755e-01, 1.42836735e-01 , 1.42836735e-01, 1.42836735e-01 , 4.76122449e-05, 1.42836735e-01 , 4.76122449e-05 , 1.42836735e-01, 1.42836...
more »

## numpy cumsum columns for varying lengths specified by list

how to cumsum n consecutive elements in each column specified by a list. cumsum resets from next row onwards. For example, lens = [2,3] ==> cumsum of first 2 rows [:2], then cumsum for next 3 rows [2:5] import numpy as np lens = [3, 2] a = np.array(...
more »

## sum groups rows of numpy matrix using list of lists of indices

slice numpy array using lists of indices and apply function, is it possible to vectorize (or nonvectorized way to do this)? vectorized would be ideal for large matrices import numpy as np index = [[1,3], [2,4,5]] a = np.array( [[ 3, 4, 6, ...
more »

## pandas: slicing along first level of multiindex

I've set up a DataFrame with two indices. But slicing doesn't behave as expected. I realize that this is a very basic problem, so I searched for similar questions: pandas: slice a MultiIndex by range of secondary index Python Pandas slice multiinde...
more »

## Conditionals on one numpy array while iterating over a second numpy array

I read through the numpy documentation, especially as it pertains to iterating over ndarrays in a for loop. Read through suggested similar S.O. posts, but I'm still failing to solve what seems to be a relatively straight-forward problem. I'd like to...
more »

## python create subset of array from boolean column mask

I need to create a function or equation that will turn this input... a = [True, False, True] b = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] into this output... c = [[1, 3], [4, 6], [7, 9]] Note: the above arrays are all numpy arrays. Thanks! ...
more »

## Keep top N values of each row in a dataframe within groups of column indices

I'm having trouble finding an elegant solution to this problem (there might not be one). I have the following example DataFrame: np.random.seed(0) df = pd.DataFrame(np.random.randn(10,10)).abs() 0 1 2 3 ...
more »

## Pandas Test Failure

After installing Python 3.6.2 and Pandas on Windows 10 64-bit I ran the test described here. The failures seem to be largely linked to two issues. The first 3 errors have to do with indexing: TestMixedIntIndex.test_argsort & TestMixedIntInde...
more »

## Numpy: Insert value into different column for each row

I'm trying to insert into a numpy matrix given a mask that defines a single cell per row. Effectively, it's inserting a value into each row but with a different column. I've tried to use np.insert() without success: >>> x array([[False, Fal...
more »

## Fastest way to calculate exponential [exp()] function of large complex array in Python

I'm developing code that integrates an ODE using scipy's complex_ode, where the integrand includes a Fourier transform and exponential operator acting on a large array of complex values. To optimize performance, I've profiled this and found the main...
more »