Keywords:
Gradient descent: It is to let the data along the direction of the maximum gradient, that is, the maximum function derivative drop down, so that it quickly close to the results.
The formula for cost functions is too long to be played here. There are so many online.
This non-linear regression is plainly a narrow version of the neural network.
Python implementations:
1 ImportNumPy as NP2 ImportRandom3 4 defGraientdescent (x,y,theta,alpha,m,numiterations):#Gradient descent algorithm5Xtrain =x.transpose ()6 forIinchRange (0,numiterations):#How many times do you repeat7Hypothesis=np.dot (X,theta)#h function8loss=hypothesis-y9 TenCost=np.sum (loss**2)/(m) One Print("Iteration%d/cost:%f"%(i,cost)) AGraient=np.dot (Xtrain,loss)/m -theta=theta-alpha*graient - returnTheta the - defGetData (numpoints,bias,variance):#generate pending Data yourself -X=np.zeros (Shape= (numpoints,2)) -Y=np.zeros (shape=numpoints) + forIinchRange (0,numpoints): -X[i][0]=1 +X[I][1] =I Ay[i]= (I+bias) +random.uniform (0,1) *Variance at returnx, y - -X,y=getdata (100,25,10) - Print("X:", X) - Print("Y:", Y) - innumiterations=100000 -alpha=0.0005 toTheta=np.ones (x.shape[1]) +Theta=graientdescent (x,y,theta,alpha,x.shape[0],numiterations) - Print(Theta)
Operation Result:
...... Too many output data to intercept only the next more than 10 lines
Iteration 99988/cost:3.930135
Iteration 99989/cost:3.930135
Iteration 99990/cost:3.930135
Iteration 99991/cost:3.930135
Iteration 99992/cost:3.930135
Iteration 99993/cost:3.930135
Iteration 99994/cost:3.930135
Iteration 99995/cost:3.930135
Iteration 99996/cost:3.930135
Iteration 99997/cost:3.930135
Iteration 99998/cost:3.930135
Iteration 99999/cost:3.930135
[30.54541676 0.99982553]
An error was encountered.
typeerror:unsupported operand type (s) for *: ' Builtin_function_or_method ' and ' float '
Because my fifth line Xtrain =x.transpose (). No parentheses at first. Direct use of Xtrain =x.transpose
Print Xtrain Yes
<built-in method Transpose of Numpy.ndarray object at 0x00000219c1d14850>
Just created a transpose method, and did not really transpose to X. The parentheses are good. Re-print the Xtrain to display the transferred x normally.
The way of the rookie--nonlinear regression of machine learning personal understanding and Python implementation