我有一个像这样的大型 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)
我想使用一个具有参数“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)
谢谢。
最佳答案
这将返回一个 columnsize = 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 - 在 python 中创建一个没有 aubarrays 作为参数传递的子数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46283042/