main.py 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. import numpy as np
  2. import unittest
  3. a = np.array([3, 2, 1], dtype='uint8')
  4. assert a.dtype == "uint8"
  5. b = np.zeros((5,5))
  6. assert b.shape == (5, 5) and b.sum() == 0
  7. c = np.ones((5,5,5))
  8. assert c.ndim == 3 and c.sum() / c.size == 1
  9. d = np.arange(-5, 5, 1)
  10. assert np.all(d == np.array([-5, -4, -3, -2, -1, 0, 1, 2, 3, 4]))
  11. e = np.linspace(0, 1, 5)
  12. assert np.all(e == np.array([0., 0.25, 0.5, 0.75, 1.0]))
  13. f = np.arange(5 * 5).reshape(5, 5)
  14. fc = f[::2,[1,3]]
  15. assert np.all(fc == np.array([[1, 3], [11, 13], [21, 23]]))
  16. g = np.ones((5, 3))
  17. gc = np.sum(g, axis=1)
  18. assert np.all(gc == np.array([3., 3., 3., 3., 3.]))
  19. h = np.arange(5) + 1
  20. hc = h*2
  21. assert np.all(hc == np.array([2., 4., 6., 8., 10.]))
  22. j = np.array([1, 2, 3, 4, 9, 7, 11, 12, 15, 14, 33])
  23. mask = (j % 3 == 0)
  24. jc = j[mask]
  25. assert np.all(jc == np.array([3, 9, 12, 15, 33]))
  26. k = np.array([1, 2, 3, 4, 5])
  27. l = np.array([2, 2, 3, 3, 4])
  28. kl = k**l
  29. assert np.all(kl == np.array([1, 4, 27, 64, 625]))
  30. m = np.array([2, 2, 2, 3, 3, 3])
  31. mc = m.std()
  32. assert mc == 0.5
  33. n = np.array([1, 2, 3, 4, 5, 6])
  34. nc = n.mean()
  35. assert nc == 3.5
  36. o = np.array([2, 2, 2, 2])
  37. oc = o.reshape((2,2))
  38. assert oc.ndim == 2 and oc.shape == (2, 2)
  39. p = np.array([1, 2, 3, 4])
  40. pc = p[::-1]
  41. assert np.all(pc == np.array([4, 3, 2, 1]))
  42. r = np.array([3, 3, 5, 5])
  43. rc = r.copy()
  44. rc[1:3] = -1
  45. assert np.all(r[1:3] == np.array([3, 5])) and np.all(rc[1:3] == np.array([-1, -1]))