Python Forum
Computing the distance between each pair of points
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Computing the distance between each pair of points
#1
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.
Reply
#2
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
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
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.
Reply
#4
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
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#5
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
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#6
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.
Reply
#7
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]]])
Reply
#8
Yes, that's the effect of np.newaxis. You said you know it...
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#9
Yes, it's just that I still don't understand the contest...
Reply
#10
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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Cloud computing advice needed sawtooth500 3 260 Apr-18-2024, 09:23 PM
Last Post: sawtooth500
  How to most effectively unpack list of name-value pair dictionaries in a dataframe? zlim 1 666 Nov-07-2023, 10:56 PM
Last Post: zlim
  Could anyone help me get the jaccard distance between my dataframes please? :) a_real_phoenix 0 1,776 Jun-27-2019, 06:01 PM
Last Post: a_real_phoenix
  Converting days to years in loop while computing values across grid cells Lightning1800 2 2,641 May-15-2018, 08:44 PM
Last Post: Lightning1800
  Error in computing FFT operation raady07 1 4,252 Jan-18-2017, 08:30 AM
Last Post: j.crater

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020