【问题标题】:Getting the Euclidean distance of two vectors in Python [duplicate]在Python中获取两个向量的欧几里得距离[重复]
【发布时间】:2018-09-09 22:41:49
【问题描述】:

我有这些数据:

data

它比这大得多,但我只是测试前 5 个条目。我正在尝试获取纬度和经度的欧几里得距离。当我简单地将纬度和经度用作向量时,我的方法有效,但是当我创建一个函数来执行此操作时,由于某种原因,我得到了完全不同的结果。

例如当我这样做时:

D = np.zeros((len(x), len(y)))
for i in range(len(x)):
    for j in range(len(y)):
        D[i][j] = np.sqrt((x[i] - x[j])**2 + (y[i] - y[j])**2)

print(D)

我得到了正确的结果:

[[0.00000000e+00 1.88271381e+02 1.87587947e+02 6.99323921e+01
  1.87539502e+02]
 [1.88271381e+02 0.00000000e+00 7.75171148e-01 1.66386511e+02
  8.46161225e-01]
 [1.87587947e+02 7.75171148e-01 0.00000000e+00 1.65616303e+02
  7.61935378e-02]
 [6.99323921e+01 1.66386511e+02 1.65616303e+02 0.00000000e+00
  1.65549538e+02]
 [1.87539502e+02 8.46161225e-01 7.61935378e-02 1.65549538e+02
  0.00000000e+00]]

但是我为什么要使用这个功能:

def eucDist(matrix):
    dist_mat = np.zeros((len(matrix), len(matrix)))
    for i in range(len(matrix)):
        for j in range(len(matrix)):
            dist_mat[i][j] = np.sqrt((A[i][0] - A[j][0])**2 + (A[i][1] - A[j][1])**2)
            return dist_mat

我得到这个错误的结果:

[[0. 0. 0. 0. 0.]
 [0. 0. 0. 0. 0.]
 [0. 0. 0. 0. 0.]
 [0. 0. 0. 0. 0.]
 [0. 0. 0. 0. 0.]]

我不确定我编写的函数有什么问题。这是我的完整代码:

# Import necessary modules
import pandas as pd
import numpy as np
import datetime


# Create necessary functions
def eucDist(matrix):
    dist_mat = np.zeros((len(matrix), len(matrix)))
    for i in range(len(matrix)):
        for j in range(len(matrix)):
            dist_mat[i][j] = np.sqrt((A[i][0] - A[j][0])**2 + (A[i][1] - A[j][1])**2)
            return dist_mat


# Import data into a dataframe
df = pd.read_csv("data2.csv")
df['year'] = pd.DatetimeIndex(df['close_date']).year
df['month'] = pd.DatetimeIndex(df['close_date']).month
df['day'] = pd.DatetimeIndex(df['close_date']).day
df['hour'] = pd.DatetimeIndex(df['close_date']).hour
df['minute'] = pd.DatetimeIndex(df['close_date']).minute
print(df.head())

# Create necessary variables
x = df.as_matrix(columns=df.columns[:1])                # latitude
y = df.as_matrix(columns=df.columns[1:2])               # longitude
year = df.as_matrix(columns=df.columns[4:5])
month = df.as_matrix(columns=df.columns[5:6])
day = df.as_matrix(columns=df.columns[6:7])
hour = df.as_matrix(columns=df.columns[7:8])
min = df.as_matrix(columns=df.columns[8:9])
p = df.as_matrix(columns=df.columns[3:4])                # close_price

A = np.c_[x, y, year, month, day, hour, min, p]          # created a matrix of all the attributes needed



Dist = eucDist(A)
print(Dist)


print('-'*50)

# Get distance of latitude and longitude
D = np.zeros((len(x), len(y)))
for i in range(len(x)):
    for j in range(len(y)):
        D[i][j] = np.sqrt((x[i] - x[j])**2 + (y[i] - y[j])**2)

print(D)

我需要让这个功能工作,所以如果有人能告诉我我需要做什么才能让这个功能工作,我将不胜感激。

【问题讨论】:

  • 你回来得太早了。 eucDist 中的 return 语句在内循环的第一次迭代中执行,而不是在外循环完成后执行。消除它。
  • 我明白了,我应该在哪里退货?
  • @chepner 我现在明白了,对不起,我是个新人

标签: python pandas matrix


【解决方案1】:

你需要把return放在fors之外,否则你只计算dist_mat[0][0]元素:

def eucDist(matrix):
dist_mat = np.zeros((len(matrix), len(matrix)))
for i in range(len(matrix)):
    for j in range(len(matrix)):
        dist_mat[i][j] = np.sqrt((A[i][0] - A[j][0])**2 + (A[i][1] - A[j][1])**2)
return dist_mat

无论如何,这里已经回答了这个问题: How can the euclidean distance be calculated with numpy?

【讨论】:

  • 我看到了那个帖子,但我不确定如何在我的具体情况下使用该功能。
  • “如果问题已经回答了”,那你为什么又回答了?
  • 阅读问题,然后发表评论。
  • 我做到了,这就是我将其作为副本关闭的原因。还有其他的讽刺或抱怨吗?
猜你喜欢
  • 1970-01-01
  • 2014-05-31
  • 2020-11-28
  • 2014-11-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-03-27
  • 1970-01-01
相关资源
最近更新 更多