cswork / OA打开练习工作台

PinterestOA #2

Bagging Classifier (Bootstrap Aggregating)

估计中等非算法题评测数据准备中,可先阅读题面

考点(AI 标注)模拟/实现位运算

Implement the missing code, denoted by ellipses. You may not modify the pre-existing code.

Your task is to implement parts of a Bagging algorithm from scratch (i.e., without importing any libraries or packages besides those already imported). As a reminder, Bagging classification comprises three major steps:

  1. Bootstrap the train sample for each base classifier.
  2. Train each base classifier based on its own bootstrapped samples.
  3. Assign class labels by majority vote of the base classifiers.

To validate the algorithm implementation, you will need to use it for some classification tasks. You will be given x_train (2D float array), y_train (1D int array of labels), and x_test.

Notes:

  • It is guaranteed that all training and test data will be float values.
  • In case of a tie in the voting, select the class label with the lower number.
  • For bootstrapping: n indices generated per classifier, where n is the number of training samples. Each index i generated by randint with 0 <= i < n. Indexes could repeat. seed should be used only in its initial place.