<p dir="auto"><code class="notranslate">scipy.spatial.distance.pdist</code> and <code class="notranslate">scipy.spatial.distance.cdist</code> always return ones when the <code class="notranslate">metric</code> parameter is set to <code class="notranslate">"minkowski"</code> and <code class="notranslate">p</code> to <code class="notranslate">np.inf</code>. This doesn't happen when you set <code class="notranslate">metric</code> to <code class="notranslate">scipy.spatial.distance.minkowski</code>.</p>
<p dir="auto">This is because <code class="notranslate">pdist</code> and <code class="notranslate">cdist</code> call internally the C function <a href="https://github.com/scipy/scipy/blob/master/scipy/spatial/src/distance_impl.h#L398"><code class="notranslate">minkowski_distance</code></a> while <code class="notranslate">scipy.spatial.distance.minkowski</code> calls <code class="notranslate">numpy.linalg.norm</code>.</p>
<h4 dir="auto">Reproducing code example:</h4>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="&gt;&gt;&gt; from scipy.spatial.distance import pdist, cdist, minkowski
&gt;&gt;&gt; import numpy as np
&gt;&gt;&gt; 
&gt;&gt;&gt; x = np.random.randn(5, 1)
&gt;&gt;&gt; y = np.random.randn(5, 1)
&gt;&gt;&gt; 
&gt;&gt;&gt; print(f&quot;x = {x}&quot;)
x = [[ 0.17434791]
 [-0.59212961]
 [-0.5377257 ]
 [ 0.7551881 ]
 [ 2.12660604]]
&gt;&gt;&gt; print(f&quot;y = {y}&quot;)
y = [[-0.67732225]
 [-0.11041384]
 [ 0.87626855]
 [-2.31480127]
 [-0.79008986]]
&gt;&gt;&gt; cdist(x, y, 'minkowski', p=np.inf)
array([[1., 1., 1., 1., 1.],
       [1., 1., 1., 1., 1.],
       [1., 1., 1., 1., 1.],
       [1., 1., 1., 1., 1.],
       [1., 1., 1., 1., 1.]])
&gt;&gt;&gt; pdist(x, 'minkowski', p=np.inf)
array([1., 1., 1., 1., 1., 1., 1., 1., 1., 1.])"><pre class="notranslate"><span class="pl-c1">&gt;&gt;</span><span class="pl-c1">&gt;</span> <span class="pl-k">from</span> <span class="pl-s1">scipy</span>.<span class="pl-s1">spatial</span>.<span class="pl-s1">distance</span> <span class="pl-k">import</span> <span class="pl-s1">pdist</span>, <span class="pl-s1">cdist</span>, <span class="pl-s1">minkowski</span>
<span class="pl-c1">&gt;&gt;</span><span class="pl-c1">&gt;</span> <span class="pl-k">import</span> <span class="pl-s1">numpy</span> <span class="pl-k">as</span> <span class="pl-s1">np</span>
<span class="pl-c1">&gt;&gt;</span><span class="pl-c1">&gt;</span> 
<span class="pl-c1">&gt;&gt;</span><span class="pl-c1">&gt;</span> <span class="pl-s1">x</span> <span class="pl-c1">=</span> <span class="pl-s1">np</span>.<span class="pl-s1">random</span>.<span class="pl-en">randn</span>(<span class="pl-c1">5</span>, <span class="pl-c1">1</span>)
<span class="pl-c1">&gt;&gt;</span><span class="pl-c1">&gt;</span> <span class="pl-s1">y</span> <span class="pl-c1">=</span> <span class="pl-s1">np</span>.<span class="pl-s1">random</span>.<span class="pl-en">randn</span>(<span class="pl-c1">5</span>, <span class="pl-c1">1</span>)
<span class="pl-c1">&gt;&gt;</span><span class="pl-c1">&gt;</span> 
<span class="pl-c1">&gt;&gt;</span><span class="pl-c1">&gt;</span> <span class="pl-en">print</span>(<span class="pl-s">f"x = <span class="pl-s1"><span class="pl-kos">{</span><span class="pl-s1">x</span><span class="pl-kos">}</span></span>"</span>)
<span class="pl-s1">x</span> <span class="pl-c1">=</span> [[ <span class="pl-c1">0.17434791</span>]
 [<span class="pl-c1">-</span><span class="pl-c1">0.59212961</span>]
 [<span class="pl-c1">-</span><span class="pl-c1">0.5377257</span> ]
 [ <span class="pl-c1">0.7551881</span> ]
 [ <span class="pl-c1">2.12660604</span>]]
