简体   繁体   English

Python:计算相对于周围坐标的坐标百分比

[英]Python: Calculate percentage of coordinates in relation to surrounding coordinates

I am having a dictionary with 3D tuple coordinates as keys and string scores as values. 我有一本字典,其中3D元组坐标作为键,而字符串分数作为值。 Now I want to get a new list/dict/array of coordinates which should be the percent value of the coordinate and all surrounding coordinates in a 12x12x12 box. 现在,我想获得一个新的坐标列表/字典/数组,它应该是12x12x12框中的坐标和所有周围坐标的百分比值。 Is there a faster way than to grab the coordinates from a dictionary like in my first approach? 是否有比像第一种方法那样从字典中获取坐标更快的方法? (sparse1 is the dictionary with the original coordinates). (sparse1是具有原始坐标的字典)。 In the code sparse1 is only an extraction of coordinates from one Tube (because i dont know how to add more characters here :( ): 在代码sparse1中,只是从一个Tube中提取坐标(因为我不知道如何在此处添加更多字符:():

import numpy as np
import itertools


sparse1 = {(32.0, 291.0, 3.0): ['-2'], (16.0, 299.0, 23.0): ['-2'], (22.0, 303.0, 24.0): ['2'], (30.0, 296.0, 16.0): ['1'], (27.0, 305.0, 10.0): ['1'], (21.0, 290.0, 16.0): ['2'], (27.0, 295.0, 15.0): ['-2'], (36.0, 293.0, -2.0): ['2'], (37.0, 292.0, 5.0): ['2'], (24.0, 312.0, 18.0): ['2'], (21.0, 307.0, 25.0): ['-2'], (20.0, 304.0, 17.0): ['1'], (26.0, 291.0, 18.0): ['-2'], (17.0, 308.0, 10.0): ['2'], (32.0, 313.0, 11.0): ['2'], (32.0, 303.0, 6.0): ['-2'], (25.0, 294.0, 10.0): ['2'], (22.0, 299.0, 19.0): ['2'], (24.0, 293.0, 2.0): ['-2'], (20.0, 303.0, 20.0): ['2'], (20.0, 293.0, 3.0): ['2'], (19.0, 298.0, 28.0): ['-2'], (29.0, 297.0, -1.0): ['2'], (31.0, 310.0, 11.0): ['-2'], (22.0, 300.0, 26.0): ['2'], (21.0, 311.0, 14.0): ['2'], (35.0, 306.0, 13.0): ['2'], (15.0, 308.0, 14.0): ['2'], (27.0, 296.0, 21.0): ['2'], (28.0, 289.0, 0.0): ['2'], (18.0, 309.0, 9.0): ['2'], (31.0, 297.0, 1.0): ['2'], (23.0, 312.0, 15.0): ['2'], (26.0, 295.0, 13.0): ['-2'], (30.0, 302.0, 9.0): ['1'], (16.0, 301.0, 26.0): ['-2'], (29.0, 307.0, 17.0): ['2'], (24.0, 299.0, 18.0): ['2'], (32.0, 290.0, -3.0): ['2'], (12.0, 301.0, 27.0): ['2'], (33.0, 311.0, 11.0): ['1'], (20.0, 291.0, 19.0): ['2'], (26.0, 310.0, 12.0): ['2'], (21.0, 305.0, 6.0): ['1'], (36.0, 289.0, 12.0): ['2'], (23.0, 290.0, 1.0): ['2'], (20.0, 306.0, 30.0): ['2'], (30.0, 308.0, 2.0): ['1'], (15.0, 296.0, 25.0): ['-2'], (28.0, 293.0, 11.0): ['-2'], (31.0, 301.0, 6.0): ['2'], (17.0, 298.0, 29.0): ['1'], (32.0, 289.0, 1.0): ['-2'], (25.0, 292.0, 17.0): ['-2'], (23.0, 307.0, 24.0): ['-2'], (21.0, 292.0, 22.0): ['1'], (27.0, 293.0, 9.0): ['2'], (26.0, 304.0, 0.0): ['2'], (20.0, 295.0, 14.0): ['2'], (25.0, 309.0, 24.0): ['2'], (24.0, 310.0, 16.0): ['-2'], (21.0, 309.0, 27.0): ['1'], (14.0, 306.0, 23.0): ['-2'], (23.0, 294.0, 12.0): ['2'], (20.0, 310.0, 19.0): ['-2'], (30.0, 309.0, 12.0): ['-2'], (33.0, 302.0, 12.0): ['2'], (26.0, 293.0, 20.0): ['1'], (30.0, 294.0, 19.0): ['2'], (25.0, 288.0, 12.0): ['2'], (29.0, 305.0, 14.0): ['2'], (24.0, 291.0, 4.0): ['2'], (33.0, 309.0, 4.0): ['2'], (28.0, 294.0, -1.0): ['-2'], (20.0, 301.0, 22.0): ['2'], (32.0, 310.0, 12.0): ['-2'], (19.0, 296.0, 30.0): ['2'], (25.0, 305.0, 21.0): ['2'], (22.0, 292.0, 4.0): ['2'], (30.0, 307.0, 1.0): ['1'], (34.0, 303.0, 7.0): ['1'], (28.0, 295.0, 6.0): ['2'], (18.0, 311.0, 11.0): ['2'], (31.0, 303.0, 3.0): ['1'], (30.0, 296.0, 11.0): ['1'], (16.0, 299.0, 28.0): ['-2'], (35.0, 289.0, 10.0): ['-2'], (13.0, 304.0, 29.0): ['2'], (29.0, 309.0, 19.0): ['2'], (25.0, 291.0, -1.0): ['2'], (24.0, 297.0, 16.0): ['1'], (27.0, 295.0, 6.0): ['2'], (33.0, 305.0, 9.0): ['-2'], (26.0, 296.0, 14.0): ['1'], (29.0, 295.0, 2.0): ['-2'], (22.0, 298.0, 26.0): ['1'], (18.0, 293.0, 19.0): ['2'], (29.0, 296.0, 13.0): ['1'], (18.0, 305.0, 27.0): ['-2'], (19.0, 309.0, 16.0): ['-2'], (31.0, 291.0, 4.0): ['-2'], (25.0, 294.0, 19.0): ['-2'], (22.0, 299.0, 28.0): ['2'], (24.0, 293.0, 11.0): ['2'], (21.0, 294.0, 20.0): ['-2'], (27.0, 291.0, 11.0): ['-2'], (26.0, 306.0, 6.0): ['-2'], (19.0, 298.0, 21.0): ['-2'], (29.0, 294.0, 0.0): ['-2'], (24.0, 308.0, 22.0): ['-2'], (30.0, 305.0, 8.0): ['-2'], (14.0, 300.0, 21.0): ['2'], (37.0, 293.0, -1.0): ['2'], (23.0, 292.0, 14.0): ['1'], (29.0, 290.0, 17.0): ['2'], (20.0, 308.0, 13.0): ['-2'], (33.0, 296.0, 10.0): ['2'], (26.0, 295.0, 22.0): ['2'], (30.0, 306.0, 7.0): ['-2'], (17.0, 304.0, 14.0): ['2'], (30.0, 302.0, 0.0): ['2'], (25.0, 290.0, 14.0): ['-2'], (29.0, 307.0, 8.0): ['-2'], (31.0, 308.0, 3.0): ['1'], (28.0, 293.0, 8.0): ['2'], (37.0, 287.0, 5.0): ['2'], (20.0, 291.0, 24.0): ['2'], (35.0, 294.0, 7.0): ['2'], (32.0, 308.0, 10.0): ['-2'], (29.0, 293.0, 2.0): ['-2'], (19.0, 294.0, 16.0): ['2'], (25.0, 307.0, 11.0): ['-2'], (22.0, 294.0, 2.0): ['-2'], (21.0, 305.0, 9.0): ['-2'], (27.0, 310.0, 20.0): ['2'], (18.0, 303.0, 14.0): ['2'], (28.0, 293.0, 4.0): ['-2'], (29.0, 296.0, -2.0): ['2'], (23.0, 307.0, 19.0): ['-2'], (28.0, 294.0, 10.0): ['1'], (27.0, 293.0, 0.0): ['-2'], (33.0, 307.0, 7.0): ['-2'], (26.0, 304.0, 11.0): ['1'], (20.0, 295.0, 23.0): ['-2'], (25.0, 309.0, 3.0): ['2'], (36.0, 293.0, 8.0): ['2'], (23.0, 294.0, 5.0): ['1'], (29.0, 298.0, 15.0): ['2'], (28.0, 291.0, 16.0): ['-2'], (15.0, 300.0, 29.0): ['1'], (19.0, 307.0, 18.0): ['-2'], (31.0, 289.0, 2.0): ['-2'], (29.0, 297.0, -2.0): ['2'], (35.0, 293.0, 4.0): ['1'], (29.0, 305.0, 1.0): ['1'], (25.0, 293.0, -3.0): ['2'], (24.0, 291.0, 13.0): ['1'], (28.0, 306.0, 9.0): ['-2'], (26.0, 308.0, 4.0): ['1'], (17.0, 295.0, 24.0): ['-2'], (32.0, 310.0, 7.0): ['2'], (19.0, 296.0, 23.0): ['-2'], (16.0, 298.0, 20.0): ['2'], (24.0, 306.0, 20.0): ['-2'], (30.0, 307.0, 10.0): ['-2'], (23.0, 298.0, 0.0): ['1'], (29.0, 302.0, 2.0): ['1'], (30.0, 305.0, 3.0): ['-2'], (31.0, 293.0, 7.0): ['1'], (17.0, 306.0, 12.0): ['2'], (30.0, 296.0, 2.0): ['-2'], (32.0, 297.0, 0.0): ['2'], (24.0, 298.0, -1.0): ['1'], (35.0, 289.0, 1.0): ['-2'], (29.0, 309.0, 10.0): ['-2'], (12.0, 307.0, 26.0): ['2'], (28.0, 310.0, 18.0): ['1'], (26.0, 294.0, -3.0): ['2'], (33.0, 295.0, -2.0): ['2'], (26.0, 296.0, 1.0): ['-2'], (32.0, 306.0, 8.0): ['-2'], (19.0, 292.0, 18.0): ['2'], (25.0, 301.0, 9.0): ['2'], (22.0, 298.0, 17.0): ['1'], (21.0, 307.0, 15.0): ['-2'], (21.0, 295.0, 25.0): ['-2'], (29.0, 296.0, 18.0): ['2'], (15.0, 302.0, 20.0): ['2'], (21.0, 310.0, 20.0): ['-2'], (31.0, 291.0, 15.0): ['-2'], (17.0, 308.0, 24.0): ['-2'], (30.0, 292.0, 7.0): ['1'], (24.0, 297.0, 26.0): ['2'], (23.0, 305.0, 21.0): ['-2'], (13.0, 308.0, 25.0): ['1'], (27.0, 291.0, 2.0): ['-2'], (27.0, 297.0, 15.0): ['2'], (26.0, 306.0, 13.0): ['2'], (20.0, 293.0, 21.0): ['-2'], (26.0, 294.0, 23.0): ['2'], (16.0, 294.0, 25.0): ['1'], (35.0, 286.0, 5.0): ['1'], (38.0, 292.0, 3.0): ['2'], (19.0, 305.0, 12.0): ['-2'], (31.0, 295.0, 0.0): ['-2'], (21.0, 312.0, 23.0): ['2'], (35.0, 291.0, 6.0): ['-2'], (16.0, 307.0, 15.0): ['1'], (29.0, 307.0, 7.0): ['-2'], (24.0, 289.0, 15.0): ['2'], (15.0, 307.0, 30.0): ['2'], (31.0, 286.0, 8.0): ['1'], (19.0, 294.0, 25.0): ['1'], (25.0, 307.0, 2.0): ['1'], (22.0, 294.0, 25.0): ['1'], (24.0, 304.0, 10.0): ['-2'], (27.0, 310.0, 13.0): ['1'], (23.0, 296.0, 2.0): ['-2'], (20.0, 306.0, 8.0): ['-2'], (21.0, 308.0, 24.0): ['-2'], (22.0, 305.0, 18.0): ['1'], (34.0, 306.0, 3.0): ['2'], (23.0, 307.0, 10.0): ['-2'], (34.0, 288.0, 11.0): ['-2'], (12.0, 305.0, 24.0): ['2'], (18.0, 296.0, 23.0): ['-2'], (33.0, 307.0, 14.0): ['2'], (20.0, 295.0, 28.0): ['2'], (32.0, 304.0, 6.0): ['-2'], (25.0, 309.0, 10.0): ['1'], (22.0, 290.0, 14.0): ['2'], (21.0, 309.0, 13.0): ['-2'], (27.0, 306.0, 16.0): ['-2'], (21.0, 303.0, 6.0): ['2'], (23.0, 294.0, 18.0): ['-2'], (15.0, 300.0, 22.0): ['-2'], (19.0, 307.0, 29.0): ['1'], (31.0, 289.0, 9.0): ['-2'], (17.0, 310.0, 26.0): ['-2'], (30.0, 294.0, 1.0): ['-2'], (23.0, 311.0, 23.0): ['2'], (13.0, 310.0, 27.0): ['2'], (28.0, 306.0, 0.0): ['1'], (27.0, 289.0, 12.0): ['1'], (26.0, 308.0, 15.0): ['-2'], (36.0, 286.0, 5.0): ['2'], (26.0, 302.0, 4.0): ['1'], (25.0, 305.0, 7.0): ['-2'], (22.0, 292.0, 22.0): ['1'], (29.0, 302.0, 11.0): ['2'], (19.0, 303.0, 14.0): ['2'], (22.0, 311.0, 13.0): ['2'], (28.0, 288.0, 6.0): ['2'], (30.0, 290.0, 6.0): ['-2'], (16.0, 305.0, 13.0): ['2'], (29.0, 309.0, 5.0): ['1'], (27.0, 295.0, 16.0): ['-2'], (28.0, 310.0, 13.0): ['-2'], (18.0, 294.0, 28.0): ['2'], (27.0, 301.0, 1.0): ['2'], (32.0, 306.0, 3.0): ['1'], (34.0, 290.0, -3.0): ['2'], (20.0, 305.0, 28.0): ['1'], (24.0, 302.0, 8.0): ['1'], (27.0, 308.0, 15.0): ['-2'], (21.0, 295.0, 16.0): ['1'], (23.0, 302.0, 4.0): ['2'], (20.0, 304.0, 14.0): ['-2'], (20.0, 302.0, 11.0): ['2'], (20.0, 292.0, 24.0): ['2'], (38.0, 290.0, 2.0): ['1'], (22.0, 307.0, 16.0): ['-2'], (17.0, 308.0, 17.0): ['1'], (30.0, 292.0, 14.0): ['-2'], (18.0, 302.0, 29.0): ['1'], (16.0, 311.0, 25.0): ['1'], (14.0, 309.0, 24.0): ['-2'], (13.0, 308.0, 22.0): ['1'], (34.0, 290.0, 13.0): ['1'], (19.0, 308.0, 20.0): ['-2'], (26.0, 300.0, 5.0): ['2'], (26.0, 294.0, 14.0): ['-2'], (29.0, 294.0, 3.0): ['-2'], (25.0, 297.0, 13.0): ['2'], (24.0, 308.0, 4.0): ['1'], (21.0, 311.0, 19.0): ['1'], (27.0, 304.0, 18.0): ['2'], (23.0, 292.0, 16.0): ['-2'], (20.0, 308.0, 27.0): ['-2'], (18.0, 309.0, 20.0): ['1'], (31.0, 295.0, 11.0): ['-2'], (17.0, 304.0, 28.0): ['-2'], (30.0, 288.0, 3.0): ['-2'], (23.0, 309.0, 25.0): ['1'], (14.0, 305.0, 23.0): ['1'], (30.0, 293.0, 16.0): ['-2'], (19.0, 311.0, 22.0): ['1'], (28.0, 304.0, 6.0): ['-2'], (31.0, 286.0, 1.0): ['1'], (26.0, 310.0, 17.0): ['-2'], (35.0, 294.0, 2.0): ['1'], (29.0, 293.0, 7.0): ['2'], (25.0, 307.0, 25.0): ['2'], (22.0, 294.0, 16.0): ['-2'], (13.0, 297.0, 28.0): ['2'], (22.0, 296.0, 1.0): ['-2'], (24.0, 304.0, 1.0): ['2'], (21.0, 299.0, 24.0): ['-2'], (20.0, 296.0, 0.0): ['2'], (19.0, 311.0, 11.0): ['2'], (15.0, 294.0, 23.0): ['2'], (22.0, 305.0, 11.0): ['-2'], (33.0, 286.0, 2.0): ['-2'], (29.0, 311.0, 11.0): ['-2'], (29.0, 289.0, 12.0): ['-2'], (34.0, 288.0, 2.0): ['-2'], (28.0, 308.0, 3.0): ['-2'], (18.0, 296.0, 30.0): ['2'], (27.0, 299.0, 3.0): ['2'], (33.0, 293.0, 10.0): ['-2'], (17.0, 301.0, 22.0): ['-2'], (32.0, 304.0, 13.0): ['1'], (25.0, 303.0, 6.0): ['-2'], (16.0, 297.0, 21.0): ['2'], (22.0, 290.0, 21.0): ['2'], (27.0, 306.0, 9.0): ['-2'], (21.0, 303.0, 13.0): ['1'], (36.0, 307.0, 9.0): ['2'], (20.0, 310.0, 12.0): ['1'], (38.0, 288.0, -1.0): ['2'], (34.0, 287.0, 5.0): ['-2'], (21.0, 296.0, 15.0): ['1'], (17.0, 310.0, 19.0): ['2'], (30.0, 294.0, 8.0): ['2'], (21.0, 304.0, 22.0): ['1'], (16.0, 309.0, 27.0): ['-2'], (14.0, 311.0, 26.0): ['2'], (35.0, 305.0, 12.0): ['2'], (34.0, 292.0, 15.0): ['2'], (27.0, 289.0, 19.0): ['2'], (30.0, 287.0, 0.0): ['2'], (25.0, 305.0, 14.0): ['1'], (16.0, 298.0, 25.0): ['-2'], (25.0, 299.0, 3.0): ['2'], (22.0, 302.0, 10.0): ['1'], (24.0, 306.0, 6.0): ['-2'], (21.0, 297.0, 17.0): ['1'], (21.0, 291.0, 2.0): ['2'], (20.0, 298.0, 25.0): ['-2'], (26.0, 305.0, 20.0): ['2'], (18.0, 311.0, 22.0): ['1'], (30.0, 305.0, 6.0): ['-2'], (17.0, 306.0, 30.0): ['2'], (24.0, 296.0, 21.0): ['-2'], (32.0, 287.0, 1.0): ['-2'], (14.0, 307.0, 21.0): ['1'], (28.0, 310.0, 4.0): ['2'], (31.0, 284.0, 3.0): ['2'], (26.0, 296.0, 19.0): ['1'], (26.0, 290.0, 0.0): ['2'], (13.0, 299.0, 26.0): ['1'], (24.0, 302.0, 3.0): ['2'], (27.0, 308.0, 0.0): ['2'], (21.0, 301.0, 26.0): ['2'], (23.0, 302.0, 13.0): ['2'], (20.0, 292.0, 15.0): ['2'], (30.0, 310.0, 4.0): ['2'], (19.0, 309.0, 13.0): ['-2'], (22.0, 307.0, 9.0): ['-2'], (29.0, 313.0, 9.0): ['2'], (29.0, 291.0, 10.0): ['-2'], (34.0, 290.0, 4.0): ['-2'], (27.0, 291.0, 20.0): ['2'], (18.0, 298.0, 24.0): ['-2'], (26.0, 296.0, -3.0): ['2'], (33.0, 295.0, 12.0): ['1'], (17.0, 303.0, 16.0): ['2'], (26.0, 294.0, 5.0): ['1'], (25.0, 297.0, 4.0): ['1'], (22.0, 300.0, 23.0): ['1'], (22.0, 309.0, 28.0): ['2'], (27.0, 304.0, 11.0): ['2'], (20.0, 308.0, 18.0): ['-2'], (30.0, 311.0, 14.0): ['1'], (19.0, 305.0, 26.0): ['-2'], (17.0, 304.0, 21.0): ['2'], (30.0, 288.0, 10.0): ['-2'], (21.0, 306.0, 20.0): ['-2'], (16.0, 307.0, 29.0): ['1'], (14.0, 305.0, 28.0): ['1'], (34.0, 294.0, 1.0): ['-2'], (32.0, 291.0, -2.0): ['1'], (25.0, 307.0, 16.0): ['-2'], (25.0, 293.0, 1.0): ['-2'], (24.0, 304.0, 24.0): ['2'], (21.0, 299.0, 23.0): ['-2'], (21.0, 293.0, 0.0): ['2'], (26.0, 307.0, 22.0): ['2'], (33.0, 286.0, 11.0): ['1'], (32.0, 285.0, 3.0): ['1'], (29.0, 289.0, 7.0): ['1'], (28.0, 308.0, 10.0): ['-2'], (28.0, 290.0, 15.0): ['-2'], (31.0, 296.0, 12.0): ['1'], (32.0, 290.0, -2.0): ['1'], (26.0, 298.0, 21.0): ['2'], (17.0, 301.0, 25.0): ['-2'], (26.0, 292.0, 2.0): ['-2'], (32.0, 294.0, 5.0): ['1'], (29.0, 286.0, 6.0): ['2'], (29.0, 297.0, 0.0): ['1'], (13.0, 301.0, 24.0): ['1'], (27.0, 306.0, 6.0): ['-2'], (21.0, 303.0, 20.0): ['2'], (27.0, 312.0, 17.0): ['2'], (19.0, 307.0, 15.0): ['-2'], (31.0, 309.0, 9.0): ['-2'], (20.0, 307.0, 23.0): ['-2'], (34.0, 292.0, 6.0): ['-2'], (32.0, 301.0, 4.0): ['2'], (34.0, 302.0, 11.0): ['2'], (18.0, 300.0, 26.0): ['-2'], (33.0, 289.0, 14.0): ['1'], (29.0, 296.0, 14.0): ['-2'], (24.0, 296.0, 2.0): ['-2'], (19.0, 303.0, 24.0): ['-2'], (25.0, 308.0, 21.0): ['1'], (31.0, 307.0, 13.0): ['-2'], (17.0, 306.0, 23.0): ['-2'], (23.0, 292.0, -1.0): ['1'], (15.0, 302.0, 24.0): ['-2'], (30.0, 308.0, 5.0): ['-2'], (14.0, 307.0, 30.0): ['2'], (34.0, 296.0, 3.0): ['2'], (18.0, 304.0, 15.0): ['1'], (24.0, 293.0, -2.0): ['1'], (26.0, 290.0, 15.0): ['-2'], (35.0, 292.0, 5.0): ['-2'], (34.0, 305.0, 9.0): ['-2'], (21.0, 301.0, 21.0): ['2'], (20.0, 302.0, 29.0): ['2'], (31.0, 291.0, -1.0): ['-2'], (26.0, 309.0, 16.0): ['-2'], (29.0, 291.0, 1.0): ['-2'], (28.0, 292.0, 4.0): ['-2'], (15.0, 309.0, 27.0): ['-2'], (24.0, 292.0, -1.0): ['1'], (28.0, 288.0, 9.0): ['2'], (31.0, 302.0, 10.0): ['1'], (35.0, 295.0, 1.0): ['2'], (20.0, 306.0, 27.0): ['-2'], (32.0, 292.0, 3.0): ['-2'], (16.0, 300.0, 23.0): ['-2'], (25.0, 291.0, 4.0): ['2'], (27.0, 304.0, 4.0): ['-2'], (27.0, 294.0, 15.0): ['-2'], (21.0, 297.0, 3.0): ['2'], (19.0, 305.0, 17.0): ['-2'], (30.0, 306.0, 1.0): ['1'], (37.0, 291.0, 5.0): ['1'], (21.0, 306.0, 29.0): ['2'], (22.0, 292.0, -1.0): ['2'], (29.0, 295.0, 6.0): ['2'], (34.0, 294.0, 8.0): ['1'], (25.0, 293.0, 18.0): ['-2'], (26.0, 288.0, 16.0): ['2'], (22.0, 296.0, 19.0): ['-2'], (24.0, 294.0, 0.0): ['-2'], (20.0, 296.0, 22.0): ['-2'], (20.0, 294.0, 3.0): ['2'], (19.0, 301.0, 30.0): ['2'], (25.0, 310.0, 23.0): ['2'], (29.0, 298.0, -1.0): ['2'], (31.0, 305.0, 11.0): ['-2'], (21.0, 310.0, 16.0): ['-2'], (36.0, 292.0, 10.0): ['1'], (28.0, 290.0, 6.0): ['2'], (18.0, 306.0, 9.0): ['-2'], (31.0, 296.0, 5.0): ['2'], (26.0, 292.0, 13.0): ['-2'], (30.0, 303.0, 13.0): ['2'], (16.0, 302.0, 24.0): ['-2'], (34.0, 307.0, 11.0): ['1'], (21.0, 303.0, 27.0): ['2'], (12.0, 302.0, 27.0): ['2'], (23.0, 300.0, 24.0): ['2'], (26.0, 311.0, 18.0): ['1'], (25.0, 312.0, 15.0): ['2'], (37.0, 289.0, 10.0): ['2'], (21.0, 304.0, 4.0): ['2'], (36.0, 290.0, 2.0): ['-2'], (29.0, 293.0, 3.0): ['-2'], (28.0, 312.0, 14.0): ['2'], (15.0, 299.0, 25.0): ['-2'], (28.0, 294.0, 11.0): ['1'], (17.0, 297.0, 29.0): ['1'], (32.0, 290.0, 1.0): ['-2'], (23.0, 306.0, 21.0): ['-2'], (25.0, 299.0, 17.0): ['2'], (16.0, 298.0, 21.0): ['1'], (21.0, 291.0, 16.0): ['2'], (27.0, 292.0, 13.0): ['-2'], (26.0, 305.0, 2.0): ['-2'], (37.0, 293.0, 7.0): ['2'], (24.0, 311.0, 18.0): ['1'], (34.0, 311.0, 13.0): ['2'], (21.0, 308.0, 27.0): ['1'], (28.0, 288.0, 4.0): ['2'], (23.0, 289.0, 14.0): ['2'], (29.0, 297.0, 4.0): ['2'], (34.0, 296.0, 10.0): ['2'], (17.0, 309.0, 14.0): ['1'], (30.0, 301.0, 4.0): ['1'], (25.0, 295.0, 14.0): ['-2'], (29.0, 304.0, 12.0): ['1'], (28.0, 307.0, 20.0): ['2'], (33.0, 308.0, 6.0): ['1'], (21.0, 310.0, 26.0): ['1'], (32.0, 311.0, 14.0): ['2'], (19.0, 299.0, 28.0): ['-2'], (25.0, 304.0, 17.0): ['1'], (31.0, 311.0, 9.0): ['1'], (21.0, 296.0, 14.0): ['2'], (28.0, 288.0, 0.0): ['2'], (18.0, 308.0, 11.0): ['1'], (31.0, 302.0, 3.0): ['1'], (30.0, 297.0, 11.0): ['2'], (25.0, 297.0, 22.0): ['2'], (16.0, 300.0, 30.0): ['2'], (13.0, 303.0, 29.0): ['2'], (29.0, 308.0, 19.0): ['2'], (34.0, 309.0, 13.0): ['2'], (29.0, 300.0, 6.0): ['2'], (27.0, 294.0, 4.0): ['-2'], (30.0, 311.0, 9.0): ['-2'], (26.0, 297.0, 12.0): ['2'], (21.0, 306.0, 6.0): ['1'], (36.0, 288.0, 4.0): ['1'], (23.0, 291.0, 3.0): ['2'], (29.0, 295.0, 13.0): ['-2'], (18.0, 302.0, 27.0): ['-2'], (28.0, 292.0, 5.0): ['1'], (31.0, 290.0, 6.0): ['-2'], (22.0, 309.0, 17.0): ['-2'], (32.0, 288.0, 15.0): ['2'], (35.0, 294.0, 0.0): ['1'], (23.0, 304.0, 24.0): ['2'], (22.0, 296.0, 26.0): ['1'], (24.0, 294.0, 11.0): ['2'], (21.0, 293.0, 18.0): ['-2'], (27.0, 290.0, 11.0): ['1'], (26.0, 307.0, 0.0): ['2'], (28.0, 291.0, -1.0): ['1'], (19.0, 301.0, 21.0): ['2'], (25.0, 310.0, 24.0): ['2'], (24.0, 299.0, 20.0): ['2'], (24.0, 309.0, 16.0): ['-2'], (21.0, 310.0, 25.0): ['1']}

    sparse2 = sparse1.copy()
    subCubes = tuple(np.arange(-6,7))          
    cube = np.array(list(itertools.product(subCubes, subCubes, subCubes)))
    for i in cube:
       for d in sparse1:
           coor = float(sparse1.get(d)[0])
           if coor > 0 :
               value=0#float(sparse1.get(tuple(d))[0])
               for c in cube:
                   value = value + max(0,float(sparse1.get(tuple(d+c),"0")[0]))               
               procentValue = coor/float(value)
               sparse2[d] = [str(procentValue)]   
           else:
               sparse2[d] = [str(0.0)]

While your solution is clever, it adds unnecessary processing due to treating each key in sparse1 as being completely unrelated to every other key except through the makeshift lookup table cube . 尽管您的解决方案很聪明,但由于将sparse1每个键sparse1视为与其他所有键完全无关,因此除了通过sparse1查找表cube之外,它都增加了不必要的处理。 If you preprocess sparse1 to calculate the Chebyshev distances between the geometric values described by the keys, you can use that to find the neighboring values and sum them. 如果对sparse1进行预处理以计算按键所描述的几何值之间的切比雪夫距离 ,则可以使用该值来找到相邻的值并将它们求和。 This eliminates the loop over cube array, replacing it with a more efficient index and sum. 这消除了在循环cube阵列,具有更有效的索引和总和取代它。

This is an attempt to replicate your code using radius_neighbors_graph from sklearn.neighbors: 这是使用复制你的代码试图radius_neighbors_graph从sklearn.neighbors:

import numpy as np
from sklearn.neighbors import radius_neighbors_graph

sparse1 = ...

X = np.array([list(k) for k in sparse1.keys()])
N = radius_neighbors_graph(X, radius=6, metric='chebyshev').astype(bool)

Y = np.array([max(0, float(v[0])) for v in sparse1.values()])
sparse2 = {}
for x, y, n in zip(X, Y, N):
    t = tuple(x)
    if sparse1[t][0][0] == '-':  # check the string value rather than converting
        sparse2[t] = sparse1[t]
    else:
        v = Y[np.squeeze(n.toarray())].sum()  # eliminates looping over cube
        v += y  # radius_neighbors_graph does not consider a point a neighbor of itself
        v += y  # potential bug in OP?  See below.
        if v <= 0:
            p = 1.
        else:
            p = y / v
        sparse2[t] = [str(p)]

There are a few things here of note: 还有一个值得注意的几件事情在这里:

  1. I convert the tuple keys into the numpy array X , which costs memory. 我转换的元组键进入numpy的阵列X ,它的价格记忆。
  2. I perform the neighbor calculations up front and store them, which costs memory (though N is sparse, so not too much). 我先执行邻居计算并将其存储,这会占用内存(尽管N很少,所以不会太多)。
  3. I convert the values of the map into floats once and store them in Y , which also costs memory, but saves having to repeatedly perform the conversions. 我将映射的值一次转换为浮点数,然后将它们存储在Y ,这也占用了内存,但省去了重复执行转换的麻烦。
  4. The line marked as a potential bug replicates a feature of your algorithm: value=float(sparse1.get(tuple(d))[0]) replicates the adding the value from (0,0,0) when looping over cube , so the OP adds this value twice. 标记为潜在错误的那行复制了您算法的功能: value=float(sparse1.get(tuple(d))[0])复制了遍历cube时从(0,0,0)的加法值,因此OP将该值相加两次。

Running some simple benchmarks, this version of the algorithm runs about 70 times faster, give or take. 通过运行一些简单的基准测试,此版本的算法运行速度(给予或接受)大约快70倍。 My guess is most of that comes from the removal of the loop over cube . 我的猜测大部分来自删除cube上的循环。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM