<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Near neighbors mean calculation in Discussions</title>
    <link>https://community.jmp.com/t5/Discussions/Near-neighbors-mean-calculation/m-p/345333#M59575</link>
    <description>&lt;P&gt;Hi everyone,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have to calculate the mean of neighbors dies/components on a microelectronic wafer for example on a 3x3 box like that:&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="box.jpg" style="width: 207px;"&gt;&lt;img src="https://community.jmp.com/t5/image/serverpage/image-id/29176iDF9CE78FAE2F4252/image-size/large?v=v2&amp;amp;px=999" role="button" title="box.jpg" alt="box.jpg" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;The reference component is in the centre of a box and therefore surrounded by 8 components whose mean I want to calculate, for each row of the data table (then calculate the same thing for a 5x5 box and 16 neighbors...)&lt;/P&gt;&lt;P&gt;It will be used as a new feature for machine learning.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: inherit;"&gt;My code is:&lt;/SPAN&gt;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;dt = current data table();
&lt;BR /&gt;// building the KD table
matrice = (dt:row &amp;lt;&amp;lt; getvalues) || (dt:col &amp;lt;&amp;lt; getvalues);

table = KDTable(matrice);

for( i=1, i&amp;lt;=nrows(dt),i++,
    // get the 3x3 neighbors
	neighbors = table &amp;lt;&amp;lt; Knearestrows(8,i);
	neighbors_number = neighbors[1,1];
	// Select the neighbors and get their metrics values and calculate the mean&lt;BR /&gt;    dt &amp;lt;&amp;lt; select rows (neighbors_number );	
	mean_selection = Col Mean( If( Selected(), :metric, . ) );	
	:mean_metric[i] = mean_selection;
	dt &amp;lt;&amp;lt; clear select
	
);&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Has someone a smart way to do that calculation more efficiently?&lt;/P&gt;&lt;P&gt;thanks!&lt;/P&gt;</description>
    <pubDate>Fri, 09 Jun 2023 22:02:16 GMT</pubDate>
    <dc:creator>Franck_R</dc:creator>
    <dc:date>2023-06-09T22:02:16Z</dc:date>
    <item>
      <title>Near neighbors mean calculation</title>
      <link>https://community.jmp.com/t5/Discussions/Near-neighbors-mean-calculation/m-p/345333#M59575</link>
      <description>&lt;P&gt;Hi everyone,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have to calculate the mean of neighbors dies/components on a microelectronic wafer for example on a 3x3 box like that:&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="box.jpg" style="width: 207px;"&gt;&lt;img src="https://community.jmp.com/t5/image/serverpage/image-id/29176iDF9CE78FAE2F4252/image-size/large?v=v2&amp;amp;px=999" role="button" title="box.jpg" alt="box.jpg" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;The reference component is in the centre of a box and therefore surrounded by 8 components whose mean I want to calculate, for each row of the data table (then calculate the same thing for a 5x5 box and 16 neighbors...)&lt;/P&gt;&lt;P&gt;It will be used as a new feature for machine learning.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: inherit;"&gt;My code is:&lt;/SPAN&gt;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;dt = current data table();
&lt;BR /&gt;// building the KD table
matrice = (dt:row &amp;lt;&amp;lt; getvalues) || (dt:col &amp;lt;&amp;lt; getvalues);

table = KDTable(matrice);

for( i=1, i&amp;lt;=nrows(dt),i++,
    // get the 3x3 neighbors
	neighbors = table &amp;lt;&amp;lt; Knearestrows(8,i);
	neighbors_number = neighbors[1,1];
	// Select the neighbors and get their metrics values and calculate the mean&lt;BR /&gt;    dt &amp;lt;&amp;lt; select rows (neighbors_number );	
	mean_selection = Col Mean( If( Selected(), :metric, . ) );	
	:mean_metric[i] = mean_selection;
	dt &amp;lt;&amp;lt; clear select
	
);&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Has someone a smart way to do that calculation more efficiently?&lt;/P&gt;&lt;P&gt;thanks!&lt;/P&gt;</description>
      <pubDate>Fri, 09 Jun 2023 22:02:16 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Near-neighbors-mean-calculation/m-p/345333#M59575</guid>
      <dc:creator>Franck_R</dc:creator>
      <dc:date>2023-06-09T22:02:16Z</dc:date>
    </item>
    <item>
      <title>Re: Near neighbors mean calculation</title>
      <link>https://community.jmp.com/t5/Discussions/Near-neighbors-mean-calculation/m-p/345551#M59605</link>
      <description>&lt;P&gt;I'm not sure how the efficiency compares, but here is an alternative for you to try out.&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;Using begin/end data update() will help for any method&lt;/LI&gt;