<span class="pl-c1">&gt;&gt;</span><span class="pl-c1">&gt;</span> <span class="pl-en">print</span>(<span class="pl-s">f"y = <span class="pl-s1"><span class="pl-kos">{</span><span class="pl-s1">y</span><span class="pl-kos">}</span></span>"</span>)
<span class="pl-s1">y</span> <span class="pl-c1">=</span> [[<span class="pl-c1">-</span><span class="pl-c1">0.67732225</span>]
 [<span class="pl-c1">-</span><span class="pl-c1">0.11041384</span>]
 [ <span class="pl-c1">0.87626855</span>]
 [<span class="pl-c1">-</span><span class="pl-c1">2.31480127</span>]
 [<span class="pl-c1">-</span><span class="pl-c1">0.79008986</span>]]
<span class="pl-c1">&gt;&gt;</span><span class="pl-c1">&gt;</span> <span class="pl-en">cdist</span>(<span class="pl-s1">x</span>, <span class="pl-s1">y</span>, <span class="pl-s">'minkowski'</span>, <span class="pl-s1">p</span><span class="pl-c1">=</span><span class="pl-s1">np</span>.<span class="pl-s1">inf</span>)
<span class="pl-en">array</span>([[<span class="pl-c1">1.</span>, <span class="pl-c1">1.</span>, <span class="pl-c1">1.</span>, <span class="pl-c1">1.</span>, <span class="pl-c1">1.</span>],
       [<span class="pl-c1">1.</span>, <span class="pl-c1">1.</span>, <span class="pl-c1">1.</span>, <span class="pl-c1">1.</span>, <span class="pl-c1">1.</span>],
       [<span class="pl-c1">1.</span>, <span class="pl-c1">1.</span>, <span class="pl-c1">1.</span>, <span class="pl-c1">1.</span>, <span class="pl-c1">1.</span>],
       [<span class="pl-c1">1.</span>, <span class="pl-c1">1.</span>, <span class="pl-c1">1.</span>, <span class="pl-c1">1.</span>, <span class="pl-c1">1.</span>],
       [<span class="pl-c1">1.</span>, <span class="pl-c1">1.</span>, <span class="pl-c1">1.</span>, <span class="pl-c1">1.</span>, <span class="pl-c1">1.</span>]])
<span class="pl-c1">&gt;&gt;</span><span class="pl-c1">&gt;</span> <span class="pl-en">pdist</span>(<span class="pl-s1">x</span>, <span class="pl-s">'minkowski'</span>, <span class="pl-s1">p</span><span class="pl-c1">=</span><span class="pl-s1">np</span>.<span class="pl-s1">inf</span>)
<span class="pl-en">array</span>([<span class="pl-c1">1.</span>, <span class="pl-c1">1.</span>, <span class="pl-c1">1.</span>, <span class="pl-c1">1.</span>, <span class="pl-c1">1.</span>, <span class="pl-c1">1.</span>, <span class="pl-c1">1.</span>, <span class="pl-c1">1.</span>, <span class="pl-c1">1.</span>, <span class="pl-c1">1.</span>])</pre></div>
<p dir="auto">but when you call <code class="notranslate">pdist</code> and <code class="notranslate">cdist</code> with the <code class="notranslate">metric </code> parameter set to <code class="notranslate">scipy.spatial.distance.minkowski</code>, you get the correct result:</p>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="&gt;&gt;&gt; cdist(x, y, minkowski, p=np.inf)
array([[0.85167017, 0.28476175, 0.70192064, 2.48914918, 0.96443778],
       [0.08519265, 0.48171577, 1.46839816, 1.72267166, 0.19796025],
       [0.13959656, 0.42731186, 1.41399425, 1.77707557, 0.25236417],
       [1.43251035, 0.86560194, 0.12108046, 3.06998937, 1.54527796],
       [2.80392829, 2.23701988, 1.25033748, 4.44140731, 2.9166959 ]])
