Python Forum

Full Version: Computing the distance between each pair of points
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
X = rand.rand(10, 2)
%matplotlib inline
import matplotlib.pyplot as plt
import seaborn; seaborn.set() # Plot styling
plt.scatter(X[:, 0], X[:, 1], s=100);
dist_sq = np.sum((X[:, np.newaxis, :] - X[np.newaxis, :, :]) ** 2, axis=-1)
what I don't undersand is this part:
differences = X[:, np.newaxis, :] - X[np.newaxis, :, :]
what's happening between these two brackets, these two colons. Help is appreciated.
https://numpy.org/doc/stable/reference/a...exing.html

Quote:Each newaxis object in the selection tuple serves to expand the dimensions of the resulting selection by one unit-length dimension. The added dimension is the position of the newaxis object in the selection tuple.

Example
>>>x[:,np.newaxis,:,:].shape
(2, 1, 3, 1)

This example depends on previous examples, so check it from the start
Yes, I know what newaxis stands for but these two colons in each bracket are making me headache. Thank you anyway. I asked the same question on the biggest python facebook group and noone was able to answer exactly.
Ah, I misunderstood your question
At the very start of the link in my previous post it explains slicing. I am sure you are familiar.
So basically it is a slice at each axis of the multidimensional array, slices are separated with commas.
Inside [], colon is translated into slice and it is passed to __getitem__ as tuple. See also https://stackoverflow.com/a/39482568/4046632
one example
import numpy as np
rand = np.random.RandomState(42)
X = rand.rand(10, 5)
print(X)
print(X[:2, ::2])
Output:
[[0.37454012 0.95071431 0.73199394 0.59865848 0.15601864] [0.15599452 0.05808361 0.86617615 0.60111501 0.70807258] [0.02058449 0.96990985 0.83244264 0.21233911 0.18182497] [0.18340451 0.30424224 0.52475643 0.43194502 0.29122914] [0.61185289 0.13949386 0.29214465 0.36636184 0.45606998] [0.78517596 0.19967378 0.51423444 0.59241457 0.04645041] [0.60754485 0.17052412 0.06505159 0.94888554 0.96563203] [0.80839735 0.30461377 0.09767211 0.68423303 0.44015249] [0.12203823 0.49517691 0.03438852 0.9093204 0.25877998] [0.66252228 0.31171108 0.52006802 0.54671028 0.18485446]] [[0.37454012 0.73199394 0.15601864] [0.15599452 0.86617615 0.70807258]]
as you can see on the first axis :2 is translated into 0:2:1 slice and it takes only first 2 elements
the second ::2 is translated into 0::2 slice so on second axis it takes elements with index 0, 2, 4
This last example of yours is perfectly clear. Now I'll have to go back to the one from the book. But first to read given docs.
It looks that this operation adds an axis to my array, turning it from 2D into 3D
X[:, np.newaxis, :]
Output:
array([[[0.37454012, 0.95071431]], [[0.73199394, 0.59865848]], [[0.15601864, 0.15599452]], [[0.05808361, 0.86617615]], [[0.60111501, 0.70807258]], [[0.02058449, 0.96990985]], [[0.83244264, 0.21233911]], [[0.18182497, 0.18340451]], [[0.30424224, 0.52475643]], [[0.43194502, 0.29122914]]])
Yes, that's the effect of np.newaxis. You said you know it...
Yes, it's just that I still don't understand the contest...
I'm now reading docs that you provided and smth doesn't work well.
import numpy as np 
x = np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
x[:,np.newaxis,:,:].shape
Error:
IndexError Traceback (most recent call last) in 2 x = np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) 3 print(x[...,0]) ----> 4 x[:,np.newaxis,:,:].shape IndexError: too many indices for array
Pages: 1 2