-
-
Notifications
You must be signed in to change notification settings - Fork 10.8k
MAINT: cleanup np.average #7382
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -167,6 +167,29 @@ def test_returned(self): | |
avg, scl = average(y, weights=w2, axis=1, returned=True) | ||
assert_array_equal(scl, np.array([1., 6.])) | ||
|
||
def test_subclasses(self): | ||
class subclass(np.ndarray): | ||
pass | ||
a = np.array([[1,2],[3,4]]).view(subclass) | ||
w = np.array([[1,2],[3,4]]).view(subclass) | ||
|
||
assert_equal(type(np.average(a, weights=w)), subclass) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should that not be better as EDIT: Although that only works for new style classes. Hmm, assert_equal does seem to work properly there. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's not clear to me what |
||
|
||
# also test matrices | ||
a = np.matrix([[1,2],[3,4]]) | ||
w = np.matrix([[1,2],[3,4]]) | ||
|
||
r = np.average(a, axis=0, weights=w) | ||
assert_equal(type(r), np.matrix) | ||
assert_equal(r, [[2.5, 10.0/3]]) | ||
|
||
def test_upcasting(self): | ||
types = [('i4', 'i4', 'f8'), ('i4', 'f4', 'f8'), ('f4', 'i4', 'f8'), | ||
('f4', 'f4', 'f4'), ('f4', 'f8', 'f8')] | ||
for at, wt, rt in types: | ||
a = np.array([[1,2],[3,4]], dtype=at) | ||
w = np.array([[1,2],[3,4]], dtype=wt) | ||
assert_equal(np.average(a, weights=w).dtype, np.dtype(rt)) | ||
|
||
class TestSelect(TestCase): | ||
choices = [np.array([1, 2, 3]), | ||
|
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.
Should this be an
or
orand
with the same for wgt? Hmmm, actually, if, which one, hmmm :), I guess or?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.
I think the type of
wgt
doesn't matter. The current code does as follows for the four combinations to think about (float
meansf4
orf8
):I suppose in the 2nd line we could cast to whatever wgt's floating type was (f4 or f8) but I'm not sure whether that's helpful or unnecessarily complicated.
(Edit: Actually the 2nd line will give back
f8
no matter what we do, sinceint
+f4
coerces tof8
anyway)