numpy

numpy where with array of tuples

主宰稳场 提交于 2021-02-05 06:48:25
问题 Why can't I find the location of a tuple in an array? Afterall, the bottom expression prints True foo = numpy.array([(5, 30), (5,), 5]) bar = numpy.where(foo==foo[0]) print(bar) Prints (array([], dtype=int64),) print((5,30)==foo[0]) Prints True 回答1: It's because: import numpy foo = numpy.array([(5, 30), (5,), 5]) bar = numpy.where(foo==foo[0]) print(foo==foo[0]) False That's why you're getting an empty array. A list comprehension alternative is [v for v in foo if v == foo[0]] will result in [

numpy where with array of tuples

眉间皱痕 提交于 2021-02-05 06:48:25
问题 Why can't I find the location of a tuple in an array? Afterall, the bottom expression prints True foo = numpy.array([(5, 30), (5,), 5]) bar = numpy.where(foo==foo[0]) print(bar) Prints (array([], dtype=int64),) print((5,30)==foo[0]) Prints True 回答1: It's because: import numpy foo = numpy.array([(5, 30), (5,), 5]) bar = numpy.where(foo==foo[0]) print(foo==foo[0]) False That's why you're getting an empty array. A list comprehension alternative is [v for v in foo if v == foo[0]] will result in [

Undo np.fft.fft2 to get the original image

孤街醉人 提交于 2021-02-05 06:25:06
问题 I've just started to learn about images frecuency domain. I have this function: def fourier_transform(img): f = np.fft.fft2(img) fshift = np.fft.fftshift(f) magnitude_spectrum = 20*np.log(np.abs(fshift)) return magnitude_spectrum And I want to implement this function: def inverse_fourier_transform(magnitude_spectrum): return img But I don't know how. My idea is to use magnitude_spectrum to get the original img . How can I do it? 回答1: You are loosing phases here: np.abs(fshift) . np.abs takes

How to count number of white and black pixels in color picture in python? How to count total pixels using numpy

和自甴很熟 提交于 2021-02-05 06:13:45
问题 I want to calculate persentage of black pixels and white pixels for the picture, its colorful one import numpy as np import matplotlib.pyplot as plt image = cv2.imread("image.png") cropped_image = image[183:779,0:1907,:] 回答1: You don't want to run for loops over images - it is dog slow - no disrespect to dogs. Use Numpy. #!/usr/bin/env python3 import numpy as np import random # Generate a random image 640x150 with many colours but no black or white im = np.random.randint(1,255,(150,640,3),

How to count num of elements vector with numpy python

霸气de小男生 提交于 2021-02-05 06:01:09
问题 For example if i have: a=np.array([[1,1,4,1,4,3,1]]) We can see that we have the number 1 four times, the number 4 twice and 3 only ones. I want to have the following result: array(4,4,2,4,2,1,4) As you can see: each cell is replaced by the count of it's element. How can i do it in the best efficient way? 回答1: One vectorized approach with np.unique and np.searchsorted - # Get unique elements and their counts unq,counts = np.unique(a,return_counts=True) # Get the positions of unique elements

How to count num of elements vector with numpy python

爷,独闯天下 提交于 2021-02-05 06:01:04
问题 For example if i have: a=np.array([[1,1,4,1,4,3,1]]) We can see that we have the number 1 four times, the number 4 twice and 3 only ones. I want to have the following result: array(4,4,2,4,2,1,4) As you can see: each cell is replaced by the count of it's element. How can i do it in the best efficient way? 回答1: One vectorized approach with np.unique and np.searchsorted - # Get unique elements and their counts unq,counts = np.unique(a,return_counts=True) # Get the positions of unique elements

randomizing two lists(numpy in) and maintaining order in python

血红的双手。 提交于 2021-02-05 06:00:08
问题 I have two 2d numpy lists. I want to shuffle it, but just outer side shuffle. If i randomize order list a, I want list b to follow list a's order. I have seen randomizing two lists and maintaining order in python but this looks not work for me. The below code is how I'm doing now. But it's too slow for big numpy lists. import numpy as np import random a = np.array([[1,2,3], [4,5,6], [7,8,9], [10,11,12]]) b = np.array([[100,200,300,400,500], [600,700,800,900,901], [101,102,103,104,105], [501

Keras: Predict model within custom loss function

谁说胖子不能爱 提交于 2021-02-05 05:57:26
问题 I am trying to use some_model.predict(x) within a custom loss function. I found this custom loss function: _EPSILON = K.epsilon() def _loss_tensor(y_true, y_pred): y_pred = K.clip(y_pred, _EPSILON, 1.0-_EPSILON) out = -(y_true * K.log(y_pred) + (1.0 - y_true) * K.log(1.0 - y_pred)) return K.mean(out, axis=-1) But the problem is that model.predict() is expecting a numpy array. So I looked for how to convert a tensor ( y_pred ) to a numpy array. I found tmp = K.tf.round(y_true) but this returns

Meaning of the function numpy.fft.fftfreq

安稳与你 提交于 2021-02-05 05:52:08
问题 Some days ago I came across this answer about the usage of the FFT In the answer there's a piece of code like this: w = np.fft.fft(data) freqs = np.fft.fftfreq(len(w)) I read about the function fftfreq in the numpy documentation (here) and i found that it returns an array with the following content: f = [0, 1, ..., n/2-1, -n/2, ..., -1] / (d*n) if n is even f = [0, 1, ..., (n-1)/2, -(n-1)/2, ..., -1] / (d*n) if n is odd In my case, the d var is equal to 1 and n is an even number. So my

Numpy Where with more than 2 conditions

可紊 提交于 2021-02-05 05:52:07
问题 Good Morning, I have the following a dataframe with two columns of integers and a Series (diff) computed as: diff = (df["col_1"] - df["col_2"]) / (df["col_2"]) I would like to create a column of the dataframe whose values are: equal to 0, if (diff >= 0) & (diff <= 0.35) equal to 1, if (diff > 0.35) equal to 2, if (diff < 0) & (diff >= - 0.35) equal to 3, if (diff < - 0.35) I tried with: df["Class"] = np.where( (diff >= 0) & (diff <= 0.35), 0, np.where( (diff > 0.35), 1, np.where( (diff < 0) &