I have a code for calculating the pairwise distances and the residuals of my data (X, Y, Z). Data is quite large (average of 7000 rows) and so my interest is code efficiency. My initial code is
import tkinter as tk from tkinter import filedialog import pandas as pd import, numpy as np from scipy.spatial.distance import pdist, squareform root = tk.Tk() root.withdraw() file_path = filedialog.askopenfilename() data = pd.read_excel(file_path) data = np.array(data, dtype=np.float) npoints, cols = data.shape pwdistance = np.zeros((npoints, npoints)) pwresidual = np.zeros((npoints, npoints)) for i in range(npoints): for j in range(npoints): pwdistance[i][j] = np.sqrt((data[:,0][i]-data[:,0][j])**2 + (data[:,1][i]-data[:,1][j])**2) pwresidual[i][j] = (data[:,2][i]-data[:,2][j])**2
pwdistance, I changed it to the following below which works extremely good.
pwdistance = squareform(pdist(data[:,:2]))
Is there a pythonic way of calculating my
pwresidual, so I do not need to use a loop and to enable my code to run faster?