python中的n维稀疏直方图。 [英] nDimensional sparse histogram in python.
问题描述
我写了一个C ++类,使用stl贴图和向量在
C ++中实现n维直方图。我想用Python编写这个代码
并希望得到一些来自这个组的输入。
C ++类非常简单..
std :: map< std :: vector< unsigned short> ;, unsigned long> histo;
比方说我想要3D直方图然后std :: vector< unsigned short>
将包含
空格中的x,y和z点,无符号长度是
计数的数量。
如果我想知道空间中特定点的值我通过在
向量[3,2,1]中如果
它在地图中找到则计数返回其他明智的零是
返回。
如果我想增加一个计数,那么histo [vector] ++会将
计数设置为1或增加
如果找到它的数量......
所以你可以看到这是一种非常简单的实现多维b $ b维稀疏直方图的方法。 />
我想在python中这也必须像
字典一样简单,其中键是向量x,y,z和值是
计数。
我的python还可以,但不是最好的,我希望有人在这里可能
想帮帮我吗?
这不行的例子......
histo = {[0,0,0] :1,[0,0,1]:2}
表示在0,0,0处有一个样本,在
0,0处有两个样本, 1
但是python告诉我TypeError:列表对象是不可用的
所以任何建议都是受欢迎的。
TIA。
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.
推荐答案
KraftDiner< bo ******* @ yahoo.com>写道:
...
KraftDiner <bo*******@yahoo.com> wrote:
...
histo = {[0,0,0]:1,[0,0,1]:2}
会指示有一个样本在0,0,0和两个样本在0,0,1
但是python告诉我TypeError:列表对象是不可用的
所以任何建议都会欢迎。
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.
使用元组而不是列表作为您的密钥:
histo = {(0,0,0 ):1,(0,0,1):2}
Alex
Use tuples, not lists, as your keys:
histo = {(0,0,0):1, (0,0,1):2}
Alex
很酷。
好的我的直方图类有两种方法1)增加一个点和2)
来获得积分。
def class histo:
def __init __(个体经营):
histo = {}
def update(点):
''''''搜索点的字典,如果它存在增加
值。
如果它不存在将值设置为1'''''
< br $>
def get(点):
返回self.histo [点]
我不知道怎么编码你pdate例程...
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...
在星期三,2006-02-01 19:06 - 0800,KraftDiner写道:
On Wed, 2006-02-01 at 19:06 -0800, KraftDiner wrote:
很酷。
好吧所以我的直方图课有两种方法1)增加一个点2)
得到重点。
def class histo:
def __init__ (个体经营):
histo = {}
def update(point):
'''''在字典中搜索点,如果存在则增加
值。<如果它不存在则将值设置为1''''
def get(point):
返回self.histo [point]
我不知道如何编写更新例程......
http://mail.python.org/mailman/listinfo/python-list
>
def update(点);
如果histo.get(指向)!=无:
histo [point] = histo [point ] + 1
问候,
John
-
此邮件已被扫描病毒和
危险内容通过MailScanner,并且
被认为是干净的。
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.
这篇关于python中的n维稀疏直方图。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!