字典键和值以分隔 numpy 数组
Posted
技术标签:
【中文标题】字典键和值以分隔 numpy 数组【英文标题】:Dictionary keys and values to separate numpy arrays 【发布时间】:2014-07-03 07:50:04 【问题描述】:我有一本字典
Samples = 5.207403005022627: 0.69973543384229719, 6.8970222167794759: 0.080782939731898179, 7.8338517407140973: 0.10308033284258854, 8.5301143255505334: 0.018640838362318335, 10.418899728838058: 0.14427355015329846, 5.3983946820220501: 0.51319796560976771
我想将keys
和values
分成2 个numpy
数组。
我试过np.array(Samples.keys(),dtype=np.float)
,但我收到错误TypeError: float() argument must be a string or a number
【问题讨论】:
【参考方案1】:您可以使用np.fromiter
直接从字典键和值视图创建numpy
数组:
在 python 3 中:
keys = np.fromiter(Samples.keys(), dtype=float)
vals = np.fromiter(Samples.values(), dtype=float)
在 python 2 中:
keys = np.fromiter(Samples.iterkeys(), dtype=float)
vals = np.fromiter(Samples.itervalues(), dtype=float)
【讨论】:
【参考方案2】:在 python 3.4 上,以下操作很简单:
Samples = 5.207403005022627: 0.69973543384229719, 6.8970222167794759: 0.080782939731898179, 7.8338517407140973: 0.10308033284258854, 8.5301143255505334: 0.018640838362318335, 10.418899728838058: 0.14427355015329846, 5.3983946820220501: 0.51319796560976771
keys = np.array(list(Samples.keys()))
values = np.array(list(Samples.values()))
np.array(Samples.values())
没有给出你在 Python 3 中所期望的原因是,在 Python 3 中,dict 的 values() 方法返回一个可迭代的视图,而在 Python 2 中,它返回一个实际的键列表.
keys = np.array(list(Samples.keys()))
实际上也可以在 Python 2.7 中工作,并且会使您的代码与版本无关。但是对list()
的额外调用会稍微减慢它的速度。
【讨论】:
list() 可能不是一个好的选择,因为它可能会混淆值,因为数据索引非常重要。【参考方案3】:在 Python 3.7 中:
import numpy as np
Samples = 5.207403005022627: 0.69973543384229719, 6.8970222167794759: 0.080782939731898179, 7.8338517407140973: 0.10308033284258854, 8.5301143255505334: 0.018640838362318335, 10.418899728838058: 0.14427355015329846, 5.3983946820220501: 0.51319796560976771
keys = np.array(list(Samples.keys()))
vals = np.array(list(Samples.values()))
注意:重要的是要说,在这个 Python 版本中,dict.keys()
和 dict.values()
分别返回 dict_keys
和 dict_values
类型的对象。
【讨论】:
【参考方案4】:如果您关心速度(Python 3.7)
rnd = np.random.RandomState(10)
for i in [10,100,1000,10000,100000]:
test_dict = j:j for j in rnd.uniform(-100,100,i)
assert len(test_dict) == i
print(f"\nFor i keys\n-----------")
%timeit keys = np.fromiter(test_dict.keys(), dtype=float)
%timeit keys = np.array(list(test_dict.keys()))
np.fromiter 快 5-7 倍
For 10 keys
-----------
712 ns ± 4.77 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)
1.65 µs ± 9.15 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)
For 100 keys
-----------
1.87 µs ± 13.7 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)
8.02 µs ± 22.3 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)
For 1000 keys
-----------
13.7 µs ± 27.7 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)
70.5 µs ± 251 ns per loop (mean ± std. dev. of 7 runs, 10000 loops each)
For 10000 keys
-----------
128 µs ± 70.6 ns per loop (mean ± std. dev. of 7 runs, 10000 loops each)
698 µs ± 455 ns per loop (mean ± std. dev. of 7 runs, 1000 loops each)
For 100000 keys
-----------
1.45 ms ± 374 ns per loop (mean ± std. dev. of 7 runs, 1000 loops each)
7.14 ms ± 6.1 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)
【讨论】:
【参考方案5】:只需将所有值分配给一个列表,然后转换为np.array()
。
import numpy as np
Samples = 5.207403005022627: 0.69973543384229719, 6.8970222167794759: 0.080782939731898179, 7.8338517407140973: 0.10308033284258854, 8.5301143255505334: 0.018640838362318335, 10.418899728838058: 0.14427355015329846, 5.3983946820220501: 0.51319796560976771
keys = np.array(Samples.keys())
vals = np.array(Samples.values())
或者,如果you want to iterate over it:
import numpy as np
Samples = 5.207403005022627: 0.69973543384229719, 6.8970222167794759: 0.080782939731898179, 7.8338517407140973: 0.10308033284258854, 8.5301143255505334: 0.018640838362318335, 10.418899728838058: 0.14427355015329846, 5.3983946820220501: 0.51319796560976771
keys = vals = []
for k, v in Samples.items():
keys.append(k)
vals.append(v)
keys = np.array(keys)
vals = np.array(vals)
【讨论】:
你能遍历这个数组吗? 你想迭代它吗?如果是这样,那么是的。 当你做keys = np.array(Samples.keys())时,你可以迭代吗?【参考方案6】:keys = np.array(dictionary.keys())
values = np.array(dictionary.values())
【讨论】:
不,它不起作用(至少在 python 3.4 中): >> np.array(Samples.keys()) Out[15]: array(dict_keys([5.207403005022627, 6.897022216779476, 7.833851740714097, 8.530114325550533, 10.418899728838058, 5.39839468202205]), dtype=object) 这不是你想要的!以上是关于字典键和值以分隔 numpy 数组的主要内容,如果未能解决你的问题,请参考以下文章