在 python 中创建一个没有作为参数传递的 aubarrays 的子数组

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

我有一个像这样的 100x15 大数组:

[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)

我想 select 使用具有参数 'k' 的函数将此数据的一部分转换为子集,其中 'k' 表示要制作的子集的数量,例如说 k=5 表示数据属性分为 3 个子集,如下所示:

[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)

并且它们存储在不同的数组中。我想使用 python 来实现它。我已经部分实现了这个。任何人都可以实现这一点并在答案中向我提供代码吗?

内循环的部分逻辑

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)

谢谢。

这个 returns 列大小为 2 的矩阵数组,适用于 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))

不幸的是我必须自己做,这是 python 中的逻辑代码。无论如何感谢@astaning 的尝试。

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
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="")