划分规则:
- 对于数值型阈值:将特征值大于等于阈值的样本划分到第一个子集,小于阈值的样本划分到第二个子集
- 对于非数值型阈值:将特征值等于阈值的样本划分到第一个子集,不等于阈值的样本划分到第二个子集
函数`divide_on_feature`接收三个参数:
1. X:数据集,二维numpy数组
2. feature_i:用于划分的特征索引,整数
3. threshold:划分阈值,可以是数值或其他类型
返回一个包含两个numpy数组的列表:
1. 第一个数组包含满足条件的样本
2. 第二个数组包含不满足条件的样本
[[1, 2], [3, 4], [5, 6]] 0 3
[array([[3, 4], [5, 6]]), array([[1, 2]])]
def divide_on_feature(X, feature_i, threshold): if isinstance(threshold, str): # Create a boolean mask where condition is met mask = X[:, feature_i] == threshold else: mask = X[:, feature_i] >= threshold # Use the mask to get the 'satisfied' and 'unsatisfied' subsets satisfied = X[mask] unsatisfied = X[~mask] return [satisfied, unsatisfied]