numpy

Image translation using numpy

对着背影说爱祢 提交于 2021-02-10 06:34:05
问题 I want to perform image translation by a certain amount (shift the image vertically and horizontally). The problem is that when I paste the cropped image back on the canvas, I just get back a white blank box. Can anyone spot the issue here? Many thanks img_shape = image.shape # translate image # percentage of the dimension of the image to translate translate_factor_x = random.uniform(*translate) translate_factor_y = random.uniform(*translate) # initialize a black image the same size as the

Python: binned_statistic_2d mean calculation ignoring NaNs in data

a 夏天 提交于 2021-02-10 06:27:01
问题 I am using scipy.stats.binned_statistic_2d to bin irregular data onto a uniform grid by finding the mean of points within every bin. x,y = np.meshgrid(sort(np.random.uniform(0,1,100)),sort(np.random.uniform(0,1,100))) z = np.sin(x*y) statistic, xedges, yedges, binnumber = sp.stats.binned_statistic_2d(x.ravel(), y.ravel(), values=z.ravel(), statistic='mean',bins=[np.arange(0,1.1,.1), np.arange(0,1.1,.1)]) plt.figure(1) plt.pcolormesh(x,y,z, vmin = 0, vmax = 1) plt.figure(2) plt.pcolormesh

Python: binned_statistic_2d mean calculation ignoring NaNs in data

守給你的承諾、 提交于 2021-02-10 06:26:09
问题 I am using scipy.stats.binned_statistic_2d to bin irregular data onto a uniform grid by finding the mean of points within every bin. x,y = np.meshgrid(sort(np.random.uniform(0,1,100)),sort(np.random.uniform(0,1,100))) z = np.sin(x*y) statistic, xedges, yedges, binnumber = sp.stats.binned_statistic_2d(x.ravel(), y.ravel(), values=z.ravel(), statistic='mean',bins=[np.arange(0,1.1,.1), np.arange(0,1.1,.1)]) plt.figure(1) plt.pcolormesh(x,y,z, vmin = 0, vmax = 1) plt.figure(2) plt.pcolormesh

Time-efficient way to replace numpy entries

泄露秘密 提交于 2021-02-10 06:10:49
问题 I have multiple arrays of the following kind: import numpy as np orig_arr = np.full(shape=(5,10), fill_value=1) #only an example, actual entries different Every entry in the array above is a number to a dictionary containing further information, which is stored in an array; toy_dict = {0:np.arange(13, 23, dtype=float), 1:np.arange(23, 33, dtype=float)} My task is to replace the entries in the orig_arr with the array stored in the dict (here it is the toy_dict ) My current approach is a naive

Difference between each elements in an numpy array

折月煮酒 提交于 2021-02-10 06:10:08
问题 If I have an array with 4 int [a,b,c,d] and I want a difference between each element to another element, which the result looks like: [a-b, a-c, a-d,b-c,b-d,c-d] The sign does matter, I try shift the array, but there should be a better way to do this, Cause this seems like some math problem that I forgot. import numpy as np array_1 = np.array([1,2,3,4]) array_2 = np.copy(array_1) array_2 = np.roll(array_2,-1) array_2[-1] = 0 array_3 = np.copy(array_2) array_3 = np.roll(array_3,-1) array_3[-1]

Assigning a list value to pandas dataframe [duplicate]

南笙酒味 提交于 2021-02-10 05:46:09
问题 This question already has an answer here : Set Pandas column values to an array (1 answer) Closed 1 year ago . I cannot seem to reassign a value in a pandas dataframe with a list. Python wants to iterate over the list and I didn't think it would do that. For example, if I have the following: import pandas as pd val1 = [0, 1, 2] val2 = [3, 4, 5] d_list = [] for v1, v2 in zip(val1, val2): d_list.append({'val1':v1, 'val2':v2}) df = pd.DataFrame(d_list) val3 = [6, 7, 8, 9] df['val3'] = [val3]*len

Max of each 2D matrix in 4D NumPy array

試著忘記壹切 提交于 2021-02-10 05:11:09
问题 I have a 4D array, which is defined as follows: B = np.array( [[[[0.5000, 0.5625], [0.5000, 0.5625]], [[1.2500, 0.5000], [0.5625, 0.6875]], [[0.5625, 0.6250], [0.5000, 0.5625]]]] ) I want to take the max of each 2D matrix, such that I get a result of: array([0.5625, 1.250, 0.6250]) Similarly, I want to take the min of each 2D matrix, such that I get a result of: array([0.5000, 0.5000, 0.5000]) However, when doing np.max(B, axis=0) , np.max(B, axis=1) , np.max(B, axis=2) , or np.max(B, axis=3)

Max of each 2D matrix in 4D NumPy array

别说谁变了你拦得住时间么 提交于 2021-02-10 05:10:10
问题 I have a 4D array, which is defined as follows: B = np.array( [[[[0.5000, 0.5625], [0.5000, 0.5625]], [[1.2500, 0.5000], [0.5625, 0.6875]], [[0.5625, 0.6250], [0.5000, 0.5625]]]] ) I want to take the max of each 2D matrix, such that I get a result of: array([0.5625, 1.250, 0.6250]) Similarly, I want to take the min of each 2D matrix, such that I get a result of: array([0.5000, 0.5000, 0.5000]) However, when doing np.max(B, axis=0) , np.max(B, axis=1) , np.max(B, axis=2) , or np.max(B, axis=3)

Randomly selecting from Pandas groups with equal probability — unexpected behavior

这一生的挚爱 提交于 2021-02-10 04:57:02
问题 I have 12 unique groups that I am trying to randomly sample from, each with a different number of observations. I want to randomly sample from the entire population (dataframe) with each group having the same probability of being selected from. The simplest example of this would be a dataframe with 2 groups. groups probability 0 a 0.25 1 a 0.25 2 b 0.5 using np.random.choice(df['groups'], p=df['probability'], size=100) Each iteration will now have a 50% chance of selecting group a and a 50%

Adding columns after comparing values in 2 dataframes with different lengths

一笑奈何 提交于 2021-02-10 04:55:53
问题 I referenced another stackoverflow, but the value came out weird and I asked again. like compare 2 columns in different dataframes df1 Name date A 2019-01-24 A 2019-02-14 B 2018-05-12 B 2019-07-21 C 2016-04-24 C 2017-09-11 D 2020-11-24 df2 Name date2 value A 2019-01-24 123124 A 2019-02-14 675756 B 2018-05-11 624622 B 2019-07-20 894321 C 2016-04-23 321032190 C 2017-09-11 201389 I would like to compare the name and date of df1 and the name and date2 of df2, and if it matches, add value to the