Related
I'm trying to speed up my calculation of calculating pairwise values. However, I'm having trouble visualizing how to do this in NumPy without being overly verbose and explicit.
Is there a way to use vector multiplication or broadcasting to speed up this operation?
Essentially what I'm doing is iterating through each row, then through all combinations of features in each row, and then taking the ratio (followed by log transform).
import pandas as pd
import numpy as np
from collections import *
# Get data
data = {'BC0a': {'1001.2': 4122,'1010.1': 6766,'1010.2': 11734,'1018.2': 5805,'1021.2': 9504,'1026.1': 18111,'1026.2': 1350,'1029.1': 3638,'1029.2': 3967,'1048.2': 9225,'1049.2': 8670,'1057.2': 21796,'1065.1': 5117,'1065.2': 7811,'1067.1': 17563,'1067.2': 8418,'1071.1': 10088,'1071.2': 9878,'1072.1': 13159,'1072.2': 8358,'1073.1': 5368,'1073.2': 2615,'1080.2': 9397,'1086.2': 1303,'2018.1': 2414,'2018.2': 1771,'2028.2': 13710,'2031.2': 3264,'2034.1': 10264,'2034.2': 4670,'2036.1': 7219,'2050.2': 11707,'3038.1': 1388,'3038.2': 13104,'3041.1': 4085,'3041.2': 9160,'3042.1': 8629,'3042.2': 11298,'A-1501-03.A': 4083,'A-1501-03.B': 7952,'A-1505-99.A': 4714,'A-1505-99.B': 4938,'A-1507-168.B': 9238,'M-1506-106.B': 10223,'M-1506-108.A': 2704,'M-1506-108.B': 5056,'M-1506-110.A': 20939,'M-1506-110.B': 18435,'M-1507-113.B': 23343,'M-1507-118.A': 5895,'M-1507-118.B': 5758,'M-1507-119.A': 4404,'M-1507-119.B': 5842,'M-1507-120.A': 9952,'M-1507-120.B': 7825,'M-1507-124.A': 12450,'M-1507-125.B': 28041,'M-1507-126.A': 7669,'M-1507-133.B': 4964,'M-1507-134.B': 1380,'M-1507-136.A': 5557,'M-1507-147.A': 3113,'M-1507-147.B': 1340,'M-1507-154.A': 2848,'M-1507-154.B': 17416,'M-1507-155.A': 5535,'M-1507-155.B': 3792,'S-1409-41.B': 3476,'S-1409-52.B': 3584,'S-1409-57.A': 2065,'S-1410-61.B': 10758,'S-1410-62.A': 5886,'S-1410-62.B': 7680,'S-1410-67.A': 4104,'S-1410-70.A': 2970,'S-1410-70.B': 9990,'S-1410-71.A': 14024,'S-1410-71.B': 11120,'S-1410-72.A': 9835,'S-1410-72.B': 7124,'S-1410-75.A': 1693,'S-1410-75.B': 3334,'S-1410-76.A': 3849,'S-1410-76.B': 12268,'S-1410-84.A': 16988,'S-1410-84.B': 19123,'S-1504-86.A': 15359},'BC0b': {'1001.2': 5935,'1010.1': 34253,'1010.2': 8016,'1018.2': 7557,'1021.2': 25213,'1026.1': 5462,'1026.2': 742,'1029.1': 11265,'1029.2': 2133,'1048.2': 7906,'1049.2': 27375,'1057.2': 7353,'1065.1': 8505,'1065.2': 18643,'1067.1': 22754,'1067.2': 7441,'1071.1': 15505,'1071.2': 18355,'1072.1': 9349,'1072.2': 21841,'1073.1': 8474,'1073.2': 6273,'1080.2': 7820,'1086.2': 2231,'2018.1': 1978,'2018.2': 6516,'2028.2': 12559,'2031.2': 2537,'2034.1': 13301,'2034.2': 3128,'2036.1': 12757,'2050.2': 17553,'3038.1': 1286,'3038.2': 13197,'3041.1': 11141,'3041.2': 19938,'3042.1': 11437,'3042.2': 12662,'A-1501-03.A': 36185,'A-1501-03.B': 38130,'A-1505-99.A': 16054,'A-1505-99.B': 14821,'A-1507-168.B': 26276,'M-1506-106.B': 9390,'M-1506-108.A': 8822,'M-1506-108.B': 10076,'M-1506-110.A': 32208,'M-1506-110.B': 31193,'M-1507-113.B': 72979,'M-1507-118.A': 5367,'M-1507-118.B': 4684,'M-1507-119.A': 66612,'M-1507-119.B': 13348,'M-1507-120.A': 33713,'M-1507-120.B': 8025,'M-1507-124.A': 16145,'M-1507-125.B': 28633,'M-1507-126.A': 18907,'M-1507-133.B': 4885,'M-1507-134.B': 42507,'M-1507-136.A': 17102,'M-1507-147.A': 5015,'M-1507-147.B': 114078,'M-1507-154.A': 16207,'M-1507-154.B': 12175,'M-1507-155.A': 9714,'M-1507-155.B': 14034,'S-1409-41.B': 4865,'S-1409-52.B': 2994,'S-1409-57.A': 3328,'S-1410-61.B': 12545,'S-1410-62.A': 12655,'S-1410-62.B': 3591,'S-1410-67.A': 3109,'S-1410-70.A': 14745,'S-1410-70.B': 16226,'S-1410-71.A': 3979,'S-1410-71.B': 2415,'S-1410-72.A': 25956,'S-1410-72.B': 19851,'S-1410-75.A': 5027,'S-1410-75.B': 1560,'S-1410-76.A': 28781,'S-1410-76.B': 11400,'S-1410-84.A': 15048,'S-1410-84.B': 5149,'S-1504-86.A': 15101},'BC1': {'1001.2': 289306,'1010.1': 161496,'1010.2': 69964,'1018.2': 47739,'1021.2': 124848,'1026.1': 68643,'1026.2': 17652,'1029.1': 35123,'1029.2': 131421,'1048.2': 178320,'1049.2': 97477,'1057.2': 72803,'1065.1': 66952,'1065.2': 116619,'1067.1': 70103,'1067.2': 60019,'1071.1': 355549,'1071.2': 253142,'1072.1': 72188,'1072.2': 181514,'1073.1': 132905,'1073.2': 123315,'1080.2': 105312,'1086.2': 26294,'2018.1': 63484,'2018.2': 341118,'2028.2': 65058,'2031.2': 140441,'2034.1': 84939,'2034.2': 49515,'2036.1': 145002,'2050.2': 103857,'3038.1': 13941,'3038.2': 79499,'3041.1': 42726,'3041.2': 124551,'3042.1': 40899,'3042.2': 40565,'A-1501-03.A': 137056,'A-1501-03.B': 166252,'A-1505-99.A': 202502,'A-1505-99.B': 61795,'A-1507-168.B': 144826,'M-1506-106.B': 92482,'M-1506-108.A': 134468,'M-1506-108.B': 167032,'M-1506-110.A': 101216,'M-1506-110.B': 100130,'M-1507-113.B': 156389,'M-1507-118.A': 178511,'M-1507-118.B': 114049,'M-1507-119.A': 127491,'M-1507-119.B': 96218,'M-1507-120.A': 227099,'M-1507-120.B': 97021,'M-1507-124.A': 63121,'M-1507-125.B': 126164,'M-1507-126.A': 62577,'M-1507-133.B': 33532,'M-1507-134.B': 104437,'M-1507-136.A': 25218,'M-1507-147.A': 180090,'M-1507-147.B': 67312,'M-1507-154.A': 78943,'M-1507-154.B': 73775,'M-1507-155.A': 111586,'M-1507-155.B': 146237,'S-1409-41.B': 134373,'S-1409-52.B': 122611,'S-1409-57.A': 139990,'S-1410-61.B': 326334,'S-1410-62.A': 360458,'S-1410-62.B': 448642,'S-1410-67.A': 250696,'S-1410-70.A': 72425,'S-1410-70.B': 192340,'S-1410-71.A': 113476,'S-1410-71.B': 160124,'S-1410-72.A': 82178,'S-1410-72.B': 118238,'S-1410-75.A': 70348,'S-1410-75.B': 277236,'S-1410-76.A': 130693,'S-1410-76.B': 256835,'S-1410-84.A': 103621,'S-1410-84.B': 56052,'S-1504-86.A': 108613},'BC10': {'1001.2': 33754,'1010.1': 16476,'1010.2': 8935,'1018.2': 1221,'1021.2': 80753,'1026.1': 716,'1026.2': 334,'1029.1': 1008,'1029.2': 1008,'1048.2': 8115,'1049.2': 27643,'1057.2': 6681,'1065.1': 1020,'1065.2': 1269,'1067.1': 5771,'1067.2': 1800,'1071.1': 4905,'1071.2': 6513,'1072.1': 12068,'1072.2': 22154,'1073.1': 889,'1073.2': 380,'1080.2': 5208,'1086.2': 1359,'2018.1': 1577,'2018.2': 5306,'2028.2': 2675,'2031.2': 1284,'2034.1': 2081,'2034.2': 1229,'2036.1': 2548,'2050.2': 4020,'3038.1': 334,'3038.2': 943,'3041.1': 429,'3041.2': 3665,'3042.1': 845,'3042.2': 1700,'A-1501-03.A': 2357,'A-1501-03.B': 2798,'A-1505-99.A': 3528,'A-1505-99.B': 1093,'A-1507-168.B': 13760,'M-1506-106.B': 2380,'M-1506-108.A': 8673,'M-1506-108.B': 7305,'M-1506-110.A': 12250,'M-1506-110.B': 9403,'M-1507-113.B': 56811,'M-1507-118.A': 9763,'M-1507-118.B': 3591,'M-1507-119.A': 15052,'M-1507-119.B': 2156,'M-1507-120.A': 13108,'M-1507-120.B': 3872,'M-1507-124.A': 5413,'M-1507-125.B': 15676,'M-1507-126.A': 5279,'M-1507-133.B': 5271,'M-1507-134.B': 1087,'M-1507-136.A': 3641,'M-1507-147.A': 4673,'M-1507-147.B': 1316,'M-1507-154.A': 6519,'M-1507-154.B': 3229,'M-1507-155.A': 5602,'M-1507-155.B': 10569,'S-1409-41.B': 2057,'S-1409-52.B': 1593,'S-1409-57.A': 13432,'S-1410-61.B': 7979,'S-1410-62.A': 5516,'S-1410-62.B': 1783,'S-1410-67.A': 6542,'S-1410-70.A': 4102,'S-1410-70.B': 6543,'S-1410-71.A': 22790,'S-1410-71.B': 12016,'S-1410-72.A': 11283,'S-1410-72.B': 1319,'S-1410-75.A': 1938,'S-1410-75.B': 2400,'S-1410-76.A': 2461,'S-1410-76.B': 10497,'S-1410-84.A': 5015,'S-1410-84.B': 1202,'S-1504-86.A': 57069},'BC100': {'1001.2': 3286,'1010.1': 10947,'1010.2': 7687,'1018.2': 2765,'1021.2': 38762,'1026.1': 33677,'1026.2': 13154,'1029.1': 5563,'1029.2': 9174,'1048.2': 13501,'1049.2': 23698,'1057.2': 3075,'1065.1': 18148,'1065.2': 24837,'1067.1': 8748,'1067.2': 11369,'1071.1': 4122,'1071.2': 41568,'1072.1': 67820,'1072.2': 42658,'1073.1': 12069,'1073.2': 38510,'1080.2': 25111,'1086.2': 3059,'2018.1': 11287,'2018.2': 46781,'2028.2': 12872,'2031.2': 43654,'2034.1': 12293,'2034.2': 4663,'2036.1': 7529,'2050.2': 26721,'3038.1': 5641,'3038.2': 27469,'3041.1': 18354,'3041.2': 24639,'3042.1': 14481,'3042.2': 19247,'A-1501-03.A': 39180,'A-1501-03.B': 58928,'A-1505-99.A': 16431,'A-1505-99.B': 17782,'A-1507-168.B': 26221,'M-1506-106.B': 2450,'M-1506-108.A': 35528,'M-1506-108.B': 45668,'M-1506-110.A': 47633,'M-1506-110.B': 76327,'M-1507-113.B': 16337,'M-1507-118.A': 2003,'M-1507-118.B': 80529,'M-1507-119.A': 33802,'M-1507-119.B': 59446,'M-1507-120.A': 29498,'M-1507-120.B': 24406,'M-1507-124.A': 2117,'M-1507-125.B': 42188,'M-1507-126.A': 12968,'M-1507-133.B': 15551,'M-1507-134.B': 72534,'M-1507-136.A': 5452,'M-1507-147.A': 311,'M-1507-147.B': 102750,'M-1507-154.A': 18811,'M-1507-154.B': 14461,'M-1507-155.A': 4477,'M-1507-155.B': 6166,'S-1409-41.B': 3421,'S-1409-52.B': 6475,'S-1409-57.A': 140259,'S-1410-61.B': 17793,'S-1410-62.A': 74388,'S-1410-62.B': 21366,'S-1410-67.A': 5405,'S-1410-70.A': 65814,'S-1410-70.B': 155655,'S-1410-71.A': 10657,'S-1410-71.B': 8941,'S-1410-72.A': 6123,'S-1410-72.B': 24783,'S-1410-75.A': 2620,'S-1410-75.B': 337,'S-1410-76.A': 45598,'S-1410-76.B': 5859,'S-1410-84.A': 9742,'S-1410-84.B': 20088,'S-1504-86.A': 5997},'BC101': {'1001.2': 12807,'1010.1': 16089,'1010.2': 28135,'1018.2': 7601,'1021.2': 29398,'1026.1': 8161,'1026.2': 2873,'1029.1': 3002,'1029.2': 23365,'1048.2': 24211,'1049.2': 12970,'1057.2': 18223,'1065.1': 5070,'1065.2': 15738,'1067.1': 11808,'1067.2': 18002,'1071.1': 10263,'1071.2': 20660,'1072.1': 30239,'1072.2': 23493,'1073.1': 9962,'1073.2': 9259,'1080.2': 57914,'1086.2': 10509,'2018.1': 8176,'2018.2': 13699,'2028.2': 23270,'2031.2': 11688,'2034.1': 7456,'2034.2': 4892,'2036.1': 5897,'2050.2': 24212,'3038.1': 822,'3038.2': 8498,'3041.1': 3023,'3041.2': 12399,'3042.1': 3497,'3042.2': 11858,'A-1501-03.A': 2538,'A-1501-03.B': 4517,'A-1505-99.A': 3186,'A-1505-99.B': 6374,'A-1507-168.B': 9800,'M-1506-106.B': 9571,'M-1506-108.A': 7972,'M-1506-108.B': 12036,'M-1506-110.A': 21647,'M-1506-110.B': 18908,'M-1507-113.B': 13387,'M-1507-118.A': 20837,'M-1507-118.B': 35744,'M-1507-119.A': 6812,'M-1507-119.B': 13414,'M-1507-120.A': 24473,'M-1507-120.B': 9791,'M-1507-124.A': 30794,'M-1507-125.B': 29891,'M-1507-126.A': 9887,'M-1507-133.B': 7909,'M-1507-134.B': 3400,'M-1507-136.A': 5674,'M-1507-147.A': 74549,'M-1507-147.B': 2853,'M-1507-154.A': 5432,'M-1507-154.B': 23947,'M-1507-155.A': 5888,'M-1507-155.B': 4172,'S-1409-41.B': 14725,'S-1409-52.B': 19069,'S-1409-57.A': 12003,'S-1410-61.B': 21601,'S-1410-62.A': 18475,'S-1410-62.B': 21780,'S-1410-67.A': 30727,'S-1410-70.A': 3549,'S-1410-70.B': 21779,'S-1410-71.A': 185412,'S-1410-71.B': 78044,'S-1410-72.A': 8771,'S-1410-72.B': 1100,'S-1410-75.A': 6739,'S-1410-75.B': 53418,'S-1410-76.A': 24381,'S-1410-76.B': 68226,'S-1410-84.A': 12141,'S-1410-84.B': 4067,'S-1504-86.A': 12996},'BC102': {'1001.2': 11219,'1010.1': 6212,'1010.2': 5608,'1018.2': 771,'1021.2': 4191,'1026.1': 1567,'1026.2': 378,'1029.1': 584,'1029.2': 534,'1048.2': 3344,'1049.2': 27804,'1057.2': 179,'1065.1': 2359,'1065.2': 6919,'1067.1': 2335,'1067.2': 301,'1071.1': 1975,'1071.2': 4508,'1072.1': 6226,'1072.2': 6786,'1073.1': 5397,'1073.2': 1984,'1080.2': 422,'1086.2': 261,'2018.1': 700,'2018.2': 298,'2028.2': 1575,'2031.2': 4126,'2034.1': 5626,'2034.2': 972,'2036.1': 773,'2050.2': 9878,'3038.1': 462,'3038.2': 1571,'3041.1': 5059,'3041.2': 3557,'3042.1': 35644,'3042.2': 9463,'A-1501-03.A': 997,'A-1501-03.B': 8698,'A-1505-99.A': 1430,'A-1505-99.B': 2856,'A-1507-168.B': 2674,'M-1506-106.B': 380,'M-1506-108.A': 6261,'M-1506-108.B': 4569,'M-1506-110.A': 3987,'M-1506-110.B': 2212,'M-1507-113.B': 391,'M-1507-118.A': 411,'M-1507-118.B': 2462,'M-1507-119.A': 2932,'M-1507-119.B': 5597,'M-1507-120.A': 8961,'M-1507-120.B': 4788,'M-1507-124.A': 2628,'M-1507-125.B': 2169,'M-1507-126.A': 3431,'M-1507-133.B': 4702,'M-1507-134.B': 667,'M-1507-136.A': 5041,'M-1507-147.A': 152,'M-1507-147.B': 819,'M-1507-154.A': 233,'M-1507-154.B': 8963,'M-1507-155.A': 2123,'M-1507-155.B': 1654,'S-1409-41.B': 1625,'S-1409-52.B': 2464,'S-1409-57.A': 3097,'S-1410-61.B': 4198,'S-1410-62.A': 4627,'S-1410-62.B': 652,'S-1410-67.A': 682,'S-1410-70.A': 1507,'S-1410-70.B': 2692,'S-1410-71.A': 8955,'S-1410-71.B': 3057,'S-1410-72.A': 5078,'S-1410-72.B': 4436,'S-1410-75.A': 2217,'S-1410-75.B': 76,'S-1410-76.A': 2578,'S-1410-76.B': 1591,'S-1410-84.A': 430,'S-1410-84.B': 3293,'S-1504-86.A': 1022},'BC103': {'1001.2': 47,'1010.1': 4121,'1010.2': 2117,'1018.2': 2568,'1021.2': 25001,'1026.1': 2212,'1026.2': 1535,'1029.1': 4580,'1029.2': 1152,'1048.2': 8221,'1049.2': 27980,'1057.2': 1380,'1065.1': 11013,'1065.2': 23179,'1067.1': 3541,'1067.2': 2241,'1071.1': 9779,'1071.2': 15474,'1072.1': 8528,'1072.2': 10340,'1073.1': 5147,'1073.2': 14658,'1080.2': 2112,'1086.2': 406,'2018.1': 5170,'2018.2': 30349,'2028.2': 6192,'2031.2': 5190,'2034.1': 12565,'2034.2': 2884,'2036.1': 6942,'2050.2': 17112,'3038.1': 540,'3038.2': 11259,'3041.1': 4713,'3041.2': 14039,'3042.1': 31265,'3042.2': 23319,'A-1501-03.A': 6818,'A-1501-03.B': 14055,'A-1505-99.A': 8326,'A-1505-99.B': 9245,'A-1507-168.B': 6074,'M-1506-106.B': 2501,'M-1506-108.A': 19348,'M-1506-108.B': 40895,'M-1506-110.A': 26179,'M-1506-110.B': 10612,'M-1507-113.B': 7493,'M-1507-118.A': 2033,'M-1507-118.B': 17832,'M-1507-119.A': 69373,'M-1507-119.B': 10119,'M-1507-120.A': 10813,'M-1507-120.B': 1515,'M-1507-124.A': 5464,'M-1507-125.B': 3609,'M-1507-126.A': 1456,'M-1507-133.B': 6704,'M-1507-134.B': 22506,'M-1507-136.A': 2103,'M-1507-147.A': 2314,'M-1507-147.B': 40975,'M-1507-154.A': 10102,'M-1507-154.B': 9181,'M-1507-155.A': 8766,'M-1507-155.B': 3702,'S-1409-41.B': 2009,'S-1409-52.B': 4647,'S-1409-57.A': 12433,'S-1410-61.B': 10995,'S-1410-62.A': 24021,'S-1410-62.B': 15828,'S-1410-67.A': 1468,'S-1410-70.A': 28608,'S-1410-70.B': 24070,'S-1410-71.A': 9840,'S-1410-71.B': 1263,'S-1410-72.A': 10484,'S-1410-72.B': 13375,'S-1410-75.A': 3502,'S-1410-75.B': 207,'S-1410-76.A': 14776,'S-1410-76.B': 659,'S-1410-84.A': 9886,'S-1410-84.B': 8042,'S-1504-86.A': 14472},'BC104': {'1001.2': 0,'1010.1': 35,'1010.2': 313,'1018.2': 35,'1021.2': 9,'1026.1': 288,'1026.2': 1,'1029.1': 0,'1029.2': 0,'1048.2': 12,'1049.2': 415,'1057.2': 37,'1065.1': 162,'1065.2': 241,'1067.1': 47,'1067.2': 22,'1071.1': 6,'1071.2': 0,'1072.1': 1,'1072.2': 7,'1073.1': 1,'1073.2': 0,'1080.2': 0,'1086.2': 0,'2018.1': 10,'2018.2': 129,'2028.2': 127,'2031.2': 57,'2034.1': 12,'2034.2': 0,'2036.1': 64,'2050.2': 64,'3038.1': 0,'3038.2': 449,'3041.1': 0,'3041.2': 20,'3042.1': 3,'3042.2': 7620,'A-1501-03.A': 21,'A-1501-03.B': 49,'A-1505-99.A': 17,'A-1505-99.B': 0,'A-1507-168.B': 0,'M-1506-106.B': 0,'M-1506-108.A': 767,'M-1506-108.B': 407,'M-1506-110.A': 1,'M-1506-110.B': 3,'M-1507-113.B': 2069,'M-1507-118.A': 2,'M-1507-118.B': 110,'M-1507-119.A': 3081,'M-1507-119.B': 1222,'M-1507-120.A': 1340,'M-1507-120.B': 151,'M-1507-124.A': 0,'M-1507-125.B': 1,'M-1507-126.A': 0,'M-1507-133.B': 1012,'M-1507-134.B': 8,'M-1507-136.A': 8,'M-1507-147.A': 385,'M-1507-147.B': 4,'M-1507-154.A': 205,'M-1507-154.B': 24,'M-1507-155.A': 0,'M-1507-155.B': 349,'S-1409-41.B': 3,'S-1409-52.B': 640,'S-1409-57.A': 3,'S-1410-61.B': 3,'S-1410-62.A': 226,'S-1410-62.B': 12,'S-1410-67.A': 33,'S-1410-70.A': 0,'S-1410-70.B': 2965,'S-1410-71.A': 0,'S-1410-71.B': 0,'S-1410-72.A': 4,'S-1410-72.B': 54,'S-1410-75.A': 218,'S-1410-75.B': 170,'S-1410-76.A': 99,'S-1410-76.B': 481,'S-1410-84.A': 0,'S-1410-84.B': 9,'S-1504-86.A': 0},'BC105': {'1001.2': 0,'1010.1': 3813,'1010.2': 5828,'1018.2': 232,'1021.2': 412,'1026.1': 4,'1026.2': 13,'1029.1': 95,'1029.2': 8,'1048.2': 2127,'1049.2': 9956,'1057.2': 18,'1065.1': 469,'1065.2': 2961,'1067.1': 988,'1067.2': 439,'1071.1': 14668,'1071.2': 3951,'1072.1': 415,'1072.2': 182,'1073.1': 1288,'1073.2': 74,'1080.2': 122,'1086.2': 696,'2018.1': 21,'2018.2': 173,'2028.2': 3865,'2031.2': 59,'2034.1': 1097,'2034.2': 255,'2036.1': 1710,'2050.2': 8870,'3038.1': 307,'3038.2': 445,'3041.1': 984,'3041.2': 5100,'3042.1': 1481,'3042.2': 139,'A-1501-03.A': 351,'A-1501-03.B': 1183,'A-1505-99.A': 11933,'A-1505-99.B': 4359,'A-1507-168.B': 8246,'M-1506-106.B': 980,'M-1506-108.A': 699,'M-1506-108.B': 4191,'M-1506-110.A': 3477,'M-1506-110.B': 36780,'M-1507-113.B': 8960,'M-1507-118.A': 543,'M-1507-118.B': 324,'M-1507-119.A': 7886,'M-1507-119.B': 1448,'M-1507-120.A': 15303,'M-1507-120.B': 1669,'M-1507-124.A': 9443,'M-1507-125.B': 3264,'M-1507-126.A': 430,'M-1507-133.B': 176,'M-1507-134.B': 952,'M-1507-136.A': 163,'M-1507-147.A': 310,'M-1507-147.B': 16023,'M-1507-154.A': 1068,'M-1507-154.B': 2703,'M-1507-155.A': 71589,'M-1507-155.B': 351,'S-1409-41.B': 956,'S-1409-52.B': 85,'S-1409-57.A': 168,'S-1410-61.B': 1492,'S-1410-62.A': 1215,'S-1410-62.B': 562,'S-1410-67.A': 218,'S-1410-70.A': 925,'S-1410-70.B': 643,'S-1410-71.A': 87,'S-1410-71.B': 428,'S-1410-72.A': 232,'S-1410-72.B': 973,'S-1410-75.A': 11737,'S-1410-75.B': 21,'S-1410-76.A': 1169,'S-1410-76.B': 1833,'S-1410-84.A': 108,'S-1410-84.B': 6469,'S-1504-86.A': 13818}}
X = pd.DataFrame(data) + 1
# Shape of X where rows are samples and columns are features
n,m = X.shape
index_samples = X.index
index_features = X.columns
X_values = X.values
# A = np.empty((n, m**2 - m))
# A[:] = np.nan
# Not the most efficient way but the best way to show what I'm trying to do
A = defaultdict(dict)
for row_pos, a in enumerate(X_values):
id_sample = index_samples[row_pos]
# col_pos = 0
for i in range(m):
a_i = a[i]
i_feature = index_features[i]
for j in range( m):
if i != j:
a_j = a[j]
j_feature = index_features[j]
# A[row_pos, col_pos] = a_i - a_j
A[id_sample]["{}/{}".format(i_feature, j_feature)] = np.log(a_i/a_j)
# col_pos += 1
df = pd.DataFrame(A).T
df.iloc[:5,:5]
BC0a/BC0b BC0a/BC1 BC0a/BC10 BC0a/BC100 BC0a/BC101
1001.2 -0.364454 -4.250907 -2.102548 0.226606 -1.133489
1010.1 -1.621745 -3.172429 -0.889908 -0.481099 -0.866140
1010.2 0.381012 -1.785419 0.272488 0.422915 -0.874474
1018.2 -0.263715 -2.106878 1.558403 0.741490 -0.269520
1021.2 -0.975581 -2.575287 -2.139590 -1.405648 -1.129143
You can use np.triu_indices to get all possible combinations by indexing thusly:
i_index, j_index = np.triu_indices(m, 1)
Using the fact that logarithms were invented to be able to do log(a / b) = log(a) - log(b), you can now do:
df = pd.DataFrame(data=np.log(X_values[:, i_index]) - np.log(X_values[:, j_index]),
index=X.index.copy(), columns=[f'{a}/{b}' for a, b in zip(X.columns[i_index], X.columns[j_index])])
While it's more elegant in my opinion, you can just as easily keep the original formulation of the ratio:
np.log(X_values[:, i_index] / X_values[:, j_index])
Notice that this is half of the data that your answer contains. The other half is just the inverse ratios, which appear as negatives in the log table. If you absolutely insist on computing those (even though it's quite wasteful), you can adjust i_index and j_index accordingly:
i_index = np.repeat(np.arange(m), m - 1)
j_index = np.delete(np.tile(np.arange(m), m), slice(None, None, m + 1))
Either way, your entire code can be expressed in about three lines.
I have an array with shape = (2, 257) and want to use each column which are vectors of shape = (2,) to create an array with shape = (2, 2) for each column.
Previously I did this by selecting each column by iterating through my input array
import numpy as np
for counter in input
x = np.array([input[0][counter], input[1][counter]])
y = np.conj(x)
y = y.T
E = x[:, None] * y
corr_matr = np.where(self.iterator == 1, E, alpha * self.altes_E[counter] + (1 - alpha) * E)
self.altes_E[counter] = corr_matr
However this is very slow and I would like to vectorize this calculation so in the end I will have an array containing my E variables for each column of my input variable. I tried to do so but I get broadcasting errors I am not able to solve.. So it will be great if someone helped me out!
Self.iterator will be removed and replaced by the first element of the new array containing all E arrays
My goal now is to have an Array which has the shape (257, 2, 2) and contains 257 2x2 corr_matr matrices. The n-th corr_matr depends on the n-1-th
Self.altes_E is this matrix I am looking for but I cant create it with my vectorized approach. Maybe you guys have an idea how I can create it vectorized without a for-loop.
Test data:
input = array([[ 3.94351315e-02+0.00000000e+00j, -1.50913336e-02+6.03795651e-04j,\n 1.99272113e-04-8.07005910e-04j, -4.67793985e-04+8.33903992e-04j,\n -2.64236148e-03+2.77521785e-05j, -1.49792915e-03+7.36359583e-04j,\n 1.50533594e-03-6.15859179e-04j, -6.54810392e-05-5.01831397e-04j,\n -1.01095434e-03-1.70553920e-04j, 5.81738784e-04+7.12800200e-04j,\n -3.11310287e-04-9.01545559e-04j, -5.86002908e-05-9.55615603e-04j,\n 1.44156235e-04+1.09251279e-03j, -4.87454341e-04+8.03194960e-05j,\n 3.78562845e-04+1.29788540e-04j, -4.87558912e-04+6.55677040e-04j,\n -4.87274113e-04-8.31101470e-04j, 8.16597471e-04+3.81774926e-04j,\n 5.89999582e-04-7.40645680e-05j, -7.03418446e-05-4.16067625e-04j,\n -1.02284759e-03+2.56541860e-04j, -7.25162530e-05-2.12897828e-04j,\n 2.86242195e-04+2.15252463e-04j, -6.97098238e-04-5.35675945e-04j,\n 4.49257188e-04+4.96744002e-04j, 2.86015111e-04+9.92285825e-05j,\n -6.63212048e-05-1.97287145e-04j, -4.96012767e-05+1.68083300e-04j,\n -3.68913382e-04-1.76126405e-04j, 3.05618600e-04-2.13305860e-05j,\n -1.22923172e-04-3.58717400e-04j, -3.92479536e-04+1.02063591e-03j,\n 6.45622389e-04-8.53094144e-04j, -3.14203107e-04+1.47936574e-04j,\n 1.54020776e-05-2.45868608e-05j, 2.78312174e-04+2.11224838e-04j,\n -1.70668244e-04+4.57545662e-04j, 1.89143085e-04-1.62612861e-04j,\n -5.05276967e-04-7.33565277e-04j, 3.87931183e-04+6.84968797e-05j,\n -3.88693353e-04+3.29574348e-04j, 1.88775042e-05+3.06450544e-04j,\n -1.02881416e-04-6.28814378e-04j, 1.50437664e-04-4.64790639e-05j,\n 6.80136794e-05+7.07755678e-04j, 4.29081846e-04-6.60769121e-06j,\n -3.89883869e-05+9.94456323e-05j, -2.88405737e-04-3.90610565e-04j,\n 2.89706554e-04-4.61313935e-04j, 8.53534820e-05+3.45993148e-04j,\n -6.48341994e-04+1.61728688e-05j, 7.08075756e-04+4.18876357e-04j,\n -2.41676738e-04-6.57686042e-04j, -4.52960231e-05+4.69549856e-04j,\n -2.98667220e-04+3.69428944e-04j, 3.09898762e-04-5.55573884e-04j,\n 3.16198618e-05-2.18262971e-04j, -2.43962041e-05+6.14800458e-04j,\n 1.22281179e-04-4.27259031e-05j, -2.02764807e-04-3.04080095e-04j,\n -2.60131161e-05-2.72507038e-04j, -3.48411552e-04-1.65106382e-04j,\n 3.60262912e-04+1.15929180e-03j, 6.44188577e-04-6.57529271e-04j,\n -6.64472633e-04+7.34788284e-05j, -2.53962823e-04-8.64577990e-05j,\n 4.12548109e-04-4.30493761e-04j, -3.89652217e-04+1.10427049e-03j,\n 2.19613546e-04-6.47350601e-04j, -3.01855256e-04-1.70512519e-04j,\n 3.52232474e-04-5.97253780e-04j, -2.46726574e-04+8.25901553e-05j,\n 5.01323354e-04+4.97268616e-04j, -3.87112186e-04+2.31773757e-04j,\n -5.51833095e-04+5.28582216e-04j, 1.52037945e-04-9.87780746e-04j,\n 1.80951699e-04-1.64293165e-04j, 9.81853960e-04+8.24736454e-04j,\n -3.08003998e-04+4.10959821e-05j, -7.18984896e-04+9.56216393e-05j,\n 9.23759625e-06-5.16799160e-04j, 3.62720586e-04-4.27438243e-04j,\n 4.54825689e-04+6.60654467e-04j, -8.45689094e-04+3.84855215e-04j,\n -2.67177134e-05-7.42870583e-04j, -1.92531972e-04-3.02859614e-04j,\n 7.90923909e-04+2.13245532e-04j, -7.46436347e-04+4.59160357e-04j,\n 2.42516064e-04+5.79452623e-04j, 2.76445921e-04-1.07193028e-03j,\n -5.72620393e-04+1.96574790e-04j, 1.34058726e-03+2.34585361e-04j,\n -3.57420647e-04-1.70451007e-04j, -2.82518121e-04+2.07459060e-04j,\n -4.28715245e-05-3.11639838e-04j, -2.31994207e-04-3.50393413e-04j,\n 1.79812526e-04+9.46405559e-06j, 6.45826858e-06-7.27244722e-05j,\n -6.54773349e-04+9.59387600e-04j, 3.84335152e-04-2.72657471e-04j,\n -4.87276832e-04-3.96541032e-04j, 6.77017368e-04+4.47778225e-04j,\n 4.45890499e-04+1.29559357e-04j, -6.51094304e-04+1.86624435e-04j,\n 3.98953747e-05-4.33256129e-04j, -2.22111375e-04-2.04651458e-04j,\n 7.55794351e-04-5.85769035e-04j, -3.94789819e-04+1.04401607e-03j,\n -1.58222133e-04+1.11208833e-05j, 9.23486664e-05-7.75775861e-04j,\n -3.43146772e-04+1.17239880e-04j, 2.84564728e-04+6.11844429e-06j,\n 4.70805410e-04+3.47810038e-04j, 1.69658330e-04-6.84370728e-06j,\n -5.22356227e-04+6.50334373e-04j, -1.73679813e-04+3.63334974e-05j,\n 4.98834691e-04-1.01761885e-03j, 5.87157344e-04+1.20334407e-04j,\n -9.03141283e-04+5.43231194e-04j, 2.12604011e-04+4.25936737e-05j,\n 1.20421975e-04+1.99248114e-04j, 1.36291525e-04-1.74378105e-04j,\n -3.22569755e-04-3.90555480e-04j, -8.56585373e-04-1.36390458e-05j,\n 8.21771053e-04+5.11195915e-04j, -1.32706058e-04-6.24514006e-05j,\n -5.38444724e-04-7.24478095e-04j, 1.28496700e-03+2.93993678e-05j,\n -6.10961533e-04+1.10329922e-04j, -3.67753624e-04+2.46454903e-04j,\n -1.97998194e-04+4.12632455e-04j, 4.80604477e-04-2.31270841e-04j,\n 1.92777568e-05-4.07627748e-04j, 2.86666234e-04+7.01966268e-05j,\n 1.15379387e-04-1.33773849e-04j, -3.17265283e-04+5.00725722e-04j,\n 1.36599939e-04-6.75427006e-04j, 6.37731351e-05+7.04635115e-04j,\n -1.96653870e-04+3.25483834e-04j, -5.86921673e-04-1.23419379e-03j,\n 3.14143574e-04+1.00360594e-04j, 3.31560079e-04+1.07206559e-03j,\n 4.92924636e-05-5.68876368e-04j, -8.41042140e-04+3.22939688e-04j,\n 5.49575067e-04-5.15098419e-05j, -3.93180240e-04-4.42074142e-05j,\n 5.60555298e-04-2.85327349e-04j, -2.38831244e-04+1.91581065e-05j,\n -7.45519046e-05+1.27549869e-04j, -5.89318659e-05-4.21902661e-04j,\n 3.75275146e-04+1.82785513e-05j, -2.34356665e-04+4.60409956e-04j,\n -1.92879655e-04+2.25686712e-05j, 2.29916609e-04-3.98052727e-04j,\n 1.14636467e-04+3.40506254e-04j, 3.66346397e-04+4.71324904e-05j,\n -3.50054041e-04-5.13589144e-04j, -2.54987713e-04+1.99572441e-04j,\n 5.45736142e-07-5.96936864e-05j, -1.24422570e-04+9.28100584e-05j,\n -3.07471848e-04-4.81938971e-05j, 8.54350904e-04+8.01221802e-05j,\n -1.36143168e-03+1.05957395e-04j, 4.67515499e-04+1.18207109e-04j,\n 2.22224539e-04-7.87005141e-04j, 2.06836573e-04+7.33415318e-04j,\n 4.11857186e-05-2.78641304e-04j, 8.46935368e-05+2.47412699e-05j,\n 9.63134514e-05-6.25919599e-04j, -1.74852058e-04+6.83190116e-04j,\n 5.17474224e-05-2.10492739e-05j, 5.02637722e-05+8.55532060e-06j,\n -1.48067521e-04+1.27482971e-04j, -3.17709988e-04-3.19972013e-04j,\n 7.93806547e-04+7.24172271e-04j, 3.71753847e-04-2.96357705e-04j,\n -7.16045744e-04-2.45445209e-04j, 1.85488700e-04-7.80975779e-05j,\n -6.33931296e-04-3.79990485e-04j, 4.23058885e-04+5.71413970e-04j,\n -1.96111954e-04-1.64805179e-04j, -2.39387453e-04+4.67926668e-04j,\n 4.48049475e-04-4.39783397e-04j, -4.57884754e-04-8.64764107e-05j,\n 2.36198689e-04-1.81618919e-04j, -3.98041496e-04+5.49284505e-04j,\n 7.71543104e-04+2.33418707e-04j, -5.37125816e-04-1.39968077e-04j,\n 1.21667266e-04+2.94272358e-05j, -4.22621149e-05-4.03145881e-04j,\n 5.15855772e-04+6.12452186e-05j, -1.75597310e-04+3.06332086e-04j,\n 9.80239412e-05-5.22006358e-04j, -3.91105404e-04+7.24365490e-04j,\n 5.41880105e-04-6.62486843e-04j, -1.44683949e-04+2.83522226e-04j,\n -1.91702886e-04+1.31554681e-04j, 5.24558737e-05-1.08982522e-04j,\n 6.74327223e-04+1.10659354e-04j, -5.79971252e-04+1.77968960e-04j,\n -2.51067236e-04-1.07853197e-04j, 3.11939498e-04-1.38577070e-04j,\n -6.04476470e-05+8.52540539e-05j, 1.34265204e-04+8.21556997e-04j,\n -1.17370999e-04-7.91857871e-04j, 2.79181388e-04-7.31803351e-04j,\n -6.90453886e-04+7.46784829e-04j, -1.16164963e-04-4.57167257e-04j,\n -4.69417951e-04-4.41735735e-04j, 1.35998487e-05+1.12007021e-03j,\n 3.29877562e-04+1.48284571e-04j, 5.83769268e-04+2.49004599e-04j,\n -7.52982150e-04-1.68075249e-03j, -3.38846404e-04+9.63951937e-04j,\n 3.55617024e-04-1.27152987e-04j, 8.13345170e-04-4.35530692e-04j,\n 2.21017421e-04+3.81957629e-04j, -2.29169840e-04+4.31749297e-04j,\n -3.49092860e-04-5.49892781e-04j, 1.47060146e-05-4.94382810e-04j,\n 8.50208905e-04+7.47663086e-05j, -1.45781683e-04+3.05060633e-04j,\n -5.68202031e-04+2.25181950e-04j, -6.70225923e-05-4.04705073e-04j,\n 5.49901664e-04+1.00974501e-03j, 1.09107837e-05-4.09919474e-04j,\n -7.08992001e-04-6.93567150e-04j, 2.38546368e-05+3.33138967e-04j,\n 6.59087722e-04+4.15920176e-04j, -9.24723091e-04+5.72433162e-04j,\n 3.16802067e-04-1.97599886e-04j, 9.59921563e-04-7.20312263e-04j,\n -2.04590275e-04-7.58383004e-04j, -9.83521376e-04+1.39303955e-03j,\n 1.00191560e-03-1.06808718e-03j, -1.44975474e-04+2.10013067e-04j,\n 1.57355072e-04-8.06148227e-05j, -9.77899528e-04+1.01124440e-03j,\n 1.40629188e-04+3.07843307e-04j, 4.58437822e-04-1.54270986e-03j,\n -3.58655124e-06+3.05346109e-04j, -8.51095471e-04+9.50186675e-04j,\n 6.49841806e-04-3.69990669e-04j, -2.42669267e-04+1.97887318e-04j,\n 1.08560919e-03-5.57172096e-04j, -1.38075404e-03+3.06128065e-04j,\n -2.99248592e-04-1.96865567e-04j, 4.34882427e-04+6.50054051e-05j,\n 1.09891678e-03+1.84768495e-04j, -7.36401037e-04-1.54611504e-03j,\n 1.95455637e-04+1.59373547e-03j, -1.65580093e-04-1.90926799e-04j,\n -1.13688576e-04+4.12506434e-04j, 1.12544155e-03+4.77414267e-04j,\n -1.31365139e-03-1.41451042e-03j, 5.79908017e-04+1.48045447e-04j,\n -4.82300426e-04+0.00000000e+00j],\n [ 3.94302788e-02+0.00000000e+00j, -1.50769610e-02+6.62688618e-04j,\n 1.63792293e-04-9.09965691e-04j, -4.20457549e-04+7.67888474e-04j,\n -2.62611773e-03+1.79287284e-04j, -1.39992458e-03+8.62646142e-04j,\n 1.43622367e-03-8.15390156e-04j, -1.32725278e-04-5.22439730e-04j,\n -1.02788776e-03-2.84542025e-05j, 7.04202642e-04+5.75840478e-04j,\n -4.85970329e-04-8.42626503e-04j, -2.85902831e-04-9.33336830e-04j,\n 3.85726455e-04+1.01063584e-03j, -4.54619102e-04+1.89852072e-04j,\n 3.96938522e-04+4.87940712e-06j, -2.64396836e-04+7.56253750e-04j,\n -7.20372356e-04-6.38146395e-04j, 8.97949930e-04+7.69071706e-05j,\n 5.23039447e-04-3.00850918e-04j, -2.30101044e-04-3.83685318e-04j,\n -8.45559655e-04+6.25181058e-04j, -1.55582013e-04-1.67400948e-04j,\n 3.48307983e-04+6.57074044e-05j, -8.67179548e-04-1.65800957e-04j,\n 6.26144481e-04+2.31985901e-04j, 2.96950721e-04-5.87318674e-05j,\n -1.59847453e-04-1.45575757e-04j, 4.17422830e-05+1.62311722e-04j,\n -4.06935839e-04+4.60837714e-05j, 2.40112226e-04-1.88702128e-04j,\n -3.12380034e-04-2.25416888e-04j, 2.83110337e-04+1.05608322e-03j,\n 1.69140411e-06-1.07341915e-03j, -1.61321781e-04+3.08879494e-04j,\n -7.12735790e-06-2.89430707e-05j, 3.44852190e-04-2.42375302e-05j,\n 1.77649387e-04+4.46179235e-04j, 3.00653183e-05-2.58839835e-04j,\n -8.75480076e-04-1.78086957e-04j, 3.15614668e-04-2.23862597e-04j,\n -3.28856251e-05+5.09215677e-04j, 2.42580737e-04+1.91265064e-04j,\n -5.38095552e-04-3.36387834e-04j, 5.89000296e-05-1.38800403e-04j,\n 5.93041602e-04+3.91920304e-04j, 2.58312359e-04-3.52047888e-04j,\n 5.41664911e-05+7.84828629e-05j, -4.87040484e-04+2.00465143e-07j,\n -2.24098958e-04-5.00037983e-04j, 3.25842394e-04+1.16315659e-04j,\n -3.33712131e-04+5.56801846e-04j, 7.20763407e-04-3.98413351e-04j,\n -6.98367513e-04-1.21573550e-04j, 3.84986333e-04+2.61687346e-04j,\n 1.91783081e-04+4.26843301e-04j, -3.61446144e-04-5.30072060e-04j,\n -1.90875135e-04-1.22069126e-04j, 5.47142772e-04+2.66855906e-04j,\n 4.08724905e-06-1.39431456e-04j, -3.61842034e-04+7.01928105e-05j,\n -2.68947677e-04-7.08310314e-05j, -2.73181365e-04+2.79827782e-04j,\n 1.21452700e-03+1.36751774e-05j, -4.48800651e-04-8.19776275e-04j,\n -1.19266297e-04+6.53314340e-04j, -1.48156234e-04+2.20506208e-04j,\n -3.25802814e-04-5.00083015e-04j, 9.93835264e-04+6.10726403e-04j,\n -5.92139594e-04-3.50046309e-04j, -2.20551315e-04+2.66541088e-04j,\n -5.38101746e-04-4.33232244e-04j, 4.53140432e-05+2.67048105e-04j,\n 5.45584100e-04-4.36233554e-04j, 1.89055956e-04+4.05146241e-04j,\n 4.91230583e-04+5.80214622e-04j, -9.70303336e-04-2.07957665e-04j,\n -1.54105042e-04-1.78764985e-04j, 8.29454691e-04-9.70980630e-04j,\n 2.93041276e-05+2.99636023e-04j, 1.11761881e-04+7.06579832e-04j,\n -5.13429489e-04+1.03407490e-05j, -4.51280720e-04-3.28230045e-04j,\n 6.09306297e-04-5.08877271e-04j, 4.69052361e-04+7.90693011e-04j,\n -7.29652303e-04+1.16791352e-04j, -2.63583592e-04+2.40084959e-04j,\n 7.80339281e-05-8.09166755e-04j, 5.91733367e-04+6.46003586e-04j,\n 5.18742468e-04-3.69670376e-04j, -1.10819540e-03-2.98322967e-05j,\n 3.35961109e-04+5.08219023e-04j, -1.37905601e-04-1.35428372e-03j,\n -7.54841905e-05+3.93744885e-04j, 2.75316936e-04+2.05765280e-04j,\n -2.85925096e-04+1.42533512e-04j, -2.49244553e-04+3.44507517e-04j,\n -5.49966896e-05-1.61934566e-04j, -6.94024634e-05+3.21277311e-05j,\n 1.14427279e-03+2.14503629e-04j, -4.06256577e-04-2.42573698e-04j,\n -1.37008788e-04+6.09181906e-04j, 9.41131852e-05-8.08853747e-04j,\n -1.05763981e-04-4.56058417e-04j, 4.74122378e-04+4.69608012e-04j,\n -3.95122830e-04+1.79715038e-04j, -5.41657307e-05+2.94641051e-04j,\n -8.99698699e-04-3.08409334e-04j, 1.08313369e-03-2.52154929e-04j,\n 9.61555026e-05+1.16751009e-04j, -6.78396810e-04+3.85853518e-04j,\n 3.08973305e-04+2.04145634e-04j, -1.66564595e-04-2.23731139e-04j,\n -3.64032556e-05-5.80136045e-04j, -1.25077116e-04-1.17649763e-04j,\n 8.22473217e-04-5.10403627e-05j, 1.35846968e-04+9.04349589e-05j,\n -1.08069909e-03+3.56424784e-04j, -3.42539982e-04-4.83310914e-04j,\n 1.02127798e-03+2.27836680e-04j, -1.38094505e-04-1.76668123e-04j,\n 2.64921676e-05-2.31094625e-04j, -2.30827528e-04+4.76342119e-05j,\n 1.46863589e-07+5.09299510e-04j, 6.74095347e-04+5.26744232e-04j,\n -3.63377125e-04-9.05099052e-04j, 6.26177364e-05+1.23244300e-04j,\n 4.31863072e-05+8.96514097e-04j, -1.06310512e-03-7.14346108e-04j,\n 5.75517243e-04+2.35967322e-04j, 4.41244980e-04-2.65860383e-05j,\n 3.70694639e-04-2.71684200e-04j, -5.39649217e-04-2.89578543e-05j,\n -2.05237670e-04+3.55566693e-04j, -2.29304581e-04-1.82601791e-04j,\n -1.63278707e-04+8.28089685e-05j, 4.85237610e-04-3.27131430e-04j,\n -3.85198889e-04+5.78130634e-04j, 1.91039368e-04-6.73331290e-04j,\n 2.83290401e-04-2.36600115e-04j, 1.56155251e-04+1.35530242e-03j,\n -2.58546407e-04-1.92172329e-04j, -1.64289988e-05-1.12061403e-03j,\n -2.06761243e-04+5.39404115e-04j, 8.90146217e-04-1.10273389e-04j,\n -5.53419489e-04-8.01172163e-05j, 3.69800922e-04+1.19385537e-04j,\n -6.06753749e-04+1.74073233e-04j, 2.37762330e-04+2.21509768e-05j,\n 9.04367392e-05-1.17178154e-04j, 6.65735029e-06+4.23161474e-04j,\n -3.66355661e-04-5.69644277e-05j, 2.72752455e-04-4.36970254e-04j,\n 1.89332418e-04-1.28477791e-05j, -2.48450704e-04+3.85021186e-04j,\n -1.06569421e-04-3.41965661e-04j, -3.70428389e-04-4.26724302e-05j,\n 3.58595638e-04+5.14301117e-04j, 2.51269109e-04-2.08539926e-04j,\n 4.88939967e-06+5.71210855e-05j, 1.18578623e-04-1.05802288e-04j,\n 3.11344492e-04+1.01891390e-05j, -8.56978432e-04+1.74893270e-05j,\n 1.33232876e-03-2.98487141e-04j, -4.85805120e-04-5.94291833e-05j,\n -7.76562206e-05+8.03680105e-04j, -3.43743448e-04-6.84465578e-04j,\n 1.92205105e-05+2.78271830e-04j, -8.62407096e-05-6.06097609e-06j,\n 7.51468262e-05+6.27872523e-04j, -1.25139166e-05-7.07494831e-04j,\n -4.26531031e-05+3.33935842e-05j, -4.93983638e-05+6.62065104e-06j,\n 9.65211897e-05-1.71035129e-04j, 4.09508943e-04+1.81017834e-04j,\n -1.00836515e-03-3.78649449e-04j, -2.28902765e-04+4.29769144e-04j,\n 7.55644169e-04-6.15984149e-05j, -1.31806876e-04+1.50093610e-04j,\n 7.41534101e-04+5.25070731e-05j, -6.37489984e-04-3.17623167e-04j,\n 2.50510928e-04+4.72741634e-05j, -3.12101871e-05-5.30198666e-04j,\n -1.61833058e-04+6.04663509e-04j, 4.33069517e-04-1.76875910e-04j,\n -9.68597935e-05+2.72233313e-04j, 1.29069366e-05-6.86698274e-04j,\n -7.70844850e-04+2.59679250e-04j, 5.06334114e-04-2.08281614e-04j,\n -1.23040807e-04+4.96912636e-05j, 2.81721303e-04+2.82239777e-04j,\n -4.32856798e-04+2.85045100e-04j, -7.18904719e-05-3.42817657e-04j,\n 2.82159126e-04+4.51272457e-04j, -2.22012038e-04-7.93265681e-04j,\n 8.25420573e-05+8.53400594e-04j, -1.00706437e-04-2.98939601e-04j,\n 3.14683041e-05-2.27519261e-04j, 4.36941077e-05+1.13381143e-04j,\n -5.18682202e-04+4.48501226e-04j, 2.30610517e-04-5.52246756e-04j,\n 2.34490912e-04-1.26264973e-04j, -7.97095177e-05+3.35952614e-04j,\n -3.45318332e-05-9.44128409e-05j, -7.57633937e-04-3.43506355e-04j,\n 7.20500401e-04+3.51295162e-04j, 4.74556384e-04+6.27668922e-04j,\n -2.81101536e-04-9.78262731e-04j, 4.46874372e-04+1.24837907e-04j,\n 6.05163789e-04-2.12023275e-04j, -1.01407397e-03-5.16929477e-04j,\n -2.98164906e-04+2.33126469e-04j, -4.86355494e-04+4.33123201e-04j,\n 1.83462356e-03-7.71867376e-07j, -7.69209261e-04-7.01104884e-04j,\n -3.18641790e-05+3.73050143e-04j, 1.15613125e-04+9.09600948e-04j,\n -4.28446346e-04+7.86494004e-05j, -3.34852105e-04-3.47817544e-04j,\n 6.26099520e-04-1.68044497e-04j, 4.68369045e-04+1.42514209e-04j,\n -2.83908873e-04+7.92399750e-04j, -2.49184244e-04-2.18710226e-04j,\n -9.19001071e-05-6.04839544e-04j, 4.08264053e-04+9.93217421e-06j,\n -1.08943196e-03+3.64046481e-04j, 4.09723216e-04+8.49350013e-05j,\n 7.86130478e-04-6.09249721e-04j, -3.38645074e-04-2.27244838e-05j,\n -4.77324227e-04+6.16456017e-04j, -5.01373021e-04-9.55310274e-04j,\n 1.70573740e-04+3.42094097e-04j, 6.89184712e-04+9.91871858e-04j,\n 7.74817539e-04-2.00708515e-04j, -1.39524815e-03-9.81590142e-04j,\n 1.09255944e-03+9.77000557e-04j, -2.06580095e-04-1.44088734e-04j,\n 1.00169291e-04+1.44579525e-04j, -1.08783213e-03-8.86595885e-04j,\n -2.96315620e-04+1.88823864e-04j, 1.58771473e-03+2.66780661e-04j,\n -2.99713732e-04+3.14700680e-05j, -1.08078974e-03-6.78289646e-04j,\n 4.79116024e-04+5.81652824e-04j, -2.43603471e-04-1.88258714e-04j,\n 7.93634703e-04+9.36679925e-04j, -6.29229943e-04-1.26267628e-03j,\n 9.93210429e-05-3.33132766e-04j, 5.00382390e-05+4.41575210e-04j,\n 1.61111886e-04+1.10747772e-03j, 1.23195666e-03-1.20419700e-03j,\n -1.43246313e-03+7.18458426e-04j, 1.23366572e-04-2.18045537e-04j,\n -4.21981246e-04+5.80438962e-05j, 2.37621151e-05+1.23103317e-03j,\n 7.49466857e-04-1.79312642e-03j, 1.24531095e-04+4.69038221e-04j,\n -8.82536309e-04+0.00000000e+00j]])
self.altes_E = np.zeros((257, 2, 2), dtype = complex)
alpha = 0.8
I have an array with certain values that I want normalize from 0 to 1. When I try to do it with minmax_scale function it returns the same set.
[2.80756379e-01 9.47215085e-01 2.98665545e-01 2.71729701e-01
9.53844447e-01 4.09155122e-01 7.73782687e-01 4.04866838e-01...]
If I slice it to 200 values (set has 400 values) function returns normalized values:
[0.28024599 0.94726674 0.29817026 0.2712117 0.95390169 0.40875301
0.77368808 0.40446111 0.0427401 0.98420903...]
if I generate for example 600 test values, it works fine.
[0. 0.00166945 0.0033389 0.00500835 0.0066778 … 0.9933222 0.99499165
0.9966611 0.99833055 1.]
So I assume that something wrong with my input, but it doesn’t raise any error that I could understand what is happening and why values haven’t been normalized.
So if someone could shed light on this and explain what I am doing wrong.
import numpy as np
from sklearn.preprocessing import minmax_scale
x = [3.4983787694629807, 6.8238036546277545, 3.5877400241270765, 3.4533384061573176, 6.856882141805892, 4.139049184316268, 5.958429090197188, 4.11765195576695, 2.314295679864955, 7.007979076325537, 4.591575092528983, 6.500165465269498, 2.8914665615540556, 3.543340276730918, 6.561891009978831, 3.6289541084867656, 3.5071777128342223, 7.056680192241884, 3.4788374494994114, 3.4464924916748862, 7.014214072992434, 3.4916155718006134, 3.5360046270365486, 7.086001703906676, 3.612050358553576, 3.896165015866581, 6.09922605707944, 3.3460547945703287, 3.910944920634269, 5.971772677577327, 5.178500485785331, 6.6090028576236675, 2.841807831193129, 5.149220347017544, 4.735677896132761, 3.8354921976610226, 7.038466835269509, 3.587747218864995, 3.555693227090981, 6.954475444783193, 3.437365707196104, 3.4325883202929894, 7.020900371706621, 3.4368379490007386, 3.475833945795732, 6.8688992156695585, 3.583554218078379, 3.520770727926182, 6.92133265995129, 4.044292336171232, 5.602564848428526, 4.411557629422499, 2.45924872382912, 7.086402867274765, 4.7372741724391965, 6.374350060657951, 2.6542178607167273, 3.508325152205329, 6.461025102348145, 3.6812426559272033, 3.5307800915694383, 7.066468458976694, 3.493276512558132, 3.4568759587746243, 7.0176506092750595, 3.478361485227409, 3.5201937331883713, 7.080979349338214, 3.5795505246747212, 3.8031183328096256, 6.245609213027018, 3.4165971380697018, 2.1723738636075023, 6.126159977852875, 5.013068437451677, 6.904030395006403, 2.7075477902159992, 4.895577020527206, 5.020958429692195, 3.9029736137362567, 7.005502917949908, 3.5789396294713756, 3.5676476014611818, 6.929131913377069, 3.4472779998622487, 3.4308686427915935, 7.018771879565203, 3.432009619429056, 3.457840956435269, 6.905877831610167, 3.5755167560389163, 3.5618219034900966, 6.97358473363722, 3.9600759412714175, 5.260824000465443, 4.692774833624142, 2.602129482124068, 7.035148649249085, 4.8915972720677825, 6.2369642563916825, 2.3878063740977975, 3.4610250988299063, 6.344643530169994, 3.745077609377894, 3.557344813692928, 7.0753891572282885, 3.5084174545346944, 3.4687392098349816, 7.018578929608326, 3.4657417965161157, 3.504674479158238, 7.073344198878297, 3.5505846727930717, 3.728275930579232, 6.374512212337401, 3.473738663253011, 2.455000320545692, 6.271460848569801, 4.853259564210921, 7.060300163815499, 2.5675976179261544, 4.625519502757723, 5.34193581659188, 3.9796698149770413, 6.961928522596542, 3.553928370489732, 3.577760192959362, 6.897549440276217, 3.4618068657264773, 3.4328376059178547, 7.019069392542759, 3.430955309222881, 3.4444356247624213, 6.935848551405397, 3.5648686589574554, 3.5823723667040626, 7.0144769584373465, 3.885706919437764, 4.947869760566333, 4.958988699542243, 2.740882010237675, 6.844670575188505, 5.052957262016582, 6.08929327319099, 2.1012153486394873, 3.4006432713737014, 6.211570326896137, 3.823951658625252, 3.5871290770943087, 7.082486008656476, 3.524032336877465, 3.481547522774735, 7.017048732257519, 3.454184925027273, 3.4896816808661573, 7.064127863850646, 3.5247589190924855, 3.6675470038864804, 6.486998043891378, 3.5179550925822936, 2.7150317741174828, 6.406130756762303, 4.700905963998837, 7.0786411289315705, 2.424024165788486, 4.340976405808333, 5.68871874308043, 4.066399458564197, 6.906772355480829, 3.5069849107646553, 3.584982183669687, 6.8586979941775175, 3.4809101839812273, 3.4385861350276734, 7.021776708035995, 3.433539453335249, 3.435646282061486, 6.959814531248965, 3.5525938762287192, 3.5880328691774226, 7.04501753482764, 3.8203341873417145, 4.672129393281222, 5.208396760624669, 2.873447040265478, 6.517042667648001, 5.219450800175008, 5.932960908282111, 4.071807320428156, 3.32681370883367, 6.060786017704781, 3.922140073470534, 3.6205701108946693, 7.086704780969265, 3.5398647239459082, 3.4949072952266786, 7.012978275363125, 3.4442572447205126, 3.4754567104312315, 7.054260858195003, 3.501860081901249, 3.6176350355084987, 6.584272251688416, 3.550139011669634, 2.9441212137915196, 6.528971419532436, 4.557509169353122, 6.971230117604573, 2.2788693414108083, 4.044384223064448, 6.045075381817579, 4.16380702973696, 6.8392444953122915, 3.4322587777495, 3.588015280869961, 6.811444662244569, 3.504571583212776, 3.448038447176144, 7.0267448030100095, 3.4394438861946495, 3.4313813610293606, 6.978663067682745, 3.539410445197603, 3.5836697468529928, 7.066336743389743, 3.763013423804524, 4.436678180031973, 5.439712464353549, 2.9978564390796802, 6.06598407921124, 5.38892971887227, 5.769885855777033, 4.7305353792164615, 3.2396389326858333, 5.891487403995336, 4.044576070907139, 3.658273834104114, 7.086908629948878, 3.5554434237079002, 3.508553789136474, 7.006154398318428, 3.4366257440898904, 3.462343969119664, 7.044561860711188, 3.4818481341401735, 3.576035272214573, 6.667629115969775, 3.5715246418069957, 3.1369130300148993, 6.639150212621939, 4.424195703276443, 6.759495560732593, 2.134069612887043, 3.7386525746914123, 6.388620586154337, 4.272297018941626, 6.7587843489580965, 3.324494111518356, 3.5853639214122595, 6.754568772053236, 3.5329516319997514, 3.4610002607293953, 7.033692354375017, 3.4481972797191154, 3.4313661148467935, 6.993158859550646, 3.5257969569299723, 3.5731476929367916, 7.07964197799761, 3.712767027132367, 4.2405670182736745, 5.652147101651347, 3.112331986409608, 5.515185072672031, 5.559088312172956, 5.602219157421699, 5.362655122965983, 3.1396940731308915, 5.703149958832117, 4.196548195898355, 3.700992351744346, 7.081897811873345, 3.569845801358458, 3.5223021303817146, 6.996232651966579, 3.4319885789163944, 3.450813363928631, 7.035729967483905, 3.464829989158296, 3.5409776462526583, 6.738403993391335, 3.583589535854453, 3.2912893051822354, 6.7362012208338795, 4.301697490919809, 6.470822564075378, 3.659773459738205, 2.0974903963829354, 6.692161174433521, 4.3919729995424195, 6.665106087684514, 3.1798600950044777, 3.5754169970262786, 6.686782075167818, 3.566547285071714, 3.47722596516465, 7.042206860585631, 3.459238171506453, 3.435134332592052, 7.003939734793729, 3.512049727586488, 3.559270908983708, 7.086176708003556, 3.6686375261134057, 4.080241002256502, 5.84537422915953, 3.215380734121881, 4.89528857023289, 5.727559855711503, 5.432265203060416, 5.934141424807943, 3.027998394822877, 5.495590110107973, 4.3831413048066254, 3.7495930218165427, 7.070434254364885, 3.5814467824887473, 3.535972176884382, 6.982737736524654, 3.43098967364103, 3.441422724579513, 7.028339316715845, 3.451013794888052, 3.5113341674962717, 6.797932388511367, 3.587942414572208, 3.4081496049032274, 6.82001018784346, 4.190357260509626, 6.134736264429092, 3.967315538885193, 2.2419098299323275, 6.926226800256952, 4.522587633421751, 6.558238517691441, 2.9967711785804605, 3.556550927861764, 6.606754741200447, 3.6063414285217674, 3.496491069547794, 7.05174661557621, 3.471997346906242, 3.4420693538840723, 7.011514159064076, 3.4983614083674994, 3.5438804663925714, 7.0871842215570755, 3.629734022986171, 3.9507935039967084, 6.019481038594121, 3.3058784855515917, 4.240129283449884, 5.892015134812656, 5.262390436269602, 6.413168227114006, 2.905958254165465, 5.269023998291438, 4.608362850137262, 3.8050205047007704, 7.051271296451422, 3.5877015700240324, 3.5493020337345564, 6.965064567979088, 3.434139087088657, 3.4347436167612146, 7.022835521591056, 3.4406465746514856, 3.4864976824344196, 6.847515844073905, 3.58620796770146, 3.4908273530643696, 6.890786354350282, 4.090156575701794, 5.77929297599503, 4.2665273948822655, 2.387152222353216, 7.062521670699347, 4.663509517553511, 6.438556051230168, 2.776539669077205, 3.52724382177749, 6.513147765418067, 3.6539307251684234, 3.5186578165204065, 7.061643896805739, 3.4859770491431257, 3.4514785509234778, 7.016259943745782, 3.4849078166839633, 3.528054396333257]
xa = np.array(x)
q = minmax_scale(xa)
# Not normalized
print(q)
d = minmax_scale(xa[:200])
# Normalized
print(d)
za = np.arange(600)
z = minmax_scale(za)
# Normalized
print(z)
You're probably making some wrong assumptions here, I can show you:
>> import numpy as np
>> from sklearn.preprocessing import minmax_scale
>> x = [3.4983787694629807, 6.8238036546277545, 3.5877400241270765, 3.4533384061573176, 6.856882141805892, 4.139049184316268, 5.958429090197188, 4.11765195576695, 2.314295679864955, 7.007979076325537, 4.591575092528983, 6.500165465269498, 2.8914665615540556, 3.543340276730918, 6.561891009978831, 3.6289541084867656, 3.5071777128342223, 7.056680192241884, 3.4788374494994114, 3.4464924916748862, 7.014214072992434, 3.4916155718006134, 3.5360046270365486, 7.086001703906676, 3.612050358553576, 3.896165015866581, 6.09922605707944, 3.3460547945703287, 3.910944920634269, 5.971772677577327, 5.178500485785331, 6.6090028576236675, 2.841807831193129, 5.149220347017544, 4.735677896132761, 3.8354921976610226, 7.038466835269509, 3.587747218864995, 3.555693227090981, 6.954475444783193, 3.437365707196104, 3.4325883202929894, 7.020900371706621, 3.4368379490007386, 3.475833945795732, 6.8688992156695585, 3.583554218078379, 3.520770727926182, 6.92133265995129, 4.044292336171232, 5.602564848428526, 4.411557629422499, 2.45924872382912, 7.086402867274765, 4.7372741724391965, 6.374350060657951, 2.6542178607167273, 3.508325152205329, 6.461025102348145, 3.6812426559272033, 3.5307800915694383, 7.066468458976694, 3.493276512558132, 3.4568759587746243, 7.0176506092750595, 3.478361485227409, 3.5201937331883713, 7.080979349338214, 3.5795505246747212, 3.8031183328096256, 6.245609213027018, 3.4165971380697018, 2.1723738636075023, 6.126159977852875, 5.013068437451677, 6.904030395006403, 2.7075477902159992, 4.895577020527206, 5.020958429692195, 3.9029736137362567, 7.005502917949908, 3.5789396294713756, 3.5676476014611818, 6.929131913377069, 3.4472779998622487, 3.4308686427915935, 7.018771879565203, 3.432009619429056, 3.457840956435269, 6.905877831610167, 3.5755167560389163, 3.5618219034900966, 6.97358473363722, 3.9600759412714175, 5.260824000465443, 4.692774833624142, 2.602129482124068, 7.035148649249085, 4.8915972720677825, 6.2369642563916825, 2.3878063740977975, 3.4610250988299063, 6.344643530169994, 3.745077609377894, 3.557344813692928, 7.0753891572282885, 3.5084174545346944, 3.4687392098349816, 7.018578929608326, 3.4657417965161157, 3.504674479158238, 7.073344198878297, 3.5505846727930717, 3.728275930579232, 6.374512212337401, 3.473738663253011, 2.455000320545692, 6.271460848569801, 4.853259564210921, 7.060300163815499, 2.5675976179261544, 4.625519502757723, 5.34193581659188, 3.9796698149770413, 6.961928522596542, 3.553928370489732, 3.577760192959362, 6.897549440276217, 3.4618068657264773, 3.4328376059178547, 7.019069392542759, 3.430955309222881, 3.4444356247624213, 6.935848551405397, 3.5648686589574554, 3.5823723667040626, 7.0144769584373465, 3.885706919437764, 4.947869760566333, 4.958988699542243, 2.740882010237675, 6.844670575188505, 5.052957262016582, 6.08929327319099, 2.1012153486394873, 3.4006432713737014, 6.211570326896137, 3.823951658625252, 3.5871290770943087, 7.082486008656476, 3.524032336877465, 3.481547522774735, 7.017048732257519, 3.454184925027273, 3.4896816808661573, 7.064127863850646, 3.5247589190924855, 3.6675470038864804, 6.486998043891378, 3.5179550925822936, 2.7150317741174828, 6.406130756762303, 4.700905963998837, 7.0786411289315705, 2.424024165788486, 4.340976405808333, 5.68871874308043, 4.066399458564197, 6.906772355480829, 3.5069849107646553, 3.584982183669687, 6.8586979941775175, 3.4809101839812273, 3.4385861350276734, 7.021776708035995, 3.433539453335249, 3.435646282061486, 6.959814531248965, 3.5525938762287192, 3.5880328691774226, 7.04501753482764, 3.8203341873417145, 4.672129393281222, 5.208396760624669, 2.873447040265478, 6.517042667648001, 5.219450800175008, 5.932960908282111, 4.071807320428156, 3.32681370883367, 6.060786017704781, 3.922140073470534, 3.6205701108946693, 7.086704780969265, 3.5398647239459082, 3.4949072952266786, 7.012978275363125, 3.4442572447205126, 3.4754567104312315, 7.054260858195003, 3.501860081901249, 3.6176350355084987, 6.584272251688416, 3.550139011669634, 2.9441212137915196, 6.528971419532436, 4.557509169353122, 6.971230117604573, 2.2788693414108083, 4.044384223064448, 6.045075381817579, 4.16380702973696, 6.8392444953122915, 3.4322587777495, 3.588015280869961, 6.811444662244569, 3.504571583212776, 3.448038447176144, 7.0267448030100095, 3.4394438861946495, 3.4313813610293606, 6.978663067682745, 3.539410445197603, 3.5836697468529928, 7.066336743389743, 3.763013423804524, 4.436678180031973, 5.439712464353549, 2.9978564390796802, 6.06598407921124, 5.38892971887227, 5.769885855777033, 4.7305353792164615, 3.2396389326858333, 5.891487403995336, 4.044576070907139, 3.658273834104114, 7.086908629948878, 3.5554434237079002, 3.508553789136474, 7.006154398318428, 3.4366257440898904, 3.462343969119664, 7.044561860711188, 3.4818481341401735, 3.576035272214573, 6.667629115969775, 3.5715246418069957, 3.1369130300148993, 6.639150212621939, 4.424195703276443, 6.759495560732593, 2.134069612887043, 3.7386525746914123, 6.388620586154337, 4.272297018941626, 6.7587843489580965, 3.324494111518356, 3.5853639214122595, 6.754568772053236, 3.5329516319997514, 3.4610002607293953, 7.033692354375017, 3.4481972797191154, 3.4313661148467935, 6.993158859550646, 3.5257969569299723, 3.5731476929367916, 7.07964197799761, 3.712767027132367, 4.2405670182736745, 5.652147101651347, 3.112331986409608, 5.515185072672031, 5.559088312172956, 5.602219157421699, 5.362655122965983, 3.1396940731308915, 5.703149958832117, 4.196548195898355, 3.700992351744346, 7.081897811873345, 3.569845801358458, 3.5223021303817146, 6.996232651966579, 3.4319885789163944, 3.450813363928631, 7.035729967483905, 3.464829989158296, 3.5409776462526583, 6.738403993391335, 3.583589535854453, 3.2912893051822354, 6.7362012208338795, 4.301697490919809, 6.470822564075378, 3.659773459738205, 2.0974903963829354, 6.692161174433521, 4.3919729995424195, 6.665106087684514, 3.1798600950044777, 3.5754169970262786, 6.686782075167818, 3.566547285071714, 3.47722596516465, 7.042206860585631, 3.459238171506453, 3.435134332592052, 7.003939734793729, 3.512049727586488, 3.559270908983708, 7.086176708003556, 3.6686375261134057, 4.080241002256502, 5.84537422915953, 3.215380734121881, 4.89528857023289, 5.727559855711503, 5.432265203060416, 5.934141424807943, 3.027998394822877, 5.495590110107973, 4.3831413048066254, 3.7495930218165427, 7.070434254364885, 3.5814467824887473, 3.535972176884382, 6.982737736524654, 3.43098967364103, 3.441422724579513, 7.028339316715845, 3.451013794888052, 3.5113341674962717, 6.797932388511367, 3.587942414572208, 3.4081496049032274, 6.82001018784346, 4.190357260509626, 6.134736264429092, 3.967315538885193, 2.2419098299323275, 6.926226800256952, 4.522587633421751, 6.558238517691441, 2.9967711785804605, 3.556550927861764, 6.606754741200447, 3.6063414285217674, 3.496491069547794, 7.05174661557621, 3.471997346906242, 3.4420693538840723, 7.011514159064076, 3.4983614083674994, 3.5438804663925714, 7.0871842215570755, 3.629734022986171, 3.9507935039967084, 6.019481038594121, 3.3058784855515917, 4.240129283449884, 5.892015134812656, 5.262390436269602, 6.413168227114006, 2.905958254165465, 5.269023998291438, 4.608362850137262, 3.8050205047007704, 7.051271296451422, 3.5877015700240324, 3.5493020337345564, 6.965064567979088, 3.434139087088657, 3.4347436167612146, 7.022835521591056, 3.4406465746514856, 3.4864976824344196, 6.847515844073905, 3.58620796770146, 3.4908273530643696, 6.890786354350282, 4.090156575701794, 5.77929297599503, 4.2665273948822655, 2.387152222353216, 7.062521670699347, 4.663509517553511, 6.438556051230168, 2.776539669077205, 3.52724382177749, 6.513147765418067, 3.6539307251684234, 3.5186578165204065, 7.061643896805739, 3.4859770491431257, 3.4514785509234778, 7.016259943745782, 3.4849078166839633, 3.528054396333257]
>> xa = np.array(x)
>> q = minmax_scale(xa)
>> x_set = set(x)
>> q_set = set(q)
>> x_set.intersection(q_set) == set()
True
As you can see, there's no intersection, it's definitely not returning the same set...
I'm working with PyGame and attempting to create a zoomable/scaleable Mandelbrot Set. I have this set up for square windows and coordinates that only from -1 to 1 on both axes in the complex plane. The way I do this is for every pixel on the screen I call this function:
#Import pygame and initialize
xSize = 50
ySize = 50
scale = 20
size = width, height = (xSize * scale), (ySize * scale)
screen = pygame.display.set_mode(size)
def getCoords(x, y):
complexX = (x/((xSize * scale)/2)) - 1
complexY = (y/((ySize * scale)/2)) - 1
return complexX, complexY
And here is the loop where I actually plot the pixels:
for y in range(0, (ySize * scale)):
for x in range(0, (xSize * scale)):
i = 0
z = getCoords(x, y)
complexNum = complex(z[0], z[1])
zOld = 0
blowsUp = False
#Check to see if (z^2 + c) "blows up"
if blowsUp:
screen.set_at((x, y), color1)
else:
screen.set_at((x, y), color0)
Essentially what I want to be able to do is to have two tuples (one for x and one for y) that contain the maximum and minimum values that get plotted from the complex plane (i.e. here I'm just plotting 1 to -1 on both the real and imaginary axes). I imagine that this would be done by editing the getCoords() function, but after much tinkering with the expression there I can't seem to find a way to do this properly.
I think your question is only marginally related to pygame programming, and is really mostly a math problem.
If I've understood what you're trying to do correctly, essentially it amounts to mapping an integer range of 0..scale to a specified subrange within ±1.0 in both the x and y dimensions. Visualize it as the transformation of the x and y coordinates in one rectangular area or box so they fit within the boundaries of another.
Here's code showing the essence of the math involved.
(Note that the code shown (largely) follows the PEP 8 - Style Guide for Python Code, which I strongly suggest you read and start following.)
scale = 2
size_x, size_y = 15, 15
subrange_x, subrange_y = (-.20, .20), (-.20, .20)
delta_x, delta_y = (subrange_x[1] - subrange_x[0]), (subrange_y[1] - subrange_y[0])
scale_x, scale_y = (size_x * scale), (size_y * scale)
def get_coords(x, y):
real = (x/scale_x * delta_x) + subrange_x[0]
imag = (y/scale_y * delta_y) + subrange_y[0]
return real, imag
for y in range(scale_y):
z_values = []
for x in range(scale_x):
z = get_coords(x, y)
complex_num = complex(z[0], z[1])
z_values.append(f'{complex_num:.2f}')
print(f'y={y:02}:', ' '.join(z_values))
Results printed:
y=00: -0.20-0.20j -0.19-0.20j -0.17-0.20j -0.16-0.20j -0.15-0.20j -0.13-0.20j -0.12-0.20j -0.11-0.20j -0.09-0.20j -0.08-0.20j -0.07-0.20j -0.05-0.20j -0.04-0.20j -0.03-0.20j -0.01-0.20j 0.00-0.20j 0.01-0.20j 0.03-0.20j 0.04-0.20j 0.05-0.20j 0.07-0.20j 0.08-0.20j 0.09-0.20j 0.11-0.20j 0.12-0.20j 0.13-0.20j 0.15-0.20j 0.16-0.20j 0.17-0.20j 0.19-0.20j
y=01: -0.20-0.19j -0.19-0.19j -0.17-0.19j -0.16-0.19j -0.15-0.19j -0.13-0.19j -0.12-0.19j -0.11-0.19j -0.09-0.19j -0.08-0.19j -0.07-0.19j -0.05-0.19j -0.04-0.19j -0.03-0.19j -0.01-0.19j 0.00-0.19j 0.01-0.19j 0.03-0.19j 0.04-0.19j 0.05-0.19j 0.07-0.19j 0.08-0.19j 0.09-0.19j 0.11-0.19j 0.12-0.19j 0.13-0.19j 0.15-0.19j 0.16-0.19j 0.17-0.19j 0.19-0.19j
y=02: -0.20-0.17j -0.19-0.17j -0.17-0.17j -0.16-0.17j -0.15-0.17j -0.13-0.17j -0.12-0.17j -0.11-0.17j -0.09-0.17j -0.08-0.17j -0.07-0.17j -0.05-0.17j -0.04-0.17j -0.03-0.17j -0.01-0.17j 0.00-0.17j 0.01-0.17j 0.03-0.17j 0.04-0.17j 0.05-0.17j 0.07-0.17j 0.08-0.17j 0.09-0.17j 0.11-0.17j 0.12-0.17j 0.13-0.17j 0.15-0.17j 0.16-0.17j 0.17-0.17j 0.19-0.17j
y=03: -0.20-0.16j -0.19-0.16j -0.17-0.16j -0.16-0.16j -0.15-0.16j -0.13-0.16j -0.12-0.16j -0.11-0.16j -0.09-0.16j -0.08-0.16j -0.07-0.16j -0.05-0.16j -0.04-0.16j -0.03-0.16j -0.01-0.16j 0.00-0.16j 0.01-0.16j 0.03-0.16j 0.04-0.16j 0.05-0.16j 0.07-0.16j 0.08-0.16j 0.09-0.16j 0.11-0.16j 0.12-0.16j 0.13-0.16j 0.15-0.16j 0.16-0.16j 0.17-0.16j 0.19-0.16j
y=04: -0.20-0.15j -0.19-0.15j -0.17-0.15j -0.16-0.15j -0.15-0.15j -0.13-0.15j -0.12-0.15j -0.11-0.15j -0.09-0.15j -0.08-0.15j -0.07-0.15j -0.05-0.15j -0.04-0.15j -0.03-0.15j -0.01-0.15j 0.00-0.15j 0.01-0.15j 0.03-0.15j 0.04-0.15j 0.05-0.15j 0.07-0.15j 0.08-0.15j 0.09-0.15j 0.11-0.15j 0.12-0.15j 0.13-0.15j 0.15-0.15j 0.16-0.15j 0.17-0.15j 0.19-0.15j
y=05: -0.20-0.13j -0.19-0.13j -0.17-0.13j -0.16-0.13j -0.15-0.13j -0.13-0.13j -0.12-0.13j -0.11-0.13j -0.09-0.13j -0.08-0.13j -0.07-0.13j -0.05-0.13j -0.04-0.13j -0.03-0.13j -0.01-0.13j 0.00-0.13j 0.01-0.13j 0.03-0.13j 0.04-0.13j 0.05-0.13j 0.07-0.13j 0.08-0.13j 0.09-0.13j 0.11-0.13j 0.12-0.13j 0.13-0.13j 0.15-0.13j 0.16-0.13j 0.17-0.13j 0.19-0.13j
y=06: -0.20-0.12j -0.19-0.12j -0.17-0.12j -0.16-0.12j -0.15-0.12j -0.13-0.12j -0.12-0.12j -0.11-0.12j -0.09-0.12j -0.08-0.12j -0.07-0.12j -0.05-0.12j -0.04-0.12j -0.03-0.12j -0.01-0.12j 0.00-0.12j 0.01-0.12j 0.03-0.12j 0.04-0.12j 0.05-0.12j 0.07-0.12j 0.08-0.12j 0.09-0.12j 0.11-0.12j 0.12-0.12j 0.13-0.12j 0.15-0.12j 0.16-0.12j 0.17-0.12j 0.19-0.12j
y=07: -0.20-0.11j -0.19-0.11j -0.17-0.11j -0.16-0.11j -0.15-0.11j -0.13-0.11j -0.12-0.11j -0.11-0.11j -0.09-0.11j -0.08-0.11j -0.07-0.11j -0.05-0.11j -0.04-0.11j -0.03-0.11j -0.01-0.11j 0.00-0.11j 0.01-0.11j 0.03-0.11j 0.04-0.11j 0.05-0.11j 0.07-0.11j 0.08-0.11j 0.09-0.11j 0.11-0.11j 0.12-0.11j 0.13-0.11j 0.15-0.11j 0.16-0.11j 0.17-0.11j 0.19-0.11j
y=08: -0.20-0.09j -0.19-0.09j -0.17-0.09j -0.16-0.09j -0.15-0.09j -0.13-0.09j -0.12-0.09j -0.11-0.09j -0.09-0.09j -0.08-0.09j -0.07-0.09j -0.05-0.09j -0.04-0.09j -0.03-0.09j -0.01-0.09j 0.00-0.09j 0.01-0.09j 0.03-0.09j 0.04-0.09j 0.05-0.09j 0.07-0.09j 0.08-0.09j 0.09-0.09j 0.11-0.09j 0.12-0.09j 0.13-0.09j 0.15-0.09j 0.16-0.09j 0.17-0.09j 0.19-0.09j
y=09: -0.20-0.08j -0.19-0.08j -0.17-0.08j -0.16-0.08j -0.15-0.08j -0.13-0.08j -0.12-0.08j -0.11-0.08j -0.09-0.08j -0.08-0.08j -0.07-0.08j -0.05-0.08j -0.04-0.08j -0.03-0.08j -0.01-0.08j 0.00-0.08j 0.01-0.08j 0.03-0.08j 0.04-0.08j 0.05-0.08j 0.07-0.08j 0.08-0.08j 0.09-0.08j 0.11-0.08j 0.12-0.08j 0.13-0.08j 0.15-0.08j 0.16-0.08j 0.17-0.08j 0.19-0.08j
y=10: -0.20-0.07j -0.19-0.07j -0.17-0.07j -0.16-0.07j -0.15-0.07j -0.13-0.07j -0.12-0.07j -0.11-0.07j -0.09-0.07j -0.08-0.07j -0.07-0.07j -0.05-0.07j -0.04-0.07j -0.03-0.07j -0.01-0.07j 0.00-0.07j 0.01-0.07j 0.03-0.07j 0.04-0.07j 0.05-0.07j 0.07-0.07j 0.08-0.07j 0.09-0.07j 0.11-0.07j 0.12-0.07j 0.13-0.07j 0.15-0.07j 0.16-0.07j 0.17-0.07j 0.19-0.07j
y=11: -0.20-0.05j -0.19-0.05j -0.17-0.05j -0.16-0.05j -0.15-0.05j -0.13-0.05j -0.12-0.05j -0.11-0.05j -0.09-0.05j -0.08-0.05j -0.07-0.05j -0.05-0.05j -0.04-0.05j -0.03-0.05j -0.01-0.05j 0.00-0.05j 0.01-0.05j 0.03-0.05j 0.04-0.05j 0.05-0.05j 0.07-0.05j 0.08-0.05j 0.09-0.05j 0.11-0.05j 0.12-0.05j 0.13-0.05j 0.15-0.05j 0.16-0.05j 0.17-0.05j 0.19-0.05j
y=12: -0.20-0.04j -0.19-0.04j -0.17-0.04j -0.16-0.04j -0.15-0.04j -0.13-0.04j -0.12-0.04j -0.11-0.04j -0.09-0.04j -0.08-0.04j -0.07-0.04j -0.05-0.04j -0.04-0.04j -0.03-0.04j -0.01-0.04j 0.00-0.04j 0.01-0.04j 0.03-0.04j 0.04-0.04j 0.05-0.04j 0.07-0.04j 0.08-0.04j 0.09-0.04j 0.11-0.04j 0.12-0.04j 0.13-0.04j 0.15-0.04j 0.16-0.04j 0.17-0.04j 0.19-0.04j
y=13: -0.20-0.03j -0.19-0.03j -0.17-0.03j -0.16-0.03j -0.15-0.03j -0.13-0.03j -0.12-0.03j -0.11-0.03j -0.09-0.03j -0.08-0.03j -0.07-0.03j -0.05-0.03j -0.04-0.03j -0.03-0.03j -0.01-0.03j 0.00-0.03j 0.01-0.03j 0.03-0.03j 0.04-0.03j 0.05-0.03j 0.07-0.03j 0.08-0.03j 0.09-0.03j 0.11-0.03j 0.12-0.03j 0.13-0.03j 0.15-0.03j 0.16-0.03j 0.17-0.03j 0.19-0.03j
y=14: -0.20-0.01j -0.19-0.01j -0.17-0.01j -0.16-0.01j -0.15-0.01j -0.13-0.01j -0.12-0.01j -0.11-0.01j -0.09-0.01j -0.08-0.01j -0.07-0.01j -0.05-0.01j -0.04-0.01j -0.03-0.01j -0.01-0.01j 0.00-0.01j 0.01-0.01j 0.03-0.01j 0.04-0.01j 0.05-0.01j 0.07-0.01j 0.08-0.01j 0.09-0.01j 0.11-0.01j 0.12-0.01j 0.13-0.01j 0.15-0.01j 0.16-0.01j 0.17-0.01j 0.19-0.01j
y=15: -0.20+0.00j -0.19+0.00j -0.17+0.00j -0.16+0.00j -0.15+0.00j -0.13+0.00j -0.12+0.00j -0.11+0.00j -0.09+0.00j -0.08+0.00j -0.07+0.00j -0.05+0.00j -0.04+0.00j -0.03+0.00j -0.01+0.00j 0.00+0.00j 0.01+0.00j 0.03+0.00j .04+0.00j 0.05+0.00j 0.07+0.00j 0.08+0.00j 0.09+0.00j 0.11+0.00j 0.12+0.00j 0.13+0.00j 0.15+0.00j 0.16+0.00j 0.17+0.00j 0.19+0.00j
y=16: -0.20+0.01j -0.19+0.01j -0.17+0.01j -0.16+0.01j -0.15+0.01j -0.13+0.01j -0.12+0.01j -0.11+0.01j -0.09+0.01j -0.08+0.01j -0.07+0.01j -0.05+0.01j -0.04+0.01j -0.03+0.01j -0.01+0.01j 0.00+0.01j 0.01+0.01j 0.03+0.01j 0.04+0.01j 0.05+0.01j 0.07+0.01j 0.08+0.01j 0.09+0.01j 0.11+0.01j 0.12+0.01j 0.13+0.01j 0.15+0.01j 0.16+0.01j 0.17+0.01j 0.19+0.01j
y=17: -0.20+0.03j -0.19+0.03j -0.17+0.03j -0.16+0.03j -0.15+0.03j -0.13+0.03j -0.12+0.03j -0.11+0.03j -0.09+0.03j -0.08+0.03j -0.07+0.03j -0.05+0.03j -0.04+0.03j -0.03+0.03j -0.01+0.03j 0.00+0.03j 0.01+0.03j 0.03+0.03j 0.04+0.03j 0.05+0.03j 0.07+0.03j 0.08+0.03j 0.09+0.03j 0.11+0.03j 0.12+0.03j 0.13+0.03j 0.15+0.03j 0.16+0.03j 0.17+0.03j 0.19+0.03j
y=18: -0.20+0.04j -0.19+0.04j -0.17+0.04j -0.16+0.04j -0.15+0.04j -0.13+0.04j -0.12+0.04j -0.11+0.04j -0.09+0.04j -0.08+0.04j -0.07+0.04j -0.05+0.04j -0.04+0.04j -0.03+0.04j -0.01+0.04j 0.00+0.04j 0.01+0.04j 0.03+0.04j 0.04+0.04j 0.05+0.04j 0.07+0.04j 0.08+0.04j 0.09+0.04j 0.11+0.04j 0.12+0.04j 0.13+0.04j 0.15+0.04j 0.16+0.04j 0.17+0.04j 0.19+0.04j
y=19: -0.20+0.05j -0.19+0.05j -0.17+0.05j -0.16+0.05j -0.15+0.05j -0.13+0.05j -0.12+0.05j -0.11+0.05j -0.09+0.05j -0.08+0.05j -0.07+0.05j -0.05+0.05j -0.04+0.05j -0.03+0.05j -0.01+0.05j 0.00+0.05j 0.01+0.05j 0.03+0.05j 0.04+0.05j 0.05+0.05j 0.07+0.05j 0.08+0.05j 0.09+0.05j 0.11+0.05j 0.12+0.05j 0.13+0.05j 0.15+0.05j 0.16+0.05j 0.17+0.05j 0.19+0.05j
y=20: -0.20+0.07j -0.19+0.07j -0.17+0.07j -0.16+0.07j -0.15+0.07j -0.13+0.07j -0.12+0.07j -0.11+0.07j -0.09+0.07j -0.08+0.07j -0.07+0.07j -0.05+0.07j -0.04+0.07j -0.03+0.07j -0.01+0.07j 0.00+0.07j 0.01+0.07j 0.03+0.07j 0.04+0.07j 0.05+0.07j 0.07+0.07j 0.08+0.07j 0.09+0.07j 0.11+0.07j 0.12+0.07j 0.13+0.07j 0.15+0.07j 0.16+0.07j 0.17+0.07j 0.19+0.07j
y=21: -0.20+0.08j -0.19+0.08j -0.17+0.08j -0.16+0.08j -0.15+0.08j -0.13+0.08j -0.12+0.08j -0.11+0.08j -0.09+0.08j -0.08+0.08j -0.07+0.08j -0.05+0.08j -0.04+0.08j -0.03+0.08j -0.01+0.08j 0.00+0.08j 0.01+0.08j 0.03+0.08j 0.04+0.08j 0.05+0.08j 0.07+0.08j 0.08+0.08j 0.09+0.08j 0.11+0.08j 0.12+0.08j 0.13+0.08j 0.15+0.08j 0.16+0.08j 0.17+0.08j 0.19+0.08j
y=22: -0.20+0.09j -0.19+0.09j -0.17+0.09j -0.16+0.09j -0.15+0.09j -0.13+0.09j -0.12+0.09j -0.11+0.09j -0.09+0.09j -0.08+0.09j -0.07+0.09j -0.05+0.09j -0.04+0.09j -0.03+0.09j -0.01+0.09j 0.00+0.09j 0.01+0.09j 0.03+0.09j 0.04+0.09j 0.05+0.09j 0.07+0.09j 0.08+0.09j 0.09+0.09j 0.11+0.09j 0.12+0.09j 0.13+0.09j 0.15+0.09j 0.16+0.09j 0.17+0.09j 0.19+0.09j
y=23: -0.20+0.11j -0.19+0.11j -0.17+0.11j -0.16+0.11j -0.15+0.11j -0.13+0.11j -0.12+0.11j -0.11+0.11j -0.09+0.11j -0.08+0.11j -0.07+0.11j -0.05+0.11j -0.04+0.11j -0.03+0.11j -0.01+0.11j 0.00+0.11j 0.01+0.11j 0.03+0.11j 0.04+0.11j 0.05+0.11j 0.07+0.11j 0.08+0.11j 0.09+0.11j 0.11+0.11j 0.12+0.11j 0.13+0.11j 0.15+0.11j 0.16+0.11j 0.17+0.11j 0.19+0.11j
y=24: -0.20+0.12j -0.19+0.12j -0.17+0.12j -0.16+0.12j -0.15+0.12j -0.13+0.12j -0.12+0.12j -0.11+0.12j -0.09+0.12j -0.08+0.12j -0.07+0.12j -0.05+0.12j -0.04+0.12j -0.03+0.12j -0.01+0.12j 0.00+0.12j 0.01+0.12j 0.03+0.12j 0.04+0.12j 0.05+0.12j 0.07+0.12j 0.08+0.12j 0.09+0.12j 0.11+0.12j 0.12+0.12j 0.13+0.12j 0.15+0.12j 0.16+0.12j 0.17+0.12j 0.19+0.12j
y=25: -0.20+0.13j -0.19+0.13j -0.17+0.13j -0.16+0.13j -0.15+0.13j -0.13+0.13j -0.12+0.13j -0.11+0.13j -0.09+0.13j -0.08+0.13j -0.07+0.13j -0.05+0.13j -0.04+0.13j -0.03+0.13j -0.01+0.13j 0.00+0.13j 0.01+0.13j 0.03+0.13j 0.04+0.13j 0.05+0.13j 0.07+0.13j 0.08+0.13j 0.09+0.13j 0.11+0.13j 0.12+0.13j 0.13+0.13j 0.15+0.13j 0.16+0.13j 0.17+0.13j 0.19+0.13j
y=26: -0.20+0.15j -0.19+0.15j -0.17+0.15j -0.16+0.15j -0.15+0.15j -0.13+0.15j -0.12+0.15j -0.11+0.15j -0.09+0.15j -0.08+0.15j -0.07+0.15j -0.05+0.15j -0.04+0.15j -0.03+0.15j -0.01+0.15j 0.00+0.15j 0.01+0.15j 0.03+0.15j 0.04+0.15j 0.05+0.15j 0.07+0.15j 0.08+0.15j 0.09+0.15j 0.11+0.15j 0.12+0.15j 0.13+0.15j 0.15+0.15j 0.16+0.15j 0.17+0.15j 0.19+0.15j
y=27: -0.20+0.16j -0.19+0.16j -0.17+0.16j -0.16+0.16j -0.15+0.16j -0.13+0.16j -0.12+0.16j -0.11+0.16j -0.09+0.16j -0.08+0.16j -0.07+0.16j -0.05+0.16j -0.04+0.16j -0.03+0.16j -0.01+0.16j 0.00+0.16j 0.01+0.16j 0.03+0.16j 0.04+0.16j 0.05+0.16j 0.07+0.16j 0.08+0.16j 0.09+0.16j 0.11+0.16j 0.12+0.16j 0.13+0.16j 0.15+0.16j 0.16+0.16j 0.17+0.16j 0.19+0.16j
y=28: -0.20+0.17j -0.19+0.17j -0.17+0.17j -0.16+0.17j -0.15+0.17j -0.13+0.17j -0.12+0.17j -0.11+0.17j -0.09+0.17j -0.08+0.17j -0.07+0.17j -0.05+0.17j -0.04+0.17j -0.03+0.17j -0.01+0.17j 0.00+0.17j 0.01+0.17j 0.03+0.17j 0.04+0.17j 0.05+0.17j 0.07+0.17j 0.08+0.17j 0.09+0.17j 0.11+0.17j 0.12+0.17j 0.13+0.17j 0.15+0.17j 0.16+0.17j 0.17+0.17j 0.19+0.17j
y=29: -0.20+0.19j -0.19+0.19j -0.17+0.19j -0.16+0.19j -0.15+0.19j -0.13+0.19j -0.12+0.19j -0.11+0.19j -0.09+0.19j -0.08+0.19j -0.07+0.19j -0.05+0.19j -0.04+0.19j -0.03+0.19j -0.01+0.19j 0.00+0.19j 0.01+0.19j 0.03+0.19j 0.04+0.19j 0.05+0.19j 0.07+0.19j 0.08+0.19j 0.09+0.19j 0.11+0.19j 0.12+0.19j 0.13+0.19j 0.15+0.19j 0.16+0.19j 0.17+0.19j 0.19+0.19j
I have two data sets, group A has hundreds numbers and group B have eight numbers. I want to find another eight numbers in group A that are closest to the four numbers in group B. Now I take one number from group B each time and compared it with group A. Is there other better way to do this?
A = [762.859793, 763.544183, 764.272883, 766.190053, 767.108693, 767.123893, 768.933893, 770.619013, 771.375173, 771.522083, 774.9478730000001, 775.138843, 775.463863, 776.332693, 776.466303, 777.650083, 778.3751129999999, 781.922783, 782.592033, 784.224423, 784.779903, 785.2743429999999, 787.098133, 787.8258030000001, 787.8593030000001, 788.194393, 790.939383, 791.379753, 793.572803, 796.093073, 796.720693, 796.852473, 798.519643, 800.145803, 800.758723, 801.239613, 801.332453, 801.819333, 805.290823, 806.404353, 807.995663, 808.683403, 810.0645030000001, 810.695673, 810.7933929999999, 811.150203, 814.270803, 814.706783, 815.6527629999999, 819.127253, 819.820733, 821.030753, 821.166193, 823.111683, 824.637013, 826.707083, 826.997243, 827.441463, 828.529713, 830.364193, 831.241203, 831.2571929999999, 832.635243, 832.9749730000001, 834.222713, 837.932063, 838.250483, 838.695363, 840.0615730000001, 841.246143, 841.300043, 841.5062230000001, 844.7620730000001, 845.1879230000001, 846.261283, 847.139523, 849.0279429999999, 850.5646929999999, 850.726553, 851.8736529999999, 853.9559830000001, 854.171933, 857.674983, 858.035633, 858.327443, 858.9393230000001, 859.365103, 860.0646929999999, 861.035573, 861.496883, 864.127743, 866.558833, 867.0207429999999, 868.135673, 869.329093, 870.5860529999999, 871.1149230000001, 871.691343, 872.156483, 875.2681230000001, 875.921803, 876.6624029999999, 879.2358929999999, 879.7697029999999, 880.6190730000001, 882.563473, 883.1651529999999, 883.6522130000001, 884.311393, 886.585383, 888.0967929999999, 889.6549630000001, 890.564143, 891.0488230000001, 892.887933, 893.3944630000001, 896.817743, 897.9441429999999, 898.577573, 898.681813, 900.280573, 901.308643, 903.324453, 905.128653, 905.9143029999999, 906.190423, 906.641533, 907.232963, 909.896413, 912.3604630000001, 913.905633, 914.0172630000001, 915.186703, 915.764243, 917.266943, 921.000943, 921.958943, 923.0551429999999, 924.235543, 924.545743, 925.517043, 925.8717429999999, 927.906943, 931.119243, 931.856543, 933.8970429999999, 933.929043, 934.2806429999999, 935.171843, 936.956243, 938.8980429999999, 941.066643, 941.633743, 942.986043, 944.437243, 945.839343, 948.351143, 949.9056429999999, 950.118243, 950.587543, 951.3468429999999, 954.000243, 954.205843, 954.944843, 955.703343, 959.0683429999999, 959.298543, 960.0447429999999, 961.379543, 965.161943, 967.108743, 967.7327429999999, 968.199043, 968.438843, 969.973543, 970.540543, 971.3676429999999, 972.284543, 973.603743, 976.753143, 978.1982429999999, 978.214943, 978.545843, 979.070643, 980.912243, 982.989643, 985.355943, 986.1942429999999, 986.3504429999999, 986.4826429999999, 988.964343, 989.596543, 992.602043, 993.117943, 993.7612429999999, 994.3083429999999, 994.401343, 995.340643, 998.5397429999999, 999.000443, 1000.593343, 1000.6357429999999, 1001.2584429999999, 1001.334243, 1003.663543, 1004.3092429999999, 1006.184043, 1006.665143, 1007.890843, 1010.872443, 1011.4528429999999, 1012.454843, 1012.648543, 1012.825443, 1013.512643, 1015.107543, 1017.5950429999999, 1018.145843, 1022.399143, 1023.348243, 1023.4881429999999, 1023.703943, 1023.892743, 1024.563343, 1026.059343, 1028.420643, 1028.633143, 1028.928743, 1029.316843, 1032.945843, 1036.954843, 1037.770843, 1038.025643, 1039.704743, 1040.994643, 1041.791843, 1042.953943, 1043.592843, 1043.708343, 1050.5268429999999, 1052.079443, 1052.649543, 1052.684943, 1055.256443, 1060.528543, 1061.940143, 1062.041143, 1065.194343, 1065.722143, 1066.910543, 1068.947743, 1073.2487429999999, 1073.632343, 1074.822043, 1075.200543, 1076.141143, 1077.981343, 1079.280243, 1081.3022429999999, 1081.893343, 1085.848643, 1086.145043, 1087.4752429999999, 1087.7197429999999, 1089.479843, 1094.0820429999999, 1094.511343, 1094.685543, 1096.808443, 1097.674643, 1100.9245429999999, 1101.318843, 1101.795743, 1106.028343, 1106.752143, 1108.441043, 1109.063443, 1110.809843, 1112.244343, 1115.205043, 1115.793543, 1116.281043, 1117.533043, 1120.772743, 1120.943343, 1122.063243, 1126.348043, 1126.588843, 1127.782343, 1127.8493429999999, 1132.284543, 1133.968643, 1134.083643, 1134.812743, 1138.425743, 1140.277243, 1142.692943, 1143.479343, 1143.508343, 1146.1264429999999, 1147.5087429999999, 1148.0667429999999, 1149.901943, 1150.083443, 1154.467843, 1155.7469429999999, 1156.378443, 1157.213743, 1157.893943, 1161.299843, 1163.574343, 1165.368743, 1166.530043, 1167.6391429999999, 1168.554843, 1169.4887429999999, 1170.586043, 1175.934543, 1178.156243, 1178.610543, 1178.921943, 1179.145843, 1179.573643, 1187.012243, 1187.262843, 1187.406943, 1189.229243, 1191.636343, 1191.886343, 1194.272843, 1195.991843, 1199.937643, 1200.047843, 1201.449543, 1204.830543, 1205.389743, 1209.462843, 1210.561743, 1212.594143, 1213.386543, 1214.1418429999999, 1214.154443, 1219.073643, 1221.015143, 1222.636843, 1223.881443, 1227.9897429999999, 1228.923443, 1229.525743, 1233.922243, 1234.473743, 1235.452343, 1235.808943, 1240.449043, 1241.093643, 1245.171843, 1245.478243, 1246.061043, 1246.452043, 1249.927343, 1252.341943, 1255.264743, 1257.632643, 1258.441243, 1258.452643, 1259.841943, 1260.042343, 1265.0739429999999, 1266.0141429999999, 1267.6002429999999, 1269.445043, 1270.469243, 1274.139643, 1274.924043, 1276.423843, 1281.449543, 1283.915543, 1284.392443, 1284.828943, 1287.5820429999999, 1288.2985429999999, 1290.2225429999999, 1290.778043, 1294.631843, 1296.719843, 1298.903343, 1301.526443, 1302.276343, 1302.490343, 1303.954143, 1309.437843, 1310.548043, 1314.348243, 1316.070543, 1316.260843, 1317.751243, 1317.935543, 1322.146643, 1328.689343, 1329.097243, 1329.546543, 1329.820643, 1333.801543, 1334.257043, 1338.509343, 1338.584243, 1339.321543, 1341.807143, 1345.492643, 1347.315443, 1348.668043, 1349.854643, 1352.199543, 1352.210943, 1356.886143, 1358.197243, 1361.030843, 1362.906043, 1364.2469429999999, 1366.268943, 1369.804343, 1374.347243, 1374.5440429999999, 1374.9235429999999, 1375.318443, 1377.875843, 1380.191043, 1381.156143, 1387.996743, 1388.038843, 1388.494543, 1390.815443, 1391.645243, 1397.819343, 1398.548343, 1399.623443, 1400.494943, 1402.532943, ]
B = [679.070505, 1358.141011]
Sort data set A
For each element of B (call it b)
Use a binary search (or interpolation search) to find where b would fit into A.
That gives you two adjacent indices; one number is larger than b, one smaller.
Choose the number of the two closer to b.
Does that get you going?
With NumPy arrays and vectorised operations:
import numpy as np
A = np.array(A)
B = np.array(B)
idx = np.abs(A - B[:, None]).argmin(1)
res = A[idx]
print(res)
array([ 762.859793, 1358.197243])
You can pre-process your long list in order to calculate the middle value between adjacent pairs. Then, you can use bisect to obtain the index of the closest number:
from bisect import bisect
A = (762.859793, 763.544183, 764.272883, 766.190053, 767.108693, 767.123893, 768.933893, 770.619013, 771.375173, 771.522083, 774.9478730000001, 775.138843, 775.463863, 776.332693, 776.466303, 777.650083, 778.3751129999999, 781.922783, 782.592033, 784.224423, 784.779903, 785.2743429999999, 787.098133, 787.8258030000001, 787.8593030000001, 788.194393, 790.939383, 791.379753, 793.572803, 796.093073, 796.720693, 796.852473, 798.519643, 800.145803, 800.758723, 801.239613, 801.332453, 801.819333, 805.290823, 806.404353, 807.995663, 808.683403, 810.0645030000001, 810.695673, 810.7933929999999, 811.150203, 814.270803, 814.706783, 815.6527629999999, 819.127253, 819.820733, 821.030753, 821.166193, 823.111683, 824.637013, 826.707083, 826.997243, 827.441463, 828.529713, 830.364193, 831.241203, 831.2571929999999, 832.635243, 832.9749730000001, 834.222713, 837.932063, 838.250483, 838.695363, 840.0615730000001, 841.246143, 841.300043, 841.5062230000001, 844.7620730000001, 845.1879230000001, 846.261283, 847.139523, 849.0279429999999, 850.5646929999999, 850.726553, 851.8736529999999, 853.9559830000001, 854.171933, 857.674983, 858.035633, 858.327443, 858.9393230000001, 859.365103, 860.0646929999999, 861.035573, 861.496883, 864.127743, 866.558833, 867.0207429999999, 868.135673, 869.329093, 870.5860529999999, 871.1149230000001, 871.691343, 872.156483, 875.2681230000001, 875.921803, 876.6624029999999, 879.2358929999999, 879.7697029999999, 880.6190730000001, 882.563473, 883.1651529999999, 883.6522130000001, 884.311393, 886.585383, 888.0967929999999, 889.6549630000001, 890.564143, 891.0488230000001, 892.887933, 893.3944630000001, 896.817743, 897.9441429999999, 898.577573, 898.681813, 900.280573, 901.308643, 903.324453, 905.128653, 905.9143029999999, 906.190423, 906.641533, 907.232963, 909.896413, 912.3604630000001, 913.905633, 914.0172630000001, 915.186703, 915.764243, 917.266943, 921.000943, 921.958943, 923.0551429999999, 924.235543, 924.545743, 925.517043, 925.8717429999999, 927.906943, 931.119243, 931.856543, 933.8970429999999, 933.929043, 934.2806429999999, 935.171843, 936.956243, 938.8980429999999, 941.066643, 941.633743, 942.986043, 944.437243, 945.839343, 948.351143, 949.9056429999999, 950.118243, 950.587543, 951.3468429999999, 954.000243, 954.205843, 954.944843, 955.703343, 959.0683429999999, 959.298543, 960.0447429999999, 961.379543, 965.161943, 967.108743, 967.7327429999999, 968.199043, 968.438843, 969.973543, 970.540543, 971.3676429999999, 972.284543, 973.603743, 976.753143, 978.1982429999999, 978.214943, 978.545843, 979.070643, 980.912243, 982.989643, 985.355943, 986.1942429999999, 986.3504429999999, 986.4826429999999, 988.964343, 989.596543, 992.602043, 993.117943, 993.7612429999999, 994.3083429999999, 994.401343, 995.340643, 998.5397429999999, 999.000443, 1000.593343, 1000.6357429999999, 1001.2584429999999, 1001.334243, 1003.663543, 1004.3092429999999, 1006.184043, 1006.665143, 1007.890843, 1010.872443, 1011.4528429999999, 1012.454843, 1012.648543, 1012.825443, 1013.512643, 1015.107543, 1017.5950429999999, 1018.145843, 1022.399143, 1023.348243, 1023.4881429999999, 1023.703943, 1023.892743, 1024.563343, 1026.059343, 1028.420643, 1028.633143, 1028.928743, 1029.316843, 1032.945843, 1036.954843, 1037.770843, 1038.025643, 1039.704743, 1040.994643, 1041.791843, 1042.953943, 1043.592843, 1043.708343, 1050.5268429999999, 1052.079443, 1052.649543, 1052.684943, 1055.256443, 1060.528543, 1061.940143, 1062.041143, 1065.194343, 1065.722143, 1066.910543, 1068.947743, 1073.2487429999999, 1073.632343, 1074.822043, 1075.200543, 1076.141143, 1077.981343, 1079.280243, 1081.3022429999999, 1081.893343, 1085.848643, 1086.145043, 1087.4752429999999, 1087.7197429999999, 1089.479843, 1094.0820429999999, 1094.511343, 1094.685543, 1096.808443, 1097.674643, 1100.9245429999999, 1101.318843, 1101.795743, 1106.028343, 1106.752143, 1108.441043, 1109.063443, 1110.809843, 1112.244343, 1115.205043, 1115.793543, 1116.281043, 1117.533043, 1120.772743, 1120.943343, 1122.063243, 1126.348043, 1126.588843, 1127.782343, 1127.8493429999999, 1132.284543, 1133.968643, 1134.083643, 1134.812743, 1138.425743, 1140.277243, 1142.692943, 1143.479343, 1143.508343, 1146.1264429999999, 1147.5087429999999, 1148.0667429999999, 1149.901943, 1150.083443, 1154.467843, 1155.7469429999999, 1156.378443, 1157.213743, 1157.893943, 1161.299843, 1163.574343, 1165.368743, 1166.530043, 1167.6391429999999, 1168.554843, 1169.4887429999999, 1170.586043, 1175.934543, 1178.156243, 1178.610543, 1178.921943, 1179.145843, 1179.573643, 1187.012243, 1187.262843, 1187.406943, 1189.229243, 1191.636343, 1191.886343, 1194.272843, 1195.991843, 1199.937643, 1200.047843, 1201.449543, 1204.830543, 1205.389743, 1209.462843, 1210.561743, 1212.594143, 1213.386543, 1214.1418429999999, 1214.154443, 1219.073643, 1221.015143, 1222.636843, 1223.881443, 1227.9897429999999, 1228.923443, 1229.525743, 1233.922243, 1234.473743, 1235.452343, 1235.808943, 1240.449043, 1241.093643, 1245.171843, 1245.478243, 1246.061043, 1246.452043, 1249.927343, 1252.341943, 1255.264743, 1257.632643, 1258.441243, 1258.452643, 1259.841943, 1260.042343, 1265.0739429999999, 1266.0141429999999, 1267.6002429999999, 1269.445043, 1270.469243, 1274.139643, 1274.924043, 1276.423843, 1281.449543, 1283.915543, 1284.392443, 1284.828943, 1287.5820429999999, 1288.2985429999999, 1290.2225429999999, 1290.778043, 1294.631843, 1296.719843, 1298.903343, 1301.526443, 1302.276343, 1302.490343, 1303.954143, 1309.437843, 1310.548043, 1314.348243, 1316.070543, 1316.260843, 1317.751243, 1317.935543, 1322.146643, 1328.689343, 1329.097243, 1329.546543, 1329.820643, 1333.801543, 1334.257043, 1338.509343, 1338.584243, 1339.321543, 1341.807143, 1345.492643, 1347.315443, 1348.668043, 1349.854643, 1352.199543, 1352.210943, 1356.886143, 1358.197243, 1361.030843, 1362.906043, 1364.2469429999999, 1366.268943, 1369.804343, 1374.347243, 1374.5440429999999, 1374.9235429999999, 1375.318443, 1377.875843, 1380.191043, 1381.156143, 1387.996743, 1388.038843, 1388.494543, 1390.815443, 1391.645243, 1397.819343, 1398.548343, 1399.623443, 1400.494943, 1402.532943, )
B = (679.070505, 763, 1358.141011, 4000)
# A seems to be already sorted, otherwise we would have to sort it
# first
middles = [(A[i] + A[i+1])/2 for i in range(len(A)-1) ]
closest = [A[bisect(middles, value)] for value in B]
print(closest)
# [762.859793, 762.859793, 1358.197243, 1402.532943]
bisect(middles, value) will return the index at which you would have to insert value in the list of middles, such that it will be just between two middle values. This way, it is guaranteed to be closest to the value between these middles.
The creation of the middles list is O(size of A), each subsequent use of bisect will be rather fast as it uses a bisection algorithm.
Make a list of pairs, (difference, number) for each element in B. For each item in A, compute its difference with each item in B and if that difference is smaller than the stored one, replace the stored data.
A = (762.859793, 763.544183, 764.272883, 766.190053, 767.108693, 767.123893, 768.933893, 770.619013, 771.375173, 771.522083, 774.9478730000001, 775.138843, 775.463863, 776.332693, 776.466303, 777.650083, 778.3751129999999, 781.922783, 782.592033, 784.224423, 784.779903, 785.2743429999999, 787.098133, 787.8258030000001, 787.8593030000001, 788.194393, 790.939383, 791.379753, 793.572803, 796.093073, 796.720693, 796.852473, 798.519643, 800.145803, 800.758723, 801.239613, 801.332453, 801.819333, 805.290823, 806.404353, 807.995663, 808.683403, 810.0645030000001, 810.695673, 810.7933929999999, 811.150203, 814.270803, 814.706783, 815.6527629999999, 819.127253, 819.820733, 821.030753, 821.166193, 823.111683, 824.637013, 826.707083, 826.997243, 827.441463, 828.529713, 830.364193, 831.241203, 831.2571929999999, 832.635243, 832.9749730000001, 834.222713, 837.932063, 838.250483, 838.695363, 840.0615730000001, 841.246143, 841.300043, 841.5062230000001, 844.7620730000001, 845.1879230000001, 846.261283, 847.139523, 849.0279429999999, 850.5646929999999, 850.726553, 851.8736529999999, 853.9559830000001, 854.171933, 857.674983, 858.035633, 858.327443, 858.9393230000001, 859.365103, 860.0646929999999, 861.035573, 861.496883, 864.127743, 866.558833, 867.0207429999999, 868.135673, 869.329093, 870.5860529999999, 871.1149230000001, 871.691343, 872.156483, 875.2681230000001, 875.921803, 876.6624029999999, 879.2358929999999, 879.7697029999999, 880.6190730000001, 882.563473, 883.1651529999999, 883.6522130000001, 884.311393, 886.585383, 888.0967929999999, 889.6549630000001, 890.564143, 891.0488230000001, 892.887933, 893.3944630000001, 896.817743, 897.9441429999999, 898.577573, 898.681813, 900.280573, 901.308643, 903.324453, 905.128653, 905.9143029999999, 906.190423, 906.641533, 907.232963, 909.896413, 912.3604630000001, 913.905633, 914.0172630000001, 915.186703, 915.764243, 917.266943, 921.000943, 921.958943, 923.0551429999999, 924.235543, 924.545743, 925.517043, 925.8717429999999, 927.906943, 931.119243, 931.856543, 933.8970429999999, 933.929043, 934.2806429999999, 935.171843, 936.956243, 938.8980429999999, 941.066643, 941.633743, 942.986043, 944.437243, 945.839343, 948.351143, 949.9056429999999, 950.118243, 950.587543, 951.3468429999999, 954.000243, 954.205843, 954.944843, 955.703343, 959.0683429999999, 959.298543, 960.0447429999999, 961.379543, 965.161943, 967.108743, 967.7327429999999, 968.199043, 968.438843, 969.973543, 970.540543, 971.3676429999999, 972.284543, 973.603743, 976.753143, 978.1982429999999, 978.214943, 978.545843, 979.070643, 980.912243, 982.989643, 985.355943, 986.1942429999999, 986.3504429999999, 986.4826429999999, 988.964343, 989.596543, 992.602043, 993.117943, 993.7612429999999, 994.3083429999999, 994.401343, 995.340643, 998.5397429999999, 999.000443, 1000.593343, 1000.6357429999999, 1001.2584429999999, 1001.334243, 1003.663543, 1004.3092429999999, 1006.184043, 1006.665143, 1007.890843, 1010.872443, 1011.4528429999999, 1012.454843, 1012.648543, 1012.825443, 1013.512643, 1015.107543, 1017.5950429999999, 1018.145843, 1022.399143, 1023.348243, 1023.4881429999999, 1023.703943, 1023.892743, 1024.563343, 1026.059343, 1028.420643, 1028.633143, 1028.928743, 1029.316843, 1032.945843, 1036.954843, 1037.770843, 1038.025643, 1039.704743, 1040.994643, 1041.791843, 1042.953943, 1043.592843, 1043.708343, 1050.5268429999999, 1052.079443, 1052.649543, 1052.684943, 1055.256443, 1060.528543, 1061.940143, 1062.041143, 1065.194343, 1065.722143, 1066.910543, 1068.947743, 1073.2487429999999, 1073.632343, 1074.822043, 1075.200543, 1076.141143, 1077.981343, 1079.280243, 1081.3022429999999, 1081.893343, 1085.848643, 1086.145043, 1087.4752429999999, 1087.7197429999999, 1089.479843, 1094.0820429999999, 1094.511343, 1094.685543, 1096.808443, 1097.674643, 1100.9245429999999, 1101.318843, 1101.795743, 1106.028343, 1106.752143, 1108.441043, 1109.063443, 1110.809843, 1112.244343, 1115.205043, 1115.793543, 1116.281043, 1117.533043, 1120.772743, 1120.943343, 1122.063243, 1126.348043, 1126.588843, 1127.782343, 1127.8493429999999, 1132.284543, 1133.968643, 1134.083643, 1134.812743, 1138.425743, 1140.277243, 1142.692943, 1143.479343, 1143.508343, 1146.1264429999999, 1147.5087429999999, 1148.0667429999999, 1149.901943, 1150.083443, 1154.467843, 1155.7469429999999, 1156.378443, 1157.213743, 1157.893943, 1161.299843, 1163.574343, 1165.368743, 1166.530043, 1167.6391429999999, 1168.554843, 1169.4887429999999, 1170.586043, 1175.934543, 1178.156243, 1178.610543, 1178.921943, 1179.145843, 1179.573643, 1187.012243, 1187.262843, 1187.406943, 1189.229243, 1191.636343, 1191.886343, 1194.272843, 1195.991843, 1199.937643, 1200.047843, 1201.449543, 1204.830543, 1205.389743, 1209.462843, 1210.561743, 1212.594143, 1213.386543, 1214.1418429999999, 1214.154443, 1219.073643, 1221.015143, 1222.636843, 1223.881443, 1227.9897429999999, 1228.923443, 1229.525743, 1233.922243, 1234.473743, 1235.452343, 1235.808943, 1240.449043, 1241.093643, 1245.171843, 1245.478243, 1246.061043, 1246.452043, 1249.927343, 1252.341943, 1255.264743, 1257.632643, 1258.441243, 1258.452643, 1259.841943, 1260.042343, 1265.0739429999999, 1266.0141429999999, 1267.6002429999999, 1269.445043, 1270.469243, 1274.139643, 1274.924043, 1276.423843, 1281.449543, 1283.915543, 1284.392443, 1284.828943, 1287.5820429999999, 1288.2985429999999, 1290.2225429999999, 1290.778043, 1294.631843, 1296.719843, 1298.903343, 1301.526443, 1302.276343, 1302.490343, 1303.954143, 1309.437843, 1310.548043, 1314.348243, 1316.070543, 1316.260843, 1317.751243, 1317.935543, 1322.146643, 1328.689343, 1329.097243, 1329.546543, 1329.820643, 1333.801543, 1334.257043, 1338.509343, 1338.584243, 1339.321543, 1341.807143, 1345.492643, 1347.315443, 1348.668043, 1349.854643, 1352.199543, 1352.210943, 1356.886143, 1358.197243, 1361.030843, 1362.906043, 1364.2469429999999, 1366.268943, 1369.804343, 1374.347243, 1374.5440429999999, 1374.9235429999999, 1375.318443, 1377.875843, 1380.191043, 1381.156143, 1387.996743, 1388.038843, 1388.494543, 1390.815443, 1391.645243, 1397.819343, 1398.548343, 1399.623443, 1400.494943, 1402.532943, )
B = (679.070505, 1358.141011)
stored = [(float('inf'), None)]*len(B)
for a in A:
for i, ((curr_diff, _), b) in enumerate(zip(stored, B)):
diff = abs(a-b)
if diff < curr_diff:
stored[i] = (diff, a)
This gives me
[(83.78928799999994, 762.859793), (0.056232000000136395, 1358.197243)]