Majority Number I&II

本文介绍了一种高效算法——摩尔投票法,用于在时间复杂度为O(n)和空间复杂度为O(1)的情况下找出数组中的多数元素。文章通过两个示例展示了如何实现该算法,并分别处理了多数元素出现次数超过一半和超过三分之一的情况。

这个题的名字是在lintcode中,在leetcode中的名字是Majority Element,虽然有少许不同,但是主要都是一样的。

这个题很简单,但是如果要满足n的时间复杂度和1的空间复杂度则需要摩尔投票法。废话不多说了,直接上代码。

I:

public class Solution {
    /**
     * @param nums: a list of integers
     * @return: find a  majority number
     */
    public int majorityNumber(ArrayList<Integer> nums) {
        // write your code
        int length = nums.size();
        if (length <= 0){
            return 0;
        }
        if (length == 1){
            return nums.get(0);
        }
        int count = 1;
        int index = 0;
        for (int i = 1; i < length; i++){
            if (nums.get(index) == nums.get(i)){
                count++;
            } else {
                count--;
                if (count == 0){
                    index = i;
                    count = 1;
                }
            }
        }
        int res = nums.get(index);
        count = 0;
        for (int i = 0; i < length; i++){
            if (res == nums.get(i)){
                count++;
            }
        }
        if (count > length / 2){
            return res;
        }
        return 0;
    }
}

II:

public class Solution {
    /**
     * @param nums: A list of integers
     * @return: The majority number that occurs more than 1/3
     */
    public int majorityNumber(ArrayList<Integer> nums) {
        // write your code
        int length = nums.size();
        if (length <= 0){
            return 0;
        }
        if (length == 1){
            return nums.get(0);
        }
        int counta = 0;
        int a = 0;
        int countb = 0;
        int b = 0;
        for (int num : nums){
            if (num == a){
                counta++;
            } else if (num == b){
                countb++;
            } else {
                
                if (counta == 0){
                    counta = 1;
                    a = num;
                } else if (countb == 0){
                    countb = 1;
                    b = num;
                } else {
                    counta--;
                    countb--;
                }
                
            }
        }
        counta = 0;
        countb = 0;
        for (int num : nums){
            if (num == a){
                counta++;
            } else if (num == b){
                countb++;
            }
        }
        if (counta > length / 3){
            return a;
        }
        if (countb > length / 3){
            return b;
        }
        return 0;
        
    }
}


shuffled_idx = shuffle(np.array(range(sample_number)), random_state=seed) train_idx = shuffled_idx[:int(0.1 * sample_number)] val_idx = shuffled_idx[int(0.1 * sample_number):int(0.2 * sample_number)] test_idx = shuffled_idx[int(0.2 * sample_number):] X_smo, y_smo = balance_MLSMOTE(labeled_X, labeled_y, args.smote_num) def MLSMOTE(X, y, n_sample): """ Give the augmented data using MLSMOTE algorithm args X: pandas.DataFrame, input vector DataFrame y: pandas.DataFrame, feature vector dataframe n_sample: int, number of newly generated sample return new_X: pandas.DataFrame, augmented feature vector data target: pandas.DataFrame, augmented target vector data """ if not isinstance(X, pd.DataFrame): X = pd.DataFrame(X) if not isinstance(y, pd.DataFrame): y = pd.get_dummies(np.array(y)) indices2 = nearest_neighbour(X) n = len(indices2) new_X = np.zeros((n_sample, X.shape[1])) target = np.zeros((n_sample, y.shape[1])) for i in range(n_sample): reference = random.randint(0, n - 1) neighbour = random.choice(indices2[reference, 1:]) all_point = indices2[reference] nn_df = y[y.index.isin(all_point)] ser = nn_df.sum(axis=0, skipna=True) target[i] = np.array([1 if val > 2 else 0 for val in ser]) ratio = random.random() gap = X.loc[reference, :] - X.loc[neighbour, :] new_X[i] = np.array(X.loc[reference, :] + ratio * gap) new_X = pd.DataFrame(new_X, columns=X.columns) target = pd.DataFrame(target, columns=y.columns) new_X = pd.concat([X, new_X], axis=0) target = pd.concat([y, target], axis=0) return new_X.values, np.argmax(target.values,axis=1) def balance_MLSMOTE(labeled_X, labeled_y, n_sample): X_list = []#存储每个类别的特征,元素为列表 y_list = [] for i in range(max(labeled_y) + 1): X_list.append(labeled_X[labeled_y == i, :]) y_list.append(labeled_y[labeled_y == i]) print("lenX_list,len y_list",len(X_list),len(y_list)) num_classes = max(labeled_y) + 1#类别数 one_hot_codes = np.eye(num_classes)#单位矩阵,便于后续独热向量 df_y_list = []#将每个标签转换为独热编码 for i in range(len(y_list)): one_hot_labels = [] for label in y_list[i]: one_hot_label = one_hot_codes[label] one_hot_labels.append(one_hot_label) df_y = pd.DataFrame(np.array(one_hot_labels)) df_y_list.append(df_y) if n_sample == None: smote_num = 0 for i in range(len(y_list)): if len(y_list[i]) > smote_num: smote_num = len(y_list[i]) majority_class = i else: smote_num = n_sample for i in range(len(y_list)): if smote_num - len(y_list[i]) > 0: X_res, y_res = MLSMOTE(X_list[i], df_y_list[i], smote_num - len(y_list[i])) else: X_res, y_res = X_list[i], y_list[i] if i == 0: X_smo = X_res y_smo = y_res else: X_smo = np.concatenate([X_smo, X_res], axis=0) y_smo = np.concatenate([y_smo, y_res], axis=0) return X_smo, np.squeeze(y_smo) idx_except_train = torch.LongTensor(range(len(labels)))[~data.train_mask.cpu()] orign_idx_train = torch.tensor(np.array(range(len(train_idx))), dtype=torch.long).cuda() new_idx_train = torch.tensor(np.array(range(len(y_smo))), dtype=torch.long).cuda() new_idx_val = torch.tensor(val_idx, dtype=torch.long).cuda() + torch.tensor(len(y_smo) - len(train_idx)).cuda() new_idx_test = torch.tensor(test_idx, dtype=torch.long).cuda() + torch.tensor(len(y_smo) - len(train_idx)).cuda() X_generate = torch.FloatTensor(np.concatenate([X_smo, all_X[idx_except_train, :]], axis=0)).cuda() y_generate = torch.LongTensor(np.concatenate([y_smo, all_y[idx_except_train]], axis=0)).cuda() 这几段是合成节点前后的数据集划分,请你分析一下,正确的划分方式应该是怎样的
最新发布
08-13
在机器学习项目中,数据集划分与合成节点的处理是影响模型性能和泛化能力的关键步骤。为了确保训练、验证和测试索引的正确性,需从以下几个方面进行分析与修正。 ### 数据集划分逻辑的分析与修正 数据集划分通常采用 `train_test_split` 函数,该方法允许将数据集划分为训练集和测试集,并支持通过参数 `stratify` 保持类别分布的一致性。在某些情况下,还需要进一步引入验证集以进行模型调参和选择。为确保划分的合理性,应采用以下方式: - **引入验证集**:将数据集划分为训练集、验证集和测试集。常见的划分比例为 60% 训练集、20% 验证集、20% 测试集。 - **保持类别分布一致性**:使用 `stratify` 参数以确保划分后的数据集中类别分布与原始数据集一致,避免因类别偏移导致模型性能下降。 - **打乱数据顺序**:设置 `shuffle=True` 以确保数据在划分前被随机打乱,防止因数据顺序影响模型训练效果。 以下是一个完整的划分示例: ```python from sklearn.model_selection import train_test_split # 提取特征和目标变量 features = data_select.drop(['幸存'], axis=1) targets = pd.DataFrame(data_select['幸存']) # 第一次划分:将数据分为训练集(60%)和临时集(40%) x_train, x_temp, y_train, y_temp = train_test_split(features, targets, test_size=0.4, random_state=123, shuffle=True, stratify=targets) # 第二次划分:将临时集进一步划分为验证集(20%)和测试集(20%) x_val, x_test, y_val, y_test = train_test_split(x_temp, y_temp, test_size=0.5, random_state=123, shuffle=True, stratify=y_temp) # 输出各数据集的形状 print('训练集:', x_train.shape, '验证集:', x_val.shape, '测试集:', x_test.shape) ``` 上述代码确保了训练集、验证集和测试集的划分比例为 6:2:2,并通过 `stratify` 保持了目标变量的类别分布一致性。 ### 合成节点处理逻辑的分析与修正 合成节点通常用于数据增强或分布式计算场景,例如在 Elasticsearch 中的协调节点(Coordinator Node),其作用是接收客户端请求并协调数据节点之间的操作。在机器学习中,合成节点的概念可以类比于数据预处理和特征工程中的数据增强或合成样本生成。 - **数据增强**:通过生成合成样本提升模型的泛化能力,尤其是在数据量较少的情况下。 - **特征工程中的合成特征**:通过组合原始特征生成新的特征,以提高模型的预测能力。 在合成节点处理过程中,需要注意以下几点: 1. **合成样本应与原始数据分布一致**:避免引入偏差,影响模型的泛化能力。 2. **合成样本的划分**:合成样本应仅在训练集中使用,验证集和测试集应保持原始数据状态,以评估模型的真实性能。 3. **合成特征的生成**:应在训练集上生成,并应用到验证集和测试集,以避免数据泄露。 以下是一个合成特征生成的示例: ```python # 在训练集上生成合成特征 x_train['合成特征'] = x_train['特征1'] * x_train['特征2'] # 在验证集和测试集上应用相同的合成特征生成逻辑 x_val['合成特征'] = x_val['特征1'] * x_val['特征2'] x_test['合成特征'] = x_test['特征1'] * x_test['特征2'] ``` 通过上述方式,可以确保合成特征的生成逻辑在训练集、验证集和测试集之间保持一致,同时避免数据泄露。 ### 总结 在机器学习项目中,合理的数据集划分和合成节点处理是确保模型性能和泛化能力的关键。通过引入验证集、保持类别分布一致性、打乱数据顺序以及合理生成合成特征,可以有效提升模型的稳定性和预测能力。在实际应用中,应根据具体任务需求调整划分比例和合成策略,以达到最佳效果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值