数据预处理--采样

采样

采样就是按照某种规则从数据集中挑选样本数据。通常应用场景数据样本过大,抽取少部分样本来训练或验证,不仅可以节约计算机资源,在特定情况下也会提升实验效果。

随机采样

随机采样是从被采样数据集中随机地抽取特定数量的数据,需要指定采样的个数。随机采样分为有放回采样无放回采样
有放回采样: 可能会出现重复数据
无放回采样:采样数据不会出现重复的样本数据

系统采样

系统采样又称等距采样,就是将总体的采样数据集按某一顺序号分成n个部分,再从第一部分随机抽取第k号数据,依次用相等间距从每一部分中各抽取一个数据来组成样本。使用场景主要针对按一定关系排好的数据。
系统样本对于样本的限定过大,往往针对于不同层级的采样需要通过分层采样实现。

分层采样

分层采样是先将采样数据集分成若干个类别,再从每一类别内随机抽取一定数量的数据,然后将这些数据组合在一起。分层采样主要被用于生成训练样本的场景中。因为在监督学习中,正负样本的比例是不可控的,当正负样本的比例过大或过小时,对于样本训练结果都是有影响的,所以通常需要分层采样来控制训练样本中的正负样本比例。
分层采样从每一个层级中随机的抽取出特定的数据,每个层级抽取的比例是可以自定义的。

示例

注:random.randint(a, b),用于生成一个指定范围内的整数。其中参数a是下限,参数b是上限,生成的随机数n: a <= n <= b
       random.sample(sequence, k),从指定序列中随机获取指定长度的片断。sample函数不会修改原有序列

import random

dataMat = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
# 分布式采样所用数据
dataMat1 = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
dataMat2 = [100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 115, 114, 116, 117, 118, 119]
dataMat3 = [1000, 1001, 1002, 1003, 1004, 1005, 1006, 1007, 1008, 1009, 1010, 1011, 1012, 1013, 1014, 1015, 1016, 1017,
            1018, 1019]


# 放回式随机采样
def RepetitionRandomSampling(dataMat, number):
    sample = []
    for i in range(number):
        sample.append(dataMat[random.randint(0, len(dataMat) - 1)])
    return sample


# 无放回式随机采样
def RandomSampling(dataMat, number):
    try:
        slice = random.sample(dataMat, number)
        return slice
    except:
        print("sample larger than population")


# 系统采样
def SystematicSampling(dataMat, number):
    length = len(dataMat)
    k = length / number
    k = k.__int__()
    sample = []
    i = 0
    if k > 0:
        while len(sample) != number:
            sample.append(dataMat[0 + i * k])
            i += 1
        return sample
    else:
        return RandomSampling(dataMat, number)


# 分布式采样
def StratifiedSampling(dataMat1, dataMat2, dataMat3, number):
    sample = []
    num = number / 3
    num=num.__int__()
    sample.append(RandomSampling(dataMat1, num))
    sample.append(RandomSampling(dataMat2, num))
    sample.append(RandomSampling(dataMat3, num))
    return sample


if __name__ == '__main__':
    print("数据集:")
    print(dataMat)
    print("无放回式采样:")
    slice = RandomSampling(dataMat, 8)
    print(slice)
    print("放回式采样:")
    sample = RepetitionRandomSampling(dataMat, 15)
    print(sample)
    print("系统采样:")
    sample = SystematicSampling(dataMat, 5);
    print(sample)
    print("分布式采样:")
    sample = StratifiedSampling(dataMat1, dataMat2, dataMat3, 9);
    print(sample)



### 点云处理流程概述 点云数据的处理通常涉及三个主要阶段:预处理、3D 主干网络设计以及高度压缩技术。以下是这些部分的技术细节及其实施方法。 #### 预处理 (Point Cloud Preprocessing) 点云预处理的目标是对原始点云数据进行清洗和优化,以便后续模型能够更高效地学习特征。常见的预处理步骤包括去噪、下采样、坐标归一化等[^1]。 - **去噪**: 去除异常值或噪声点可以提高点云的质量。常用的方法有统计分析法和基于邻域密度的算法。 - **下采样**: 减少点的数量以降低计算复杂度并保留重要几何结构。Voxel Grid 是一种常用的下采样策略[^2]。 - **坐标归一化**: 将点云缩放到统一的空间范围内有助于提升训练稳定性。这一步可以通过简单的线性变换完成。 ```python import numpy as np def normalize_point_cloud(points): centroid = np.mean(points, axis=0) points -= centroid furthest_distance = np.max(np.sqrt(np.sum(abs(points)**2,axis=-1))) points /= furthest_distance return points ``` #### 3D 主干网络 (3D Backbone Network) 构建有效的三维主干网络对于提取点云中的深层特征至关重要。目前主流架构分为体素网格(Voxels)、多视图(Multi-view) 和点网(PointNet/PointNet++)三类[^3]。 - **PointNet++**: 这是一种分层神经网络框架,它通过递归分区机制捕获局部区域内的细粒度模式,并逐步聚合全局上下文信息[^4]。 ```python import torch.nn as nn class PointNetSetAbstraction(nn.Module): def __init__(self, ...): # Initialization parameters omitted for brevity. super(PointNetSetAbstraction, self).__init__() ... def forward(self, xyz, features=None): new_xyz, new_features = sample_and_group(...)(xyz, features) new_features = self.mlp(new_features) return new_xyz, new_features ``` #### 高度压缩 (Height Compression) 为了减少存储需求或者加速推理过程,在某些场景下可能需要对点的高度维度施加额外约束或简化操作。例如,LiDAR 数据常采用 BEV(Bird's Eye View) 投影方式来消除垂直方向上的冗余信息[^5]。 - **BEV 转换**: 将三维空间映射到二维平面表示形式,从而便于传统 CNN 处理。 ```python def convert_to_bev(lidar_points, grid_size=(200, 200), height_range=(-2.5, 1)): bev_map = np.zeros(grid_size + (3,), dtype=np.float32) min_x, max_x = -grid_size[0]/2., grid_size[0]/2. min_y, max_y = -grid_size[1]/2., grid_size[1]/2. for point in lidar_points: x_idx = int((point[0]-min_x)/(max_x-min_x)*grid_size[0]) y_idx = int((point[1]-min_y)/(max_y-min_y)*grid_size[1]) if height_range[0] <= point[2] <= height_range[1]: bev_map[x_idx][y_idx][:] += [1, point[2], point[3]] return bev_map ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值