annotate tests/test_broadcasting.py @ 51:e1eb45c4763e

[compat] fix variable scoping for py3
author Jeff Hammel <k0scist@gmail.com>
date Sun, 17 Sep 2017 18:08:55 -0700
parents 926c127305fa
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
14
ec5f97abf8ed stub np broadcasting test
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
1 #!/usr/bin/env python
ec5f97abf8ed stub np broadcasting test
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
2
ec5f97abf8ed stub np broadcasting test
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
3 """
ec5f97abf8ed stub np broadcasting test
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
4 illustration of numpy broadcasting
ec5f97abf8ed stub np broadcasting test
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
5 """
ec5f97abf8ed stub np broadcasting test
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
6
ec5f97abf8ed stub np broadcasting test
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
7 import os
ec5f97abf8ed stub np broadcasting test
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
8 import unittest
ec5f97abf8ed stub np broadcasting test
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
9 import numpy as np
ec5f97abf8ed stub np broadcasting test
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
10
ec5f97abf8ed stub np broadcasting test
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
11 #apples, beef, eggs, potatos
ec5f97abf8ed stub np broadcasting test
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
12 calories = [[56., 0.0, 4.4, 68.], # carbs
ec5f97abf8ed stub np broadcasting test
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
13 [1.2, 104., 52., 8.], # protein
ec5f97abf8ed stub np broadcasting test
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
14 [1.8, 135., 99., 0.9]] # fat
ec5f97abf8ed stub np broadcasting test
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
15
ec5f97abf8ed stub np broadcasting test
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
16 class TestBroadcasting(unittest.TestCase):
ec5f97abf8ed stub np broadcasting test
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
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
ec5f97abf8ed stub np broadcasting test
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
25
ec5f97abf8ed stub np broadcasting test
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
26 if __name__ == '__main__':
ec5f97abf8ed stub np broadcasting test
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
27 unittest.main()