# simple but difficult numpy memory reference problem

Advertisements import numpy as np a = np.array([0,0,0,0,0,0,0,0]) b = np.array([4,2,3]) c = np.array([5,5,2]) for i, e in enumerate(c): a[e] += b[i] print(a) # [0 0 3 0 0 6 0 0] a = np.array([0,0,0,0,0,0,0,0]) b = np.array([4,2,3]) c = np.array([5,5,2]) a[c] += b[np.arange(len(b))] print(a) # [0 0 3 0 0 2 0 0] The… Read More simple but difficult numpy memory reference problem

# Calculate sliding window median for Pandas df

Advertisements This is the follow-up question to link So, I have the following df: df = pd.DataFrame({‘col1’:[1,2,3,4,5,6,7,8,9,10], ‘col2’:[10,15,20,25,30,35,40,45,50,55], ‘col3’:[‘A’,’A’,’A’,’A’,’B’,’B’,’B’,’B’,’B’,’B’]}) I would like to calculate the median values for col1 and col2 for every ‘window’ rows. To do that I have the following function: def calculate_median_val(df, window): return df.groupby(np.arange(len(df))//window)[‘col1′,’col2’].median() There are two problems with this function:… Read More Calculate sliding window median for Pandas df

# How to replace each element in a numpy array with multiple values?

Advertisements I have a 2D numpy array with size n * m, and I need to replace each value with 2 valus and result in an array with size n * 2m. Replace pattern:1 with [1,0], 2 with [0,1] and 0 with [0,0] Input: [[1,0,2],[2,2,1]] Desired Output: [[1,0,0,0,0,1],[0,1,0,1,1,0]] It can be easily done with a… Read More How to replace each element in a numpy array with multiple values?

# Reshaping numpy array raises ValueError

Advertisements I have a NumPy array as follows: arr = np.array([np.zeros(s) for s in range(2, 10)]) I want to reshape each subarray form shape (s) to shape (1, s), so i wrote this: arr = np.array([np.zeros(s).reshape(1, s) for s in range(2, 10)]) However, ValueError is raised: ValueError: could not broadcast input array from shape (2,)… Read More Reshaping numpy array raises ValueError

# np.concatenate(np.stack the different arrays with a general solution

Advertisements I have 6 array with same size. I have a general function and based on a value I should consider 2, 3, 5, 6 array of these and concate them with the following way. Could you please help me with a general solution for this? Here I only provide a simple example and in… Read More np.concatenate(np.stack the different arrays with a general solution

# Convert pandas column values based on groupings of values

Advertisements I have a pandas columns with values 1.0, 2.0, 3.0, 4.0, and 5.0 like below: 0 5.0 1 2.0 2 3.0 3 3.0 4 5.0 … 1039 5.0 1040 1.0 1041 2.0 1042 4.0 1043 1.0 I want rows with values 1.0 or 2.0 to all have a value of 1.0, 3.0 and 4.0… Read More Convert pandas column values based on groupings of values

# What is the "peak to peak" analogy refrenced by numpy's .ptp function?

Advertisements The np.ptp function returns the range between minimum and maximum values along a specified axis. The numpy docs state that the "ptp" name is an acronym for "peak to peak". Can someone explain this analogy? I would have thought this function would have been named "valley to peak". >Solution : This is a common… Read More What is the "peak to peak" analogy refrenced by numpy's .ptp function?

# Numpy np.where condition with multiple columns

Advertisements I have a dataframe import pandas as pd import numpy as np data = pd.DataFrame({"col1": [0, 1, 1, 1,1, 0], "col2": [False, True, False, False, True, False] }) data I’m trying to create a column col3 where col1=1 and col2==True its 1 else 0 Using np.where: data.assign(col3=np.where(data["col1"]==1 & data["col2"], 1, 0)) col1 col2 col3… Read More Numpy np.where condition with multiple columns

# Making an array from lists of different sizes and filling the gaps with Nan in Python

Advertisements Let’s say I have a list of lists, and they can have different sizes: arr=list([[0. , 1.5, 3. , 0. , 1.5],[0. , 1.5, 3. ],[0., 1.33, 2.67, 4. ]]) I want to make this array numpy compatible and make a filled array based on the maximum length and fill the gaps with Nan:… Read More Making an array from lists of different sizes and filling the gaps with Nan in Python

# Joining two multi-dimentional arrays by adding a new dimention in Python

Advertisements Let’s say I have two arrays of shape (3,2,3) a = np.array([[[1,2,3],[4,5,6]],[[7,8,9],[10,11,12]],[[13,14,15],[16,17,18]]]) b = np.array([[[1,2,3],[4,5,6]],[[7,8,9],[10,11,12]],[[13,14,15],[16,17,18]]]) a.shape b.shape I would like to join these two arrays by adding a new dimention to get (2,3,2,3) like this: c = np.array([[[[1,2,3],[4,5,6]],[[7,8,9],[10,11,12]],[[13,14,15],[16,17,18]]], [[[1,2,3],[4,5,6]],[[7,8,9],[10,11,12]],[[13,14,15],[16,17,18]]]]) c.shape How would I do this? >Solution : You can use numpy.stack (Join a sequence… Read More Joining two multi-dimentional arrays by adding a new dimention in Python