Implement a Weighted Hard-Voting Classifier

Quick Overview

Implement a weighted hard-voting ensemble classifier in Python with fit and predict, where each member adds its weight to the label it predicts and ties go to the smallest sorted label. It tests input validation, mapping labels to columns, vectorized vote counting in NumPy and agreement with scikit-learn's hard VotingClassifier.

Implement a Weighted Hard-Voting Classifier

Role: Machine Learning Engineer

Category: Machine Learning

Difficulty: medium

Interview Round: Other

Implement `WeightedHardVotingClassifier(estimators, weights=None)` with `fit(X, y)` and `predict(X)`. Each fitted member casts one vote of its weight for its predicted label. Each sample's prediction is the label with the highest total weight. The class must meet these requirements: - `fit` deep-copies each estimator, fits the copy, stores the copies in `estimators_`, sets `classes_ = np.unique(y)` and returns `self`. - `weights=None` means equal weights. A zero weight contributes nothing. - Ties go to the smallest label in sorted `classes_` order (lexicographic for strings). - In `fit`, raise `ValueError` for no estimators, a weights length different from the estimator count, or any negative weight. - In `predict`, raise `ValueError` if a member predicts a label not in `classes_`. Return an array of labels drawn from `classes_`. Estimators are any objects with `fit(X, y)` and `predict(X)`. Results must match scikit-learn's `VotingClassifier(voting="hard")` with the same weights. For example, with `classes_ = [0, 1, 2]`, three members and three samples: ```text weights = [2.0, 1.0, 1.0] member 0 predicts [2, 2, 1] member 1 predicts [1, 0, 2] member 2 predicts [1, 0, 0] Output: [1, 0, 1] ``` In sample 0, label 2 collects weight 2 from member 0 and label 1 collects 1 + 1 = 2 from members 1 and 2, so the tie goes to the smaller label, 1. In sample 1, labels 2 and 0 tie at 2 and the answer is 0. In sample 2, label 1 gets 2 while labels 2 and 0 get 1 each, so label 1 wins. ```hint Breaking ties Lay the vote totals out so that the column order matches sorted `classes_`, then check which index `np.argmax` returns when several columns share the maximum. ``` ```hint Mapping labels to columns Labels can be strings or integers with gaps, such as 3, 7 and 10. You need each predicted label's position in the sorted `classes_` array. A binary search over that array finds it, and comparing the label stored at that position with the prediction tells you when a label is missing. ``` ### Constraints & Assumptions - `X` has `n` rows, and every member's `predict(X)` returns `n` labels. - Labels in one problem share a type (all integers or all strings), so `np.unique` can sort them. ### What a Strong Answer Covers - Validating the estimators and weights in `fit`, and fitting deep copies so the caller's estimators are left untouched. - A vote layout whose column order follows sorted `classes_`, so the tie rule comes from how the maximum is taken. - Mapping integer or string labels to columns and rejecting a label that is not in `classes_`. - Vectorized vote accumulation, with its time and memory cost in terms of members, samples and classes. - Edge cases such as zero weights, all weights zero and floating-point ties, and why the output matches scikit-learn's hard voting. ### Follow-up Questions - How would you add soft voting? - How does the cost scale with many classes or samples? - How would you test that your class agrees with scikit-learn's `VotingClassifier(voting="hard")`?

Overview: Implement a weighted hard-voting ensemble classifier in Python with fit and predict, where each member adds its weight to the label it predicts and ties go to the smallest sorted label. It tests input validation, mapping labels to columns, vectorized vote counting in NumPy and agreement with scikit-learn's hard VotingClassifier.

General logo
General
Nov 25, 2026
mediumMachine Learning EngineerOtherMachine Learning
0
0

Implement WeightedHardVotingClassifier(estimators, weights=None) with fit(X, y) and predict(X). Each fitted member casts one vote of its weight for its predicted label. Each sample's prediction is the label with the highest total weight.

The class must meet these requirements:

  • fit deep-copies each estimator, fits the copy, stores the copies in estimators_ , sets classes_ = np.unique(y) and returns self .
  • weights=None means equal weights. A zero weight contributes nothing.
  • Ties go to the smallest label in sorted classes_ order (lexicographic for strings).
  • In fit , raise ValueError for no estimators, a weights length different from the estimator count, or any negative weight.
  • In predict , raise ValueError if a member predicts a label not in classes_ . Return an array of labels drawn from classes_ .

Estimators are any objects with fit(X, y) and predict(X). Results must match scikit-learn's VotingClassifier(voting="hard") with the same weights.

For example, with classes_ = [0, 1, 2], three members and three samples:

weights = [2.0, 1.0, 1.0]
member 0 predicts [2, 2, 1]
member 1 predicts [1, 0, 2]
member 2 predicts [1, 0, 0]
Output: [1, 0, 1]

In sample 0, label 2 collects weight 2 from member 0 and label 1 collects 1 + 1 = 2 from members 1 and 2, so the tie goes to the smaller label, 1. In sample 1, labels 2 and 0 tie at 2 and the answer is 0. In sample 2, label 1 gets 2 while labels 2 and 0 get 1 each, so label 1 wins.

Constraints & Assumptions

  • X has n rows, and every member's predict(X) returns n labels.
  • Labels in one problem share a type (all integers or all strings), so np.unique can sort them.

What a Strong Answer Covers Guidance

  • Validating the estimators and weights in fit , and fitting deep copies so the caller's estimators are left untouched.
  • A vote layout whose column order follows sorted classes_ , so the tie rule comes from how the maximum is taken.
  • Mapping integer or string labels to columns and rejecting a label that is not in classes_ .
  • Vectorized vote accumulation, with its time and memory cost in terms of members, samples and classes.
  • Edge cases such as zero weights, all weights zero and floating-point ties, and why the output matches scikit-learn's hard voting.

Follow-up Questions Guidance

  • How would you add soft voting?
  • How does the cost scale with many classes or samples?
  • How would you test that your class agrees with scikit-learn's VotingClassifier(voting="hard") ?