code: def test_polyfit(self):
      def f(x):
          return x * (x - 1) * (x - 2)
  
      def f2(x):
          return x**4 + x**2 + 1
  
      # Test exceptions
      assert_raises(ValueError, poly.polyfit, [1], [1], -1)
      assert_raises(TypeError, poly.polyfit, [[1]], [1], 0)
      assert_raises(TypeError, poly.polyfit, [], [1], 0)
      assert_raises(TypeError, poly.polyfit, [1], [[[1]]], 0)
      assert_raises(TypeError, poly.polyfit, [1, 2], [1], 0)
      assert_raises(TypeError, poly.polyfit, [1], [1, 2], 0)
      assert_raises(TypeError, poly.polyfit, [1], [1], 0, w=[[1]])
      assert_raises(TypeError, poly.polyfit, [1], [1], 0, w=[1, 1])
      assert_raises(
          ValueError,
          poly.polyfit,
          [1],
          [1],
          [
              -1,
          ],
      )
      assert_raises(ValueError, poly.polyfit, [1], [1], [2, -1, 6])
      assert_raises(TypeError, poly.polyfit, [1], [1], [])
  
      # Test fit
      x = np.linspace(0, 2)
      y = f(x)
      #
      coef3 = poly.polyfit(x, y, 3)
      assert_equal(len(coef3), 4)
      assert_almost_equal(poly.polyval(x, coef3), y)
      coef3 = poly.polyfit(x, y, [0, 1, 2, 3])
      assert_equal(len(coef3), 4)
      assert_almost_equal(poly.polyval(x, coef3), y)
      #
      coef4 = poly.polyfit(x, y, 4)
      assert_equal(len(coef4), 5)
      assert_almost_equal(poly.polyval(x, coef4), y)
      coef4 = poly.polyfit(x, y, [0, 1, 2, 3, 4])
      assert_equal(len(coef4), 5)
      assert_almost_equal(poly.polyval(x, coef4), y)
      #
      coef2d = poly.polyfit(x, np.array([y, y]).T, 3)
      assert_almost_equal(coef2d, np.array([coef3, coef3]).T)
      coef2d = poly.polyfit(x, np.array([y, y]).T, [0, 1, 2, 3])
      assert_almost_equal(coef2d, np.array([coef3, coef3]).T)
      # test weighting
      w = np.zeros_like(x)
      yw = y.copy()
      w[1::2] = 1
      yw[0::2] = 0
      wcoef3 = poly.polyfit(x, yw, 3, w=w)
      assert_almost_equal(wcoef3, coef3)
      wcoef3 = poly.polyfit(x, yw, [0, 1, 2, 3], w=w)
      assert_almost_equal(wcoef3, coef3)
      #
      wcoef2d = poly.polyfit(x, np.array([yw, yw]).T, 3, w=w)
      assert_almost_equal(wcoef2d, np.array([coef3, coef3]).T)
      wcoef2d = poly.polyfit(x, np.array([yw, yw]).T, [0, 1, 2, 3], w=w)
      assert_almost_equal(wcoef2d, np.array([coef3, coef3]).T)
      # test scaling with complex values x points whose square
      # is zero when summed.
      x = [1, 1j, -1, -1j]
      assert_almost_equal(poly.polyfit(x, x, 1), [0, 1])
      assert_almost_equal(poly.polyfit(x, x, [0, 1]), [0, 1])
      # test fitting only even Polyendre polynomials
      x = np.linspace(-1, 1)
      y = f2(x)
      coef1 = poly.polyfit(x, y, 4)
      assert_almost_equal(poly.polyval(x, coef1), y)
      coef2 = poly.polyfit(x, y, [0, 2, 4])
      assert_almost_equal(poly.polyval(x, coef2), y)
      assert_almost_equal(coef1, coef2)
  
imports: from functools import reduce
  from fractions import Fraction
  import numpy
  import numpy.polynomial.polynomial
  import pickle
  from copy import deepcopy
  from numpy.testing import assert_almost_equal, assert_raises, assert_equal, assert_, assert_array_equal, assert_raises_regex
label_desc: Use image node_id 3 for calling the TestMisc obj's underlying member method code with example usage: obj.test_polyfit() and returns: unknown, unknown