Mercurial > hg > tvii
annotate tests/test_broadcasting.py @ 67:4bf2145c16f8
and quick transposition function
author | Jeff Hammel <k0scist@gmail.com> |
---|---|
date | Sun, 17 Dec 2017 12:53:47 -0800 |
parents | 926c127305fa |
children |
rev | line source |
---|---|
14 | 1 #!/usr/bin/env python |
2 | |
3 """ | |
4 illustration of numpy broadcasting | |
5 """ | |
6 | |
7 import os | |
8 import unittest | |
9 import numpy as np | |
10 | |
11 #apples, beef, eggs, potatos | |
12 calories = [[56., 0.0, 4.4, 68.], # carbs | |
13 [1.2, 104., 52., 8.], # protein | |
14 [1.8, 135., 99., 0.9]] # fat | |
15 | |
16 class TestBroadcasting(unittest.TestCase): | |
17 def test_basic(self): | |
15
926c127305fa
[np] this is how broadcasting works
Jeff Hammel <k0scist@gmail.com>
parents:
14
diff
changeset
|
18 """example of broadcasting""" |
926c127305fa
[np] this is how broadcasting works
Jeff Hammel <k0scist@gmail.com>
parents:
14
diff
changeset
|
19 |
926c127305fa
[np] this is how broadcasting works
Jeff Hammel <k0scist@gmail.com>
parents:
14
diff
changeset
|
20 A = np.array(calories) |
926c127305fa
[np] this is how broadcasting works
Jeff Hammel <k0scist@gmail.com>
parents:
14
diff
changeset
|
21 cal = A.sum(axis=0) |
926c127305fa
[np] this is how broadcasting works
Jeff Hammel <k0scist@gmail.com>
parents:
14
diff
changeset
|
22 print (cal) |
926c127305fa
[np] this is how broadcasting works
Jeff Hammel <k0scist@gmail.com>
parents:
14
diff
changeset
|
23 percentage = 100*A/cal.reshape(1,4) |
926c127305fa
[np] this is how broadcasting works
Jeff Hammel <k0scist@gmail.com>
parents:
14
diff
changeset
|
24 print (percentage) |
14 | 25 |
26 if __name__ == '__main__': | |
27 unittest.main() |