&lt;LI&gt;Using Nearest Neighbor is clever (and maybe even more efficient) but be aware that it will always return the 8 nearest, even at the edges of the wafer where the neighbors will all be on one side.&lt;/LI&gt;
&lt;LI&gt;Matrix subscripting may be more efficient than Col Mean().&lt;/LI&gt;
&lt;/UL&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;dt = current data table();
metrics = dt[0, "metric"];
rows = dt[0, "row"];
cols = dt[0, "col"];
d = 1;	// 1 =&amp;gt; 3x3, 2 =&amp;gt; 5x5

dt &amp;lt;&amp;lt; Begin Data Update();
For Each Row(
	neighbors = Loc( :col - d &amp;lt;= cols &amp;lt;= :col + d &amp;amp; :row - d &amp;lt;= rows &amp;lt;= :row + d );
	mean all = Mean( metrics[neighbors] );
	nn = N Rows( neighbors );
	mean without center = (mean all * nn - :metric) / (nn - 1);
	:mm = mean without center;
);
dt &amp;lt;&amp;lt; End Data Update();
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 04 Jan 2021 20:32:23 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Near-neighbors-mean-calculation/m-p/345551#M59605</guid>
      <dc:creator>XanGregg</dc:creator>
      <dc:date>2021-01-04T20:32:23Z</dc:date>
    </item>
    <item>
      <title>Re: Near neighbors mean calculation</title>
      <link>https://community.jmp.com/t5/Discussions/Near-neighbors-mean-calculation/m-p/345561#M59612</link>
      <description>&lt;P&gt;I'm pretty sure the size of the problem will determine whether the kdtable() or loc() approach is better. I'd guess loc() will win for a 10x10 or smaller matrix, and kdtable for 100x100 or bigger.&lt;/P&gt;&lt;P&gt;Xan's point about the edges is important. Loc() might be the easiest way to get the right answer.&lt;/P&gt;</description>
      <pubDate>Mon, 04 Jan 2021 23:09:27 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Near-neighbors-mean-calculation/m-p/345561#M59612</guid>
      <dc:creator>Craige_Hales</dc:creator>
      <dc:date>2021-01-04T23:09:27Z</dc:date>
    </item>
    <item>
      <title>Re: Near neighbors mean calculation</title>
      <link>https://community.jmp.com/t5/Discussions/Near-neighbors-mean-calculation/m-p/345696#M59625</link>
      <description>&lt;P&gt;Thank you very much it's very interesting!&lt;BR /&gt;I sometimes use "begin data update" but I didn't think of it here: good point. With that, the Loc() solution is actually a bit slower than knearestrows() if you add the begin update I find for 20,000 lines:&lt;/P&gt;
