在多元范围内寻找最接近的项目

问题描述

我可以研究的任何方向本身都会很棒,不确定是否可以使用 scikit 或 numpy 解决这个问题。我正在尝试解决一个优化问题,在包含 X 元素的数组中,返回最接近最佳范围的项目。

最佳范围:[613.47,45.235,6.5,36.39]

[557.7000000000002,46.67503968686453,3.7847422859507507,36.209384835209164] 
[559.9763265306124,46.861468420280296,3.792026948342387,36.344933344740625] 
[562.2526530612248,47.04789715369608,3.799311610734023,36.48048185427209]   
[564.5289795918369,47.23432588711185,3.806596273125658,36.61603036380356]   
[566.8053061224491,47.42075462052762,3.813880935517294,36.75157887333503]   
[569.0816326530613,47.60718335394339,3.8211655979089305,36.88712738286649]  
[571.3579591836736,47.793612087359165,3.8284502603005652,37.022675892397956]
[573.6342857142859,47.98004082077494,3.8357349226922013,37.15822440192942]  
[575.9106122448982,48.16646955419072,3.8430195850838373,37.29377291146089]  
[578.1869387755104,48.35289828760649,3.850304247475473,37.42932142099235]   
[580.4632653061226,48.53932702102226,3.857588909867108,37.56486993052383]   
[582.7395918367348,48.72575575443804,3.864873572258744,37.70041844005529]   
[585.0159183673471,48.91218448785381,3.87215823465038,37.835966949586755]   
[587.2922448979593,49.09861322126958,3.8794428970420154,37.971515459118216] 
[589.5685714285715,49.285041954685354,3.8867275594336514,38.107063968649676]
[591.8448979591839,49.47147068810114,3.894012221825287,38.24261247818115]   
[594.1212244897961,49.65789942151691,3.9012968842169222,38.37816098771262]  
[596.3975510204083,49.84432815493268,3.908581546608559,38.513709497244086]  
[598.6738775510206,50.03075688834845,3.9158662090001943,38.64925800677554]  
[600.9502040816327,50.21718562176422,3.92315087139183,38.784806516307015]   
[603.226530612245,50.40361435518,3.9304355337834656,38.92035502583848]
[605.5028571428572,50.59004308859576,3.9377201961751016,39.05590353536994]
[607.7791836734694,50.77647182201154,3.9450048585667368,39.19145204490142]
[610.0555102040819,50.96290055542733,3.9522895209583724,39.327000554432885]
[612.3318367346941,51.14932928884309,3.9595741833500084,39.46254906396434]
[614.6081632653063,51.33575802225887,3.9668588457416445,39.59809757349581]
[616.8844897959184,51.52218675567464,3.9741435081332797,39.733646083027274]
[619.1608163265307,51.70861548909041,3.9814281705249157,39.86919459255874]
[621.4371428571429,51.89504422250618,3.988712832916551,40.0047431020902]
[623.7134693877551,52.08147295592197,3.9959974953081865,40.14029161162166]
[625.9897959183675,52.267901689337734,4.0032821576998225,40.275840121153145]
[628.2661224489798,52.45433042275351,4.010566820091459,40.411388630684606]
[630.542448979592,52.64075915616929,4.017851482483095,40.54693714021608]
[632.8187755102042,52.82718788958506,4.02513614487473,40.68248564974754]
[635.0951020408164,53.01361662300082,4.032420807266366,40.818034159278994]
[637.3714285714287,53.20004535641661,4.039705469658001,40.95358266881047]
[639.647755102041,53.386474089832376,4.046990132049637,41.08913117834193]
[641.924081632653,53.57290282324814,4.054274794441273,41.224679687873405]
[644.2004081632654,53.75933155666391,4.061559456832908,41.360228197404865]
[646.4767346938777,53.9457602900797,4.068844119224544,41.49577670693633]
[648.7530612244899,54.13218902349547,4.0761287816161795,41.6313252164678]
[651.0293877551022,54.31861775691124,4.083413444007816,41.76687372599926]
[653.3057142857144,54.50504649032702,4.090698106399452,41.902422235530736]
[655.5820408163266,54.691475223742785,4.097982768791087,42.037970745062196]
[657.8583673469388,54.87790395715855,4.105267431182723,42.173519254593664]
[660.1346938775512,55.064332690574346,4.112552093574359,42.30906776412514]
[662.4110204081635,55.25076142399011,4.119836755965994,42.44461627365659]
[664.6873469387756,55.43719015740588,4.12712141835763,42.58016478318806]
[666.963673469388,55.62361889082166,4.134406080749265,42.71571329271953]
[669.2400000000002,55.81004762423743,4.141690743140901,42.85126180225099]

最优输出 = 第 25 个元素。

解决方法

您可以计算均方误差 (MSE) 来衡量最佳范围与数据之间的距离:

//---output is a HTML tag at which the input of the user should be entered-------------------------------
const output=document.querySelector('.Output');

//------I store the input (pressed keys) into an array
let input=[]
//-------------------------------------------------

//--------------keybord support--------------------
document.addEventListener('keydown',function(e) {
    if (e.key != "+" || e.key !="-" || e.key !="*" || e.key !="/") {
        let internalVariable = 0;
        input.push(parseInt(e.key));
        internalVariable=input.join('');
        output.innerHTML=internalVariable;
    }   

    if (e.key=="+") {
        console.log(typeof e.key,input)**-> Test if condition works**
    }
    
>>> o
[613.47,45.235,6.5,36.39]

>>> m
array([[557.7,46.67503969,3.78474229,36.20938484],[559.97632653,46.86146842,3.79202695,36.34493334],[562.25265306,47.04789715,3.79931161,36.48048185],...
       [664.68734694,55.43719016,4.12712142,42.58016478],[666.96367347,55.62361889,4.13440608,42.71571329],[669.24,55.81004762,4.14169074,42.8512618 ]])
,

如果最佳是指具有最接近欧几里得范数的向量,则这是一个带有 np.linalg.norm 的单行:

import numpy as np

arr = np.array([
       [557.7000000000002,46.67503968686453,3.7847422859507507,36.209384835209164],...
[669.2400000000002,55.81004762423743,4.141690743140901,42.85126180225099]
               ])

target = np.array([613.47,36.39])



idx_thenorm = np.linalg.norm((arr-target),axis=1).argmin()

print (idx_thenorm,arr[idx_thenorm])

结果:

24 [612.33183673 51.14932929 3.95957418 39.46254906]