0

I have a CSV file and a target vector. My aim is to calculate Euclidean distance with each row points from the target vector but I'm unable to perform this action because different errors have been occurring.

import os
import NumPy as np 

from glob import glob

import matplotlib.pyplot as plt
import scipy
import tensorflow as tf

import math
import pandas as pd
import earthpy as et
from collections import Counter
# URL for .csv with avg monthly precip data
#avg_monthly_precip_url = "https://ndownloader.figshare.com/files/12710618"
#et.data.get_data(url=avg_monthly_precip_url)
#avg_monthly_precip_url = "https://ndownloader.figshare.com/files/12710618"
#et.data.get_data(url=avg_monthly_precip_url)
fname = os.path.join("dataset.csv")
avg_monthly_precip = pd.read_csv(fname)
print(avg_monthly_precip.describe())
AG = []
AG.append(avg_monthly_precip)
hj=[]
 ap=avg_monthly_precip.iloc[0:1,0:4]
 hj.append(ap)
 p2=np.reshape(hj,(1,4))
print(p2)
Target vector = [70, 60, 59,10]
 distance = math.sqrt( ((p1[0]-p2[0])**2)+((p1[1]-p2[1])**2)+((p1[2]-p2[2])**2)+((p1[3]- 
 p2[3])**2))
  print(distance)
khelwood
  • 55,782
  • 14
  • 81
  • 108
  • Not sure what your errors are, but your calcs are pretty inefficient. Check this recent post on euclidean distance https://stackoverflow.com/questions/69701568/how-to-calculate-pairwise-euclidean-distance-between-a-collection-of-vectors/69701721#69701721 – Riley Oct 26 '21 at 09:31
  • Please include a [MRE] with example input and expected output. Your code is not valid python (wrong module name: `import numpy as np`, invalid indentations). There are several [distance functions](https://docs.scipy.org/doc/scipy/reference/generated/scipy.spatial.distance.cdist.html) in `scipy` that work well with a `pandas.dataframe`. – Michael Szczesny Oct 26 '21 at 09:35

0 Answers0