newbie doubt ..numpy array
if this is too silly a qn pls forgive I was learning numpy.ndarrays thru the tutorial. myarr=numpy.array( [ [10, 20, 30, 40],[1,2,3,4],[5,6,7,8] ] ) if i want to access the element 3 i can do it by myarr[1, 2] but then myarr[1][2] will also give the same result..is there any reason why two types of indexing is allowed? gordon p.s(i tried to post to numpy grp but it is not appearing there!) -- http://mail.python.org/mailman/listinfo/python-list
eucledian dist calculations
hi i am using python to do some image data calculations..I use the following numpy.ndarrays ,(i have given their shapes and ranks) weights=ndarray :shape(100,30),ndim=2 will have vals like 2458121847.49 (of type 'numpy.float64') input_weight=ndarray :shape(30,),ndim=1 (similar to above but diff vals) distance =ndarray :shape(30,),ndim=1 mindistance==ndarray :shape(30,),ndim=1 now i am calculating the euclidian distance of 'input_weight' from 'weight' since this is the cumulative diff i do this in this way for image in range(100): temp=0.0 for j in range(30): distance[j]=abs(input_weight[j]-weights[image,j]) if(image==0): #at the start copy from distance to mindistance mindistance=distance.copy() if (sum(mindistance) > sum(distance)): imgindex=image # i use this later to access a list mindistance=distance.copy() # now normalise the mindistance array if (max(mindistance) > 0.0): mindistance=mindistance/(max(mindistance)) dist=sum(mindistance) this gives me the correct results but i am worried if this is a bit unpythonish? (been a java programmer for a long time..) i wd like to know if there is a better way gordon -- http://mail.python.org/mailman/listinfo/python-list
Re: eucledian dist calculations
> > 1. 'temp' is not used > 2. Lose the superfluous parentheses in 'if' statements > 3. Put space around operators > 4. I've never used any of numpy & friends, but: > (a) Can't you replace the inner loop with something like this: >distance = abs(input_weight - weights[image, :]) > (b) I doubt that you need the .copy() > 5. Lose the hard-wired numbers like 30 and 100 > 6. Put it inside a function and *TEST* it > thanx John , will go thru those and do the cleanup gordon -- http://mail.python.org/mailman/listinfo/python-list
Re: getting values from cache
also if i were to write unit test for this method ,how shd i do it? shd i be checking all values in the matrix created inside and so on? gordon -- http://mail.python.org/mailman/listinfo/python-list
getting values from cache
hi i am writing code to check a folder containing images and then process thir vals using PIL and do some calc to create a matrix of values .if the folder has any new imgs added the program will do all calc again and dump the values into a cachefile.If the folder contents remain unaltered the program should not do calc but load the vals from cachefile..it is assumed that malicious alterations are not made on the folder and so i wont be doing any thorogh check but just checking if contents of folder have changed..i do something like this def checkCache(self): filenameslist=getfilenameslist() # made by parsing folder before this try: f=open(cachefile) except IOError: #no cache found ,do all calc mynumpymatrix1,imgwdth,imght=docalculations() f2=open(cachefile,"w") #dump values as tuple pickle.dump((filenameslist,imgwdth,imght,mynumpymatrix1),f2) f2.close() else: #cache exists, need to check if folder contents changed oldfilenameslist,wd,ht, mynumpymatrix1=pickle.load(f) f.close() if(filenamelist==oldfilelist): #if oldfilenamelst same,it means folder hasn't changed #use the vals from cache. else: #folder changed mynumpymatrix1,imgwdth,imght=docalculations() f3=open(cachefile,"w") pickle.dump((filenameslist,imgwdth,imght,mynumpymatrix1),f3) f3.close() this works and does what i need in my code..but i want to know if a more elegant solution is possible i am not worried about someone deliberately renaming files like .jpeg to aaa.jped and a.jpeg to deceive the checking since it is assumed that noone has permission to modify the folder except a trusted admin/code will be grateful for your suggestions tia -- http://mail.python.org/mailman/listinfo/python-list
Re: eucledian dist calculations
sorry..the last post didn't appear! > Are you sure? What happens if the vector with the smallest > sum(distance) is the first one? > initially i will set imgindex =0 so if the first one is of smallest sum..,the image will be reteived by imgindex 0 > (a) Can't you replace the inner loop with something like this: >distance = abs(input_weight - weights[image, :]) good suggestion ..was doing it java way! > (b) I doubt that you need the .copy() > 5. Lose the hard-wired numbers like 30 and 100 those nums were only for clarification.. > 6. Put it inside a function and *TEST* it > will do... thanx john ,your post really helped gordon -- http://mail.python.org/mailman/listinfo/python-list
problem with join
hi i am using python on WinXP..i have a string 'folder ' that i want to join to a set of imagefile names to create complete qualified names so that i can create objects out of them folder='F:/brown/code/python/fgrp1' filenms=['amber1.jpg', 'amber3.jpg', 'amy1.jpg', 'amy2.jpg'] filenameslist=[] for x in filenms: myfile=join(folder,x) filenameslist.append(myfile) now when i print the filenameslist i find that it looks like ['F:/brown/code/python/fgrp1\\amber1.jpg', 'F:/brown/code/python/fgrp1\\amber3.jpg', 'F:/brown/code/python/fgrp1\ \amy1.jpg', 'F:/brown/code/python/fgrp1\\amy2.jpg'] is there some problem with the way i use join? why do i get \\ infront of the basename? i would prefer it like 'F:/brown/code/python/fgrp1/basename.jpg', can anyone pls help gordon -- http://mail.python.org/mailman/listinfo/python-list
Re: problem with join
> If the string is only used to open a file, and never shown to the user, > what you prefer is irrelevant, isn't it? guess thats right.. > Back to your code, try this: > > from os.path import join, normpath > folder = 'F:/brown/code/python/fgrp1' > names = ['amber1.jpg', 'amber3.jpg', 'amy1.jpg', 'amy2.jpg'] > filenameslist = [normpath(join(folder, name)) for name in names] thanks Gabriel.. gordon -- http://mail.python.org/mailman/listinfo/python-list
Re: newbie question structure of function
> > > resultname=""" > > > That starts a string literal. > > i am not sure if this is the right way..i used it in case > matchdistance < threshold return False.then the filename will not be > taken from the filenameslist and so returns as an empty string. >a one-tuple why not use None instead? if (matchdistance < threshold): resultname=filenameslist[index] else: resultname=None will that not be better? gordon -- http://mail.python.org/mailman/listinfo/python-list