Comparing Gini and Entropy
Ashby Mutual's modellers keep arguing about which impurity measure the rule-book ought to use. To settle it with evidence rather than opinion, the build tool is being changed to report both scores for the same group, side by side, so a whole season of builds can be compared afterwards. Gini impurity and entropy answer the same question on different scales: both are zero when every claim in the group ended the same way, and both are at their largest when the claims are spread evenly across the outcomes — but that largest value is a different number for each of them.
Task: Print both impurity scores for this group.
Input
Two lines. The first holds n, the number of possible outcomes. The second holds n whole numbers separated by single spaces — how many of the group's claims ended in each outcome. Counts may be zero.
Output
Two lines. The first holds the word gini, a single space, and the Gini impurity. The second holds the word entropy, a single space, and the entropy in bits. Both numbers carry exactly 4 digits after the decimal point, both labels are lowercase, and the gini line comes first.
Example:
Input:
2
40 10
Output:
gini 0.3200
entropy 0.7219
Sign in to solve this problem
Reading problems is free for everyone — solving them (Run, Submit, and tracking what you've solved) needs an account.
Sign in