numpy

Fastest way to add matrices of different shapes in Python/Numba

Fastest way to add matrices of different shapes in Python/Numba Question: I want to "add" two matrices, a matrix a with shape (K,T) and a matrix b of shape (K,N), to result in a matrix of shape (K,T,N) The following works ok: import numpy as np from numba import njit @njit def add_matrices(a, b): K, …

Total answers: 2

Updating pandas column faster than for loop

Updating pandas column faster than for loop Question: I need to update a dataframe column with an additional comparison. I manage this with a for loop with a couple of conditions import pandas as pd df = pd.DataFrame({‘Signal’:[1,1,1,0,0,0,0,0,0,1],’F1′:[5,5,5,5,5,5,5,5,5,5],’F2′:[5,5,5,5,5,6,4,4,4,4]}) for i in range(1,len(df)): if (df[‘Signal’].iloc[i-1] == 1) & (df[‘F1’].iloc[i]<=(df[‘F2’]).iloc[i]): df[‘Signal’].iloc[i] = 1 The for loop checks …

Total answers: 1

Tricky create calculation that pulls in retro values using Pandas

Tricky create calculation that pulls in retro values using Pandas Question: I have a dataset where I would like to create a new column called ‘aa_cumul’, by taking the sum, (Where the first instance of a numerical value occurs) for a specific city and ID of the value in the column,’new_r_aa’, which is 2, and …

Total answers: 2

How to find the indices of the maximum values of the fifth column?

How to find the indices of the maximum values of the fifth column? Question: I have array A as: A = [[0.0, 1.0, 3.0, -1.0, -1008.0], [0.0, 1.0, 3.0, -1.0, -1008.0], [0.0, 1.0, 3.0, 1.0, -1328.0], [0.0, 1.0, 3.0, 1.0, -1328.0], [0.0, 1.0, 3.0, -1.0, -1328.0], [0.0, 1.0, 3.0, -1.0, -1328.0], [0.0, 1.0, 3.0, -1.0, …

Total answers: 4

List of lists of mixed types to numpy array

List of lists of mixed types to numpy array Question: I have data imported from csv and they are stored in list of lists as: data=[[‘1’, ‘ 1.013831’, ‘ 1.713332’, ‘ 1.327002’, ‘ 3.674446’, ‘ 19.995361’, ‘ 09:44:24’, ‘ 2.659884’], [‘2’, ‘ 1.013862’, ‘ 1.713164’, ‘ 1.326761’, ‘ 3.662183’, ‘ 19.996973’, ‘ 09:49:27’, ‘ 2.668791’], …

Total answers: 1

How to find number of common decimal digits of two arrays element wise?

How to find number of common decimal digits of two arrays element wise? Question: I have a complex function I want to approximate, and I use np.polyfit to find the polynomial to approximate it. And I want to find the statistics of correct decimal places to determine the quality of the approximation. But it is …

Total answers: 2

Small value artifacts from applying scipy.signal.convolve

Small value artifacts from applying scipy.signal.convolve Question: This Python 3.11.5 script: import numpy as np from skimage.io import imread from scipy.signal import convolve image = np.flipud(imread(‘conv-test.bmp’).astype(np.float32)) con = convolve(image, np.ones((3, 3))/9, mode=’valid’) print(image.min()) print(np.logical_and(image > 0, image < 1).any()) print(np.logical_and(con > 0, con < 0.00001).any()) produces: 0.0 False True How is it possible to get …

Total answers: 1

Column-wise sum of nested list

Column-wise sum of nested list Question: Is there a more pythonic or more simple way to accomplish this? I am taking the column-wise sum of items in a nested list. I have a working example but I’m sure nested loops are not the way to do. There has to be something, maybe in NumPy or …

Total answers: 1

Why can't we use a fill_value when reshaping a dataframe (array)?

Why can't we use a fill_value when reshaping a dataframe (array)? Question: I have this dataframe : df = pd.DataFrame([list("ABCDEFGHIJ")]) ​ 0 1 2 3 4 5 6 7 8 9 0 A B C D E F G H I J I got an error when trying to reshape the dataframe/array : np.reshape(df, (-1, …

Total answers: 2

2D to 3D numpy array by blocks

2D to 3D numpy array by blocks Question: I have the following 2D dataframe conc corresponding to gas concentrations on 4 layers at a series of wavelengths wl : conc = wl gas1 gas2 gas3 layer 0 5000 10 13 250 1 1 5000 20 14 260 2 2 5000 30 15 270 3 3 …

Total answers: 4