473,385 Members | 1,693 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

nDimensional sparse histogram in python.

Hi, I wrote a C++ class that implements an n dimensional histogram in
C++, using stl maps and vectors. I want to code this up now in Python
and would like some input from this group.

The C++ class was VERY simple..

std::map<std::vector<unsigned short>, unsigned long> histo;

Say for example I want a 3D histogram then std::vector<unsigned short>
would contains
the x, y, and z points in space and the unsigned long is the number of
counts.
If I want to know the value at a specific point in space I pass in a
vector [3,2,1] and if
it is found in the map then the count is returned other wise zero is
returned.
If I want to increment a count then histo[vector]++ will either set the
count to 1 or increment
the count if it is found...

So as you can see this is a very simple way to implement a multi
dimensional sparse histogram.

I'm thinking that in python this must also be as simple as a
dictionary, where the key is the vector x,y,z and the value is the
count.

My python is ok, but not the best and I was hoping some one here might
want to help me out?
This doesn't work for example...

histo = {[0,0,0]:1, [0,0,1]:2}
Would indicate that there is one sample at 0,0,0 and two samples at
0,0,1
but python tells me TypeError: list objects are unhashable

So any suggestions would be welcome.
TIA.

Feb 2 '06 #1
12 2818
KraftDiner <bo*******@yahoo.com> wrote:
...
histo = {[0,0,0]:1, [0,0,1]:2}
Would indicate that there is one sample at 0,0,0 and two samples at
0,0,1
but python tells me TypeError: list objects are unhashable

So any suggestions would be welcome.


Use tuples, not lists, as your keys:

histo = {(0,0,0):1, (0,0,1):2}
Alex
Feb 2 '06 #2
Cool.
Ok so my histogram class had two methods 1) To increment a point and 2)
to get the point.

def class histo:
def __init__(self):
histo = {}
def update(point):
'''searches the dictionary for point and if it exists increment the
value.
if it doesn't exist set the value to 1'''

def get(point):
return self.histo[point]

I'm not sure how to code up the update routine...

Feb 2 '06 #3
On Wed, 2006-02-01 at 19:06 -0800, KraftDiner wrote:
Cool.
Ok so my histogram class had two methods 1) To increment a point and 2)
to get the point.

def class histo:
def __init__(self):
histo = {}
def update(point):
'''searches the dictionary for point and if it exists increment the
value.
if it doesn't exist set the value to 1'''

def get(point):
return self.histo[point]

I'm not sure how to code up the update routine...

--
http://mail.python.org/mailman/listinfo/python-list


def update(point);

if histo.get(point) != None:
histo[point] = histo[point] + 1

Regards,

John
--
This message has been scanned for viruses and
dangerous content by MailScanner, and is
believed to be clean.

Feb 2 '06 #4
KraftDiner <bo*******@yahoo.com> wrote:
Cool.
Ok so my histogram class had two methods 1) To increment a point and 2)
to get the point.

def class histo:
def __init__(self):
histo = {}
def update(point):
'''searches the dictionary for point and if it exists increment the
value.
if it doesn't exist set the value to 1'''
def update(self, point):
self.histo[point] = 1 + self.histo.get(point, 0)

def get(point):
def (self, point):

actually.
return self.histo[point]

I'm not sure how to code up the update routine...


See above.
Alex
Feb 2 '06 #5
The dictionary is sorted by the point key.
Is there a way to sort the dictionary by the value?
Seems to me this goes against the purpose of a dictionary but....
thats what I need to do..

Feb 2 '06 #6
KraftDiner wrote:
The dictionary is sorted by the point key.
Is there a way to sort the dictionary by the value?
Seems to me this goes against the purpose of a dictionary but....
thats what I need to do..


Well, it's probably not all that efficient, but it is simple code:

sortedList = [(v, k) for k, v in self.histo.items()]
sortedList.sort()

Should do it...

-Kirk McDonald
Feb 2 '06 #7
Ok so this is nice.. Just one thing.. When you try to get a value from
a dictionary
and it isn't found in the dictionary things go bad...

Take this for example:

class histogram(object):
def __init__(self):
self.histo = {}

def update(self, point):
if self.histo.get(point) != None:
self.histo[point] = self.histo[point] + 1
else:
self.histo[point] = 1

def get(self, point):
return self.histo[point]
hist = histogram()
hist.update((0,0,0))
hist.update((0,0,1))
hist.update((0,0,1))
hist.get((0,0,0))
hist.get((0,0,1))
hist.get((0,0,2))

spews out this error:

Traceback (most recent call last):
File "histogram.py", line 21, in ?
hist.get((0,0,2))
File "histogram.py", line 12, in get
return self.histo[point]
KeyError: (0, 0, 2)

Feb 2 '06 #8
KraftDiner wrote:
Ok so this is nice.. Just one thing.. When you try to get a value from
a dictionary
and it isn't found in the dictionary things go bad...

Take this for example:

class histogram(object):
def __init__(self):
self.histo = {}

