m2m模型翻译
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

249 lines
8.1 KiB

7 months ago
  1. import pytest
  2. import os
  3. import numpy as np
  4. from numpy.testing import (
  5. assert_, assert_equal, assert_array_equal, assert_array_almost_equal,
  6. assert_raises, _assert_valid_refcount,
  7. )
  8. class TestRegression:
  9. def test_poly1d(self):
  10. # Ticket #28
  11. assert_equal(np.poly1d([1]) - np.poly1d([1, 0]),
  12. np.poly1d([-1, 1]))
  13. def test_cov_parameters(self):
  14. # Ticket #91
  15. x = np.random.random((3, 3))
  16. y = x.copy()
  17. np.cov(x, rowvar=True)
  18. np.cov(y, rowvar=False)
  19. assert_array_equal(x, y)
  20. def test_mem_digitize(self):
  21. # Ticket #95
  22. for i in range(100):
  23. np.digitize([1, 2, 3, 4], [1, 3])
  24. np.digitize([0, 1, 2, 3, 4], [1, 3])
  25. def test_unique_zero_sized(self):
  26. # Ticket #205
  27. assert_array_equal([], np.unique(np.array([])))
  28. def test_mem_vectorise(self):
  29. # Ticket #325
  30. vt = np.vectorize(lambda *args: args)
  31. vt(np.zeros((1, 2, 1)), np.zeros((2, 1, 1)), np.zeros((1, 1, 2)))
  32. vt(np.zeros((1, 2, 1)), np.zeros((2, 1, 1)), np.zeros((1,
  33. 1, 2)), np.zeros((2, 2)))
  34. def test_mgrid_single_element(self):
  35. # Ticket #339
  36. assert_array_equal(np.mgrid[0:0:1j], [0])
  37. assert_array_equal(np.mgrid[0:0], [])
  38. def test_refcount_vectorize(self):
  39. # Ticket #378
  40. def p(x, y):
  41. return 123
  42. v = np.vectorize(p)
  43. _assert_valid_refcount(v)
  44. def test_poly1d_nan_roots(self):
  45. # Ticket #396
  46. p = np.poly1d([np.nan, np.nan, 1], r=False)
  47. assert_raises(np.linalg.LinAlgError, getattr, p, "r")
  48. def test_mem_polymul(self):
  49. # Ticket #448
  50. np.polymul([], [1.])
  51. def test_mem_string_concat(self):
  52. # Ticket #469
  53. x = np.array([])
  54. np.append(x, 'asdasd\tasdasd')
  55. def test_poly_div(self):
  56. # Ticket #553
  57. u = np.poly1d([1, 2, 3])
  58. v = np.poly1d([1, 2, 3, 4, 5])
  59. q, r = np.polydiv(u, v)
  60. assert_equal(q*v + r, u)
  61. def test_poly_eq(self):
  62. # Ticket #554
  63. x = np.poly1d([1, 2, 3])
  64. y = np.poly1d([3, 4])
  65. assert_(x != y)
  66. assert_(x == x)
  67. def test_polyfit_build(self):
  68. # Ticket #628
  69. ref = [-1.06123820e-06, 5.70886914e-04, -1.13822012e-01,
  70. 9.95368241e+00, -3.14526520e+02]
  71. x = [90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103,
  72. 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115,
  73. 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 129,
  74. 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141,
  75. 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156, 157,
  76. 158, 159, 160, 161, 162, 163, 164, 165, 166, 167, 168, 169,
  77. 170, 171, 172, 173, 174, 175, 176]
  78. y = [9.0, 3.0, 7.0, 4.0, 4.0, 8.0, 6.0, 11.0, 9.0, 8.0, 11.0, 5.0,
  79. 6.0, 5.0, 9.0, 8.0, 6.0, 10.0, 6.0, 10.0, 7.0, 6.0, 6.0, 6.0,
  80. 13.0, 4.0, 9.0, 11.0, 4.0, 5.0, 8.0, 5.0, 7.0, 7.0, 6.0, 12.0,
  81. 7.0, 7.0, 9.0, 4.0, 12.0, 6.0, 6.0, 4.0, 3.0, 9.0, 8.0, 8.0,
  82. 6.0, 7.0, 9.0, 10.0, 6.0, 8.0, 4.0, 7.0, 7.0, 10.0, 8.0, 8.0,
  83. 6.0, 3.0, 8.0, 4.0, 5.0, 7.0, 8.0, 6.0, 6.0, 4.0, 12.0, 9.0,
  84. 8.0, 8.0, 8.0, 6.0, 7.0, 4.0, 4.0, 5.0, 7.0]
  85. tested = np.polyfit(x, y, 4)
  86. assert_array_almost_equal(ref, tested)
  87. def test_polydiv_type(self):
  88. # Make polydiv work for complex types
  89. msg = "Wrong type, should be complex"
  90. x = np.ones(3, dtype=complex)
  91. q, r = np.polydiv(x, x)
  92. assert_(q.dtype == complex, msg)
  93. msg = "Wrong type, should be float"
  94. x = np.ones(3, dtype=int)
  95. q, r = np.polydiv(x, x)
  96. assert_(q.dtype == float, msg)
  97. def test_histogramdd_too_many_bins(self):
  98. # Ticket 928.
  99. assert_raises(ValueError, np.histogramdd, np.ones((1, 10)), bins=2**10)
  100. def test_polyint_type(self):
  101. # Ticket #944
  102. msg = "Wrong type, should be complex"
  103. x = np.ones(3, dtype=complex)
  104. assert_(np.polyint(x).dtype == complex, msg)
  105. msg = "Wrong type, should be float"
  106. x = np.ones(3, dtype=int)
  107. assert_(np.polyint(x).dtype == float, msg)
  108. def test_ndenumerate_crash(self):
  109. # Ticket 1140
  110. # Shouldn't crash:
  111. list(np.ndenumerate(np.array([[]])))
  112. def test_asfarray_none(self):
  113. # Test for changeset r5065
  114. assert_array_equal(np.array([np.nan]), np.asfarray([None]))
  115. def test_large_fancy_indexing(self):
  116. # Large enough to fail on 64-bit.
  117. nbits = np.dtype(np.intp).itemsize * 8
  118. thesize = int((2**nbits)**(1.0/5.0)+1)
  119. def dp():
  120. n = 3
  121. a = np.ones((n,)*5)
  122. i = np.random.randint(0, n, size=thesize)
  123. a[np.ix_(i, i, i, i, i)] = 0
  124. def dp2():
  125. n = 3
  126. a = np.ones((n,)*5)
  127. i = np.random.randint(0, n, size=thesize)
  128. a[np.ix_(i, i, i, i, i)]
  129. assert_raises(ValueError, dp)
  130. assert_raises(ValueError, dp2)
  131. def test_void_coercion(self):
  132. dt = np.dtype([('a', 'f4'), ('b', 'i4')])
  133. x = np.zeros((1,), dt)
  134. assert_(np.r_[x, x].dtype == dt)
  135. def test_who_with_0dim_array(self):
  136. # ticket #1243
  137. import os
  138. import sys
  139. oldstdout = sys.stdout
  140. sys.stdout = open(os.devnull, 'w')
  141. try:
  142. try:
  143. np.who({'foo': np.array(1)})
  144. except Exception:
  145. raise AssertionError("ticket #1243")
  146. finally:
  147. sys.stdout.close()
  148. sys.stdout = oldstdout
  149. def test_include_dirs(self):
  150. # As a sanity check, just test that get_include
  151. # includes something reasonable. Somewhat
  152. # related to ticket #1405.
  153. include_dirs = [np.get_include()]
  154. for path in include_dirs:
  155. assert_(isinstance(path, str))
  156. assert_(path != '')
  157. def test_polyder_return_type(self):
  158. # Ticket #1249
  159. assert_(isinstance(np.polyder(np.poly1d([1]), 0), np.poly1d))
  160. assert_(isinstance(np.polyder([1], 0), np.ndarray))
  161. assert_(isinstance(np.polyder(np.poly1d([1]), 1), np.poly1d))
  162. assert_(isinstance(np.polyder([1], 1), np.ndarray))
  163. def test_append_fields_dtype_list(self):
  164. # Ticket #1676
  165. from numpy.lib.recfunctions import append_fields
  166. base = np.array([1, 2, 3], dtype=np.int32)
  167. names = ['a', 'b', 'c']
  168. data = np.eye(3).astype(np.int32)
  169. dlist = [np.float64, np.int32, np.int32]
  170. try:
  171. append_fields(base, names, data, dlist)
  172. except Exception:
  173. raise AssertionError()
  174. def test_loadtxt_fields_subarrays(self):
  175. # For ticket #1936
  176. from io import StringIO
  177. dt = [("a", 'u1', 2), ("b", 'u1', 2)]
  178. x = np.loadtxt(StringIO("0 1 2 3"), dtype=dt)
  179. assert_equal(x, np.array([((0, 1), (2, 3))], dtype=dt))
  180. dt = [("a", [("a", 'u1', (1, 3)), ("b", 'u1')])]
  181. x = np.loadtxt(StringIO("0 1 2 3"), dtype=dt)
  182. assert_equal(x, np.array([(((0, 1, 2), 3),)], dtype=dt))
  183. dt = [("a", 'u1', (2, 2))]
  184. x = np.loadtxt(StringIO("0 1 2 3"), dtype=dt)
  185. assert_equal(x, np.array([(((0, 1), (2, 3)),)], dtype=dt))
  186. dt = [("a", 'u1', (2, 3, 2))]
  187. x = np.loadtxt(StringIO("0 1 2 3 4 5 6 7 8 9 10 11"), dtype=dt)
  188. data = [((((0, 1), (2, 3), (4, 5)), ((6, 7), (8, 9), (10, 11))),)]
  189. assert_equal(x, np.array(data, dtype=dt))
  190. def test_nansum_with_boolean(self):
  191. # gh-2978
  192. a = np.zeros(2, dtype=bool)
  193. try:
  194. np.nansum(a)
  195. except Exception:
  196. raise AssertionError()
  197. def test_py3_compat(self):
  198. # gh-2561
  199. # Test if the oldstyle class test is bypassed in python3
  200. class C():
  201. """Old-style class in python2, normal class in python3"""
  202. pass
  203. out = open(os.devnull, 'w')
  204. try:
  205. np.info(C(), output=out)
  206. except AttributeError:
  207. raise AssertionError()
  208. finally:
  209. out.close()