Skip to main content
added 962 characters in body
Source Link
F.F.
  • 47
  • 7

My question is somewhat similar, but still fairly tangential & a bit more involved compared to the following post: Create an ndarray from arrays of different lengths

I have 3 time-series variables I track in arr1, arr2 & arr3. Every time a set of conditions is met (call this a "transition"), I want to store these arrays into what a I call a db_arr. I then reset the 3 arrays per some logic. So on & so forth till my loop ends. The len of these arrays can differ. So after 1 transition, db_arr should be a 2d array with 3 rows (one for each arr) & variable # of columns. I use vstack to concatenate arr1, arr2 & arr3, which requires dimensions to match. So I pad the 3 arrays to the len of some other reference array A:

arr1 = np.lib.pad(arr1, (0, len(A)-len(arr1)), 'constant', constant_values=(0,0)) 

Same for arr2 & arr3. I can then vstack & concatenate them to db_arr, yielding the desired 2d array of shape=(3,len(A)).

I'd like to concatenate these 2d 'slices' along a 3rd dimension, stacking them to create depth. So say after 5 transitions, db_arr would have shape=(3,len(A),5).

Q1: I've looked at dstack, stack, etc., but I can't seem to get the desired concatenation along this new axis. Feel like I'm missing something simple.

Q2: I stated len(A) as being constant - it's not. The len of arr1, arr2, arr3 as well as A vary from transition to transition. So not only must I pad arrays per 2d slice as above, I also need to do so for each previous slice as new transitions yield slices of different lengths. I can pad against the max length of all slices at each new transition, but I'm wondering if there's a simpler, more elegant, 'pythonic' approach?

To be clear, I can't create an empty array or zero-array in advance since dimensions of my ndarray are unknown a priori.

Example: Let timeseries = np.linspace(1,500,50) Loop through timeseries. arr1 & arr2 are constructed / appended to per some logic. Certain conditions trigger a 'transition', which require emptying arr1 & arr2, but concatenating them to db_arr first. At the time of the first transition, say arr1 = [1,2,3] & arr2 = [4,5,6,7]. Assume transition occurs at index = 14 of timeseries. vstack requires equal dims, so I can either pad arr1 to be of same len as arr2, or pad both to be of len(timeseries) at time of transition (i.e. 15). Either allows me to then vstack((arr1,arr2)), then concatenate to db_array.

Now say the next transition occurs at index 40. arr1 = [8,9,10,11,12] & arr2 = [13,14]. I can pad them to be same len using either of aforementioned methods. I want to concatenate this 'slice' along a third dimension. However I pad arr1 & arr2, they will be of different len than the prior slice comprised of arr1 & arr2 vstacked. Thus my question.

My question is somewhat similar, but still fairly tangential & a bit more involved compared to the following post: Create an ndarray from arrays of different lengths

I have 3 time-series variables I track in arr1, arr2 & arr3. Every time a set of conditions is met (call this a "transition"), I want to store these arrays into what a I call a db_arr. I then reset the 3 arrays per some logic. So on & so forth till my loop ends. The len of these arrays can differ. So after 1 transition, db_arr should be a 2d array with 3 rows (one for each arr) & variable # of columns. I use vstack to concatenate arr1, arr2 & arr3, which requires dimensions to match. So I pad the 3 arrays to the len of some other reference array A:

arr1 = np.lib.pad(arr1, (0, len(A)-len(arr1)), 'constant', constant_values=(0,0)) 

Same for arr2 & arr3. I can then vstack & concatenate them to db_arr, yielding the desired 2d array of shape=(3,len(A)).

I'd like to concatenate these 2d 'slices' along a 3rd dimension, stacking them to create depth. So say after 5 transitions, db_arr would have shape=(3,len(A),5).

Q1: I've looked at dstack, stack, etc., but I can't seem to get the desired concatenation along this new axis. Feel like I'm missing something simple.

Q2: I stated len(A) as being constant - it's not. The len of arr1, arr2, arr3 as well as A vary from transition to transition. So not only must I pad arrays per 2d slice as above, I also need to do so for each previous slice as new transitions yield slices of different lengths. I can pad against the max length of all slices at each new transition, but I'm wondering if there's a simpler, more elegant, 'pythonic' approach?

To be clear, I can't create an empty array or zero-array in advance since dimensions of my ndarray are unknown a priori.

