10月20日改变:这个类Invdisttree组合了反距离权重

scipy.spatial.KDTree。

忘记原来的强力回答;

这是分散数据插值的选择方法。

""" invdisttree.py: inverse-distance-weighted interpolation using KDTree

fast, solid, local

"""

from __future__ import division

import numpy as np

from scipy.spatial import cKDTree as KDTree

# http://docs.scipy.org/doc/scipy/reference/spatial.html

__date__ = "2010-11-09 Nov" # weights, doc

#...............................................................................

class Invdisttree:

""" inverse-distance-weighted interpolation using KDTree:

invdisttree = Invdisttree( X, z ) -- data points, values

interpol = invdisttree( q, nnear=3, eps=0, p=1, weights=None, stat=0 )

interpolates z from the 3 points nearest each query point q;

For example, interpol[ a query point q ]

finds the 3 data points nearest q, at distances d1 d2 d3

and returns the IDW average of the values z1 z2 z3

(z1/d1 + z2/d2 + z3/d3)

/ (1/d1 + 1/d2 + 1/d3)

= .55 z1 + .27 z2 + .18 z3 for distances 1 2 3

q may be one point, or a batch of points.

eps: approximate nearest, dist <= (1 + eps) * true nearest

p: use 1 / distance**p

weights: optional multipliers for 1 / distance**p, of the same shape as q

stat: accumulate wsum, wn for average weights

How many nearest neighbors should one take ?

a) start with 8 11 14 .. 28 in 2d 3d 4d .. 10d; see Wendel's formula

b) make 3 runs with nnear= e.g. 6 8 10, and look at the results --

|interpol 6 - interpol 8| etc., or |f - interpol*| if you have f(q).

I find that runtimes don't increase much at all with nnear -- ymmv.

p=1, p=2 ?

p=2 weights nearer points more, farther points less.

In 2d, the circles around query points have areas ~ distance**2,

so p=2 is inverse-area weighting. For example,

(z1/area1 + z2/area2 + z3/area3)

/ (1/area1 + 1/area2 + 1/area3)

= .74 z1 + .18 z2 + .08 z3 for distances 1 2 3

Similarly, in 3d, p=3 is inverse-volume weighting.

Scaling:

if different X coordinates measure different things, Euclidean distance

can be way off. For example, if X0 is in the range 0 to 1

but X1 0 to 1000, the X1 distances will swamp X0;

rescale the data, i.e. make X0.std() ~= X1.std() .

A nice property of IDW is that it's scale-free around query points:

if I have values z1 z2 z3 from 3 points at distances d1 d2 d3,

the IDW average

(z1/d1 + z2/d2 + z3/d3)

/ (1/d1 + 1/d2 + 1/d3)

is the same for distances 1 2 3, or 10 20 30 -- only the ratios matter.

In contrast, the commonly-used Gaussian kernel exp( - (distance/h)**2 )

is exceedingly sensitive to distance and to h.

"""

# anykernel( dj / av dj ) is also scale-free

# error analysis, |f(x) - idw(x)| ? todo: regular grid, nnear ndim+1, 2*ndim

def __init__( self, X, z, leafsize=10, stat=0 ):

assert len(X) == len(z), "len(X) %d != len(z) %d" % (len(X), len(z))

self.tree = KDTree( X, leafsize=leafsize ) # build the tree

self.z = z

self.stat = stat

self.wn = 0

self.wsum = None;

def __call__( self, q, nnear=6, eps=0, p=1, weights=None ):

# nnear nearest neighbours of each query point --

q = np.asarray(q)

qdim = q.ndim

if qdim == 1:

q = np.array([q])

if self.wsum is None:

self.wsum = np.zeros(nnear)

self.distances, self.ix = self.tree.query( q, k=nnear, eps=eps )

interpol = np.zeros( (len(self.distances),) + np.shape(self.z[0]) )

jinterpol = 0

for dist, ix in zip( self.distances, self.ix ):

if nnear == 1:

wz = self.z[ix]

elif dist[0] < 1e-10:

wz = self.z[ix[0]]

else: # weight z s by 1/dist --

w = 1 / dist**p

if weights is not None:

w *= weights[ix] # >= 0

w /= np.sum(w)

wz = np.dot( w, self.z[ix] )

if self.stat:

self.wn += 1

self.wsum += w

interpol[jinterpol] = wz

jinterpol += 1

return interpol if qdim > 1 else interpol[0]

#...............................................................................

if __name__ == "__main__":

import sys

N = 10000

Ndim = 2

Nask = N # N Nask 1e5: 24 sec 2d, 27 sec 3d on mac g4 ppc

Nnear = 8 # 8 2d, 11 3d => 5 % chance one-sided -- Wendel, mathoverflow.com

leafsize = 10

eps = .1 # approximate nearest, dist <= (1 + eps) * true nearest

p = 1 # weights ~ 1 / distance**p

cycle = .25

seed = 1

exec "\n".join( sys.argv[1:] ) # python this.py N= ...

np.random.seed(seed )

np.set_printoptions( 3, threshold=100, suppress=True ) # .3f

print "\nInvdisttree: N %d Ndim %d Nask %d Nnear %d leafsize %d eps %.2g p %.2g" % (

N, Ndim, Nask, Nnear, leafsize, eps, p)

def terrain(x):

""" ~ rolling hills """

return np.sin( (2*np.pi / cycle) * np.mean( x, axis=-1 ))

known = np.random.uniform( size=(N,Ndim) ) ** .5 # 1/(p+1): density x^p

z = terrain( known )

ask = np.random.uniform( size=(Nask,Ndim) )

#...............................................................................

invdisttree = Invdisttree( known, z, leafsize=leafsize, stat=1 )

interpol = invdisttree( ask, nnear=Nnear, eps=eps, p=p )

