Использование numpy.linalg.norm
проще , потому что вы полагаетесь на уже написанную функцию.
Цель этого упражнения - улучшить ваши знания в области программирования и математики.
Начните с формулы Евклидово расстояние и напишите функцию, которая будет выполнять каждый шаг, необходимый для вычисления.
# Calculate the Euclidiean distance between two points (P and Q)
def calculate_Euclidean_distance(point_P_list, point_Q_list):
log.debug('Enter calculate_Euclidean_distance')
log.info('point_P_list: (%s)' % ', '.join(map(str, point_P_list)))
log.info('point_Q_list: (%s)' % ', '.join(map(str, point_Q_list)))
# Store the sum of squared distances between coordinates
sum_of_coordinate_distance_squared = 0
# For each coordinate p and q in the points P and Q square the difference
for p, q in zip(point_P_list, point_Q_list):
distance_pq_squared = (p - q) ** 2
sum_of_coordinate_distance_squared += distance_pq_squared
# The distance between points P and Q is the square root of the sum of coordinate differences squared
distance_PQ = math.sqrt(sum_of_coordinate_distance_squared)
log.info('distance_PQ: ' + str(distance_PQ))
[2019-09-27 15:03:33,257] [main] Start example.py execution
[2019-09-27 15:03:33,259] [calculate_Euclidean_distance] Enter calculate_Euclidean_distance
[2019-09-27 15:03:33,260] [calculate_Euclidean_distance] point_P_list: (9, 20)
[2019-09-27 15:03:33,261] [calculate_Euclidean_distance] point_Q_list: (18, 8)
[2019-09-27 15:03:33,261] [calculate_Euclidean_distance] distance_PQ: 15.0
[2019-09-27 15:03:33,261] [main] End example.py execution