Best Python code snippet using fMBT_python
hmm7.py
Source:hmm7.py
1import random2import os3import sys4import math5from numpy import *6inpath = "D://python2.7.6//MachineLearning//hmm"7filename="RenMinData2.txt"8outfile = "D://python2.7.6//MachineLearning//hmm//wordDic.txt"9outfileo = "D://python2.7.6//MachineLearning//hmm//omitProb.txt"10outfilep= "D://python2.7.6//MachineLearning//hmm//pi.txt"11outfilet= "D://python2.7.6//MachineLearning//hmm//transProb.txt"12outfile2 = "D://python2.7.6//MachineLearning//hmm//markSentence.txt" 13testDocName= "test.txt" 14result=[]15######################16def loadData():17 allMarkList=[]18 wordDic={}19 charaDic={}20 f=open(inpath+'/'+filename,'r')21 content=f.readline()22 while content:23 eachMark=[]24 25 words=content.strip('\n').split(' ');#print words##26 for eachwid in words: ########### go through each danci'wo men'27 wid=eachwid.strip()28 if len(wid)<2:29 #print '<',wid30 continue31 32 #if wid not in wordDic.keys():33 #wordDic[wid]={}34 35 if len(wid)==2:36 #print '1',wid37 if wid not in charaDic.keys():38 charaDic[wid]={'b':0,'m':0,'e':0,'s':0}39 charaDic[wid]['s']+=1;eachMark.append('s')40 41 42 43 if len(wid)>2: #4 zijie 6 zijie44 #print '3', wid45 r=range(0,len(wid))[0:len(wid)-1:2];#print r46 for i in r:############go through each zi'wo' r=[0, 2, 4, 6, 8]47 #print wid[i:i+2],i# wid[i,i+2],wid[i:i+1] not work48 if wid[i:i+2] not in charaDic.keys():49 charaDic[wid[i:i+2]]={'b':0,'m':0,'e':0,'s':0}50 if i==0:51 charaDic[wid[i:i+2]]['b']+=1;eachMark.append('b');#print 'b'52 if i==len(wid)-2:53 charaDic[wid[i:i+2]]['e']+=1;eachMark.append('e');#print 'e'54 elif i!=len(wid)-2 and i!=0: ####else not work , one chara will be marked both b and m55 charaDic[wid[i:i+2]]['m']+=1;eachMark.append('m');#print 'm'56 57 allMarkList.append(eachMark)58 content=f.readline()59 ###############################60 #### output61 outPutfile=open(outfile,'w')62 for wid in charaDic.keys():63 outPutfile.write(str(wid));64 outPutfile.write(':')65 outPutfile.write(str(charaDic[wid]))66 outPutfile.write('\n')67 outPutfile.close()68 69 outPutfile=open(outfile2,'w')70 for eachM in allMarkList:71 outPutfile.write(str(eachM))72 outPutfile.write('\n')73 outPutfile.close()74 #################################75 # pi each state as first charact in each sentence ,count freq76 #################################77 pi={'b':0,'m':0,'e':0,'s':0}78 clas=['b','m','e','s']79 for sentence in allMarkList:80 for c in clas:81 if sentence[0]==c:82 pi[c]+=183 #print 'pi',pi;84 sm=0 #if not initiallize sm, will warning:local variable 'sm' referenced before assignment85 for c in clas:86 sm+=float(pi[c])87 #print sm88 for (k,v) in pi.items():89 if v!=0:90 pi[k]=math.log(float(v)/sm) #v=float(pi[k])/sm # not work v remain not divided,91 else:pi[k]=092 #print 'pi',pi93 ############################ 94 outPutfile=open(outfilep,'w')95 for (k,v) in pi.items():96 outPutfile.write(str(k));97 outPutfile.write(':')98 outPutfile.write(str(v))99 outPutfile.write(' ')100 outPutfile.close() 101 ######################################102 # each state total count freq103 ######################################104 state={'b':0,'m':0,'e':0,'s':0}105 for sentence in allMarkList:106 for chara in sentence:107 for c in clas:108 if chara==c:109 state[c]+=1 110 #print 'state freq',state111 112 ############################################113 # trans prob114 ############################################115 print 'num of sentence',len(allMarkList);116 transProb={'b':{'b':0,'m':0,'e':0,'s':0},'m':{'b':0,'m':0,'e':0,'s':0},'e':{'b':0,'m':0,'e':0,'s':0},'s':{'b':0,'m':0,'e':0,'s':0}}117 for index1 in range(len(allMarkList)):118 for index2 in range(len(allMarkList[index1])):119 for si in clas:120 for sii in clas:121 if index2+1 in range(len(allMarkList[index1])): 122 if allMarkList[index1][index2]==si and allMarkList[index1][index2+1]==sii:123 transProb[si][sii]+=1124 #print 'transprob',transProb125 for si in clas:126 for sii in clas:127 if transProb[si][sii]==0.0:transProb[si][sii]=0#math.log(1/float(state[si]*state[si]*state[si]))####128 else:transProb[si][sii]= math.log(float(transProb[si][sii]-1)/float(state[si]))129 ##############################130 outPutfile=open(outfilet,'w')131 for (k,v) in transProb.items():132 outPutfile.write(str(k));outPutfile.write(' ')133 for k1 in v.keys():134 outPutfile.write(str(k1));135 outPutfile.write(':');136 outPutfile.write(str(v[k1]));137 outPutfile.write(' ')138 outPutfile.write('\n')139 outPutfile.close() 140 141 #print 'transprob',transProb142 143 ############################################144 # omit probabilistic145 ############################################146 for (k,v) in charaDic.items():147 for c in clas:148 if v[c]!=0:149 charaDic[k][c]=math.log(float(v[c])/float(state[c]))150 else:charaDic[k][c]=-100000151 152 #############################153 outPutfile=open(outfileo,'w')154 for (k,v) in charaDic.items():155 outPutfile.write(str(k));156 outPutfile.write(' ')157 for k1 in v.keys():158 outPutfile.write(str(k1));159 outPutfile.write(':');160 outPutfile.write(str(v[k1]));161 outPutfile.write(' ')162 outPutfile.write('\n')163 outPutfile.close() 164 #########################################165 166 return pi,transProb,charaDic167def loadModel():168 #load pi transprob omitprob from txt since parameter trained for once only go through 200000sentence for once169 170 ############################pi171 pi={}172 f=open(outfilep,'r')173 content=f.readline()174 while content:175 content=content.strip('\n').split(' ');#print content ['e:0', 's:-1.23214368129', 'b:-0.344840486292', 'm:0', '']176 for elem in content:177 if len(elem)<1:break #######necesssary178 #print elem,elem[0]179 if elem[0] not in pi.keys():180 #print elem[2:]#elem[2:-1] last postion not included181 pi[str(elem[0])]=float(elem[2:])182 content=f.readline()183 #print pi184 ################################trans185 transProb={}186 f=open(outfilet,'r')187 content=f.readline()188 while content:189 content=content.strip('\n').split(' ');#print content ['e:0', 's:-1.23214368129', 'b:-0.344840486292', 'm:0', '']190 for elem in content:191 if len(elem)<1:break #######necesssary192 193 if len(elem)==1:194 if elem not in transProb.keys():195 #print elem[2:]#elem[2:-1] last postion not included196 transProb[elem]={}197 if len(elem)>1:198 if elem[0] not in transProb[content[0]]:199 transProb[content[0]][elem[0]]=float(elem[2:])200 content=f.readline()201 #print transProb202 ################################omitprob203 omitProb={}204 f=open(outfileo,'r')205 content=f.readline()206 while content:207 content=content.strip('\n').split(' ');#print content ['e:0', 's:-1.23214368129', 'b:-0.344840486292', 'm:0', '']208 for elem in content:209 if len(elem)<1:break #######necesssary210 211 if len(elem)==2:212 if elem not in transProb.keys():213 #print elem[2:] #if elem[2:-1] last postion not included214 omitProb[elem]={}215 if len(elem)>2:216 if elem[0] not in omitProb[content[0]]:217 omitProb[content[0]][elem[0]]=float(elem[2:])218 content=f.readline()219 #################################220 print 'finish load'221 return pi,transProb,omitProb222 223 224 225 226def normalize(pi,transProb,omitProb):227 clas=['b','m','e','s'];s=0.0228 ####################229 #pi230 ####################231 for c in clas:232 s=pi[c]*pi[c]+s233 s=math.sqrt(s)234 for c in clas:235 pi[c]=float(pi[c]/s)236 #print 'pi',pi237 ############################ 238 outPutfile=open(outfilep,'w')239 for (k,v) in pi.items():240 outPutfile.write(str(k));241 outPutfile.write(':')242 outPutfile.write(str(v))243 outPutfile.write(' ')244 outPutfile.close() 245 ###############################246 #transprob247 ###############################248 for c in clas:249 for cc in clas:250 s=transProb[c][cc]*transProb[c][cc]+s251 s=math.sqrt(s)252 for cc in clas:253 transProb[c][cc]/=s254 #print 'transprob',transProb255 ############################256 outPutfile=open(outfilet,'w')257 for (k,v) in transProb.items():258 outPutfile.write(str(k));outPutfile.write(' ')259 for k1 in v.keys():260 outPutfile.write(str(k1));261 outPutfile.write(':');262 outPutfile.write(str(v[k1]));263 outPutfile.write(' ')264 outPutfile.write('\n')265 outPutfile.close() 266 ##################################267 #omitprob268 #################################269 for (k,v) in omitProb.items():270 for c in clas:271 s=v[c]*v[c]+s272 s=math.sqrt(s)273 for c in clas:274 omitProb[k][c]=omitProb[k][c]/s275 ############################276 outPutfile=open(outfileo,'w')277 for (k,v) in omitProb.items():278 outPutfile.write(str(k));279 outPutfile.write(' ')280 for k1 in v.keys():281 outPutfile.write(str(k1));282 outPutfile.write(':');283 outPutfile.write(str(v[k1]));284 outPutfile.write(' ')285 outPutfile.write('\n')286 outPutfile.close()287 ##################288 return pi,transProb,omitProb289 290 291 292 293 294def test(omitProb):295 result=[];omitProb1=[]296 f=open(inpath+'/'+testDocName,'r')297 content=f.readline()298 while content:299 content=content.strip('\n');#print content,len(content)#300 301 n=range(len(content))[::2];#print n #[0, 2, 4, 6] len=8302 303 for i in n:304 omitProb1.append(omitProb[content[i:i+2]])305 result.append(0)306 content=f.readline() #without this doc cannot close307 return omitProb1,result,len(result)308######################total failue 309def seekAllPath(omitProb1,pi,transProb):310 #print'seekbestpath' #### track the flow /change of t311 st1List=['b','m','e','s'];scoreMin=inf;scoreMin0=inf; #in pursuit of prob max, seek min312 313 tt=len(omitProb1)314 pathRecord={}315 for i in range(tt):316 pathRecord[i]=[]317 #print pathRecord #not work when di gui call function itself318 319 for t in range(tt): #tt=4 [0 1 2 3]320 if t==1:321 for st1 in st1List:322 #print 'st1',st1323 #scoreMin=inf###wrong if not locate here324 scoreDic=initialDic325 for st in st1List:326 if transProb[st][st1]!=0 and scoreDic[st]!=0:327 s=scoreDic[st]+transProb[st][st1]+omitProb1[t][st1]; 328 pathi=[]329 pathi.append(st);pathi.append(st1);pathi.append(s);330 pathRecord[t].append(pathi)331 #print 't 1', st+'->'+st1 ,s332 #print 't 1',pathRecord333 if t>=2 and t!=tt-1:334 #scoreDic=seekBestPath(omitProb1,pi,transProb,t-1);print'1',t,scoreDic #di gui not work335 for st1 in range(len(st1List)):336 #scoreMin=inf###always forget to reset the value to compare 337 for st in range(len(st1List)): #[0 1 2 3]338 if transProb[st1List[st]][st1List[st1]]!=0:339 s=scoreDic[st1List[st]]+transProb[st1List[st]][st1List[st1]]+omitProb1[t][st1List[st1]];#print '2', transProb[st1List[st]][st1List[st1]],s,st1List[st],st1List[st1] 340 #print st1List[st]+'->'+st1List[st1],s341 pathi=[]342 pathi.append(st1List[st]);pathi.append(st1List[st1]);pathi.append(s);343 pathRecord[t].append(pathi)344 #print 't 2345...',pathRecord345 if t==tt-1:346 for st1 in range(len(st1List)):347 for st in range(len(st1List)): #[0 1 2 3]348 if transProb[st1List[st]][st1List[st1]]!=0 and st1List[st1] not in ['m','b']:349 s=scoreDic[st1List[st]]+transProb[st1List[st]][st1List[st1]]+omitProb1[t][st1List[st1]];#print '2', transProb[st1List[st]][st1List[st1]],s,st1List[st],st1List[st1] 350 #print st1List[st]+'->'+st1List[st1],s351 pathi=[]352 pathi.append(st1List[st]);pathi.append(st1List[st1]);pathi.append(s);353 pathRecord[t].append(pathi)354 #print 't 2345...',pathRecord355 356 if t==0:357 initialDic={'b':0,'m':0,'e':0,'s':0}358 for s in st1List:359 if pi[s]!=0:360 initialDic[s]=float(pi[s]+omitProb1[0][s])361 pathRecord[t].append(initialDic)362 #print 't 0',pathRecord363 return pathRecord364 365def judge(pi,transProb,omitProb1):366 clas=['b','m','e','s'];score={'b':0,'m':0,'e':0,'s':0}; s=0367 numChara=len(omitProb1)368 for t in range(numChara): # 3 char 0 1 2369 if t==1:370 for s1 in clas:371 for s0 in ['s','b']:372 if transProb[s0][s1]!=0:373 score[s1]=omitProb1[t-1][s0]+omitProb1[t][s1]+pi[s0]+transProb[s0][s1] #score[s0] wrong374 print '0',score375 if t>=2 and t!=numChara-1:376 for s1 in clas:377 tempMax=None378 for s0 in clas:379 if transProb[s0][s1]!=0:380 s=score[s0]+transProb[s0][s1]#+omitProb1[t][s1] 381 if transProb[s0][s1]!=0 and(s>tempMax or tempMax==None):#transProb[s0][s1]!=0 without this, s0=b will go into this 'if' 382 tempMax=s #when s0==b, tempmax=0 is max383 maxState=s0384 score[s1]=tempMax+omitProb1[t][s1]385 print maxState,'->',s1,score[s1],tempMax,omitProb1[t][s1]386 if t==numChara-1:387 for s1 in ['s','e']:388 tempMax=None389 for s0 in clas:390 if transProb[s0][s1]!=0:391 s=score[s0]+transProb[s0][s1]#+omitProb1[t][s1]392 if transProb[s0][s1]!=0 and(s>tempMax or tempMax==None):393 tempMax=s394 maxState=s0395 score[s1]=tempMax+omitProb1[t][s1]396 print maxState,'->',s1,score[s1]397 398######main399#pi,transProb,omitProb=loadData() # go through 200000000doc only once,train to get parameter400#pi,transProb,omitProb=normalize(pi,transProb,omitProb)401pi,transProb,omitProb=loadModel() 402omitProb1,result,lenSentence=test(omitProb)403pathRecord=seekAllPath(omitProb1,pi,transProb)404print pathRecord405judge(pi,transProb,omitProb1)406 407 408 409 410 411 ...
RF5.1 cluster new way to fill absent cabin ,old way fill age.py
Source:RF5.1 cluster new way to fill absent cabin ,old way fill age.py
1import random2import os3import sys4import math5 6inpath = "D://python2.7.6//MachineLearning//titanic-randomforest//data"7outfile1 = "D://python2.7.6//MachineLearning//titanic-randomforest//1.txt"8outfile2 = "D://python2.7.6//MachineLearning//titanic-randomforest//2.txt"9outfile3 = "D://python2.7.6//MachineLearning//titanic-randomforest//3.txt"10outfile4 = "D://python2.7.6//MachineLearning//titanic-randomforest//4.txt"11outfile5 = "D://python2.7.6//MachineLearning//titanic-randomforest//5.txt"12outfile6 = "D://python2.7.6//MachineLearning//titanic-randomforest//6.txt"13outfile7 = "D://python2.7.6//MachineLearning//titanic-randomforest//7.txt"14 15numCenter=30 #must not >=numPsg xi16######################17def loadData():18 global dataDic;dataDic={};global numPsg;global psgList;psgList=[];global featDic;19 20 for filename in os.listdir(inpath):21 content=open(inpath+'/'+filename,'r')22 data=content.readlines();23 feat=data[0].strip('\n')24 dataDic[feat]={};#print data[0].strip('\n')25 i=026 for d in data[1:]:27 dataDic[feat][i]=d.strip('\n')28 if len(d.strip('\n'))<1:dataDic[feat][i]=-1029 30 i+=131 #####################32 outPutfile=open(outfile1,'w')33 for feat,data in dataDic.items():34 #print len(data),feat,'loaded'35 numPsg=len(data)36 outPutfile.write(str(feat));37 outPutfile.write(str(data))38 outPutfile.write('\n')39 outPutfile.close()40 ########already get original data, now amend41 featDic={}42 for feat in dataDic.keys():43 featDic[feat]=set(dataDic[feat].values())44 del featDic['Survived']45 outPutfile=open(outfile2,'w')46 for k,v in featDic.items():47 outPutfile.write(str(k))48 outPutfile.write(str(v));49 outPutfile.write('\n')50 outPutfile.close()51 ############## str into float 52 for i in range(numPsg):53 eachPsg=[{},'tlable','plable']54 for feat in dataDic.keys():55 if feat in ['Pclass','Embarked','Sex','Cabin']:56 eachPsg[0][feat]=dataDic[feat][i] ###i start from 0,57 elif feat in ['Age','Fare','Parch','SibSp']:58 eachPsg[0][feat]=float(dataDic[feat][i])59 eachPsg[1]=int(dataDic['Survived'][i])60 #########for one passenger,all feat finish61 psgList.append(eachPsg)62 63 ############################64 outPutfile=open(outfile3,'w')65 for psg in psgList:66 outPutfile.write(str(psg));67 outPutfile.write('\n')68 outPutfile.close()69def absentData():70 global dataDic;global numPsg;global psgList;global featDic;71 #####age absent:after study distribution between fare and age, random age at(15,50)72 for psg in psgList:73 if psg[0]['Age']==-10:74 if psg[0]['Fare']<=50:75 psg[0]['Age']=random.randint(0,80)76 else:psg[0]['Age']=random.randint(15,50)77 #####embark absent:after see distribution, find 1 'S' is dominant78 for psg in psgList:79 if psg[0]['Embarked']==-10:psg[0]['Embarked']='S'80 ###########81 ###########82 outPutfile=open(outfile4,'w')83 for psg in psgList:84 outPutfile.write(str(psg));85 outPutfile.write('\n')86 outPutfile.close()87def transformFeat():#all include continuous variabal into 0 1 88 global dataDic;global numPsg;global psgList;global featDic;89 ###############str feat into 0 190 global strFeatDic;91 strFeatDic={'Pclass':('1','2','3'),\92 'Embarked':('C','S','Q'),\93 'Sex':('female','male'),\94 'Cabin':('C','A','B','D','E','F','G','T')}95 for psg in psgList[:]:96 for f,v in strFeatDic.items():97 for i in v[1:]:98 if psg[0][f]==i:99 psg[0][i]=1100 else:psg[0][i]=0101 ########cabin str 'b12' into(zimu,shuzi) 102 cab=psg[0]['Cabin'] #cab may =(zimu,shuzi)-10 DATA ABSENT or DATA PRESENT str103 if type(cab)==str and cab!=-10: #if 'D' not 'D12'104 zimu,shuzi=splitCab(cab)105 psg[0]['Cabin']=(zimu,shuzi);#print psg[0]['Cabin']106 if cab==-10:107 psg[0]['Cabin']=(-10,-10)108 109 110 '''############### now all cabin =(zimu,shuzi)111 cab=psg[0]['Cabin'] #cab only =(zimu,shuzi)112 for i in ('A','B','C','D','E','F','G','T')[1:]:113 if i in cab:114 psg[0][i]=1115 psg[0]['cabNum']=cab[1]'''116 ##############del str feat117 del psg[0]['Pclass'];del psg[0]['Embarked'];del psg[0]['Sex'];#del psg[0]['Cabin']118 119 ############now continuous into 0-1120 continuFeat={'Fare':{'fare1':(0,100),'fare2':(101,200),'fare3':(201,300)},\121 122 'Age':{'age1':(0,10),'age2':(11,20),'age3':(21,30),'age4':(31,40),'age5':(41,50),'age6':(51,100)},\123 124 'Parch':{'child1':(0,1),'child2':(2,3),'child3':(4,5)},\125 126 'SibSp':{'sib1':(0,1),'sib2':(2,3)}}127 128 129 for psg in psgList[:]:130 for k,v in continuFeat.items():131 for f,num in continuFeat[k].items():132 psg[0][f]=0133 if int(psg[0][k]) in range(num[0],num[1]+1):134 psg[0][f]=1135 #################new structure for psg:[{feat:0,1 include survived},truelabel 01, predict,{center and distance}]136 psg[0]['Survived']=psg[1]137 psg[1]=[psg[0]['Cabin'][0],psg[0]['Cabin'][1]];#(zimu,shuzi)(-10,-10)138 ##############139 del psg[0]['Fare'];del psg[0]['Age'];del psg[0]['Parch'];del psg[0]['Cabin']140 del psg[0]['SibSp'];141 142 143 ########################## 144 outPutfile=open(outfile5,'w')145 for psg in psgList:146 outPutfile.write(str(psg));147 outPutfile.write('\n')148 outPutfile.close() 149'''one psg:[{'child1': 1, 'child3': 0, 'sib1': 1,150'3': 1, '2': 0, 'Survived': 0, 'age5': 0, 'A': 0,151'sib2': 0, 'child2': 0, 'B': 0, 'E': 0, 'D': 0,152'G': 0, 'F': 0, 'age6': 0, 'age4': 0, 'age3': 1,153'age2': 0, 'age1': 0, 'Q': 0,'S': 1, 'T': 0,154'fare2': 0, 'fare1': 1, 'fare3': 0, 'male': 1}, (-10,-10)or ('C', 85), 'plable']'''155def initialCenter():156 global psgList;global center157 feaD=psgList[0][0].keys()158 center={}159 for i in range(numCenter):160 center[i]={}161 for fea in feaD:162 center[i][fea]=random.sample([0,1],1)[0]163 #print center164def cluster():165 global psgList;global center;global numPsg166 #############calc distance167 for psg in psgList[:]:168 ###for each psg find closest center and distance169 minDis=None;minCen=0;170 for cen,fea in center.items():171 dis=calcDis(fea,psg[0])172 if minDis ==None or minDis>dis:173 minDis=dis174 minCen=cen175 psg[2]=[minCen,minDis]176 #print psg[2]177 ################calc total distance178 totalLose=0.0179 for psg in psgList[:]:180 totalLose+=psg[2][1]181 print '%d centerios ,total psg distance %f'%(numCenter,totalLose)182 ###############183 #for psg in psgList[:7]:184 #print psg[1],psg[2]185 ##############calc new centerior186 feaD=psgList[0][0].keys()187 for cen,fea in center.items():188 for f,v in fea.items():189 ##########calc mean by each center each feat 190 meani=0.0;numMember=0.0191 for psg in psgList[:]:192 if psg[2][0]==cen:193 meani+=psg[0][f]194 numMember+=1195 meani/=(numMember+0.0001)196 center[cen][f]=meani197 #print center198 return totalLose199 200 201 202def show():203 global psgList204 ###############show distribution,see each centerior/cluster, what kind of zimu shuzi in it205 global zimuL;global shuziL;206 zimuL=[];shuziL=[]207 for i in range(numCenter):208 eachCzm=[];eachCsz=[];209 for psg in psgList[:]:210 if psg[2][0]==i:211 eachCzm.append(psg[1][0])212 eachCsz.append(psg[1][1])213 zimuL.append(eachCzm)214 shuziL.append(eachCsz)215 #########216 for i in range(len(zimuL)):217 zimuL[i]=set(zimuL[i])218 for i in range(len(shuziL)):219 shuziL[i]=set(shuziL[i])220 #print 'finally','zimu',zimuL,'shuzi',shuziL 221def labelCabin():222 global psgList;global zimuL;global shuziL; 223 #############remove -10 from set224 for i in range(len(zimuL)):225 if -10 in zimuL[i]:226 zimuL[i].remove(-10)227 ######what if after remove 0 in the set , some cluster is empty itself before remove -10228 if len(zimuL[i])==0:229 zimuL[i]=['C','A','B','D','E','F','G','T']230 231 for i in range(len(shuziL)):232 if -10 in shuziL[i]:233 shuziL[i].remove(-10)234 if len(shuziL[i])==0:235 shuziL[i]=[random.randint(1,150)]236 #print '1',shuziL,zimuL237 ############label (cabin zimu , cabin shuzi)238 for psg in psgList:239 240 ###########label cabin zimu241 if psg[1][0]==-10:242 for i in range(numCenter):243 if psg[2][0]==i:244 psg[1][0]=random.sample(zimuL[i],1)[0] #sample return [0] not 0245 ###########label cabin shuzi246 if psg[1][1]==-10:247 for i in range(numCenter):248 if psg[2][0]==i:249 psg[1][1]=random.sample(shuziL[i],1)[0] #sample return [0] not 0250 ######251 252 ####################253 outPutfile=open(outfile6,'w')254 for psg in psgList:255 outPutfile.write(str(psg));256 outPutfile.write('\n')257 outPutfile.close()258 ##################continuous variable cabin number into 01259 numCab={'cabN1':(0,10),'cabN2':(11,20),'cabN3':(21,30),'cabN4':(31,40),\260 'cabN5':(41,50),'cabN6':(51,60),'cabN7':(61,70),'cabN8':(71,80),\261 'cabN9':(81,90),'cabN10':(91,100),'cabN11':(101,110),'cabN12':(111,120),\262 'cabN13':(121,130)}263 for psg in psgList:264 labelCab=psg[1][0]265 if labelCab in psg[0]:266 psg[0][labelCab]=1267 ##########268 for k,v in numCab.items():269 psg[0][k]=0270 if int(psg[1][1]) in range(int(v[0]),int(v[1])+1):271 psg[0][k]=1272 ###########273 psg[1]=psg[0]['Survived']274 del psg[0]['Survived']275 ################################276 outPutfile=open(outfile7,'w')277 for psg in psgList:278 for k,v in psg[0].items():279 outPutfile.write(str(k))280 outPutfile.write(':')281 outPutfile.write(str(v))282 outPutfile.write(' ')283 outPutfile.write(str(psg[1]))284 outPutfile.write('\n')285 outPutfile.close()286 287 288 289 290 291 292################################# SUPPORT 293def splitCab(cab):294 295 cabL=[];zimu=-10;shuzi=-10296 if ' ' in cab:297 cabL=cab.split(' ');#print cabL298 for pos in cabL:299 if len(pos)==1:300 zimu=pos301 #print zimu302 if len(pos)>1:303 zimu=pos[0];shuzi=int(pos[1:])304 #print zimu,shuzi305 continue ##important choose long length 'a123'rather than 'a'306 if ' 'not in cab:307 if len(cab)==1:308 zimu=cab309 if len(cab)>1:310 zimu=cab[0];shuzi=int(cab[1:])311 312 return zimu,shuzi313 314def calcDis(dic1,dic2):315 dis=0.0316 for k,v in dic1.items():317 dis+=(v-dic2[k])**2318 dis=math.sqrt(dis)319 return dis320 321 322 323 324###########################main325loadData()326absentData()327transformFeat()328######to cluter in order to see distribution of cabin group329initialCenter()330lose1=cluster()331lose0=0.0332i=0333while abs(lose0-lose1)>0.01 and i<30:334 lose0=lose1335 lose1=cluster()336 i+=1337 338show()339###################340labelCabin()341#3 centerios ,total psg distance 1170.226266342#100 centerios ,total psg distance 714.829224343#8 centerios ,total psg distance 1040.752066344#700 centerios ,total psg distance 630.691374 ,without age 413...
DbmsAdvisor.py
Source:DbmsAdvisor.py
...41 folder = self.__generateRandomString__() 42 self.args['print'].subtitle("DBMSADVISOR library ?")43 logging.info("Simulate the file creation in the {0} folder with DBMSAdvisor".format(folder))44 logging.info('The file is not created remotly because the folder should not exist')45 status = self.putFile(folder,'temp.txt',data='data in file')46 if status == True or self.ERROR_BAD_FOLDER_OR_BAD_SYSTEM_PRIV in str(status):47 self.args['print'].goodNews("OK")48 else : 49 self.args['print'].badNews("KO")50def runDbmsadvisorModule(args):51 '''52 Run the DBMSAdvisor module53 '''54 status = True55 if checkOptionsGivenByTheUser(args,["test-module","putFile"]) == False : return EXIT_MISS_ARGUMENT56 dbmsAdvisor = DbmsAdvisor(args)57 status = dbmsAdvisor.connection(stopIfError=True)58 if args['test-module'] == True :59 args['print'].title("Test if the DBMSAdvisor library can be used")60 status = dbmsAdvisor.testAll()61 #Option 1: putLocalFile62 if args['putFile'] != None:63 args['print'].title("Put the {0} local file in the {1} path (named {2}) of the {3} server".format(args['putFile'][2],args['putFile'][0],args['putFile'][1],args['server']))64 status = dbmsAdvisor.putFile(args['putFile'][0], args['putFile'][1],localFile=args['putFile'][2]) 65 if status == True:66 args['print'].goodNews("The {0} local file was put in the remote {1} path (named {2})".format(args['putFile'][2],args['putFile'][0],args['putFile'][1]))67 else :68 args['print'].badNews("The {0} local file was not put in the remote {1} path (named {2}): {3}".format(args['putFile'][2],args['putFile'][0],args['putFile'][1],str(status)))...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!