print "average distances to nearest points: %s" % \

np.mean( invdisttree.distances, axis=0 )

print "average weights: %s" % (invdisttree.wsum / invdisttree.wn)

# see Wikipedia Zipf's law

err = np.abs( terrain(ask) - interpol )

print "average |terrain() - interpolated|: %.2g" % np.mean(err)

# print "interpolate a single point: %.2g" % \

# invdisttree( known[0], nnear=Nnear, eps=eps )

python反距离权重法_使用Python进行反距离加权(IDW)插值相关推荐

  1. python反距离权重法_反距离权重法 (Spatial Analyst)—ArcMap | 文档

    使用反距离权重法 (IDW) 获得的像元输出值限定在插值时用到的值范围之内.因为反距离权重法是加权平均距离,所以该平均值不可能大于最大输入或小于最小输入.因此,如果尚未对这些极值采样,便无法创建山脊或 ...

  2. python反距离权重法_先从IDW(反距离权重)插值开始吧

    IDW方法是一个很不错,很方便,很快...(自行百度)的方法.至少我必须要用到... 首先附上idw插值~: 我写的不是很好,如果你们想要更优化的可以再找找其他的版本. 然后 正确的补缺idw插值代码 ...

  3. python画有权重网络图_使用Python的networkx绘制精美网络图教程

    最近因为数学建模3天速成Python,然后做了一道网络的题,要画网络图.在网上找了一些,发现都是一些很基础的丑陋红点图,并且关于网络的一些算法也没有讲,于是自己进http://networkx.git ...

  4. 反距离加权插值法例题_GMS插值中的反距离权重法(Inverse distance weighted)

    反距离权重法是GMS地层插值的默认方法,了解一些关于它的原理会帮助得到更好的插值结果.这次主要介绍Shepard's method方法.反距离权重法基本思路:插值点受附近点的影响最大,而距离较远的点的 ...

  5. ArcGIS空间插值方法反距离权重法(IDW)的工作原理

    反距离权重 (IDW) 插值使用一组采样点的线性权重组合来确定像元值.权重是一种反距离函数.进行插值处理的表面应当是具有局部因变量的表面. 此方法假定所映射的变量因受到与其采样位置间的距离的影响而减小 ...

  6. Python中ArcPy实现Excel时序数据读取、反距离加权IDW插值与批量掩膜

    1 任务需求   首先,我们来明确一下本文所需实现的需求.   现有一个记录有北京市部分PM2.5浓度监测站点在2019年05月18日00时至23时(其中不含19时)等23个逐小时PM2.5浓度数据的 ...

  7. python 过采样 权重实现_不平衡数据集的处理 - osc_sqq5osi1的个人空间 - OSCHINA - 中文开源技术交流社区...

    一.不平衡数据集的定义 所谓的不平衡数据集指的是数据集各个类别的样本量极不均衡.以二分类问题为例,假设正类的样本数量远大于负类的样本数量,通常情况下通常情况下把多数类样本的比例接近100:1这种情况下 ...

  8. python数独伪代码回溯法_数独 #回溯算法 #CTF

    1. intro:巅峰极客的一道逆向 刷巅峰极客2020里的rev题fu!kpy,复杂得不行但是看到if d[1][0] != '8' or d[1][7] != '2'和if check(h1) ! ...

  9. python input函数赋值法_大佬们 我是刚开始学python的小白 遇到这种赋值方式 实在不懂这个a+b是赋值给谁的 求解...

    该楼层疑似违规已被系统折叠 隐藏此楼查看此楼 这个是python独有的赋值方法 萌新不懂很正常啦 这个叫做"元组赋值法" 他会把前后两个部分当成一个元组去操作 所以在赋值过程中值不 ...

最新文章

  1. java packetmaster_TCP中间件_java_server
  2. SecureCRT的上传下载小技巧(Linux)
  3. oracle中的查询语句(关于出库入库信息表,明细表,把捆包箱表,单位信息表的集中查询)...
  4. java泛型中<?>和<T>有什么区别?
  5. esplise自定义快捷代码补全_【Eclipse】_Eclipse自动补全增强方法 常用快捷键
  6. 数据结构与算法分析-用C语言实现栈(数组方式)
  7. 教师计算机专业知识考试试题及答案,信息技术学科教师基本功测试题及答案
  8. OSPF算法详细说明
  9. Android进阶: 10分钟实现NDK-JNI 开发教程
  10. python all 函数_Python all()函数
  11. 银行账户系统需求分析实例
  12. seo教程之对搜索引擎的研究
  13. 诗词大全给力版_思维导图 | 6种高效记忆法,教你速背古诗词!
  14. eclipse 的preferences下没有server
  15. smartSVN 新建仓库
  16. saver.save和saver.restore
  17. 代理ip的使用场景。
  18. 软件“生命”系统进化论——软件以负熵为生
  19. Asp.Net Web Api 部署------在云服务器IIS上部署Web Api程序
  20. 比赛报名 | 2019AIIA杯电梯调度算法大赛正式启动

热门文章

  1. 格式工厂 wav 比特率_TunesKit Audio Converter for Mac(音频格式转换软件)
  2. 一程序员辞职开发赌博软件,2年涉案4千万被抓
  3. 【workqueue】flush_work函数解析
  4. 除了X站,程序员还喜欢上这些网站...
  5. 申请注册GMAIL的免费企业邮箱
  6. 优品优男所谓“日有所思,夜有所梦”
  7. OpenCV Error:Insufficient memory(Failed to allocate 1244164 bytes)
  8. 解决Error creating bean with name ‘redisConnectionFactory‘ defined in class path resource...问题
  9. 身份证/异地身份证在北京办理的解决办法
  10. Concurrency-with-Modern-Cpp学习笔记 - 线程