<div dir="ltr"><div class="gmail_extra"><div class="gmail_quote">On Wed, May 17, 2017 at 9:50 AM, Nissim Derdiger <span dir="ltr"><<a href="mailto:NissimD@elspec-ltd.com" target="_blank">NissimD@elspec-ltd.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">






<div>
<font face="Times New Roman" size="3"><span style="font-size:12pt">
<div>Hi,</div>
<div style="margin-top:5pt;margin-bottom:5pt">In my script, I need to compare big NumPy arrays (2D or 3D), and return a list of all cells with difference bigger than a defined threshold. </div>
<div style="margin-top:5pt;margin-bottom:5pt">The compare itself can be done easily done with "allclose" function, like that:</div>
<div><font face="Courier New" size="2" color="#548DD4"><span style="font-size:10pt">Threshold = 0.1</span></font></div>
<div><font face="Courier New" size="2" color="#548DD4"><span style="font-size:10pt">if (np.allclose(Arr1, Arr2, Threshold, equal_nan=True)):</span></font></div>
<div><font face="Courier New" size="2" color="#548DD4"><span style="font-size:10pt">    Print('Same')</span></font></div>
<div style="margin-top:5pt;margin-bottom:5pt">But this compare does not return <b><u>which</u></b> cells are not the same.</div>
<div style="margin-top:5pt;margin-bottom:5pt"><font face="Calibri" size="2"><span style="font-size:11pt"> </span></font></div>
<div style="margin-top:5pt;margin-bottom:5pt">The easiest (yet naive) way to know which cells are not the same is to use a simple for loops code like this one:</div>
<div><font face="Courier New" size="2" color="#548DD4"><span style="font-size:10pt">def CheckWhichCellsAreNotEqualInAr<wbr>rays(Arr1,Arr2,Threshold):</span></font></div>
<div><font face="Courier New" size="2" color="#548DD4"><span style="font-size:10pt">   if not Arr1.shape == Arr2.shape:</span></font></div>
<div><font face="Courier New" size="2" color="#548DD4"><span style="font-size:10pt">       return ['Arrays size not the same']</span></font></div>
<div><font face="Courier New" size="2" color="#548DD4"><span style="font-size:10pt">   Dimensions = Arr1.shape  </span></font></div>
<div><font face="Courier New" size="2" color="#548DD4"><span style="font-size:10pt">   Diff = []</span></font></div>
<div><font face="Courier New" size="2" color="#548DD4"><span style="font-size:10pt">   for i in range(Dimensions [0]):</span></font></div>
<div><font face="Courier New" size="2" color="#548DD4"><span style="font-size:10pt">       for j in range(Dimensions [1]):</span></font></div>
<div><font face="Courier New" size="2" color="#548DD4"><span style="font-size:10pt">           if not np.allclose(Arr1[i][j], Arr2[i][j], Threshold, equal_nan=True):</span></font></div>
<div><font face="Courier New" size="2" color="#548DD4"><span style="font-size:10pt">               Diff.append(',' + str(i) + ',' + str(j) + ',' + str(Arr1[i,j]) + ',' </span></font></div>
<div><font face="Courier New" size="2" color="#548DD4"><span style="font-size:10pt">               + str(Arr2[i,j]) + ',' + str(Threshold) + ',Fail\n')</span></font></div>
<div><font face="Courier New" size="2" color="#548DD4"><span style="font-size:10pt">       return Diff</span></font></div>
<div style="margin-top:5pt;margin-bottom:5pt">(and same for 3D arrays - with 1 more for loop)</div>
<div style="margin-top:5pt;margin-bottom:5pt">This way is very slow when the Arrays are big and full of none-equal cells.</div>
<div style="margin-top:5pt;margin-bottom:5pt"><font face="Calibri" size="2"><span style="font-size:11pt"> </span></font></div>
<div style="margin-top:5pt;margin-bottom:5pt">Is there a fast straight forward way in case they are not the same - to get a list of the uneven cells? maybe some built-in function in the NumPy itself?</div></span></font></div></blockquote></div><div class="gmail_extra"><br></div>Use `close_mask = np.isclose(Arr1, Arr2, Threshold, equal_nan=True)` to return a boolean mask the same shape as the arrays which is True where the elements are close and False where they are not. You can invert it to get a boolean mask which is True where they are "far" with respect to the threshold: `far_mask = ~close_mask`. Then you can use `i_idx, j_idx = np.nonzero(far_mask)` to get arrays of the `i` and `j` indices where the values are far. For example:</div><div class="gmail_extra"><br></div><div class="gmail_extra">for i, j in zip(i_idx, j_idx):</div><div class="gmail_extra">    print("{0}, {1}, {2}, {3}, {4}, Fail".format(i, j, Arr1[i, j], Arr2[i, j], Threshold))<br clear="all"><div><br></div>-- <br><div class="gmail_signature" data-smartmail="gmail_signature">Robert Kern</div>
</div></div>