numpy

Dot product two 4D Numpy array

老子叫甜甜 提交于 2021-02-18 17:51:07
问题 I have a 4D Numpy array of shape (15, 2, 320, 320). In other words, each element of the [320 x 320] matrix is a matrix of size [15 x 2]. Now, I would like to compute the dot product for each element of the [320x320] matrix, then extract the diagonal array. Currently, I am using 2 "for" loops, and the code works well (see the snippet). However, the calculation speed is too slow (when I process a large data). Anyone can show me how to vectorize the computation without the loops. A = np.random

Python 3D interpolation speedup

你离开我真会死。 提交于 2021-02-18 15:21:55
问题 I have following code used to interpolate 3D volume data. Y, X, Z = np.shape(volume) xs = np.arange(0, X) ys = np.arange(0, Y) zs = np.arange(0, Z) points = list(zip(np.ravel(result[:, :, :, 1]), np.ravel(result[:, :, :, 0]), np.ravel(result[:, :, :, 2]))) interp = interpolate.RegularGridInterpolator((ys, xs, zs), volume, bounds_error=False, fill_value=0, method='linear') new_volume = interp(points) new_volume = np.reshape(new_volume, (Y, X, Z)) This code takes about 37 seconds to execute on

Python 3D interpolation speedup

▼魔方 西西 提交于 2021-02-18 15:21:48
问题 I have following code used to interpolate 3D volume data. Y, X, Z = np.shape(volume) xs = np.arange(0, X) ys = np.arange(0, Y) zs = np.arange(0, Z) points = list(zip(np.ravel(result[:, :, :, 1]), np.ravel(result[:, :, :, 0]), np.ravel(result[:, :, :, 2]))) interp = interpolate.RegularGridInterpolator((ys, xs, zs), volume, bounds_error=False, fill_value=0, method='linear') new_volume = interp(points) new_volume = np.reshape(new_volume, (Y, X, Z)) This code takes about 37 seconds to execute on

Numpy dot product of very large arrays

生来就可爱ヽ(ⅴ<●) 提交于 2021-02-18 15:09:21
问题 I have a problem of very slow running code. A try to calculate the dot product of a very large binary matrix (170544 X 22) by its transpose. First I tried this code import numpy as np start = time.time() import warnings warnings.filterwarnings("ignore", message='genfromtxt', category=UserWarning) np.set_printoptions(threshold=np.nan) fin = open('E:/myscripts/Abin.txt', 'rb') # input file (170544X22 binary matrix) fin1 = open('E:/myscripts/AbinT.txt', 'rb') # input file (22X170544 binary

Matplotlib normalize colorbar (Python)

孤街醉人 提交于 2021-02-18 12:45:39
问题 I'm trying to plot a contourf-plot using matplotlib (and numpy of course). And it works, it plots what it should plot, but unfortunatelly I cannot set the colorbar range. The problem is that I have a plenty of plots and need all of them to have the same colorbar (same min and max, same colors). I copy&past-ed almost every code snippet I found on the internet, but without success. My code so far: import numpy as np; import matplotlib as mpl; import matplotlib.pyplot as plt; [...] plotFreq,

Opencv Python open dng format

梦想的初衷 提交于 2021-02-18 12:31:02
问题 I can't figure out how to open a dng file in opencv. The file was created when using the pro options of the Samsung Galaxy S7. The images that are created when using those options are a dng file as well as a jpg of size 3024 x 4032 (I believe that is the dimensions of the dng file as well). I tried using the answer from here (except with 3 colors instead of grayscale) like so: import numpy as np fd = open("image.dng", 'rb') rows = 4032 cols = 3024 colors = 3 f = np.fromfile(fd, dtype=np.uint8

Rendering a float array to 24-bit RGB image (using PIL for example)

醉酒当歌 提交于 2021-02-18 11:44:09
问题 x is a numpy.float32 array, with values from -200 to 0 . These are dB (decibel) values. When I do (as recommended here): Image.fromarray(x, mode='F') I get a greyscale or sometimes nearly black image. How to map a float in [-200, 0] to a 24-bit RGB byte array (using a colormap) that can be read with the Python module PIL with Image.fromarray(x, mode='RGB') ? Edit: The required .wav audio file is here, for which we want to plot the spectrogram. Here is some code to test: import scipy, numpy as

How to match pairs of values contained in two numpy arrays

限于喜欢 提交于 2021-02-18 11:12:11
问题 I have two sets of coordinates and want to find out which coordinates of the coo set are identical to any coordinate in the targets set. I want to know the indices in the coo set which means I'd like to get a list of indices or of bools. import numpy as np coo = np.array([[1,2],[1,6],[5,3],[3,6]]) # coordinates targets = np.array([[5,3],[1,6]]) # coordinates of targets print(np.isin(coo,targets)) [[ True False] [ True True] [ True True] [ True True]] The desired result would be one of the

How to match pairs of values contained in two numpy arrays

﹥>﹥吖頭↗ 提交于 2021-02-18 11:11:56
问题 I have two sets of coordinates and want to find out which coordinates of the coo set are identical to any coordinate in the targets set. I want to know the indices in the coo set which means I'd like to get a list of indices or of bools. import numpy as np coo = np.array([[1,2],[1,6],[5,3],[3,6]]) # coordinates targets = np.array([[5,3],[1,6]]) # coordinates of targets print(np.isin(coo,targets)) [[ True False] [ True True] [ True True] [ True True]] The desired result would be one of the

How to match pairs of values contained in two numpy arrays

為{幸葍}努か 提交于 2021-02-18 11:11:38
问题 I have two sets of coordinates and want to find out which coordinates of the coo set are identical to any coordinate in the targets set. I want to know the indices in the coo set which means I'd like to get a list of indices or of bools. import numpy as np coo = np.array([[1,2],[1,6],[5,3],[3,6]]) # coordinates targets = np.array([[5,3],[1,6]]) # coordinates of targets print(np.isin(coo,targets)) [[ True False] [ True True] [ True True] [ True True]] The desired result would be one of the