-
-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
stats.utils: Don't count zeros as nans
- Loading branch information
Showing
4 changed files
with
70 additions
and
31 deletions.
There are no files selected for viewing
Empty file.
This file contains 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,48 @@ | ||
import unittest | ||
|
||
import numpy as np | ||
import scipy as sp | ||
|
||
from Orange.statistics.util import stats | ||
|
||
|
||
class TestStats(unittest.TestCase): | ||
def test_stats(self): | ||
arr = np.array([[1, 4, 9], | ||
[-2, 10, 0], | ||
[0, np.nan, np.nan], | ||
[0, np.nan, 0]]) | ||
|
||
expected = [[-2, 1, -0.25, (1.25 ** 2 + 1.75 ** 2 + .25 ** 2 + .25 ** 2) / 4, 0, 4], | ||
[4, 10, 7, 3 ** 2, 2, 2], | ||
[0, 9, 3, (6 ** 2 + 3 ** 2 + 3 ** 2) / 3, 1, 3]] | ||
np.testing.assert_almost_equal(stats(arr, compute_variance=True), expected) | ||
|
||
sparr = sp.sparse.csc_matrix(arr) | ||
np.testing.assert_almost_equal(stats(sparr, compute_variance=True), expected) | ||
|
||
sparr = sparr.tocsr() | ||
np.testing.assert_almost_equal(stats(sparr, compute_variance=True), expected) | ||
|
||
weights = np.array([1, 2, 0, 3]) | ||
e0 = (1 * 1 - 2 * 2 + 0 * 0 + 3 * 0) / (1 + 2 + 0 + 3) | ||
e1 = (1 * 4 + 2 * 10) / 3 | ||
e2 = (1 * 9 + 2 * 0 + 3 * 0) / 6 | ||
expected = [[-2, 1, e0, ((e0 - 1) ** 2 + 2 * (e0 + 2) ** 2 + 3 * e0 ** 2) / 6, 0, 4], | ||
[4, 10, e1, ((e1 - 4) ** 2 + 2 * (e1 - 10) ** 2) / 3, 2, 2], | ||
[0, 9, e2, ((e2 - 9) ** 2 + 2 * e2 ** 2 + 3 * e2 ** 2) / 6, 1, 3]] | ||
|
||
np.testing.assert_almost_equal( | ||
stats(arr, weights=weights, compute_variance=True), expected) | ||
|
||
sparr = sp.sparse.csc_matrix(arr) | ||
np.testing.assert_almost_equal( | ||
stats(sparr, weights=weights, compute_variance=True), expected) | ||
|
||
sparr = sparr.tocsr() | ||
np.testing.assert_almost_equal( | ||
stats(sparr, weights=weights, compute_variance=True), expected) | ||
|
||
|
||
if __name__ == "__main__": | ||
unittest.main() |
This file contains 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 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