牛客题解 | 根据特征阈值划分数据集

题目

题目链接

根据特征阈值划分数据集(Feature Thresholding)是一种常用的数据处理方法,用于将数据集分为两部分,一部分满足特征阈值,另一部分不满足特征阈值。

本题的关键是,要知道python中数值类型和字符串类型是不同的,不能直接比较,需要使用isinstance函数判断阈值类型,并且常用的数值类型也就int和float两种。
标准代码如下
def divide_on_feature(X, feature_i, threshold):
    # Define the split function based on the threshold type
    split_func = None
    if isinstance(threshold, int) or isinstance(threshold, float):
        # For numeric threshold, check if feature value is greater than or equal to the threshold
        split_func = lambda sample: sample[feature_i] >= threshold
    else:
        # For non-numeric threshold, check if feature value is equal to the threshold
        split_func = lambda sample: sample[feature_i] == threshold

    # Create two subsets based on the split function
    X_1 = np.array([sample for sample in X if split_func(sample)])
    X_2 = np.array([sample for sample in X if not split_func(sample)])

    # Return the two subsets
    return [X_1, X_2]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值