main.py 1.6 KB

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