[Exercise] Stochastic gradient descent, logistic regression

Source: Internet
Author: User

Code:

1 ImportNumPy as NP2 ImportCSV3 ImportMath as Mt4 5 defHypo (TT,XX):#hypothesis function6exp=MT.E7tmp=0.08      forIinchRange (0,4):9tmp+=tt[i]*Xx[i]TenAns=mt.pow (EXP,TMP)/(1 +Mt.pow (exp,tmp)) One     returnans A  - defGDA (Tt,iter):#Random Gradient descent -      forIinchRange (1,num+1):#[1..100] thetmp=Hypo (Tt,x[i]) -          forJinchRange (0,4):#[0..3] -Tt[j]=tt[j]+iternum (iter,i) * (y[i]-tmp) *X[i][j] -  + defLikeli (TT):#Calculate likelihood function -tmp=0.0 +      forIinchRange (1,num+1): Atm=Hypo (Tt,x[i]) attmp+= (Y[i]*mt.log (tm) + (1-y[i]) *mt.log (1-TM)) -     returntmp -  - defIternum (K,J):#The functions for calculating the iteration factor are "different iterations and different iteration factors" -TMP=0.1/(k+j+1) +0.1 -     returntmp in  -Trainfile=file ('Train.csv','RB') toTrainread=Csv.reader (trainfile) +Testfile=file ('Test.csv','RB') -Testread=Csv.reader (testfile) the  *X=np.zeros ((105,5), float)#(x, y) is a training sample $Y=np.zeros (105, float)Panax NotoginsengTx=np.zeros (5,float)#parameter -Ty=np.zeros (5, float) theDx=np.zeros ((105,5), float)#(Dx,dy) is a test sample +Dy=np.zeros (105, float) A  thenum=0 +  forLineinchTrainread: -Num=num+1 $x[num]= Line $Y[num]=x[num][4] - #x[1..100] y[1..100] - trainfile.close () the  -dnum=0;Wuyi  forLineinchTestread: theDnum+=1 -dx[dnum]= Line WuDy[dnum]=dx[dnum][4] - testfile.close () About  $  forIinchRange (1,num+1): -     Print(X[i],y[i]) - Print(" ----- ") -  forIinchRange (1,dnum+1): A     Print(Dx[i],dy[i]) +  theIter=0 -lx=99999.0 $ly=Likeli (Ty) the  while(Mt.fabs (LY-LX) >0.01): the     Print(Iter,likeli (Ty), Ty) thelx=ly the GDA (ty,iter) -Iter+=1 inly=Likeli (Ty) the  the Print("  ") About  thepart=0.5 the  forIinchRange (1,dnum+1): thetmp=Hypo (Ty,dx[i]) +     if(tmp<Part ): -ans=0 the     Else:BayiAns=1 the     Print(Dy[i],tmp,ans)
View Code

Operation Result:

1//Training Sample Process:2//(ID, likelihood function value, [parameter])3(0,-69.314718055994589, Array ([0., 0., 0., 0., 0.]))4(1, -179.19591664172614, Array ([0.47418735, 0.06857472, 0.6080708, 0.21795486, 0. ]))5(2, -121.62179146512122, Array ([0.33221938,-0.24682766, 0.97199997, 0.37909118, 0. ]))6(3, -80.005102980506564, Array ([0.22184855,-0.51616823, 1.29138288, 0.51894067, 0. ]))7(4, -52.113434710918014, Array ([0.13470815,-0.74770815, 1.57528938, 0.64244598, 0. ]))8(5, -31.590025749624512, Array ([0.04771585,-0.95553279, 1.81895741, 0.74985357, 0. ]))9(6, -17.452182808491344, Array ([-0.04598164,-1.14217819, 2.02004087, 0.84120529, 0. ]))Ten(7, -10.267976018117292, Array ([-0.1248766,-1.29754958, 2.1927993, 0.92091932, 0. ])) One(8, -6.6580675453339095, Array ([-0.18789329,-1.42677758, 2.34511257, 0.9918262, 0. ])) A(9, -4.6188250760341996, Array ([-0.24093233,-1.5375812, 2.47958712, 1.05499614, 0. ])) -( -3.3744821853240419, Array ([-0.2866801,-1.63418248, 2.59854375, 1.11126847, 0. ])) -(One, -2.5747996461385223, array ([-0.32646178,-1.71915875, 2.7042384, 1.16151128, 0. ])) the( -2.0385459675596458, Array ([-0.36122587,-1.79441754, 2.79869276, 1.20656554, 0. ])) -( -1.6650980025135245, Array ([-0.39176125,-1.86149956, 2.88364276, 1.24718908, 0. ])) -( -1.3960367006166241, Array ([-0.41873723,-1.92167844, 2.96054485, 1.28403573, 0. ])) -( -1.1961863968105035, Array ([-0.442717,-1.97600919, 3.03060539, 1.31765663, 0. ])) +( -1.0436583937366917, Array ([-0.46416987,-2.02536393, 3.09481781, 1.34851106, 0. ])) -( -0.92441126768741677, Array ([-0.48348429,-2.07046263, 3.15399946, 1.37698019, 0. ])) +( -0.82917964441403547, Array ([-0.50098067,-2.11190002, 3.20882445, 1.40338032, 0. ])) A( -0.75168763375440906, Array ([-0.51692311,-2.15016851, 3.25985117, 1.42797471, 0. ])) at( -0.68758023105857857, Array ([-0.53152965,-2.18567731, 3.3075448, 1.45098335, 0. ])) -( -0.633767252199817, Array ([-0.5449808,-2.21876813, 3.35229525, 1.47259108, 0. ])) -( -0.5880105475158135, Array ([-0.55742672,-2.2497278, 3.39443149, 1.4929541, 0. ])) -( -0.54865823718178242, Array ([-0.56899284,-2.27879848, 3.43423286, 1.51220514, 0. ])) -( -0.51446987092368923, Array ([-0.57978456,-2.30618579, 3.47193808, 1.53045774, 0. ])) -( -0.48449905631347406, Array ([-0.58989091,-2.33206535, 3.5077524, 1.54780952, 0. ])) in( -0.45801316245868812, Array ([-0.59938755,-2.35658801, 3.5418533, 1.56434493, 0. ])) -( -0.43443740873146985, Array ([-0.60833908,-2.37988406, 3.57439518, 1.58013741, 0. ])) to( -0.41331528565404385, Array ([-0.61680104,-2.40206665, 3.60551298, 1.59525118, 0. ])) +( -0.39428010255982965, Array ([-0.62482134,-2.42323451, 3.63532529, 1.60974263, 0. ])) -( -0.37703423805211778, Array ([-0.6324416,-2.44347427, 3.6639368, 1.62366156, 0. ])) the( -0.36133380365882289, Array ([-0.63969811,-2.46286221, 3.69144031, 1.6370521, 0. ])) *( -0.34697716567487236, Array ([-0.64662266,-2.48146587, 3.71791846, 1.64995354, 0. ])) $( -0.33379625351555026, Array ([-0.65324325,-2.49934526, 3.74344514, 1.662401, 0. ]))Panax Notoginseng( -0.32164990574178071, Array ([-0.65958462,-2.5165539, 3.76808662, 1.67442599, 0. ])) -( -0.31041872365028822, Array ([-0.66566872,-2.53313973, 3.79190257, 1.68605687, 0. ])) the( -0.30000105253087223, Array ([-0.67151512,-2.54914582, 3.8149469, 1.69731925, 0. ])) +    A  the//predict new Data results: +//(original category, hypothesis value, classification result) -(0.0, 0.004116287123555463, 0) $(0.0, 0.004491299234282269, 0) $(0.0, 0.001997774439620067, 0) -(0.0, 9.711727014021101e-05, 0) -(1.0, 0.9986958360885878, 1) the(1.0, 0.999907833813241, 1) -(1.0, 0.998089176390621, 1)Wuyi(1.0, 0.9999771709114254, 1) the(1.0, 0.9998452542803238, 1) -  WuProcess finished with exit code 0
View Code

Training samples and data download:

Original:http://archive.ics.uci.edu/ml/datasets/iris

CSV file: Http://files.cnblogs.com/files/pdev/stochastic_GDA.zip

[Exercise] Stochastic gradient descent, logistic regression

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.