import numpy as np
def test_run():
a = np.random.random((5, 4))
print a
print a.shape
if __name__ == "__main__":
test_run()
import numpy as np
def test_run():
a = np.random.random((5, 4))
print a.size
if __name__ == "__main__":
test_run()
import numpy as np
def test_run():
np.random.seed(693)
a = np.random.randint(0, 10, size=(5, 4))
print "Array:\n", a
if __name__ == "__main__":
test_run()
Locate maximum value
import numpy as np
def get_max_index(a):
return a.argmax()
def test_run():
a = np.array([9, 6, 2, 3, 12, 14, 7, 10], dtype=np.int32)
print "Array:", a
print "Maximum value", a.max()
print "Index of max.:", get_max_index(a)
if __name__ == "__main__":
test_run()
Timing python operations
import time
def time_run():
t1 = time.time()
print "ML4T"
t2 = time.time()
print "The time taken by print statement is ",t2 - t1," seconds"
if __name__ == "__main__":
test_run()
Accessing array element
import numpy as np
def test_run():
a = np.random.rand(5, 4)
print "Array:\n", a
element = a[3, 2]
print element
if __name__ == "__main__":
test_run()
Indexing an array with another array
import numpy as np
def test_run():
a = np.random.rand(5)
indices = np.array([1,1,2,3])
print a[indices]
if __name__ == "__main__":
test_run()