&gt;&gt;&gt; pdist(x, minkowski, p=np.inf)
array([0.76647752, 0.71207361, 0.58084018, 1.95225812, 0.05440391,
       1.3473177 , 2.71873564, 1.29291379, 2.66433173, 1.37141794])
&gt;&gt;&gt; 
"><pre class="notranslate"><span class="pl-c1">&gt;&gt;</span><span class="pl-c1">&gt;</span> <span class="pl-en">cdist</span>(<span class="pl-s1">x</span>, <span class="pl-s1">y</span>, <span class="pl-s1">minkowski</span>, <span class="pl-s1">p</span><span class="pl-c1">=</span><span class="pl-s1">np</span>.<span class="pl-s1">inf</span>)
<span class="pl-en">array</span>([[<span class="pl-c1">0.85167017</span>, <span class="pl-c1">0.28476175</span>, <span class="pl-c1">0.70192064</span>, <span class="pl-c1">2.48914918</span>, <span class="pl-c1">0.96443778</span>],
       [<span class="pl-c1">0.08519265</span>, <span class="pl-c1">0.48171577</span>, <span class="pl-c1">1.46839816</span>, <span class="pl-c1">1.72267166</span>, <span class="pl-c1">0.19796025</span>],
       [<span class="pl-c1">0.13959656</span>, <span class="pl-c1">0.42731186</span>, <span class="pl-c1">1.41399425</span>, <span class="pl-c1">1.77707557</span>, <span class="pl-c1">0.25236417</span>],
       [<span class="pl-c1">1.43251035</span>, <span class="pl-c1">0.86560194</span>, <span class="pl-c1">0.12108046</span>, <span class="pl-c1">3.06998937</span>, <span class="pl-c1">1.54527796</span>],
       [<span class="pl-c1">2.80392829</span>, <span class="pl-c1">2.23701988</span>, <span class="pl-c1">1.25033748</span>, <span class="pl-c1">4.44140731</span>, <span class="pl-c1">2.9166959</span> ]])
<span class="pl-c1">&gt;&gt;</span><span class="pl-c1">&gt;</span> <span class="pl-en">pdist</span>(<span class="pl-s1">x</span>, <span class="pl-s1">minkowski</span>, <span class="pl-s1">p</span><span class="pl-c1">=</span><span class="pl-s1">np</span>.<span class="pl-s1">inf</span>)
<span class="pl-en">array</span>([<span class="pl-c1">0.76647752</span>, <span class="pl-c1">0.71207361</span>, <span class="pl-c1">0.58084018</span>, <span class="pl-c1">1.95225812</span>, <span class="pl-c1">0.05440391</span>,
       <span class="pl-c1">1.3473177</span> , <span class="pl-c1">2.71873564</span>, <span class="pl-c1">1.29291379</span>, <span class="pl-c1">2.66433173</span>, <span class="pl-c1">1.37141794</span>])
