克里金插值學習筆記

一、類定義:

OrdinaryKriging(x,y,z,variogram_model, variogram_model, variogram_parameters, variogram_function, nlags, weight, anisotropy_scaling, anisotropy_angle, verbose, enable_plotting, enable_statistics, coordinates_type)

參數說明:

x,y,z:座標x,y,以及數值(z)

variogram_model(方差模型):7個可選值,包括linear, power, gaussian, spherical, exponential, hole-effect,以及custom

缺省是linear,如選用custom則需提供方差模型的參數以及函數。提醒的是hole-effect模型僅僅在針對一維問題上,存在技術的正確性。

variogram_parameters(方差模型參數):可選,list或dict類型,由所選模型決定,不提供的話則採用“軟”(soft)L1範式最小化方案。

採用dict(字典)形式,參數如下:

linear - {'slope': slope, 'nugget': nugget}

power - {'scale': scale, 'exponent': exponent, 'nugget': nugget}

gaussian - {'sill': s, 'range': r, 'nugget': n} / {'psill': p, 'range': r, 'nugget':n}

spherical - {'sill': s, 'range': r, 'nugget': n} / {'psill': p, 'range': r, 'nugget':n}

exponential - {'sill': s, 'range': r, 'nugget': n} / {'psill': p, 'range': r, 'nugget':n}

hole-effect - {'sill': s, 'range': r, 'nugget': n} / {'psill': p, 'range': r, 'nugget':n}

基臺值(Sill):代表變量在空間上的總變異性大小,psill = sill - nugget,所以有兩種參數字典方式可選

採用list(列表)形式,參數如下:

linear - [slope, nugget]

power - [scale, exponent, nugget]

gaussian - [sill, range, nugget]

spherical - [sill, range, nugget]

exponential - [sill, range, nugget]

hole-effect - [sill, range, nugget]

列表形式,只能採用sill,不用使用psill值

模型選擇 custom(定製),方差參數選項是必須提供,因爲方差模型不會自動適應數據,而且參數必須以特定的list(列表)格式提供,按照回調函數(callable function)裏使用的順序提供。

代碼不會檢查列表中是否提供了合適數量的參數,所以如參數不正確,可能會觸發一個難以理解的異常信息。值得注意的是,雖然列表格式期待的參數是全基臺值(sill),代碼內部使用的是部分基臺值(psill)

variogram_function :定製(custom)類型方差模型必須提供此參數,兩個參數,一是方差模型列表參數,二是計算方差模型的距離值,前述參數會傳遞過來。

nlags : int,可選,對於半方差模型的平均箱數(bins),缺省值爲6

weight :bool, 可選,標誌半方差的小滯(smaller lags)是否比自動計算方差模型權重更大,常規是權重由邏輯函數計算而來,所以小滯的權重設爲1,長滯的權重設爲0,邏輯權重的中心設爲小滯到大滯距離的70%數值。本參數設爲True,意味權重需要考慮,缺省爲False。(Kitanidis建議小滯的值在適應方差模型時更重要,所以保留此選項以便於設置權重)

anisotropy_scaling : float, 可選,大面拉伸值,在考慮各向異性時有用,缺省 1(意味沒有拉伸),指在旋轉數據幀的 y 方向拉伸比例(如果anisotropy_angle !=0時),此參數在coordinate_types值設爲'geographic'時無效。

anisotropy_angle : float, 可選,逆時針角度(單位:弧度),在考慮各向異性時,旋轉座標系統的角度值,同上對於coordinate_types值設爲'geographic'時無效。

verbose : bool, 可選,設置監測克里金過程文字是否輸出,缺省False。

enable_plotting : bool, 可選,結果是否繪圖輸出,缺省False。

enable_statistics : bool, 可選,缺省False。

coordinates_type : str, 可選,'euclidean' / 'geographic'. 前者平面座標系,後者球面座標系,球面座標系,x爲lon,y爲lat,lon值爲 [0, 360],lat爲 [-90, 90],缺省:'euclidean'.

 

二、核心方法

execute(self, style, xpoints, ypoints, mask=None, backend='vectorized', n_closest_points = None):

當前確保提供值在結果中屬於無誤差的,爲確切值,未來會允許存在誤差

1、輸入參數:

style : str,定義如何處理後面的輸入點參數,定義爲'grid',則處理xpoints,ypoints爲一個矩形網格的x、y座標值列表,定義爲'point‘,則處理爲座標對的列表值。定義爲’masked‘,則處理xpoints,ypoints爲矩形網格的x、y座標,且採用掩膜(mask)評估特定的點值

xpoints : 矩陣數組

ypoints : 矩陣數組,如果style 類型爲'points',xpoints,ypoints需有相同的維數

mask : 掩膜矩陣,bool, 維度與xpoints,ypoints相對應,定義哪些網格點數據被排他計算

backend : str, 可選,定義克里計算方法。’vectorized',向量運算法,速度更快,消耗內存更大,適用於大網格點或大數據量;'loop' 循環計算法,速度慢,消耗內存少。Cpython將使用loop,缺省是'vectorized'.

n_closest_points : int, 可選,對於移窗法計算時,確定計算採用臨近點的數量,可加快計算速度(對於大數據量)。但是Kitanidis 提醒,移窗法可能產生不可預期的異常值,如果方差模型沒選擇好的話

2、返回值:

zvalues : ndarray

sigmasq : ndarray

三、範例

import numpy as np

import pykrige.kriging_tools as kt

from pykrige.ok import OrdinaryKriging



data = np.array([[0.3, 1.2, 0.47],

[1.9, 0.6, 0.56],

[1.1, 3.2, 0.74],

[3.3, 4.4, 1.47],

[4.7, 3.8, 1.74]])



gridx = np.arange(0.0, 5.5, 0.5)

gridy = np.arange(0.0, 5.5, 0.5)



# Create the ordinary kriging object. Required inputs are the X-coordinates of

# the data points, the Y-coordinates of the data points, and the Z-values of the

# data points. If no variogram model is specified, defaults to a linear variogram

# model. If no variogram model parameters are specified, then the code automatically

# calculates the parameters by fitting the variogram model to the binned

# experimental semivariogram. The verbose kwarg controls code talk-back, and

# the enable_plotting kwarg controls the display of the semivariogram.

OK = OrdinaryKriging(data[:, 0], data[:, 1], data[:, 2], variogram_model='linear',

verbose=False, enable_plotting=True)



# Creates the kriged grid and the variance grid. Allows for kriging on a rectangular

# grid of points, on a masked rectangular grid of points, or with arbitrary points.

# (See OrdinaryKriging.__doc__ for more information.)

z, ss = OK.execute('grid', gridx, gridy)



# Writes the kriged grid to an ASCII grid file.

kt.write_asc_grid(gridx, gridy, z, filename="output.asc")

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章