numpy

numba eager compilation? Whats the pattern?

馋奶兔 提交于 2021-02-05 09:13:58
问题 I looked into eager compilation on numba's website and couldnt figure out, how to specify the types: The example they use is this: from numba import jit, int32 @jit(int32(int32, int32)) def f(x, y): # A somewhat trivial example return x + y # source: http://numba.pydata.org/numba-doc/latest/user/jit.html#eager-compilation as you can see it gets 2 variables as input and returns one single variable. all of them should be int32. One way to understand the decorator is that @jit(int32(int32, int32

numba eager compilation? Whats the pattern?

与世无争的帅哥 提交于 2021-02-05 09:11:21
问题 I looked into eager compilation on numba's website and couldnt figure out, how to specify the types: The example they use is this: from numba import jit, int32 @jit(int32(int32, int32)) def f(x, y): # A somewhat trivial example return x + y # source: http://numba.pydata.org/numba-doc/latest/user/jit.html#eager-compilation as you can see it gets 2 variables as input and returns one single variable. all of them should be int32. One way to understand the decorator is that @jit(int32(int32, int32

Numpy apply function to every item in array

冷暖自知 提交于 2021-02-05 08:56:27
问题 So let's say I have a 2d array. How can I apply a function to every single item in the array and replace that item with the return? Also, the function's return will be a tuple, so the array will become 3d. Here is the code in mind. def filter_func(item): if 0 <= item < 1: return (1, 0, 1) elif 1 <= item < 2: return (2, 1, 1) elif 2 <= item < 3: return (5, 1, 4) else: return (4, 4, 4) myarray = np.array([[2.5, 1.3], [0.4, -1.0]]) # Apply the function to an array print(myarray) # Should be

Euclidean Distance Between All Points in an 2 Vectors

≡放荡痞女 提交于 2021-02-05 08:56:09
问题 If I have two single-dimensional arrays of length M and N what is the most efficient way to calculate the euclidean distance between all points with the resultant being an NxM array? I'm trying to figure this out with Numpy but am pretty new to it so I'm a little stuck. Currently I am doing it this way: def get_distances(x,y): #compute distances between all points distances = np.zeros((len(y),len(x))) for i in range(len(y)): for j in range(len(x)): distances[i,j] = (x[j] - y[i])**2 return

Python | Numpy array images transformations

与世无争的帅哥 提交于 2021-02-05 08:43:05
问题 I have a Numpy image array with shape (1000, 50, 100, 3) ( class 'numpy.ndarray' ) which is containing 1000 images RGB (height = 50, width = 100, channels = 3). I want to first convert the RGB values to YUV values and rescale them to obtain yuv values. A prototypical implementation of a pixel-wise converter is given below. My question : Is there a simple way how I can carry out this transformation? def yuv(_pixel): R, G, B = _pixel[0], _pixel[1], _pixel[2] Y = 0.299 * R + 0.587 * G + 0.114 *

Python | Numpy array images transformations

柔情痞子 提交于 2021-02-05 08:43:00
问题 I have a Numpy image array with shape (1000, 50, 100, 3) ( class 'numpy.ndarray' ) which is containing 1000 images RGB (height = 50, width = 100, channels = 3). I want to first convert the RGB values to YUV values and rescale them to obtain yuv values. A prototypical implementation of a pixel-wise converter is given below. My question : Is there a simple way how I can carry out this transformation? def yuv(_pixel): R, G, B = _pixel[0], _pixel[1], _pixel[2] Y = 0.299 * R + 0.587 * G + 0.114 *

How to calculate the angle of ellipse Gaussian distribution

无人久伴 提交于 2021-02-05 08:40:16
问题 I make following Python Code to calculate center and size of Gaussian-like distribution basis of moment method. But, I can't make the code to calculate the angle of gaussian. Please look at pictures. First Picture is original data. Second picture is reconstruct data from the result of moment method. But, second picture is insufficient reconstruction. Because, original data is inclined distribution. I have to, I think, calculate the angle of axis for Gaussian-like distribution. To assume that

Scipy ndimage.convolve skips the summation of channels

旧时模样 提交于 2021-02-05 08:37:10
问题 I'm trying to use scipy 's ndimage.convolve function to perform a convolution on a 3 dimensional image (RGB, width, height). Taking a look here: It is clear to see that for any input, each kernel/filter should only ever have an output of NxN , with strictly a depth of 1. This is a problem with scipy , as when you do ndimage.convolve with an input of size (3, 5, 5) and a filter/kernel of size (3, 3, 3) , the result of this operation produces an output size of (3, 5, 5) , clearly not summing

Concatenate 3D numpy arrays by row

删除回忆录丶 提交于 2021-02-05 08:35:40
问题 I have the following 2 3D numpy arrays that I want to concatenate. The arrays look like this: a = np.array([[[1,1,1], [2,2,2], [3,3,3]], [["a","a","a"], ["b","b","b"], ["c","c","c"]]]) b = np.array([[[4,4,4], [5,5,5], [6,6,6], [7,7,7], [8,8,8], [9,9,9]], [["d","d","d"], ["e","e","e"], ["f","f","f"], ["g","g","g"], ["h","h","h"], ["i","i","i"]]]) I want to concatenate the two arrays to become one 3D array like: [[['1' '1' '1'] ['2' '2' '2'] ['3' '3' '3'] ['4' '4' '4'] ['5' '5' '5'] ['6' '6' '6

Wondering why scipy.spatial.distance.sqeuclidean is twice slower than numpy.sum((y1-y2)**2)

倖福魔咒の 提交于 2021-02-05 08:26:28
问题 Here is my code import numpy as np import time from scipy.spatial import distance y1=np.array([0,0,0,0,1,0,0,0,0,0]) y2=np.array([0. , 0.1, 0. , 0. , 0.7, 0.2, 0. , 0. , 0. , 0. ]) start_time = time.time() for i in range(1000000): distance.sqeuclidean(y1,y2) print("--- %s seconds ---" % (time.time() - start_time)) ---15.212640523910522 seconds--- start_time = time.time() for i in range(1000000): np.sum((y1-y2)**2) print("--- %s seconds ---" % (time.time() - start_time)) ---8.381187438964844--