Find the closest values of an array

Recently, I face a quite simple problem : I have an array A (100,200) containing values I want to interpolate and I have another array I (5000), containing interpolated values. How to efficiently find tthe closest interpolated values of rach elements of A in I ?
That post give me 90% of the answer. It details how to efficiently find the index of a single value v  into an array arr. The one line magic formula is :


idx = (np.abs(arr - v)).argmin()

but here v is a number and arr is a 1 dimensional array. How my problem can fit in that solution ?

By using broadcasting of course !

I must find for each dimensions of my array the one which fits the values of I. So the solution of my problem is :


idx = (np.abs(A - I[:,None,None])).argmin(axis=0)

Hence idx is idx.shape == A.shape, and it contains indexes where I has the closest values of A.

 

Advertisements

Sublime Text Extensions

If Sublime Text is become my favourite text editor, it’s principally due to the huge amount of available extensions.

I’d like to share here my default ones.

Package Control

Once sublime text installed, the first step consist into adding the package control, in order to install the following extension in a heartbeat:

Once package control install, you could install further extensions by hitting : CTRL+SHIFT+P and choose “install package”

 

My Favourite Extensions

Now, I’ll list some useful package for python development :

  • GitSavy : Manage git repository directly in Sublime Text. The interface is very clear and this truly improve the git experience. You can just take a look on the animated gif on the github page for convincing you.
  • GitGutter : Display modification between your current code and last commit
  • Ctags(*): Brings the classical vim Ctags to sublime text. You can thus directly go to function definition using the “CTRL+T CTRL+T” (twice) shortcut.
  • Origami : Split windows using keyboard shortcuts
  • SideBarEnhancements : To increase te capability of sidebar (rename files, quickview,…)
  • LaTexTools : Editing Latex into sublime text
  • Pylinter : A python linter directly integrated in Sublime Text ( Note: require a manual installation and configuration of pylint.) I stop using pylinter due to the huge amount of used CPU. I probably get back later.

Many other useful extensions can be found on https://packagecontrol.io/ ,  here and  here (French)

 

(*) Ctags also require to specify a language to operate correctly. For e.g. Python, this can be specified in the user settings of ctags with :

{
“commands”:”–languages=python”
}

Then you can just it “CTRL+T CTRL+R” to launch/reload the ctags indexation of your files.