1

I want to split my numpy array into separate arrays. The separation must be based on the index. The split count is given by the user.

For example, The input array: my_array=[1,2,3,4,5,6,7,8,9,10]

If user gives split count =2, then, the split must be like

my_array1=[1,3,5,7,9]
my_array2=[2,4,6,8,10]

if user gives split count=3, then the output array must be

my_array1=[1,4,7,10]
my_array2=[2,5,8]
my_array3=[3,6,9]

could anyone please explain, I did for split count 2 using even odd concept

for i in range(len(outputarray)):
    if i%2==0:
        even_array.append(outputarray[i])  
    else:
        odd_array.append(outputarray[i])

I don't know how to do the split for variable counts like 3,4,5 based on the index.

Uday
  • 57
  • 12
  • Checkout: [Partition array into N chunks with Numpy](https://stackoverflow.com/questions/14406567/partition-array-into-n-chunks-with-numpy) – DarrylG Feb 26 '22 at 14:57

2 Answers2

0

Here is a python-only way of doing your task

def split_array(array, n=3):
    arrays = [[] for _ in range(n)]
    for x in range(n):
        for i in range(n):
            arrays[i] = [array[x] for x in range(len(array)) if x%n==i]
    return arrays

Input:

my_array=[1,2,3,4,5,6,7,8,9,10]
print(split_array(my_array, n=3))

Output:

[[1, 4, 7, 10], [2, 5, 8], [3, 6, 9]]
MD Mushfirat Mohaimin
  • 1,966
  • 3
  • 10
  • 22
  • could you please explain arrays[i] = [array[x] for x in range(len(array)) if x%n==i] @Mushfirat Mohaimin – Uday Feb 27 '22 at 06:26
0

You can use indexing by vector (aka fancy indexing) for it:

>>> a=np.array([1,2,3,4,5,6,7,8,9,10])
>>> n = 3
>>> [a[np.arange(i, len(a), n)] for i in range(n)]

[array([ 1,  4,  7, 10]), array([2, 5, 8]), array([3, 6, 9])]

Explanation

arange(i, len(a), n) generates an array of integers starting with i, spanning no longer than len(a) with step n. For example, for i = 0 it generates an array

 >>> np.arange(0, 10, 3)
 array([0, 3, 6, 9])

Now when you index an array with another array, you get the elements at the requested indices:

 >>> a[[0, 3, 6, 9]]
 array([ 1,  4,  7, 10])

These steps are repeated for i=1..2 resulting in the desired list of arrays.

Antony Hatchkins
  • 31,947
  • 10
  • 111
  • 111
  • could you please explain how the loop works @Antony Hatchkins – Uday Feb 27 '22 at 06:18
  • could you please explain how to write the separated array in separate text files @Antony Hatchkins – Uday Feb 27 '22 at 07:05
  • 1
    Something along the lines of `for i, r in enumerate(res): np.savetxt(f'array{i}.txt', r)` – Antony Hatchkins Feb 27 '22 at 07:33
  • 1
    I've added an explanation of how the loop works – Antony Hatchkins Feb 27 '22 at 07:41
  • Is there any other way to save the separated array in separate text file as the np.save() saves each element as float values with one line space. The actual requirement is to save the separated array integers in the separate file with one tab space between each element. Could You please mention any other way of writing into the file @Antony Hatchkins – Uday Feb 27 '22 at 15:26
  • 1
    It is , `..., fmt='%d', newline='\t')` but you could as well find the docs for `np.savetxt` on the net or press shift-tab in jupyter to read it right in the notebook. – Antony Hatchkins Feb 27 '22 at 19:26