My question is somewhat similar, but still fairly tangential & a bit more involved compared to the following post: Create an ndarray from arrays of different lengths

I have 3 time-series variables I track in arr1, arr2 & arr3. Every time a set of conditions is met (call this a "transition"), I want to store these arrays into what a I call a db_arr. I then reset the 3 arrays per some logic. So on & so forth till my loop ends. The len of these arrays can differ. So after 1 transition, db_arr should be a 2d array with 3 rows (one for each arr) & variable # of columns. I use vstack to concatenate arr1, arr2 & arr3, which requires dimensions to match. So I pad the 3 arrays to the len of some other reference array A:

arr1 = np.lib.pad(arr1, (0, len(A)-len(arr1)), 'constant', constant_values=(0,0)) 

Same for arr2 & arr3. I can then vstack & concatenate them to db_arr, yielding the desired 2d array of shape=(3,len(A)).

I'd like to concatenate these 2d 'slices' along a 3rd dimension, stacking them to create depth. So say after 5 transitions, db_arr would have shape=(3,len(A),5).

Q1: I've looked at dstack, stack, etc., but I can't seem to get the desired concatenation along this new axis. Feel like I'm missing something simple.

Q2: I stated len(A) as being constant - it's not. The len of arr1, arr2, arr3 as well as A vary from transition to transition. So not only must I pad arrays per 2d slice as above, I also need to do so for each previous slice as new transitions yield slices of different lengths. I can pad against the max length of all slices at each new transition, but I'm wondering if there's a simpler, more elegant, 'pythonic' approach?

To be clear, I can't create an empty array or zero-array in advance since dimensions of my ndarray are unknown a priori.

Example: Let timeseries = np.linspace(1,500,50) Loop through timeseries. arr1 & arr2 are constructed / appended to per some logic. Certain conditions trigger a 'transition', which require emptying arr1 & arr2, but concatenating them to db_arr first. At the time of the first transition, say arr1 = [1,2,3] & arr2 = [4,5,6,7]. Assume transition occurs at index = 14 of timeseries. vstack requires equal dims, so I can either pad arr1 to be of same len as arr2, or pad both to be of len(timeseries) at time of transition (i.e. 15). Either allows me to then vstack((arr1,arr2)), then concatenate to db_array.

Now say the next transition occurs at index 40. arr1 = [8,9,10,11,12] & arr2 = [13,14]. I can pad them to be same len using either of aforementioned methods. I want to concatenate this 'slice' along a third dimension. However I pad arr1 & arr2, they will be of different len than the prior slice comprised of arr1 & arr2 vstacked. Thus my question.

Source Link
F.F.
  • 47
  • 7

Construct ndarrays from 1d arrays of different lengths

My question is somewhat similar, but still fairly tangential & a bit more involved compared to the following post: Create an ndarray from arrays of different lengths

I have 3 time-series variables I track in arr1, arr2 & arr3. Every time a set of conditions is met (call this a "transition"), I want to store these arrays into what a I call a db_arr. I then reset the 3 arrays per some logic. So on & so forth till my loop ends. The len of these arrays can differ. So after 1 transition, db_arr should be a 2d array with 3 rows (one for each arr) & variable # of columns. I use vstack to concatenate arr1, arr2 & arr3, which requires dimensions to match. So I pad the 3 arrays to the len of some other reference array A:

arr1 = np.lib.pad(arr1, (0, len(A)-len(arr1)), 'constant', constant_values=(0,0)) 

Same for arr2 & arr3. I can then vstack & concatenate them to db_arr, yielding the desired 2d array of shape=(3,len(A)).

I'd like to concatenate these 2d 'slices' along a 3rd dimension, stacking them to create depth. So say after 5 transitions, db_arr would have shape=(3,len(A),5).

Q1: I've looked at dstack, stack, etc., but I can't seem to get the desired concatenation along this new axis. Feel like I'm missing something simple.

Q2: I stated len(A) as being constant - it's not. The len of arr1, arr2, arr3 as well as A vary from transition to transition. So not only must I pad arrays per 2d slice as above, I also need to do so for each previous slice as new transitions yield slices of different lengths. I can pad against the max length of all slices at each new transition, but I'm wondering if there's a simpler, more elegant, 'pythonic' approach?

To be clear, I can't create an empty array or zero-array in advance since dimensions of my ndarray are unknown a priori.