在Python

时间:2018-03-30 22:12:13

标签: python pandas matrix

我有这些数据:

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)

我需要让这个功能起作用,所以如果有人能告诉我我需要做些什么才能让它发挥作用我真的很感激。

1 个答案:

答案 0 :(得分:-1)

你需要把回报放在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?

相关问题