You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

NumPy数组分块后如何从每个分块为各列特征生成2-3个数据点

实现方案

你可以根据自身需求选择「随机采样原始数据点」或「生成统计特征代表点」两种实现逻辑,完整可运行代码如下:

import numpy as np

# 原有分块逻辑保持不变
data = np.array([[-1, -1], [-2, -1.9], [-3, -2], [1, 1.5], [2.7, 1], [3, 2], [-4, 10], 
[-10, 5], [-6, -10],
             [-11, -2], [-2, -9], [-5, -5], [4, 6.5], [5.7, 6], [7, 2], [-9.5, -10], [10, -5], 
 [-6.4, -1.1]])

def chunk_data(X, chunk_counts=10):
    len_ = len(X)
    half_size = len_ // 2
    first_chunk = [X[: half_size].tolist()]
    rest_data = X[half_size:]
    rest_chunks_n = np.array_split(rest_data, chunk_counts - 1)
    rest_chunks = [arr.tolist() for arr in rest_chunks_n]
    return first_chunk + rest_chunks
                     
X = chunk_data(data, chunk_counts=3)

# 新增按分块、按列生成点的功能
def generate_points_per_chunk(chunks, point_count=3, mode='stat'):
    """
    :param chunks: 分块后的列表,每个元素是二维列表
    :param point_count: 每列生成的点数,可设置为2或3
    :param mode: 生成模式:random为随机采样原始数据,stat为生成统计特征点
    """
    result = []
    for chunk in chunks:
        chunk_np = np.array(chunk)
        feature_num = chunk_np.shape[1]
        chunk_res = []
        for col_idx in range(feature_num):
            col_data = chunk_np[:, col_idx]
            if mode == 'random':
                # 无放回随机采样指定数量的原始数据点
                selected = np.random.choice(col_data, size=point_count, replace=False)
            elif mode == 'stat':
                # 用列的统计值生成代表点:2个点取最小/最大,3个点额外加中位数
                if point_count == 3:
                    selected = np.array([np.min(col_data), np.median(col_data), np.max(col_data)])
                else:
                    selected = np.array([np.min(col_data), np.max(col_data)])
            chunk_res.append(selected.tolist())
        result.append(chunk_res)
    return result

# 调用示例:每列生成3个统计特征点
output = generate_points_per_chunk(X, point_count=3, mode='stat')
print(output)

说明

  • 返回结果和分块顺序一一对应,每个分块下的第N个子列表就是第N个特征列的生成点集合
  • 若需要保留原始数据的样本特征,可将mode参数改为random

内容的提问来源于stack exchange,提问作者user4556432

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.03 12:18:04