Reputation: 613
I have this file:
This can be read as:
data = np.loadtxt('test_2_stack_overflow.csv', delimiter=',')
or
dfr = pd.read_csv('test_2_stack_overflow.csv',header=None)
The column index represent the x coordinate and the row the y coordinate of a pixel whose value is reported in the file.
I would like to select all the pairs that have the same distance (+-a tollerance). The easiest way could be to apply two loops but I would like to take advantage of the numpy or pandas.
I am thinking to apply a sort of
diff = dfr.apply(lambda col: ((col.name - col.index)**2 + (col.name - col.index)**2)**0.5)
This however seems not feasible. I am also thinking to follow another path. I could create a two dimensional vector of coordinates, this could be also be more general.
I could create a vector for the coordinates:
xx = np.arange(4)
yy = np.arange(4)
coord = np.array(np.meshgrid(xx,yy)).T.reshape(-1,2)
After that I could compute the distances between each pair:
ref = pd.DataFrame.from_records(coord, columns=('x', 'y'))
diff2 = dfr.apply(
lambda col: np.sqrt(
abs(ref.iloc[col.name]['x'] - ref.iloc[col.index]['x']) ** 2 +
abs(ref.iloc[col.name]['y'] - ref.iloc[col.index]['y']) ** 2
)
)
However, I get the following:
As you can notice, the values are all integer and that is not possible. For example, the points with coordinates (0,2) and (3,1) should have a distance of (sqrt(10))
What do you think? Should I choose a better strategy ans/or is there any error?
Thanks
Upvotes: 0
Views: 51
Reputation: 120429
Maybe, you are looking for cdist
:
from scipy.spatial.distance import cdist
xx = np.arange(4)
yy = np.arange(4)
grid = np.array(np.meshgrid(xx, yy)).T.reshape(-1, 2)
dist = cdist(grid, grid)
Output:
>>> dist
array([[0. , 1. , 2. , 3. , 1. ,
1.41421356, 2.23606798, 3.16227766, 2. , 2.23606798,
2.82842712, 3.60555128, 3. , 3.16227766, 3.60555128,
4.24264069],
[1. , 0. , 1. , 2. , 1.41421356,
1. , 1.41421356, 2.23606798, 2.23606798, 2. ,
2.23606798, 2.82842712, 3.16227766, 3. , 3.16227766,
3.60555128],
[2. , 1. , 0. , 1. , 2.23606798,
1.41421356, 1. , 1.41421356, 2.82842712, 2.23606798,
2. , 2.23606798, 3.60555128, 3.16227766, 3. ,
3.16227766],
[3. , 2. , 1. , 0. , 3.16227766,
2.23606798, 1.41421356, 1. , 3.60555128, 2.82842712,
2.23606798, 2. , 4.24264069, 3.60555128, 3.16227766,
3. ],
[1. , 1.41421356, 2.23606798, 3.16227766, 0. ,
1. , 2. , 3. , 1. , 1.41421356,
2.23606798, 3.16227766, 2. , 2.23606798, 2.82842712,
3.60555128],
[1.41421356, 1. , 1.41421356, 2.23606798, 1. ,
0. , 1. , 2. , 1.41421356, 1. ,
1.41421356, 2.23606798, 2.23606798, 2. , 2.23606798,
2.82842712],
[2.23606798, 1.41421356, 1. , 1.41421356, 2. ,
1. , 0. , 1. , 2.23606798, 1.41421356,
1. , 1.41421356, 2.82842712, 2.23606798, 2. ,
2.23606798],
[3.16227766, 2.23606798, 1.41421356, 1. , 3. ,
2. , 1. , 0. , 3.16227766, 2.23606798,
1.41421356, 1. , 3.60555128, 2.82842712, 2.23606798,
2. ],
[2. , 2.23606798, 2.82842712, 3.60555128, 1. ,
1.41421356, 2.23606798, 3.16227766, 0. , 1. ,
2. , 3. , 1. , 1.41421356, 2.23606798,
3.16227766],
[2.23606798, 2. , 2.23606798, 2.82842712, 1.41421356,
1. , 1.41421356, 2.23606798, 1. , 0. ,
1. , 2. , 1.41421356, 1. , 1.41421356,
2.23606798],
[2.82842712, 2.23606798, 2. , 2.23606798, 2.23606798,
1.41421356, 1. , 1.41421356, 2. , 1. ,
0. , 1. , 2.23606798, 1.41421356, 1. ,
1.41421356],
[3.60555128, 2.82842712, 2.23606798, 2. , 3.16227766,
2.23606798, 1.41421356, 1. , 3. , 2. ,
1. , 0. , 3.16227766, 2.23606798, 1.41421356,
1. ],
[3. , 3.16227766, 3.60555128, 4.24264069, 2. ,
2.23606798, 2.82842712, 3.60555128, 1. , 1.41421356,
2.23606798, 3.16227766, 0. , 1. , 2. ,
3. ],
[3.16227766, 3. , 3.16227766, 3.60555128, 2.23606798,
2. , 2.23606798, 2.82842712, 1.41421356, 1. ,
1.41421356, 2.23606798, 1. , 0. , 1. ,
2. ],
[3.60555128, 3.16227766, 3. , 3.16227766, 2.82842712,
2.23606798, 2. , 2.23606798, 2.23606798, 1.41421356,
1. , 1.41421356, 2. , 1. , 0. ,
1. ],
[4.24264069, 3.60555128, 3.16227766, 3. , 3.60555128,
2.82842712, 2.23606798, 2. , 3.16227766, 2.23606798,
1.41421356, 1. , 3. , 2. , 1. ,
0. ]])
Upvotes: 1