用于分隔numpy数组的字典键和值

Vei*_*pse 15 python arrays dictionary numpy

我有一本字典

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}
Run Code Online (Sandbox Code Playgroud)

我想分开keys,并values成2 numpy列.我试过np.array(Samples.keys(),dtype=np.float)但是我得到了一个错误TypeError: float() argument must be a string or a number

ank*_*tis 27

您可以使用从字典键和值视图np.fromiter直接创建numpy数组:

在python 3中:

keys = np.fromiter(Samples.keys(), dtype=float)
vals = np.fromiter(Samples.values(), dtype=float)
Run Code Online (Sandbox Code Playgroud)

在python 2中:

keys = np.fromiter(Samples.iterkeys(), dtype=float)
vals = np.fromiter(Samples.itervalues(), dtype=float)
Run Code Online (Sandbox Code Playgroud)


pra*_*ksh 12

在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()))
Run Code Online (Sandbox Code Playgroud)

原因np.array(Samples.values())并没有给出你在Python 3中的预期,在Python 3中,dict的values()方法返回一个可迭代的视图,而在Python 2中,它返回一个实际的键列表.

keys = np.array(list(Samples.keys()))实际上也将在Python 2.7中工作,并使您的代码更加与版本无关.但额外的召唤list()将使其缓慢下降.

  • list() 可能不是一个好的选择,因为它可能会混淆值,因为数据索引非常重要。 (3认同)

Hos*_*mes 6

在 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()))
Run Code Online (Sandbox Code Playgroud)

注意:值得注意的是,在这个Python版本中dict.keys(), 和分别返回类型为和dict.values()的对象。dict_keysdict_values


Ore*_*ren 5

如果您关心速度(Python 3.7)

\n
rnd = np.random.RandomState(10)\n\nfor i in [10,100,1000,10000,100000]:\n    test_dict = {j:j for j in rnd.uniform(-100,100,i)}\n    assert len(test_dict) == i\n    print(f"\\nFor {i} keys\\n-----------")\n    \n    %timeit keys = np.fromiter(test_dict.keys(), dtype=float)\n    \n    %timeit keys = np.array(list(test_dict.keys()))\n
Run Code Online (Sandbox Code Playgroud)\n

np.fromiter 快 5-7 倍

\n
For 10 keys\n-----------\n712 ns \xc2\xb1 4.77 ns per loop (mean \xc2\xb1 std. dev. of 7 runs, 1000000 loops each)\n1.65 \xc2\xb5s \xc2\xb1 9.15 ns per loop (mean \xc2\xb1 std. dev. of 7 runs, 1000000 loops each)\n\nFor 100 keys\n-----------\n1.87 \xc2\xb5s \xc2\xb1 13.7 ns per loop (mean \xc2\xb1 std. dev. of 7 runs, 1000000 loops each)\n8.02 \xc2\xb5s \xc2\xb1 22.3 ns per loop (mean \xc2\xb1 std. dev. of 7 runs, 100000 loops each)\n\nFor 1000 keys\n-----------\n13.7 \xc2\xb5s \xc2\xb1 27.7 ns per loop (mean \xc2\xb1 std. dev. of 7 runs, 100000 loops each)\n70.5 \xc2\xb5s \xc2\xb1 251 ns per loop (mean \xc2\xb1 std. dev. of 7 runs, 10000 loops each)\n\nFor 10000 keys\n-----------\n128 \xc2\xb5s \xc2\xb1 70.6 ns per loop (mean \xc2\xb1 std. dev. of 7 runs, 10000 loops each)\n698 \xc2\xb5s \xc2\xb1 455 ns per loop (mean \xc2\xb1 std. dev. of 7 runs, 1000 loops each)\n\nFor 100000 keys\n-----------\n1.45 ms \xc2\xb1 374 ns per loop (mean \xc2\xb1 std. dev. of 7 runs, 1000 loops each)\n7.14 ms \xc2\xb1 6.1 \xc2\xb5s per loop (mean \xc2\xb1 std. dev. of 7 runs, 100 loops each)\n
Run Code Online (Sandbox Code Playgroud)\n