Question: Python code. Any way I can better my code. Problem #4 Find the best ft by minimizing the objoctive function (m)-Xm -yll using gradient descent
Problem #4 Find the best ft by minimizing the objoctive function (m)-Xm -yll using gradient descent Follow these steps a Define the function f(m) b. Define the vector function Vf(m) C Wrte a short sagt that mglermonts gradont descent Use step sue ". IO-, and toleranco -10-8 Koep track of te number of iterations d Plot the data again and the solution yD mx+ b on the same figure Make sure you include time commands to find the CPU time it took to find the solution this way 59]: def fobj(X,m,y): return 0.5"np. linale.norm(.dot (m)-ydata)..2 def gradf (X,m, ydata): return ((XT. dot (x). dot (m)-XT.dot (ydata))) t start_DG time.time() Tau- 8.01 delta- 8.8000801 deltagrad-8.0086088960001 k-8 kmax: 100e mk np.zeros(X. shape[1]) fk fobj(x, mk, ydata) gradk- gradf(x, mk, data) while fk delta and k deltagrad: nk mk- Tau gradk fk- fobj(X, mk, ydata) gradk- gradf(X, mk, ydata) t DG- time.time()-t start DG ValueErrorTraceback (nost recent call last) cipython-input-59-532b3ddfbe7b> in cmodule>) 13 fk- fobj(x, mk, ydata) 14 gradk gradf(x, mk, data) .15 while fk > delta andkkmax and gradk > deltagrad: 16 ak ak- Tau gradk 17 fk- fobj(X,-, ydata) ValueError: The truth value of an array with more than one element is anbiguous. Use a. any() or a.all
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
