code: # coding: utf-8
  # cf.http://d.hatena.ne.jp/white_wheels/20100327/p3
  import numpy as np
  import matplotlib.pylab as plt
  from mpl_toolkits.mplot3d import Axes3D
  
  
  def _numerical_gradient_no_batch(f, x):
      h = 1e-4 # 0.0001
      grad = np.zeros_like(x)
  
      for idx in range(x.size):
          tmp_val = x[idx]
          x[idx] = float(tmp_val) + h
          fxh1 = f(x) # f(x+h)
          
          x[idx] = tmp_val - h 
          fxh2 = f(x) # f(x-h)
          grad[idx] = (fxh1 - fxh2) / (2*h)
          
          x[idx] = tmp_val # 还原值
          
      return grad
  
  
  def numerical_gradient(f, X):
      if X.ndim == 1:
          return _numerical_gradient_no_batch(f, X)
      else:
          grad = np.zeros_like(X)
          
          for idx, x in enumerate(X): # (0, seq[0]), (1, seq[1]), (2, seq[2]), ...
              grad[idx] = _numerical_gradient_no_batch(f, x)
          
          return grad
  
  
  def function_2(x):
      if x.ndim == 1:
          return np.sum(x**2)
      else:
          return np.sum(x**2, axis=1)
  
  
  def tangent_line(f, x):
      d = numerical_gradient(f, x)
      print(d)
      y = f(x) - d*x
      return lambda t: d*t + y
       
  if __name__ == '__main__':
      x0 = np.arange(-2, 2.5, 0.25)
      x1 = np.arange(-2, 2.5, 0.25)
  
      X, Y = np.meshgrid(x0, x1)  # 构建平面方格18*18=324
      X = X.flatten() # (324,1)
      Y = Y.flatten()
  
      grad = numerical_gradient(function_2, np.array([X, Y]))    # 拼成一个数组
      plt.figure()
      plt.quiver(X, Y, -grad[0], -grad[1],  angles="xy",color="#666666")  # headwidth=10,scale=40,color="#444444") 绘制二维矢量场图
      plt.xlim([-2, 2])
      plt.ylim([-2, 2])
      plt.xlabel('x0')
      plt.ylabel('x1')
      plt.grid()
      plt.legend()
      plt.draw()
      plt.show()
apis: matplotlib.pylab.xlim numpy.zeros_like matplotlib.pylab.grid numpy.sum matplotlib.pylab.quiver matplotlib.pylab.ylabel matplotlib.pylab.legend matplotlib.pylab.draw matplotlib.pylab.figure matplotlib.pylab.show numpy.arange matplotlib.pylab.xlabel matplotlib.pylab.ylim numpy.array numpy.meshgrid