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

标签 python arrays numpy sub-array

我有一个像这样的大型 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/

相关文章:

python - 使用基于正则表达式的模式匹配验证用户输入的逗号分隔坐标

IS8601格式的Python记录器输出日期

javascript - Node 为数组中的空值分配多少内存

python - 对数组进行渐进排序,就像 Excel 一样

python - 如何将字符串更改为浮点列表和n维列表的 'n'?

python - 使用 MatPlotLib 和 Numpy 将高斯拟合到直方图 - Y 缩放错误?

python - 检查范围的元素是否在列表的列表中

python - 从 pandas qcut 间隔中删除小数点(将间隔转换为整数)

ios - RESideMenu-两个数组

php - 如何使用 foreach PHP 循环 3 维数组