Creating a subarray with no of aubarrays passed as arguments in python

471 views Asked by At

I have a large 100x15 array like this:

[a b c d e f g h i  j  k  l  m  n  o] 
[1 2 3 4 5 6 7 8 9 10 11 12 13 14 15]
[1 2 3 4 5 6 7 8 9 10 11 12 13 14 15]
[1 2 3 4 5 6 7 8 9 10 11 12 13 14 15]
[1 2 3 4 5 6 7 8 9 10 11 12 13 14 15]
[1 2 3 4 5 6 7 8 9 10 11 12 13 14 15]
.
.
.(Up to 100 rows)

I want to select a portion of this data into a subset using a function which has an argument 'k' in which 'k' denotes the no of subsets to be made, like say k=5 means the data attributes are divided into 3 subsets like below:

[a b c d e] [f g h i j]  [k  l  m  n  o] 
[1 2 3 4 5] [6 7 8 9 10] [11 12 13 14 15]
[1 2 3 4 5] [6 7 8 9 10] [11 12 13 14 15]
[1 2 3 4 5] [6 7 8 9 10] [11 12 13 14 15]
[1 2 3 4 5] [6 7 8 9 10] [11 12 13 14 15]
.
.
.(Up to 100 rows)

and they are stored in a different array. I want to implement this using python. I have implemented this partially. Can any one implement this and provide me the code in the answer?

Partial logic for the inner loop

given k
set start_index = 0
end_index = length of array/k = increment

for j from start_index to end_index
  start_index=end_index + 1
  end_index = end_index + increment
  //newarray[][] (I'm not sure abt here)

Thank You.

3

There are 3 answers

0
PSN On BEST ANSWER

Unfortunately I have to do it myself and this is the code in python for the logic. Anyway thanks to @astaning for the attempt.

def build_rotationtree_model(k):
 mtx =np.array([[2.95,6,63,23],[2,53,7,79],[3.57,5,65,32],[3.16,5,47,34],[21,2.58,4,46],[3.1,2.16,6,22],[3.5,3.27,3,52],[12,2.56,4,42]])    
 #Length of attributes (width of matrix)
 a = mtx.shape[1]
 newArray =[[0 for x in range(k)] for y in range(len(mtx))]
 #Height of matrix(total rows)
 b = mtx.shape[0]
 #Seperation limit
 limit = a/k
 #Starting of sub matrix
 start = 0
 #Ending of sub matrix
 end = a/k
 print(end)
 print(a)

 #Loop
 while(end != a):
    for i in range(0,b-1):
        for j in range(start,int(end)):
            newArray[i][j] = mtx[i][j]
        print(newArray[i])  
    #Call LDA function and add the result to Sparse Matrix
    #sparseMat = LDA(newArray) SHould be inside a loop
    start = end + 1
    end = end + limit
2
astaning On

This returns an array of matrices with columnsize = 2 , which works for k=2:

import numpy as np

def portion(mtx, k):

    array = []
    array.append( mtx[:, :k])

    for i in range(1, mtx.shape[1]-1):

        array.append( mtx[:, k*i:k*(i+1)])

    return array[:k+1]

mtx = np.matrix([[1,2,3,10,13,14], [4,5,6,11,15,16], [7,8,9,12,17,18]])
k = 2
print(portion(mtx, k))
1
Udhaya Kumar On
a=list(input())
for i in range(0,len(a)):
  for j in range(i,len(a)):
    for k in range(i,j+1):
      print(a[k],end=" ")
      print("\n",end="")