&lt;P&gt;old script without begin update: 116s !&lt;/P&gt;
&lt;P&gt;knearestrows() with begin update: 5s&lt;/P&gt;
&lt;P&gt;Loc() with begin update: 14s&lt;/P&gt;
&lt;P&gt;but for the knearestrows I&amp;nbsp;had to change the way I calculate the mean since we can no longer use select rows with the begin/end update.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;For the edges you are perfectly right, I had thought of putting in:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;neighbors = table &amp;lt;&amp;lt; Knearestrows( {8, 1.5}, i );&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;BR /&gt;To avoid this problem using a limited radius&lt;/P&gt;
&lt;P&gt;Thanks again!&lt;/P&gt;
&lt;DIV&gt;
&lt;DIV class="gtx-trans-icon"&gt;&amp;nbsp;&lt;/DIV&gt;
&lt;/DIV&gt;
&lt;DIV&gt;
&lt;DIV class="gtx-trans-icon"&gt;&amp;nbsp;&lt;/DIV&gt;
&lt;/DIV&gt;</description>
      <pubDate>Tue, 05 Jan 2021 20:01:32 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Near-neighbors-mean-calculation/m-p/345696#M59625</guid>
      <dc:creator>Franck_R</dc:creator>
      <dc:date>2021-01-05T20:01:32Z</dc:date>
    </item>
    <item>
      <title>Re: Near neighbors mean calculation</title>
      <link>https://community.jmp.com/t5/Discussions/Near-neighbors-mean-calculation/m-p/345701#M59627</link>
      <description>&lt;P&gt;on the other hand I realize that the solution of the radius limit is not perfect because we still take an extra element in the array&lt;BR /&gt;for example a radius of 1.5 contains a value of 2 because this is the value that stops the neighbors search... So it doesn't give me exactly what I want, I'll have to search a little more!&lt;/P&gt;</description>
      <pubDate>Tue, 05 Jan 2021 09:00:03 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Near-neighbors-mean-calculation/m-p/345701#M59627</guid>
      <dc:creator>Franck_R</dc:creator>
      <dc:date>2021-01-05T09:00:03Z</dc:date>
    </item>
    <item>
      <title>Re: Near neighbors mean calculation</title>
      <link>https://community.jmp.com/t5/Discussions/Near-neighbors-mean-calculation/m-p/345727#M59633</link>
      <description>&lt;P&gt;You could pad enough rows and cols of missing values, left, right, top, bottom.&amp;nbsp; Depending how far you go with extending the neighborhood, you might have 8 complete missing value wafers surrounding the real wafer.&amp;nbsp;&lt;/P&gt;&lt;P&gt;There is another way to do this, also requiring the dummy rows and cols, which will be really fast:&lt;/P&gt;&lt;P&gt;JMP 2D matrices can be indexed as 1D linear matrices.&lt;/P&gt;&lt;P&gt;[ 1 2,&lt;/P&gt;&lt;P&gt;&amp;nbsp; 3 4,&lt;/P&gt;&lt;P&gt;&amp;nbsp; 5 6 ] (3 rows, 2 cols)&amp;nbsp;&lt;/P&gt;&lt;P&gt;also looks like&amp;nbsp;&lt;/P&gt;&lt;P&gt;[ 1,&lt;/P&gt;&lt;P&gt;&amp;nbsp; 2,&lt;/P&gt;&lt;P&gt;&amp;nbsp; 3,&lt;/P&gt;&lt;P&gt;&amp;nbsp; 4,&lt;/P&gt;&lt;P&gt;&amp;nbsp; 5,&lt;/P&gt;&lt;P&gt;&amp;nbsp; 6] (6 rows, 1 col when using one subscript)&lt;/P&gt;&lt;P&gt;If the wafer is NRows x NCols then put it in the middle of a 3NRows x 3NCols matrix (I'll call it M3).&lt;/P&gt;&lt;P&gt;You can make a matrix of 1D subscripts to index M3 by adding 1 to move horizontally and by adding 3NCols to move vertically. To extract a 3x3 submatrix from M3, use the index matrix and the shape() function.&lt;/P&gt;&lt;P&gt;something like this:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;M3 = [. . . . . . . . .,
. . . . . . . . .,
. . . . . . . . .,
. . . 1 2 3 . . .,
. . . 4 5 6 . . .,
. . . 7 8 9 . . .,
. . . . . . . . .,
. . . . . . . . .,
. . . . . . . . .];

indexer2x2 = [1 2 10 11]; // top left 2x2 sub-matrix

