Why aren't the values from the for loop appending to the array?

I’m trying to append values from a for loop into a numpy array. While the values are correct, the array only returns none values. The values entered are from another numpy array. import numpy as np import matplotlib.pyplot as plt j=np.array([14,15,16,16,16,22,22,24,24,25,25,25,25,25]) u=np.unique(j) def P(age): sum=0 for i in range(14): if j[i]==age: sum=sum+1 else: sum=sum print(sum/14)… Read More Why aren't the values from the for loop appending to the array?

How to perform basic math on numpy ndarray

So, I have a numpy ndarray with dimensions (984, 1977, 2). What I want to accomplish is to have a numpy ndarray where I do basic math on the final values. So let’s say data is my ndarray. And data[0][0] equals to [72 46]. So I want to perform (72 – 46) / (72 +… Read More How to perform basic math on numpy ndarray

Converting a numpy image array based on a boolean mask

I have 2 numpy arrays. One is a 3D integer array (image RGB values) with dimensions (988, 790, 3) and the other is a mask boolean array with the same shape. I want to use the mask to convert False values in the image array to black and leave true values as is. I tried… Read More Converting a numpy image array based on a boolean mask

loop-free operations on two-dimensional numpy arrays

This may seem a silly question to the community, but I didn’t manage to find an answer online. Imagine one has a situation like this: area_vector = np.zeros(np.shape(normal)) for i in range(len(area)): area_vector[i] = area[i] * normal[i] normal is a N x 3 array and area a 1D array of size N. As we can… Read More loop-free operations on two-dimensional numpy arrays

Numpy add one column to other columns and remove

Say I have an 2d numpy array like this [[1,2,3], [4,5,6], [7,8,9]] I then want to convert it to [[3,4], [9,10], [15,16]] This could be a variable number of columns, I want to add the first column to every other column and remove it as well afterwards. >Solution : a = a[…, [0]] + a[…,… Read More Numpy add one column to other columns and remove

Get average between consecutive pairs of numpy array

Say I have a numpy array like this [1,2,3,4,5] I want to generate an array that is the equal to the average of consecutive elements [1.5,2.5,3.5,4.5] Is there any efficient way to do this outside of just iterating through? I’m not really sure what to do because reshaping doesn’t really work and I’m trying to… Read More Get average between consecutive pairs of numpy array

"ValueError: x and y must be the same size" when plotting with matplotlib

I’m writing a python code that implements euler’s method to solve a 1st order ODE for an arbitrary range of values of time-step h. The simplest solution i’ve come up with is to declare a Python list to store the results at the end of the ‘outer for’ loop: y = [] for h in… Read More "ValueError: x and y must be the same size" when plotting with matplotlib

Add the same value to every row in a numpy array

I have a numpy array that looks like this: [[0.67058825 0.43529415 0.33725491] [0.01568628 0.30980393 0.96862751] [0.24705884 0.63529414 0.29411766] [0.27843139 0.63137257 0.37647063] [0.26274511 0.627451 0.33333334] [0.25098041 0.61960787 0.30980393]] I want to add a 1 to every row like this: [[0.67058825 0.43529415 0.33725491 1] [0.01568628 0.30980393 0.96862751 1] [0.24705884 0.63529414 0.29411766 1] [0.27843139 0.63137257 0.37647063 1] [0.26274511… Read More Add the same value to every row in a numpy array

How to remove numpy array row which matches the string in list

I have got an array which looks like array = array([[‘Mango’, 0.75, 0.25], [‘Honey’, 0.75, 0.25], [‘Grape’, 0.625, 0.375], [‘Pineapple’, 0.5, 0.5]], dtype=object) and a list item = {‘Honey’,’Grape’} now, have to remove the rows from the array which matches the items in the list. Expected Output: array = array([[‘Mango’, 0.75, 0.25], [‘Pineapple’, 0.5, 0.5]],… Read More How to remove numpy array row which matches the string in list

Efficient negation of subdiagonal values of a 2d numpy array

How would one efficiently negate the subdiagonal entries of a 2d numpy array? >Solution : You may use numpy.tril_indices to compute the indices of the subdiagonal entries, considering a diagonal offset of k = -1, and negate them with a mask, for instance: import numpy as np a = np.arange(16).reshape(4, 4) >>> array([[ 0, 1,… Read More Efficient negation of subdiagonal values of a 2d numpy array