Decision Tree Best Split

Xem dạng PDF

Gửi bài giải

Điểm: 100,00
Giới hạn thời gian: 2.0s
Giới hạn bộ nhớ: 256M

Tác giả:
Dạng bài
Ngôn ngữ cho phép
Python

Task

Given a list of 1D feature values and binary labels, find the threshold that maximises Gini impurity gain. For each candidate threshold t (taken from the unique feature values), split samples into left (feature <= t) and right (feature > t), then compute gain = Gini(parent) - (|left|/n)Gini(left) - (|right|/n)Gini(right), where Gini(S) = 1 - p² - (1-p)² and p is the fraction of positive labels in S. Return the threshold and the best gain achieved.

Input

  • Line 1: n — number of samples
  • Lines 2 to n+1: feature label — one float feature value and one integer label (0 or 1) per line

Output

Print threshold gain on a single line, both as floats with up to 10 significant digits.

Example

Input:

6
1.0 0
2.0 0
3.0 1
4.0 1
5.0 1
6.0 0

Output:

2 0.25

Scaffolding

Submit a Python file defining:

def best_split(features: list[float], labels: list[int]) -> tuple[float, float]:
    ...

Receives a list of 1D feature values and a list of binary labels, and returns a tuple (threshold, gain) where threshold is the best split point and gain is the corresponding Gini impurity gain.


Bình luận

Hãy đọc nội quy trước khi bình luận.


Không có bình luận tại thời điểm này.