<span class="pl-c1">&gt;&gt;</span><span class="pl-c1">&gt;</span> </pre></div>
<h4 dir="auto">Scipy/Numpy/Python version information:</h4>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="&gt;&gt;&gt; import sys, scipy, numpy; print(scipy.__version__, numpy.__version__, sys.version_info)
1.5.2 1.17.4 sys.version_info(major=3, minor=7, micro=3, releaselevel='final', serial=0)"><pre class="notranslate"><span class="pl-c1">&gt;&gt;</span><span class="pl-c1">&gt;</span> <span class="pl-k">import</span> <span class="pl-s1">sys</span>, <span class="pl-s1">scipy</span>, <span class="pl-s1">numpy</span>; <span class="pl-en">print</span>(<span class="pl-s1">scipy</span>.<span class="pl-s1">__version__</span>, <span class="pl-s1">numpy</span>.<span class="pl-s1">__version__</span>, <span class="pl-s1">sys</span>.<span class="pl-s1">version_info</span>)
<span class="pl-c1">1.5</span>.<span class="pl-c1">2</span> <span class="pl-c1">1.17</span>.<span class="pl-c1">4</span> <span class="pl-s1">sys</span>.<span class="pl-en">version_info</span>(<span class="pl-s1">major</span><span class="pl-c1">=</span><span class="pl-c1">3</span>, <span class="pl-s1">minor</span><span class="pl-c1">=</span><span class="pl-c1">7</span>, <span class="pl-s1">micro</span><span class="pl-c1">=</span><span class="pl-c1">3</span>, <span class="pl-s1">releaselevel</span><span class="pl-c1">=</span><span class="pl-s">'final'</span>, <span class="pl-s1">serial</span><span class="pl-c1">=</span><span class="pl-c1">0</span>)</pre></div>
<p dir="auto">Using <code class="notranslate">scipy.spatial.distance.cdist</code> with the Minkowski distance and <code class="notranslate">p=np.inf</code> does not produce expected result. In particular, I'm not sure how <code class="notranslate">cdist</code> is producing all distance of only 1's (see Minkowski below). Using Chebyshev distance produce expected results, but they should be equivalent for <math-renderer class="js-inline-math" style="display: inline" data-static-url="https://github.githubassets.com/static" data-run-id="c8d69386fee2ae85aebfe39af040c9f1">$p=\infty$</math-renderer>.</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="import numpy as np
import scipy.spatial as sp

a = np.array([[1.11132, 1.15693], [0.98214, 1.01018]])
b = np.array([[1.14994, 1.15386 ], [0.973258, 1.17015 ], [0.939203, 1.09205 ]])

print(&quot;a:\n{}&quot;.format(a))
print(&quot;b:\n{}&quot;.format(b))

mink = sp.distance.cdist(a, b, 'minkowski', p=np.inf)
print(&quot;Minkowski:\n{}&quot;.format(mink))

cheb = sp.distance.cdist(a, b, 'chebyshev')
print(&quot;Chebyshev:\n{}&quot;.format(cheb))

a:
[[1.11132 1.15693]
 [0.98214 1.01018]]
b:
[[1.14994  1.15386 ]
 [0.973258 1.17015 ]
 [0.939203 1.09205 ]]
Minkowski:
[[1. 1. 1.]
 [1. 1. 1.]]
Chebyshev:
[[0.03862  0.138062 0.172117]
 [0.1678   0.15997  0.08187 ]]"><pre class="notranslate"><code class="notranslate">import numpy as np
import scipy.spatial as sp

a = np.array([[1.11132, 1.15693], [0.98214, 1.01018]])
b = np.array([[1.14994, 1.15386 ], [0.973258, 1.17015 ], [0.939203, 1.09205 ]])

print("a:\n{}".format(a))
print("b:\n{}".format(b))

mink = sp.distance.cdist(a, b, 'minkowski', p=np.inf)
print("Minkowski:\n{}".format(mink))

cheb = sp.distance.cdist(a, b, 'chebyshev')
print("Chebyshev:\n{}".format(cheb))

a:
[[1.11132 1.15693]
 [0.98214 1.01018]]
b:
[[1.14994  1.15386 ]
 [0.973258 1.17015 ]
 [0.939203 1.09205 ]]
Minkowski:
[[1. 1. 1.]
 [1. 1. 1.]]
Chebyshev:
[[0.03862  0.138062 0.172117]
 [0.1678   0.15997  0.08187 ]]
</code></pre></div>
<h3 dir="auto">Scipy/Numpy/Python version information:</h3>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="scipy.__version__  = 1.0.0
numpy.__version__ = 1.14.2"><pre class="notranslate"><code class="notranslate">scipy.__version__  = 1.0.0
numpy.__version__ = 1.14.2
</code></pre></div>
<p dir="auto">Thanks,<br>
Jesse</p>