-
Notifications
You must be signed in to change notification settings - Fork 2
Rand score #4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
minhsphuc12
wants to merge
4
commits into
chuvanan:master
Choose a base branch
from
minhsphuc12:rand_score
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Rand score #4
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
e402e3e
add few item to todo list
minhsphuc12 e5eca2d
remove F-measure line from todo list as already implemented
minhsphuc12 249af3a
add code, helper function, tests for mutual info score
minhsphuc12 ff217fa
add code, test, export for adjusted rand score
minhsphuc12 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,96 @@ | ||
| ##' @title | ||
| ##' Clustering Metrics Parameters | ||
| ##' | ||
| ##' @description | ||
| ##' Documentation for shared parameters of functions that compute clustering | ||
| ##' metrics. | ||
| ##' | ||
| ##' @param actual \code{[numeric]} The ground truth numeric vector. | ||
| ##' @param predicted \code{[numeric]} The predicted numeric vector, where each | ||
| ##' element in the vector is a prediction of the corresponding elements in | ||
| ##' \code{actual}. | ||
| ##' @name clustering_params | ||
| ##' @include helper-functions.r | ||
| NULL | ||
|
|
||
|
|
||
| ##' @title | ||
| ##' Adjusted Mutual Information Score / Mututal Information Score | ||
| ##' | ||
| ##' | ||
| ##' @description | ||
| ##' | ||
| ##' \code{mtr_mutual_info_score} measures the similarity, or mutual dependence | ||
| ##' between two variable. The worst possible score is 0, higher values are | ||
| ##' better. | ||
| ##' | ||
| ##' | ||
| ##' @inheritParams clustering_params | ||
| ##' @importFrom stats var | ||
| ##' @seealso \code{\link{mtr_adjusted_rand_score}} | ||
| ##' @return A numeric scalar output | ||
| ##' @author Phuc Nguyen | ||
| ##' @examples | ||
| ##' | ||
| ##' act <- sample(1:10, 100, replace = T) | ||
| ##' pred <- sample(1:10, 100, replace = T) | ||
| ##' mtr_mutual_info_score(act, pred) | ||
| ##' | ||
| ##' act <- rep(c('a', 'b', 'c'), times = 4) | ||
| ##' pred <- rep(c('a', 'b', 'c'), each = 4) | ||
| ##' mtr_mutual_info_score(act, pred) | ||
| ##' | ||
| ##' @export | ||
| mtr_mutual_info_score <- function(actual, predicted) { | ||
| chec_empty_vec(actual) | ||
| check_equal_length(actual, predicted) | ||
| entropy(actual) + entropy(predicted) - joint_entropy(vec_1 = actual, | ||
| vec_2 = predicted) | ||
| } | ||
|
|
||
| mtr_normalized_mutual_info_score <- function(actual, predicted) { | ||
| mtr_mutual_info_score(actual = actual, predicted = predicted) / | ||
| mean(c(entropy(vec = actual), entropy(vec = predicted))) | ||
| } | ||
|
|
||
| mtr_adjusted_mutual_info_score <- function(actual, predicted) { | ||
| (mtr_mutual_info_score(actual, predicted) - expected_mutual_info(actual, predicted)) / | ||
| (mean(c(entropy(actual), entropy(predicted))) - expected_mutual_info(actual, predicted)) | ||
| } | ||
|
|
||
| ##' @title | ||
| ##' Adjusted Rand Score | ||
| ##' | ||
| ##' | ||
| ##' @description | ||
| ##' | ||
| ##' \code{mtr_adjusted_rand_score} measures the similarity, or mutual dependence | ||
| ##' between two variable. Perfect score is 1. Score between total random vectors | ||
| ##' is close to 0. Score can be negative. | ||
| ##' | ||
| ##' | ||
| ##' @inheritParams clustering_params | ||
| ##' @importFrom base choose | ||
| ##' @seealso \code{\link{mtr_mutual_info_score}} | ||
| ##' @return A numeric scalar output | ||
| ##' @author Phuc Nguyen | ||
| ##' @examples | ||
| ##' | ||
| ##' act <- sample(1:10, 100, replace = T) | ||
| ##' pred <- sample(1:10, 100, replace = T) | ||
| ##' mtr_adjusted_rand_score(act, pred) | ||
| ##' | ||
| ##' act <- rep(c('a', 'b', 'c'), times = 4) | ||
| ##' pred <- rep(c('a', 'b', 'c'), each = 4) | ||
| ##' mtr_adjusted_rand_score(act, pred) | ||
| ##' | ||
| ##' @export | ||
|
|
||
| mtr_adjusted_rand_score <- function(actual, predicted) { | ||
| check_equal_length(actual, predicted) | ||
| N = length(actual) | ||
| a = sum(choose(table(actual, predicted), 2)) | ||
| b = sum(choose(table(actual), 2)) * sum(choose(table(predicted), 2)) / choose(N, 2) | ||
| c = 1/2 * (sum(choose(table(actual), 2)) + sum(choose(table(predicted), 2))) | ||
| (a - b) / (c - b) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
|
|
||
| ## test correctness ------------------------------------------------------------ | ||
|
|
||
| vec_a = c(0, 1, 2, 0, 3, 4, 5, 1) | ||
| vec_b = c(1, 1, 0, 0, 2, 2, 2, 2) | ||
|
|
||
| tinytest::expect_equal( | ||
| mtr_mutual_info_score(vec_a, vec_b), | ||
| target = 0.693147180559945, | ||
| tol = 1e-7 | ||
| ) | ||
|
|
||
| tinytest::expect_equal( | ||
| mtr_normalized_mutual_info_score(vec_a, vec_b), | ||
| target = 0.5163977794943221, | ||
| tol = 1e-7 | ||
| ) | ||
|
|
||
| tinytest::expect_equal( | ||
| mtr_adjusted_mutual_info_score(vec_a, vec_b), | ||
| target = -0.10526315789473674, | ||
| tol = 1e-7 | ||
| ) | ||
|
|
||
| tinytest::expect_equal( | ||
| mtr_adjusted_rand_score(vec_a, vec_b), | ||
| target = -0.12903225806451613, | ||
| tol = 1e-7 | ||
| ) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This line is changed as a mistake and would be fixed in next commit