For( x = 2, x &amp;lt;= 5, x += 1,
	For( y = 2, y &amp;lt;= 5, y += 1,
		Write( "\!n ", x, " ", y, " ", Shape( M3[indexer2x2 + x + y * 9], 2 ) );
	
	)
);&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;FONT face="courier new,courier"&gt;&lt;STRONG&gt;2 2 [. ., . 1]&lt;/STRONG&gt;&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;&lt;STRONG&gt;2 3 [. 1, . 4]&lt;/STRONG&gt;&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;&lt;STRONG&gt;2 4 [. 4, . 7]&lt;/STRONG&gt;&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;&lt;STRONG&gt;2 5 [. 7, . .]&lt;/STRONG&gt;&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;&lt;STRONG&gt;3 2 [. ., 1 2]&lt;/STRONG&gt;&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;&lt;STRONG&gt;3 3 [1 2, 4 5]&lt;/STRONG&gt;&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;&lt;STRONG&gt;3 4 [4 5, 7 8]&lt;/STRONG&gt;&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;&lt;STRONG&gt;3 5 [7 8, . .]&lt;/STRONG&gt;&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;&lt;STRONG&gt;4 2 [. ., 2 3]&lt;/STRONG&gt;&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;&lt;STRONG&gt;4 3 [2 3, 5 6]&lt;/STRONG&gt;&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;&lt;STRONG&gt;4 4 [5 6, 8 9]&lt;/STRONG&gt;&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;&lt;STRONG&gt;4 5 [8 9, . .]&lt;/STRONG&gt;&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;&lt;STRONG&gt;5 2 [. ., 3 .]&lt;/STRONG&gt;&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;&lt;STRONG&gt;5 3 [3 ., 6 .]&lt;/STRONG&gt;&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;&lt;STRONG&gt;5 4 [6 ., 9 .]&lt;/STRONG&gt;&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;&lt;STRONG&gt;5 5 [9 ., . .]&lt;/STRONG&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;You might need this too:&amp;nbsp;&lt;LI-MESSAGE title="Using Loc with a 2D Matrix" uid="195207" url="https://community.jmp.com/t5/JSL-Cookbook/Using-Loc-with-a-2D-Matrix/m-p/195207#U195207" discussion_style_icon_css="lia-mention-container-editor-message lia-img-icon-tkb-thread lia-fa-icon lia-fa-tkb lia-fa-thread lia-fa"&gt;&lt;/LI-MESSAGE&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;You can make indexer3x3, etc and just reuse the M3 for each level. the for loop x=3, x&amp;lt;=5 needs to go a bit further each time.&amp;nbsp;&lt;/P&gt;&lt;P&gt;edit: the shape function may be unneeded if you are just getting the mean of the indexed elements, but it helps show what happened above. more: JSL matrices have special behavior when the index is less than 1 and you will not get the error message you might hope for! if the indexer contains a zero (or -1 etc) it will seem very strange.&lt;/P&gt;</description>
      <pubDate>Tue, 05 Jan 2021 13:01:44 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Near-neighbors-mean-calculation/m-p/345727#M59633</guid>
      <dc:creator>Craige_Hales</dc:creator>
      <dc:date>2021-01-05T13:01:44Z</dc:date>
    </item>
    <item>
      <title>Re: Near neighbors mean calculation</title>
      <link>https://community.jmp.com/t5/Discussions/Near-neighbors-mean-calculation/m-p/345798#M59647</link>
      <description>&lt;P&gt;Hi &lt;a href="https://community.jmp.com/t5/user/viewprofilepage/user-id/16285"&gt;@Franck_R&lt;/a&gt; ,&lt;/P&gt;
&lt;P&gt;Perhaps this can also help you:&amp;nbsp;&lt;LI-MESSAGE title="Add-In: Spatial Data Analysis" uid="21907" url="https://community.jmp.com/t5/JMP-Add-Ins/Add-In-Spatial-Data-Analysis/m-p/21907#U21907" discussion_style_icon_css="lia-mention-container-editor-message lia-img-icon-tkb-thread lia-fa-icon lia-fa-tkb lia-fa-thread lia-fa"&gt;&lt;/LI-MESSAGE&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If what you are looking for is the spatial correlation, Moran's I is perhaps the most basic concept.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Ron&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 05 Jan 2021 20:06:41 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Near-neighbors-mean-calculation/m-p/345798#M59647</guid>
      <dc:creator>ron_horne</dc:creator>
      <dc:date>2021-01-05T20:06:41Z</dc:date>
    </item>
    <item>
      <title>Re: Near neighbors mean calculation</title>
      <link>https://community.jmp.com/t5/Discussions/Near-neighbors-mean-calculation/m-p/345938#M59665</link>
      <description>&lt;P&gt;Thanks for all this, I'm going to dig more deeply into it&lt;/P&gt;</description>
      <pubDate>Wed, 06 Jan 2021 07:44:55 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Near-neighbors-mean-calculation/m-p/345938#M59665</guid>
      <dc:creator>Franck_R</dc:creator>
      <dc:date>2021-01-06T07:44:55Z</dc:date>
    </item>
  </channel>
</rss>

