"""类函数""" classAIDW: def__init__(self, x, y, m,p=2): self.m = m # 搜索邻域内的样点数 self.nbrs = NearestNeighbors(n_neighbors=m, algorithm='ball_tree').fit(x) self.thresh = 360/m self.p = p self.y = y self.x = x
deffit(self, x_new): indices = self.nbrs.kneighbors(x_new, return_distance=False) x_sample = self.x[indices[0]] y_sample = self.y[indices[0]] x_ = x_sample-x_new zi = [] ki = 1 for i in range(1, self.m-1): for j in range(i): cos_ = np.sum(x_[i]*x_[j])/((np.sum(x_[i]**2)**(1/2))*(np.sum(x_[j]**2)**(1/2))) radian = np.arccos(cos_) angle = radian*180/np.pi if angle>=self.thresh:continue else: ki*=np.sin(radian)**self.p di = np.sum(x_[i]**2)**(1/2) zi.append(ki/(di**self.p)) z = np.sum(np.array(zi)*y_sample[1:-1])/np.sum(zi) return z
"""demo""" import numpy as np import matplotlib.pyplot as plt