def update(self, point):
if self.histo.get(point) != None:
self.histo[point] = self.histo[point] + 1
else:
self.histo[point] = 1

def get(self, point):
return self.histo[point]
hist = histogram()
hist.update((0,0,0))
hist.update((0,0,1))
hist.update((0,0,1))
hist.get((0,0,0))
hist.get((0,0,1))
hist.get((0,0,2))

spews out this error:

Traceback (most recent call last):
File "histogram.py", line 21, in ?
hist.get((0,0,2))
File "histogram.py", line 12, in get
return self.histo[point]
KeyError: (0, 0, 2)


Try this:

class histogram(object):
def __init__(self):
self.histo = {}

def update(self, point):
self.histo[point] = self.histo.get(point, 0) + 1

def get(self, point):
return self.histo.get(point, 0)

dict.get's default return value is your friend.

-Kirk McDonald
Feb 2 '06 #9
Hi
you can use has_key() to check whether
the particular value is in the key set or not.
a = {}
a[1] = 22
a[2] = 33
a {1: 22, 2: 33} a.has_key(3) False a.has_key(1) True


-raj
KraftDiner wrote: Ok so this is nice.. Just one thing.. When you try to get a value from
a dictionary
and it isn't found in the dictionary things go bad...

Take this for example:

class histogram(object):
def __init__(self):
self.histo = {}

def update(self, point):
if self.histo.get(point) != None:
self.histo[point] = self.histo[point] + 1
else:
self.histo[point] = 1

def get(self, point):
return self.histo[point]
hist = histogram()
hist.update((0,0,0))
hist.update((0,0,1))
hist.update((0,0,1))
hist.get((0,0,0))
hist.get((0,0,1))
hist.get((0,0,2))

spews out this error:

Traceback (most recent call last):
File "histogram.py", line 21, in ?
hist.get((0,0,2))
File "histogram.py", line 12, in get
return self.histo[point]
KeyError: (0, 0, 2)


Feb 2 '06 #10
Many thanks everyone.

One last quick question...
The dictionary, I believe, is sorted by the key.
Is there a way to sort it by the value?
Say I wanted to put out a list of the frequency sorted by highest to
lowest?

Feb 2 '06 #11
KraftDiner wrote:
Many thanks everyone.

One last quick question...
The dictionary, I believe, is sorted by the key.
Is there a way to sort it by the value?
Say I wanted to put out a list of the frequency sorted by highest to
lowest?


The dictionary itself is actually unordered; a C++ std::map this ain't.
To sort its elements, you need to build a list of the items and sort
that, e.g.:

items = [(v, k) for k, v in self.histo.items()]
items.sort()

This will give you a list of (value, key) tuples sorted by value.

-Kirk McDonald
Feb 2 '06 #12
KraftDiner <bo*******@yahoo.com> wrote:
Many thanks everyone.

One last quick question...
The dictionary, I believe, is sorted by the key.
Dictionaries are NOT sorted -- they're hashtables.
Is there a way to sort it by the value?
Say I wanted to put out a list of the frequency sorted by highest to
lowest?


import operator
for k, v in sorted(somedict, key=operator.itemgetter(1), reverse=1):
print k, v

emits key/value pairs, one per line, for any dictionary sorted by
descending order of values. Embellish to taste.
Alex
Feb 2 '06 #13

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Oracle3001 | last post by:
Hi All, I am trying to use JAI to build a histogram of an image i have. I have posted the code below, and the error I get at runtime. I have taken the code from the offical java examples, so I am...
1
by: bleh | last post by:
....to include a removeData(datatoremove) function, to mirror the existing addData(datatoadd) function. If anybody knows of somewhere where this has been done already, if you could point me in...
2
by: kevin parks | last post by:
hi. I've been banging my head against this one a while and have asked around, and i am throwing this one out there in the hopes that some one can shed some light on what has turned out to be a...
6
by: Dan Stromberg | last post by:
Does anyone have a python implementation (or python C/C+ extension) that would allow me to perform set operations (union, intersection, difference, number of elements, &c) over very large...
10
by: draghuram | last post by:
Hi, Is there any special support for sparse file handling in python? My initial search didn't bring up much (not a thorough search). I wrote the following pice of code: options.size =...
13
by: momobear | last post by:
hi, is there a way to let python operate on sequence of int or short? In C, we just need declare a point, then I could get the point value, just like: short* k = buffer, //k is a point to a...
2
by: Daniel Nogradi | last post by:
How does one do a histogram on only a part of an image? This is what I found in the PIL documentation about histogram( ): """ im.histogram(mask) =list Returns a histogram for those parts of...
5
by: arnuld | last post by:
this is a programme that counts the "lengths" of each word and then prints that many of stars(*) on the output . it is a modified form of K&R2 exercise 1-13. the programme runs without any...
13
by: Bruza | last post by:
I need to implement a "random selection" algorithm which takes a list of as input. Each of the (obj, prob) represents how likely an object, "obj", should be selected based on its probability of...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.