python - numpy contour plot with cost function -
a = np.array(x) b = np.array(y) a_transpose = a.transpose() a_trans_times_a = np.dot(a_transpose,a) a_trans_times_b = np.dot(a_transpose,b) def cost(theta): x_times_theta = np.dot(a, theta) _y_minus_x_theta = b - x_times_theta _y_minus_x_theta_transpose = _y_minus_x_theta.transpose() return np.dot(_y_minus_x_theta_transpose, _y_minus_x_theta) n = 256 p = np.linspace(-100,100, n) q= np.linspace(-100,100, n) p, q = np.meshgrid(p,q) pl.contourf(p, q, cost(np.array([p,q])) ,8, alpha =0.75, cmap = 'jet') c = pl.contour(p,q, cost(np.array([p,q])), 8, colors = 'black', linewidth = 0.5 )
hi, i'm trying make contour plot using cost function on 2 parameters, involving matrix multiplication. i've tested cost function , works in interactive session. however, running on linspace makes error "valueerror: objects not aligned". understand has how structure p,q. solution involve writing loop explicitly array of outputs? how write this?
edit: a,b matrices correct size. cost function takes 2-vector , outputs number.
it's hard know without having @ hand shapes of , b, error caused np.array[p,q]
being 3-dimensional array. seems expect 2-dimensional , np.dot(a,theta)
perform matrix multiplication.
presumably want theta
the angular coordinate @ particular x , y value. in case should do
theta = np.arctan2(q,p) #this 2d array of theta coordinates costarray = cost(theta) pl.contourf(p,q,costarray,8,alpha=0.75,cmap='jet')
Comments
Post a Comment