In [1]:
# Load Biospytial modules and etc.
%matplotlib inline
import sys
sys.path.append('/apps')
import django
django.setup()
import pandas as pd
import matplotlib.pyplot as plt
## Use the ggplot style
plt.style.use('ggplot')

In [2]:
# My mac
#data = pd.read_csv("/RawDataCSV/plotsClimateData_11092017.csv")
# My Linux desktop
data = pd.read_csv("/RawDataCSV/idiv_share/plotsClimateData_11092017.csv")


/opt/conda/envs/biospytial/lib/python2.7/site-packages/IPython/core/interactiveshell.py:2717: DtypeWarning: Columns (24) have mixed types. Specify dtype option on import or set low_memory=False.
  interactivity=interactivity, compiler=compiler, result=result)

In [3]:
data.SppN.mean()


Out[3]:
5.1824128104220382

In [4]:
import geopandas as gpd

In [5]:
from django.contrib.gis import geos
from shapely.geometry import Point

In [6]:
data['geometry'] = data.apply(lambda z: Point(z.LON, z.LAT), axis=1)
new_data = gpd.GeoDataFrame(data)

Let´s reproject to Alberts or something with distance


In [7]:
new_data.crs = {'init':'epsg:4326'}

Uncomment to reproject

proj string taken from: http://spatialreference.org/


In [8]:
#new_data =  new_data.to_crs("+proj=aea +lat_1=29.5 +lat_2=45.5 +lat_0=37.5 +lon_0=-96 +x_0=0 +y_0=0 +ellps=GRS80 +datum=NAD83 +units=m +no_defs ")

Model Fitting Using a GLM

The general model will have the form: $$ Biomass(x,y) = \beta_1 AET + \beta_2 Age + Z(x,y) + \epsilon $$ Where: $\beta_1$ and $\beta_2$ are model parameters, $Z(x,y)$ is the Spatial Autocorrelation process and $\epsilon \sim N(0,\sigma^2)$


In [9]:
##### OLD #######
len(data.lon)
#X = data[['AET','StandAge','lon','lat']]
#X = data[['SppN','lon','lat']]
X = data[['lon','lat']]
#Y = data['plotBiomass']
Y = data[['SppN']]
## First step in spatial autocorrelation
#Y = pd.DataFrame(np.zeros(len(Y)))
## Let´s take a small sample only for the spatial autocorrelation
#import numpy as np
#sample_size = 2000
#randindx = np.random.randint(0,X.shape[0],sample_size)
#nX = X.loc[randindx]
#nY = Y.loc[randindx]
nX = X
nY = Y

In [10]:
## Small function for systematically selecting the k-th element of the data.
#### Sughgestion use for now a small k i.e. 10
systematic_selection = lambda k : filter(lambda i : not(i % k) ,range(len(data)))

In [12]:
idx = systematic_selection(50)
print(len(idx))
nX = X.loc[idx]
nY = Y.loc[idx]
new_data = data.loc[idx]


737

In [13]:
len(new_data)


Out[13]:
737

In [22]:
# Import GPFlow
import GPflow as gf
k = gf.kernels.Matern12(2,ARD=False,active_dims = [0,1]) + gf.kernels.Bias(1)
#k = gf.kernels.Matern12(2, lengthscales=1, active_dims = [0,1] ) + gf.kernels.Bias(1)
l = gf.likelihoods.Poisson()
model = gf.gpmc.GPMC(nX.as_matrix(), nY.as_matrix().reshape(len(nY),1).astype(float), k, l)

In [24]:
#model = gf.gpr.GPR(nX.as_matrix(),nY.as_matrix().reshape(len(nY),1).astype(float),k)
## If priors
#model.kern.matern12.lengthscales.prior = gf.priors.Gaussian(25.0,3.0)
#model.kern.matern32.variance.prior = GPflow.priors.Gamma(1.,1.)
#model.kern.bias.variance.prior = gf.priors.Gamma(1.,1.)
## Optimize

%time model.optimize(maxiter=100) # start near MAP


CPU times: user 35.9 s, sys: 3.04 s, total: 39 s
Wall time: 14.9 s
Out[24]:
      fun: array([ 1913.86902295])
 hess_inv: <740x740 LbfgsInvHessProduct with dtype=float64>
      jac: array([  6.44528368e+00,   3.66167605e+00,   2.37126916e+00,
         3.99084934e+00,   4.58043614e+00,   2.44289078e+00,
         3.35879111e+00,   2.38506185e+00,   3.80880764e+00,
         1.71325578e+00,   3.89539947e+00,   2.76303685e+00,
         3.02354141e+00,   3.37260714e+00,   2.61401844e+00,
         1.21994417e+00,   3.72547986e+00,   7.23979866e-01,
         1.20894006e+00,   2.39778410e+00,   1.25884622e+00,
         1.12012964e+00,   1.40526563e+00,   1.36770157e+00,
         3.79159785e+00,   2.58935825e+00,   3.41322737e+00,
         3.13107522e+00,   2.20581891e+00,   2.16769663e+00,
         1.62946618e+00,   2.09358530e+00,   2.11091892e+00,
         2.37531408e+00,   1.81464034e+00,  -1.70074688e-02,
         2.00651916e+00,   1.39050955e+00,   2.13008291e+00,
         2.08779971e+00,   1.59072557e+00,   8.85038673e-01,
         1.42096409e+00,   9.34597443e-01,   1.20844821e+00,
         9.19622799e-01,   6.78397510e-01,   8.49665685e-01,
         2.47282322e-01,   7.00216164e-01,   4.07072068e-01,
         9.41045019e-01,   1.17226066e+00,   6.37126088e-01,
         1.91325671e+00,   1.36836137e+00,   9.18578754e-01,
         7.93245441e-01,   1.02423045e+00,   9.38937199e-01,
         2.68225615e-01,   6.95726689e-01,   2.04501186e-01,
         4.59926362e-01,   9.87704059e-01,   1.09101324e+00,
         1.61323926e+00,   8.21729108e-01,   7.99780599e-01,
         1.65107077e-01,   7.00976309e-02,   5.55198400e-01,
         1.16581778e-01,   8.04768732e-01,   3.79639043e-01,
         2.11746571e+00,   2.18822663e+00,   1.73613604e+00,
         1.14962525e+00,   1.67809047e+00,   1.05870209e+00,
         1.38984467e+00,   1.60101205e+00,   1.56042573e+00,
         1.31153922e+00,   1.52459983e+00,   1.33987865e+00,
         9.14007283e-01,   1.24879634e+00,   5.98075661e-01,
         9.25655990e-01,   9.65296739e-01,   3.19938067e-01,
         6.55656729e-01,   1.39939865e+00,   1.60563436e+00,
         1.15197295e+00,   1.13133233e+00,   1.00154114e+00,
         1.50463245e+00,   9.37803896e-01,   7.66691311e-01,
         9.80883353e-01,   9.67007417e-01,   1.18076121e+00,
        -2.78028078e-01,   1.13131632e+00,   2.02883818e+00,
         4.71215799e-01,   6.00976168e-02,   1.11502794e+00,
         1.24005744e+00,   9.38611671e-01,   1.14774879e+00,
        -2.00634875e-01,   1.30596167e+00,  -3.30221519e-01,
         5.64958349e-01,   9.76984022e-01,   6.87944121e-01,
         9.77174525e-01,   5.83696458e-01,  -2.07136249e-01,
         6.43427265e-01,   6.61493705e-01,   1.03280377e+00,
         5.54644246e-01,   7.17657012e-01,   6.37493990e-01,
         1.09082779e+00,   8.86897873e-01,   2.73810603e-01,
         9.29382459e-01,   6.82375334e-01,   4.77540057e-01,
        -4.89355913e-02,   3.02551719e-01,  -2.60540131e-01,
        -1.98565092e-01,   3.90620758e-01,   5.02590488e-01,
         8.27705982e-01,   5.54196080e-01,   3.74696816e-01,
         5.94933616e-01,   1.05940473e+00,   7.56376516e-02,
         3.58981871e-01,   7.53571374e-01,   1.20098615e+00,
        -6.26240948e-01,   4.34277350e-01,   1.10948217e-01,
         5.90334217e-02,   1.86983117e-02,  -5.67852741e-01,
         5.87901840e-01,   2.47814801e-01,   2.57326565e-01,
         2.81597887e-01,  -8.29391148e-01,   6.32695487e-01,
         9.10709888e-01,   6.04027895e-01,   9.26783769e-01,
         7.90246173e-01,   6.24479619e-01,   7.17225033e-01,
         5.60711876e-01,   5.75325342e-01,   6.61933360e-01,
         1.00242225e+00,   1.10666738e+00,   1.41895658e+00,
         2.73409139e-01,   9.48378112e-01,   8.13656682e-01,
         4.23076894e-02,   6.88280096e-02,   5.74751684e-01,
        -1.90749700e-01,   5.15328867e-01,  -4.86839432e-01,
         8.26688649e-01,   1.45352255e+00,   7.98980581e-01,
         1.07092638e+00,   9.94024125e-01,   7.40325858e-02,
         4.27134296e-02,   1.05385389e+00,   3.32237946e-02,
         2.56434367e-01,   9.21316758e-01,   2.73621578e-01,
        -2.05806987e-01,   1.60401373e-01,   2.80612853e-01,
         1.20711546e-01,   5.37769895e-01,  -1.13948946e-01,
         1.92844740e-01,  -3.06414930e-01,   5.94734486e-01,
        -2.65850878e-01,   2.43229034e-02,  -2.31815685e-01,
         2.99535492e-01,   5.22125502e-01,   6.59972790e-01,
         3.76110611e-01,   6.49790611e-01,   7.17314696e-01,
         6.64725219e-01,   4.15296515e-01,   4.03971045e-01,
        -3.07274396e-01,   1.53472259e-01,   2.93352449e-01,
         3.02354983e-01,   6.26047998e-01,   8.27177206e-01,
         9.19676319e-01,   6.38705936e-01,  -2.16325809e-02,
         9.27341441e-03,   2.36433249e-01,   5.68025433e-01,
        -2.88590440e-02,  -1.66272076e-01,   5.23319413e-02,
         4.05185154e-01,   1.15976986e-01,   4.23484795e-01,
        -4.35575727e-02,   1.44227976e-01,   1.48660201e-01,
         4.82916659e-01,   2.75553776e-01,   6.40532073e-01,
         6.44781451e-02,  -2.31918150e-02,   3.91396209e-01,
         1.30205275e-01,   7.41936074e-01,   2.34924345e-01,
         6.93465032e-01,   4.42766312e-01,   3.98139422e-01,
         2.01884968e-02,  -3.74419947e-01,   9.60057247e-01,
         1.72751136e-01,   6.35725722e-01,   5.75983378e-01,
         7.43108557e-01,  -9.94590331e-02,   9.58457616e-01,
         1.51456842e-01,   2.88247241e-01,   8.43115920e-01,
         7.01317201e-01,   4.27197060e-01,   4.52637800e-01,
         4.54156578e-01,   1.30616164e-01,   9.77439984e-01,
         4.48722555e-01,  -2.86721110e-01,  -9.41361209e-02,
         2.38037116e-01,   5.86919282e-01,   4.53868590e-01,
         8.60136546e-01,   1.67614286e-01,  -1.01162197e-01,
        -1.17414977e-01,   2.95810407e-01,   3.39772492e-01,
         1.01757101e-01,   1.01971996e+00,   6.10996360e-01,
         3.20646565e-01,   5.54320257e-01,   4.03614715e-01,
         9.46997314e-02,  -1.21034584e-01,   1.67890495e-01,
         5.22188792e-01,   5.02215708e-01,   8.66331274e-01,
        -5.22809682e-01,   1.40563669e-01,   8.21210090e-01,
         9.73164378e-01,   5.05665064e-01,   5.30394793e-02,
        -1.96482727e-02,   4.36269995e-01,   3.01952266e-01,
         1.35972880e+00,   1.45555877e-01,   9.39237916e-02,
        -7.36814297e-01,  -9.43458997e-02,   6.78672252e-01,
         2.94641552e-01,   4.03474292e-01,   1.13425896e-01,
         4.57831246e-01,   1.36436697e+00,   4.47516716e-01,
         3.85110311e-01,   5.22321301e-02,  -1.64388006e-01,
        -1.95191262e-02,   1.48279941e-01,  -1.51343147e-01,
         3.95031564e-01,  -7.18679535e-01,  -3.70543373e-01,
         8.88643080e-01,   4.10986375e-01,   2.47309745e-01,
         3.05526605e-01,  -4.49727530e-02,  -4.49498222e-01,
         9.69122461e-01,  -5.26218119e-01,   3.28973523e-01,
         1.79217109e-01,  -2.17673068e-01,   3.37760828e-01,
         3.30131937e-01,  -2.91628016e-01,   8.65397096e-01,
         2.36697086e-01,  -1.55179003e-02,   4.71143815e-01,
         1.36125721e+00,   5.62809180e-01,   3.62512778e-01,
         6.11588712e-01,  -1.27084705e-01,   9.41675317e-01,
         4.22760265e-01,   1.39059249e-01,   7.00909042e-02,
        -4.87546659e-01,   1.61513753e-03,   3.37147097e-01,
         3.58813980e-01,   2.67367241e-01,   9.68593202e-01,
         5.70700141e-02,   2.78633912e-01,   7.79716030e-01,
         4.76768539e-01,   3.95322908e-01,   2.80076020e-01,
         1.18509362e-01,   1.61893932e-01,   1.77204404e-02,
         4.65990860e-01,   2.87318848e-01,   1.95000906e-01,
        -2.89558927e-01,   6.78866010e-01,  -3.25669470e-01,
         3.49686535e-02,   5.54522637e-01,  -2.19858255e-01,
        -2.50150415e-01,   7.73930468e-01,   4.50943853e-01,
        -5.60060157e-01,  -3.73221765e-03,   2.30719507e-01,
         4.11682780e-01,   7.25643777e-01,   3.67794284e-01,
         5.95068732e-01,  -3.45182588e-01,   9.60501973e-02,
        -1.50251260e-02,   1.14605157e-01,   2.23218775e-01,
         5.13012014e-01,   5.48365344e-01,   4.13500312e-01,
         3.66830404e-01,   1.88507045e-01,   2.44873874e-01,
         1.34874159e-01,   4.37614987e-01,   7.97487964e-02,
        -2.17700555e-01,   7.56027996e-02,   3.53195433e-01,
         4.59549628e-01,   5.29223230e-01,  -1.15163259e-01,
         1.05082444e+00,   5.27578012e-01,   8.19828647e-01,
         2.96774825e-01,   5.45936553e-01,   4.05620998e-01,
         6.89223845e-01,   5.83802678e-01,   8.42687947e-01,
         4.56082958e-03,  -4.51922333e-02,   1.27974456e-01,
        -1.55517101e-01,   2.92856039e-01,  -3.59165716e-02,
         4.58648061e-01,   2.03105481e-01,   1.96779385e-01,
        -2.65551581e-01,  -1.12109107e-01,   3.55171229e-01,
         2.71582500e-01,  -1.30811902e-01,   2.50360515e-01,
        -6.62223332e-01,   4.45369734e-01,   5.69869233e-01,
        -6.07202099e-04,   2.10177430e-01,   9.93721517e-02,
        -2.25820826e-01,   1.57139813e-01,  -4.22363638e-01,
         1.31057277e-01,  -2.54349586e-01,   2.16747594e-01,
         6.24742620e-01,   1.48362706e-03,  -3.30573816e-02,
         5.27216551e-02,   3.96853505e-01,   2.08262177e-01,
         1.10034804e+00,  -5.94766145e-01,  -3.24021546e-01,
         2.41401430e-01,   4.17206674e-01,   6.88588218e-01,
         4.91379693e-01,  -1.64886080e-01,   3.65875120e-02,
         4.55565224e-01,   8.05601961e-01,  -1.39789015e-01,
        -1.87553025e-01,   2.85938746e-01,   2.34811384e-01,
         3.48052244e-01,   7.01328210e-01,  -7.08000630e-01,
        -1.45309508e-01,   5.64106574e-01,   2.10931355e-01,
         2.17293783e-01,   2.55018693e-01,  -5.79697899e-01,
         1.44856465e-01,  -4.35437962e-01,  -4.27554809e-01,
        -1.23912538e-01,   3.77488951e-01,   9.57506798e-01,
         4.01889755e-01,   4.92355237e-01,   5.34328139e-01,
         5.48446820e-01,   3.96260530e-01,   4.69956033e-01,
         5.26741600e-01,   2.68556761e-01,  -2.74522535e-01,
         3.35566503e-01,   1.74269340e-01,   3.96465478e-01,
         1.07058020e-01,   5.36702790e-01,  -4.94421486e-02,
         3.22391914e-01,   7.89981380e-02,   2.58493140e-01,
         9.48975765e-02,   1.47552588e-01,   2.68374060e-01,
        -5.56319879e-02,   7.43628813e-01,  -2.79868719e-01,
        -4.97834925e-01,   7.45004775e-02,   2.08726891e-01,
         7.23200382e-02,   3.42801312e-01,   9.49684616e-02,
         1.81949574e-01,   2.29614283e-01,   3.00690246e-01,
         3.27070864e-01,  -7.84708378e-01,   3.36544778e-01,
         2.50293295e-01,   1.38992747e-01,   2.70344082e-01,
         3.96735495e-01,   2.85635109e-01,   3.68817321e-01,
         1.72251545e-01,   2.65747376e-01,   7.98471673e-02,
        -6.29032993e-01,  -4.46753866e-01,  -2.95757102e-02,
        -1.98081473e-01,   2.74098964e-01,  -6.15077477e-02,
        -8.14908736e-02,   7.68459867e-02,  -3.51194123e-02,
         2.63950122e-01,   4.04585837e-02,  -8.66318297e-02,
         7.69704775e-02,  -1.97850679e-02,  -2.20595501e-01,
         2.48495793e-01,   1.54458095e-01,   1.93760212e-01,
        -4.19088668e-01,   1.01054522e-01,  -5.37596006e-02,
         9.29896383e-01,   4.92441343e-01,  -2.56477760e-01,
        -2.51891420e-01,  -2.91027285e-01,  -9.70514294e-02,
         5.70247395e-01,  -2.75243927e-01,   2.73250126e-01,
         1.95834731e-01,   3.40902372e-02,   3.90370599e-01,
        -2.36039592e-01,  -1.05352599e-01,  -3.57751087e-01,
        -1.45565926e-01,  -2.84427012e-01,   7.54348476e-02,
        -3.21770558e-01,   6.53901641e-02,   1.75732110e-01,
        -2.22917132e-01,  -2.08782339e-01,   8.95890620e-01,
        -4.51196793e-01,   5.56956110e-02,   3.18084172e-01,
        -2.30707065e-02,   1.39035651e-01,   2.85531104e-01,
        -9.02151579e-01,  -8.06505814e-01,   4.89880576e-01,
        -2.66226410e-01,  -2.07221431e-01,   2.31426721e-01,
         4.44373896e-01,  -5.93125145e-01,  -2.75646183e-01,
        -3.31761133e-01,   4.85520506e-03,   8.48966304e-02,
         2.86544462e-02,   6.11484940e-01,   7.01877605e-01,
         9.21723373e-01,   5.41173159e-01,  -1.89766198e-01,
         4.23946068e-01,   3.66145966e-01,   6.22881616e-01,
         6.42643873e-02,   3.76765972e-01,   3.32410454e-01,
         3.00782955e-01,  -1.63616489e-01,   6.03620494e-02,
         6.15902703e-01,   1.11218128e-01,   2.77568128e-01,
        -1.41915683e-01,   1.30923674e-01,  -3.60984364e-02,
         2.69501379e-01,   4.27280289e-01,   5.48393379e-01,
        -1.08580453e-01,   2.02504493e-01,   3.34982505e-01,
         4.20203475e-01,   4.27904355e-01,   5.55474826e-01,
         1.79142191e-01,   4.30535446e-01,   3.50442767e-01,
        -2.31909338e-01,  -3.98306181e-01,   3.20812975e-01,
         1.41324640e-01,  -3.38691783e-01,  -1.33151501e-01,
         2.32656707e-02,   3.18232891e+00,   4.39791758e-01,
         4.84937606e-01,   1.34415611e+00,   1.17672095e+00,
        -3.00520593e-01,   7.58847084e-01,  -4.58175841e-02,
        -1.33767158e-01,   1.13334637e-01,   8.60226529e-01,
         3.10002400e-01,   6.47460651e-02,   1.17801254e-01,
         1.28294825e+00,   1.24885369e-01,   2.42007141e-01,
        -4.81713047e-01,  -2.20815955e-01,  -2.01063089e-01,
         6.41360009e-01,   1.87474673e-01,   1.75334761e+00,
         1.98547218e-01,   1.04273963e-01,   1.56194191e-01,
         6.90162797e-01,   2.50442075e-01,   1.55858958e-01,
         4.17312999e-01,   2.20277146e-01,   5.58216918e-02,
         2.83722418e-01,   5.88858397e-01,   2.77632737e-01,
         9.43244240e-03,   2.55023745e-01,   3.10997941e-01,
         7.73275251e-01,  -1.99164513e-01,   8.01907606e-02,
         1.00028380e-01,   1.20295368e-01,   2.97047136e-01,
         4.82057080e-01,   3.33643896e-01,   3.03627658e-01,
        -4.45993704e-01,   3.19106896e-01,   2.21620570e-01,
        -1.49342530e-01,  -4.25326799e-01,   1.70220925e-02,
         2.54475966e-01,   3.71943783e-01,   2.23826934e-01,
         5.96715249e-01,   1.93153167e+00,   1.62018789e+00,
         3.67280454e-01,   4.37511918e-01,   5.09441881e-01,
         9.99120723e-01,   4.60127254e-01,   4.69352299e-01,
         8.64254556e-01,  -2.11069931e-01,   6.03144786e-01,
        -2.65185123e-01,  -1.31538022e-01,   7.02394971e-01,
         4.50513501e-01,   2.51444628e-02,  -6.05897709e-01,
         1.02865184e-01,   9.94057272e-01,  -2.48636039e-01,
        -2.54838754e-02,   1.61920400e-01,   1.05263506e+00,
        -3.29641983e-01,   2.65012017e-01,   3.16962852e-01,
         3.92625727e-01,   1.82143609e-01,   3.92011063e-01,
         3.65241721e-01,  -6.88460813e-02,  -2.98522498e-01,
         1.06347423e-01,   4.00284018e-01,  -7.05023238e-01,
         1.97461746e-02,   1.48218895e-01,   3.09651453e-01,
         1.26878827e-01,   2.07540801e-01,  -1.01473537e+00,
         2.49509272e-01,  -1.28509438e-01,   1.30509746e-01,
        -5.61477719e-02,  -4.91383607e-02,  -3.35120778e-01,
         2.63675526e-01,  -2.63321280e-01,   3.26452020e-01,
        -8.17470680e-02,  -7.98386257e-01,   4.20595722e-01,
        -2.86909271e-02,  -4.07319744e-01,  -1.10019013e-01,
        -6.71473602e-02,   2.23725391e-01,   3.29583728e+00,
         7.57448040e-01,  -2.61760763e-01])
  message: 'STOP: TOTAL NO. of ITERATIONS EXCEEDS LIMIT'
     nfev: 114
      nit: 101
   status: 1
  success: False
        x: array([  4.48414714e-01,   2.55980668e-01,   2.22665202e-01,
        -1.79286532e-02,   2.21353963e-01,   2.37394147e-01,
         1.89596822e-01,   2.03578746e-01,   2.84171302e-01,
         6.97182660e-02,  -1.40138418e-01,   1.05474351e-01,
         3.14559892e-01,   2.20872849e-01,   1.24669541e-01,
        -1.52341724e-01,   1.28900804e-01,   9.72628594e-02,
         1.03215869e-01,   1.19223157e-01,   1.25651930e-01,
         1.46891570e-01,   1.08199046e-01,   1.22577228e-01,
         3.91131956e-02,  -4.84692922e-02,  -1.44063476e-01,
        -2.81305957e-02,  -2.69343271e-02,   3.67525476e-02,
         8.78936376e-02,  -3.68992189e-02,   1.66253202e-01,
        -1.57384725e-01,  -3.45792465e-03,  -2.05326151e-01,
        -1.46074395e-01,  -3.68434601e-02,   9.83002035e-02,
         1.97222047e-01,   1.74221827e-01,   2.66626442e-02,
         1.96685336e-01,   1.50484041e-01,   2.21602162e-01,
         1.39267068e-01,  -1.69825183e-01,  -3.04032715e-02,
        -3.33148370e-02,   2.00094402e-01,   1.67271890e-01,
         5.58365430e-02,   9.21196153e-02,  -5.94357314e-02,
         7.24064977e-02,  -2.88218383e-01,   1.22790888e-01,
         1.70713291e-01,   1.69173793e-01,  -4.63212521e-02,
        -1.09659626e-01,   1.61655785e-02,  -5.37027675e-02,
        -5.05878976e-02,   1.21022702e-02,   8.00889894e-02,
         1.55786221e-01,   9.67675744e-02,   5.06491497e-02,
         2.14880441e-01,   1.28855441e-01,   1.38790421e-01,
        -1.58436895e-01,   3.76042086e-02,  -1.09219461e-01,
         5.59513769e-02,   9.63276223e-02,   4.48774671e-02,
        -2.49430068e-01,  -7.04325302e-02,   1.07996239e-01,
         5.03198891e-02,   4.91458035e-02,  -1.92286839e-01,
        -4.43819415e-03,   1.06589085e-01,   1.03795976e-01,
        -2.42391594e-02,  -1.05230595e-02,   9.99608005e-02,
        -2.14180921e-01,   8.93751398e-02,   9.99706244e-02,
        -4.11128443e-02,  -1.91832747e-01,  -6.81933491e-02,
        -4.95305935e-02,   1.04925902e-02,  -4.74388309e-02,
        -5.90414594e-02,   1.06201562e-01,  -3.19372669e-02,
         1.63642971e-02,  -4.25508810e-02,  -1.80275442e-01,
        -2.25107688e-01,  -5.16049598e-02,   2.17307599e-01,
         1.02720611e-01,   1.51959176e-01,  -6.56021630e-02,
        -6.59585074e-02,  -5.77146283e-02,   1.36120357e-01,
         2.09664818e-01,  -1.80047031e-01,   3.02031019e-01,
         8.64146904e-02,   1.20156148e-01,   5.71750217e-02,
        -1.85387360e-01,  -6.62111788e-03,   2.10746997e-01,
        -1.93937077e-01,  -5.15423078e-02,   2.93089328e-02,
        -8.72224363e-02,   1.17749192e-01,   2.17891322e-01,
        -7.35400987e-02,   1.59334076e-01,  -8.43818873e-02,
        -7.40159237e-03,   1.49854748e-01,   2.08202575e-01,
        -2.13684559e-01,  -9.99534944e-03,   9.99254633e-03,
         3.07663673e-01,   1.35743692e-01,  -3.92614390e-02,
         1.40761742e-01,  -2.59282877e-02,  -1.49104744e-01,
         1.39228500e-01,  -2.07851999e-01,  -2.11912014e-01,
        -2.09861340e-01,   7.48040554e-02,   8.23872893e-02,
         1.45921175e-01,   8.24678355e-02,   1.84592793e-01,
        -6.76238675e-02,  -2.55863907e-01,  -2.99312569e-01,
        -1.19157850e-02,  -7.26310724e-02,   6.05168101e-02,
         1.16250591e-01,   3.09330016e-01,  -2.47273057e-02,
        -7.97155216e-02,   4.38188774e-02,  -1.97861048e-01,
        -7.73739697e-02,  -2.07797243e-01,   9.76163697e-02,
         1.40563260e-02,   8.56946768e-02,   1.01489560e-01,
         1.78352069e-01,  -2.79657541e-03,   1.94402506e-01,
         9.15603335e-02,   3.38799817e-02,   7.79351717e-02,
         1.99968588e-01,   1.71189542e-01,  -4.55935521e-02,
         1.64165263e-01,   4.12515309e-03,   1.99104486e-01,
         1.42886747e-01,   1.14793740e-01,   1.09215771e-01,
         1.86687841e-01,   1.36901086e-01,   1.90015244e-01,
         1.44323024e-01,   5.56093092e-02,   1.49700735e-01,
         1.16874322e-01,   1.38154857e-01,   6.81386130e-02,
         1.25942837e-01,   3.21290635e-02,   9.80073286e-02,
         5.82316150e-02,   2.33852201e-02,   1.41380186e-01,
        -4.98961750e-02,   1.52778054e-01,   9.70649196e-02,
         1.04977209e-02,   5.37733976e-02,   1.45005113e-01,
        -1.49172874e-01,   4.41753558e-02,  -2.65763989e-01,
         1.49512097e-01,  -1.91485769e-01,   1.49393242e-01,
        -6.40458554e-02,  -5.24338120e-02,   3.12215415e-02,
         1.16816359e-01,  -7.40564031e-02,  -1.05987418e-01,
         1.81786704e-01,   9.40125347e-02,   7.16072511e-02,
         1.04884415e-01,   1.56665784e-01,   6.01765027e-02,
        -2.35286322e-01,   3.33716824e-02,   1.33845945e-01,
         9.76678153e-02,   7.05493816e-02,  -5.45225376e-03,
         7.36936611e-02,   1.17708222e-01,   6.19839519e-02,
        -7.42978991e-02,  -1.17489693e-01,   8.11674344e-02,
         3.22802414e-03,   1.59933818e-01,   4.54159261e-02,
        -8.69625386e-02,  -1.29338663e-02,   1.22537489e-01,
        -7.16857667e-02,   5.96286037e-02,   1.33742846e-02,
        -3.29176090e-02,   5.81320766e-02,   6.64602714e-02,
         3.74408668e-02,   9.16879934e-02,   1.46655251e-01,
         1.18354220e-01,   1.41832527e-01,   1.62217480e-01,
        -6.73343271e-02,   8.82784158e-02,   8.04077337e-02,
        -2.96246608e-02,   7.02446162e-02,   1.34687238e-01,
         7.48295777e-02,  -1.02589112e-01,  -9.79558660e-02,
         4.03412497e-02,  -7.90943960e-02,   4.43068413e-02,
        -2.67521483e-04,   7.14139217e-02,   4.37819352e-02,
         4.81049588e-02,   6.55051010e-02,   8.03222231e-02,
         3.49293603e-03,   1.16878589e-01,   3.03222672e-02,
        -3.25770598e-01,  -1.36545267e-01,   7.87495569e-02,
         1.60181658e-01,  -3.25372408e-02,   6.01981792e-02,
         1.10610052e-01,   1.02217362e-02,  -1.17209661e-01,
        -1.07688674e-01,   1.83812030e-01,   5.84602171e-02,
        -2.26883074e-01,   3.28788372e-03,   1.25462223e-01,
         5.64526073e-02,   6.78100080e-03,  -1.31519473e-01,
         1.61167496e-01,   8.30321966e-03,   7.57800443e-02,
        -2.36734495e-01,   5.85342928e-02,  -1.11967198e-01,
        -5.85981071e-04,  -9.45981790e-03,   1.80389153e-01,
        -6.77343687e-01,   1.44892064e-01,  -6.00552829e-02,
        -9.73013370e-02,  -1.01551103e-01,   9.23068060e-02,
        -1.85719717e-02,   5.87472919e-02,  -2.31305819e-02,
        -4.44197481e-02,   1.22947941e-01,   1.99132638e-02,
        -2.00010167e-01,  -7.55989487e-02,   9.51091063e-02,
        -2.09088712e-01,   2.22425779e-01,   1.41735407e-01,
         5.14841293e-02,  -8.20480336e-02,   4.38069071e-02,
        -2.68474481e-01,  -1.94818246e-01,  -1.44774049e-01,
         2.21183082e-01,   1.10296570e-01,   1.87720389e-01,
        -5.05611336e-02,   1.30526370e-01,   1.55424313e-01,
         5.19860344e-02,   1.28622473e-01,   7.90983096e-03,
        -9.63826603e-02,   1.05890825e-01,   1.11035782e-01,
         1.48993690e-01,  -2.13892037e-02,  -1.74167024e-01,
         8.52854627e-02,   2.09558360e-01,  -9.94117401e-03,
         7.32179484e-02,  -4.97441580e-02,   2.29477165e-01,
         1.32607108e-01,   1.27526322e-02,  -3.02559590e-02,
         1.07799726e-01,   1.83227543e-01,   2.84326767e-02,
         4.89782436e-03,  -1.24908642e-01,  -7.11121111e-02,
         8.39949724e-02,   2.05019824e-01,   1.95580945e-01,
        -1.55486190e-02,   9.99370451e-02,  -5.12010571e-03,
        -8.74089930e-02,   6.60600273e-02,  -2.57977575e-02,
        -5.08017938e-02,  -2.32147536e-01,  -3.55636089e-02,
        -5.00806239e-02,  -4.30272259e-02,  -1.97527342e-01,
        -1.13902989e-01,  -1.96983478e-01,   3.64105159e-02,
        -1.41369352e-02,  -3.03872905e-02,  -3.47834724e-02,
        -1.06035590e-01,   4.91097344e-02,  -3.36788464e-02,
        -5.23270037e-02,   1.43857930e-02,  -3.28573382e-02,
        -1.38826307e-01,  -1.94577576e-01,   1.01127460e-02,
        -1.11950385e-01,  -1.04992196e-01,  -1.12571338e-01,
        -1.02614438e-01,  -4.07462540e-02,  -1.07732649e-01,
        -2.47616476e-03,   3.84841315e-02,   5.67900745e-02,
         4.39431946e-02,   1.39565348e-01,   1.31668907e-01,
        -1.47085601e-02,   1.22734686e-02,   1.17043197e-01,
        -2.14298075e-01,   9.76673392e-02,   2.83682519e-02,
         2.45061550e-03,  -2.28790403e-01,  -1.09811134e-01,
        -2.21319506e-01,   5.87833634e-02,   1.20085872e-01,
         1.12624380e-01,   4.93953475e-02,   2.80478030e-02,
         4.73116028e-02,  -1.68177958e-02,   3.48144670e-02,
         4.53458626e-02,  -2.34025263e-03,   1.16776548e-01,
         9.45102363e-02,   1.09475277e-01,  -4.24032933e-02,
         4.48600909e-02,   3.10530988e-02,   8.36045116e-02,
         9.81205354e-02,   7.23797721e-03,   8.63653596e-03,
         1.63053753e-01,   1.37544029e-01,   2.38340208e-01,
         1.77959982e-01,   1.77759510e-01,  -6.15201788e-03,
        -4.03036415e-02,  -4.49674901e-02,   5.02910047e-02,
         1.01549159e-01,  -5.72628469e-02,  -6.15270929e-02,
         1.03699857e-01,  -9.61261364e-03,   6.60274642e-02,
         3.57640058e-02,  -2.03106086e-01,   9.17806270e-02,
         1.27494421e-01,  -1.28667465e-01,   3.63486897e-02,
         8.92174887e-02,   1.75321457e-02,  -4.34858864e-02,
        -1.03046334e-02,   8.56596794e-02,   1.69405540e-01,
         7.18942175e-03,   6.45547645e-02,   7.48729269e-02,
        -4.21742793e-02,   3.28186390e-02,   1.97907645e-01,
         4.39740780e-03,   1.25895061e-02,   9.67757574e-02,
         5.59560633e-02,  -1.11126919e-01,  -4.76728491e-01,
        -3.95203119e-02,   8.51930253e-02,   5.86855023e-02,
        -1.32218627e-01,  -1.21347974e-01,   8.73396617e-02,
        -1.19620794e-01,  -1.08239703e-01,  -1.01539283e-01,
        -5.97484470e-02,  -6.20966315e-02,  -1.00120373e-01,
        -6.78539889e-02,   2.78824260e-02,  -6.39683650e-02,
        -2.05455642e-02,   1.08673343e-01,  -1.05429078e-01,
        -2.07138587e-01,   4.01118326e-02,  -2.16011332e-01,
        -4.58484765e-02,  -3.97896212e-02,   7.00832066e-02,
        -5.71820592e-02,  -2.62965012e-02,   2.34000527e-02,
        -2.36781439e-01,   1.01894645e-01,  -1.24975862e-01,
        -1.37805193e-01,  -7.86648742e-02,  -5.23031877e-02,
        -2.77882773e-02,   3.91788916e-02,  -2.06050496e-01,
        -1.54126133e-02,   2.40843713e-02,   3.51219128e-02,
        -4.47892136e-02,  -3.36728005e-01,  -1.90155840e-01,
        -3.26705688e-03,   1.18083772e-01,  -6.49298360e-02,
         5.75934215e-02,  -1.39560838e-01,  -4.80436416e-03,
         4.59142679e-02,   1.60929689e-01,   6.93135636e-02,
        -6.85090439e-02,   1.84033599e-01,   1.52094725e-01,
        -1.55328345e-02,   7.77840475e-02,  -3.68340646e-03,
         1.30087331e-01,  -3.83601527e-02,   9.40553796e-02,
         4.11211501e-02,  -1.71494450e-01,   1.41592266e-01,
        -5.21893651e-02,   4.65003925e-02,  -9.88103686e-02,
         9.02477246e-02,  -7.71967774e-03,  -7.95862449e-02,
         4.21337708e-02,   1.74422674e-01,  -2.63361573e-01,
         2.06210908e-01,   8.59851140e-02,  -1.23075008e-01,
         1.18386500e-01,   3.16235155e-02,   1.46774429e-01,
        -1.00704656e-01,   1.79463537e-01,   1.69276771e-01,
         1.37644669e-01,  -4.24765742e-02,  -2.83712797e-01,
         4.19618309e-02,   9.62864421e-02,   8.74883480e-04,
         8.37360432e-03,   1.09728009e-01,   4.20469869e-02,
         1.45126598e-01,  -1.83762312e-02,   3.86635153e-02,
         8.69823451e-02,   3.58537924e-04,  -1.04487183e-01,
        -2.45852537e-01,   5.14327089e-02,   1.19603458e-01,
         2.85768887e-02,   1.08473534e-01,   1.05474765e-01,
         2.20459676e-01,   1.46270680e-01,   7.50594352e-02,
         1.97907230e-01,   1.48012811e-01,   1.65757473e-02,
        -5.87251654e-02,   1.53539234e-01,   6.69509268e-02,
         1.21907588e-01,   1.51393475e-01,  -2.63809742e-04,
         2.41674922e-02,  -2.22419706e-01,   4.45444038e-02,
         3.27989364e-02,  -2.89163527e-01,  -1.55113408e-01,
        -1.00844963e-01,   1.23985422e-01,  -2.28174605e-01,
         2.28146346e-01,   1.39339786e-01,   1.70601181e-02,
        -7.11815963e-03,   4.01522057e-02,  -5.07584481e-02,
         2.16524993e-01,   5.50088078e-02,   4.98188647e-02,
        -4.70365645e-02,  -7.54697494e-02,  -4.79838494e-02,
        -1.09040227e-01,  -1.09500533e-01,  -1.00045826e-01,
         4.41687730e-02,  -1.08207002e-01,  -1.07927252e-01,
        -8.82630267e-02,  -9.09400376e-02,  -2.13603337e-01,
        -9.60832841e-02,   3.10071648e-02,  -9.68290681e-02,
        -1.21319957e-02,   3.20076769e-02,   7.74974606e-02,
         1.74561184e-02,   9.43795242e-02,   6.86565025e-02,
         1.34606927e-01,   8.82879786e-02,   1.27684525e-01,
         3.01693189e-02,   1.57034996e-01,   1.36229575e-01,
         1.38071561e-01,  -2.53134826e-01,   7.76376490e-02,
         1.27002004e-01,   2.83792913e-02,  -8.70124444e-02,
         4.78445033e-02,   7.22063874e-02,   1.12118901e-01,
         7.64935537e-02,   1.19837796e-01,  -4.55258084e-02,
         2.14174065e-01,   9.84080974e-02,   9.06559766e-02,
        -7.30136498e-02,   8.69776513e-02,   1.27742318e-01,
         4.39258793e-02,  -2.73302888e-02,   6.58541634e-02,
         8.52276073e-02,  -6.05889766e-02,  -4.64643463e-02,
        -2.30426846e-01,   8.40224833e-03,   4.34120037e-03,
        -4.43145900e-02,   2.70375518e-02,  -1.03619488e-01,
        -1.38906925e-01,   2.26609165e-02,  -1.01974856e-01,
         7.92645558e-02,  -2.49703331e-01,   6.85769022e-03,
        -5.75569664e-02,  -1.16740931e-01,   6.45636538e-03,
         1.01311024e-02,   5.46279469e-02,  -2.10341031e-01,
         1.59814515e-02,   1.26607666e-01,  -1.17623882e-01,
         1.47459515e-01,  -1.54374667e-01,  -4.94605430e-02,
         6.10530255e-02,   3.71143418e-04,  -1.25649938e-02,
         1.27805037e-01,   2.07061127e-01,   1.60948010e-01,
         3.46812797e-03,  -6.03664894e-03,   9.07043501e-02,
        -7.77421549e-03,   4.23033378e-02,   6.99738133e-02,
        -1.12243048e-02,  -1.16454029e-02,   1.05765734e-01,
         1.01957068e-01,  -1.13298270e-01,   7.20562393e-02,
        -2.75689751e-03,   2.62257125e-02,   3.92531799e-02,
         6.00809695e-02,   1.36869803e-01,   2.28936019e-02,
         1.06593523e-01,  -1.25092432e-01,  -1.39701259e-01,
         1.43435852e-01,   1.56955351e-01,  -6.81471080e-02,
        -2.51083108e-01,   7.91610824e-02,   8.88977367e-02,
        -1.17000255e-01,   1.72084722e-01,   1.72922943e-02,
         1.59501748e-01,  -2.54246262e-02,   1.46573579e-01,
         1.49634062e-01,  -7.85896896e-02,   1.00551817e-01,
        -3.05451100e-02,   9.56232340e-02,   1.62702525e-01,
         3.19564617e-02,  -1.94911485e-02,  -1.87556197e-02,
        -1.14175211e-01,  -8.95948669e-02,   4.19087800e-02,
         1.12978735e-01,   3.66152636e-01,  -4.06523847e-02,
         5.87593566e-02,  -3.47792191e-01,  -2.07739205e-01,
        -1.75074220e-02,  -3.25719715e-01,  -1.46003667e-02,
        -8.80783038e-02,   7.43829388e-02,   2.56135095e+00,
        -6.91135597e-01,   2.81189070e+01])

In [25]:
model.kern


Out[25]:
Namevaluespriorconstraint
kern.bias.variance[ 2.63571738]None+ve
kern.matern12.lengthscales[ 0.40613709]None+ve
kern.matern12.variance[ 28.11890802]None+ve

In [26]:
samples = model.sample(50, verbose=True, epsilon=1, Lmax=100)


warning: numerical instability.                  Rejecting this proposal prematurely
warning: numerical instability.                  Rejecting this proposal prematurely
warning: numerical instability.                  Rejecting this proposal prematurely
warning: numerical instability.                  Rejecting this proposal prematurely
warning: numerical instability.                  Rejecting this proposal prematurely
warning: numerical instability.                  Rejecting this proposal prematurely
warning: numerical instability.                  Rejecting this proposal prematurely
warning: numerical instability.                  Rejecting this proposal prematurely
warning: numerical instability.                  Rejecting this proposal prematurely
warning: numerical instability.                  Rejecting this proposal prematurely
warning: numerical instability.                  Rejecting this proposal prematurely
warning: numerical instability.                  Rejecting this proposal prematurely
warning: numerical instability.                  Rejecting this proposal prematurely
warning: numerical instability.                  Rejecting this proposal prematurely
warning: numerical instability.                  Rejecting this proposal prematurely
warning: numerical instability.                  Rejecting this proposal prematurely
warning: numerical instability.                  Rejecting this proposal prematurely
warning: numerical instability.                  Rejecting this proposal prematurely
warning: numerical instability.                  Rejecting this proposal prematurely
warning: numerical instability.                  Rejecting this proposal prematurely
warning: numerical instability.                  Rejecting this proposal prematurely
warning: numerical instability.                  Rejecting this proposal prematurely
warning: numerical instability.                  Rejecting this proposal prematurely
warning: numerical instability.                  Rejecting this proposal prematurely
warning: numerical instability.                  Rejecting this proposal prematurely
warning: numerical instability.                  Rejecting this proposal prematurely
warning: numerical instability.                  Rejecting this proposal prematurely
warning: numerical instability.                  Rejecting this proposal prematurely
warning: numerical instability.                  Rejecting this proposal prematurely
warning: numerical instability.                  Rejecting this proposal prematurely

Fitted parameters (From HEC)


In [ ]:
model.kern.lengthscales = 25.4846122373
model.kern.variance = 10.9742076021
model.likelihood.variance = 4.33463026664

In [ ]:
%time mm = k.compute_K_symm(X.as_matrix())

In [ ]:
import numpy as np
Nn = 500
dsc = data
predicted_x = np.linspace(min(dsc.lon),max(dsc.lon),Nn)
predicted_y = np.linspace(min(dsc.lat),max(dsc.lat),Nn)
Xx, Yy = np.meshgrid(predicted_x,predicted_y)
## Fake richness
fake_sp_rich = np.ones(len(Xx.ravel()))
predicted_coordinates = np.vstack([ Xx.ravel(), Yy.ravel()]).transpose()
#predicted_coordinates = np.vstack([section.SppN, section.newLon,section.newLat]).transpose()

In [ ]:
len(predicted_coordinates)

In [ ]:
#We will calculate everything with the new model and parameters
#model = gf.gpr.GPR(X.as_matrix(),Y.as_matrix().reshape(len(Y),1).astype(float),k)

In [ ]:
%time means,variances = model.predict_y(predicted_coordinates)
#USING k-partition = 10 import cartopy plt.figure(figsize=(17,11)) proj = cartopy.crs.PlateCarree() ax = plt.subplot(111, projection=proj) ax = plt.axes(projection=proj) #algo = new_data.plot(column='SppN',ax=ax,cmap=colormap,edgecolors='') #ax.set_extent([-93, -70, 30, 50]) ax.set_extent([-125, -60, 20, 50]) #ax.set_extent([-95, -70, 25, 45]) #ax.add_feature(cartopy.feature.LAND) ax.add_feature(cartopy.feature.OCEAN) ax.add_feature(cartopy.feature.COASTLINE) ax.add_feature(cartopy.feature.BORDERS, linestyle=':') ax.add_feature(cartopy.feature.LAKES, alpha=0.9) ax.stock_img() #ax.add_geometries(new_data.geometry,crs=cartopy.crs.PlateCarree()) #ax.add_feature(cartopy.feature.RIVERS) mm = ax.pcolormesh(Xx,Yy,means.reshape(Nn,Nn),transform=proj ) #cs = plt.contour(Xx,Yy,np.sqrt(variances).reshape(Nn,Nn),linewidths=2,cmap=plt.cm.Greys_r,linestyles='dotted') cs = plt.contour(Xx,Yy,means.reshape(Nn,Nn),linewidths=2,colors='k',linestyles='dotted',levels=[4.0,5.0,6.0,7.0,8.0]) plt.clabel(cs, fontsize=16,inline=True,fmt='%1.1f') #ax.scatter(new_data.lon,new_data.lat,edgecolors='',cmap=plt.cm.Accent,alpha=0.6) plt.colorbar(mm) plt.title("Predicted Species Richness") #(x.LON > -90) & (x.LON < -80) & (x.LAT > 40) & (x.LAT < 50)

In [ ]:
-

Predictions with +2std.Dev


In [ ]:
#Using k-partition = 7
import cartopy
plt.figure(figsize=(17,11))

proj = cartopy.crs.PlateCarree()
ax = plt.subplot(111, projection=proj)


ax = plt.axes(projection=proj)
#algo = new_data.plot(column='SppN',ax=ax,cmap=colormap,edgecolors='')


#ax.set_extent([-93, -70, 30, 50])
ax.set_extent([-125, -60, 20, 50])
#ax.set_extent([-95, -70, 25, 45])

#ax.add_feature(cartopy.feature.LAND)
ax.add_feature(cartopy.feature.OCEAN)
ax.add_feature(cartopy.feature.COASTLINE)
ax.add_feature(cartopy.feature.BORDERS, linestyle=':')
ax.add_feature(cartopy.feature.LAKES, alpha=0.9)
ax.stock_img()
#ax.add_geometries(new_data.geometry,crs=cartopy.crs.PlateCarree())
#ax.add_feature(cartopy.feature.RIVERS)
mm = ax.pcolormesh(Xx,Yy,means.reshape(Nn,Nn) + (2* np.sqrt(variances).reshape(Nn,Nn)),transform=proj )
#cs = plt.contour(Xx,Yy,np.sqrt(variances).reshape(Nn,Nn),linewidths=2,cmap=plt.cm.Greys_r,linestyles='dotted')
cs = plt.contour(Xx,Yy,means.reshape(Nn,Nn) + (2 * np.sqrt(variances).reshape(Nn,Nn)),linewidths=2,colors='k',linestyles='dotted',levels=range(1,20))
plt.clabel(cs, fontsize=16,inline=True,fmt='%1.1f')
#ax.scatter(new_data.lon,new_data.lat,edgecolors='',color='white',alpha=0.6)
plt.colorbar(mm)
plt.title("Predicted Species Richness + 2stdev")

Predicted means


In [ ]:
#Using k-partition = 7
import cartopy
plt.figure(figsize=(17,11))

proj = cartopy.crs.PlateCarree()
ax = plt.subplot(111, projection=proj)


ax = plt.axes(projection=proj)
#algo = new_data.plot(column='SppN',ax=ax,cmap=colormap,edgecolors='')


#ax.set_extent([-93, -70, 30, 50])
ax.set_extent([-125, -60, 20, 50])
#ax.set_extent([-95, -70, 25, 45])

#ax.add_feature(cartopy.feature.LAND)
ax.add_feature(cartopy.feature.OCEAN)
ax.add_feature(cartopy.feature.COASTLINE)
ax.add_feature(cartopy.feature.BORDERS, linestyle=':')
ax.add_feature(cartopy.feature.LAKES, alpha=0.9)
ax.stock_img()
#ax.add_geometries(new_data.geometry,crs=cartopy.crs.PlateCarree())
#ax.add_feature(cartopy.feature.RIVERS)
mm = ax.pcolormesh(Xx,Yy,means.reshape(Nn,Nn),transform=proj )
#cs = plt.contour(Xx,Yy,np.sqrt(variances).reshape(Nn,Nn),linewidths=2,cmap=plt.cm.Greys_r,linestyles='dotted')
cs = plt.contour(Xx,Yy,means.reshape(Nn,Nn),linewidths=2,colors='k',linestyles='dotted',levels=range(1,20))
plt.clabel(cs, fontsize=16,inline=True,fmt='%1.1f')
#ax.scatter(new_data.lon,new_data.lat,edgecolors='',color='white',alpha=0.6)
plt.colorbar(mm)
plt.title("Predicted Species Richness")

In [ ]:
#Using k-partition = 7
import cartopy
plt.figure(figsize=(17,11))

proj = cartopy.crs.PlateCarree()
ax = plt.subplot(111, projection=proj)


ax = plt.axes(projection=proj)
#algo = new_data.plot(column='SppN',ax=ax,cmap=colormap,edgecolors='')


#ax.set_extent([-93, -70, 30, 50])
ax.set_extent([-125, -60, 20, 50])
#ax.set_extent([-95, -70, 25, 45])

#ax.add_feature(cartopy.feature.LAND)
ax.add_feature(cartopy.feature.OCEAN)
ax.add_feature(cartopy.feature.COASTLINE)
ax.add_feature(cartopy.feature.BORDERS, linestyle=':')
ax.add_feature(cartopy.feature.LAKES, alpha=0.9)
ax.stock_img()
#ax.add_geometries(new_data.geometry,crs=cartopy.crs.PlateCarree())
#ax.add_feature(cartopy.feature.RIVERS)
mm = ax.pcolormesh(Xx,Yy,means.reshape(Nn,Nn) - (2* np.sqrt(variances).reshape(Nn,Nn)),transform=proj )
#cs = plt.contour(Xx,Yy,np.sqrt(variances).reshape(Nn,Nn),linewidths=2,cmap=plt.cm.Greys_r,linestyles='dotted')
cs = plt.contour(Xx,Yy,means.reshape(Nn,Nn) - (2 * np.sqrt(variances).reshape(Nn,Nn)),linewidths=2,colors='k',linestyles='dotted',levels=[4.0,5.0,6.0,7.0,8.0])
plt.clabel(cs, fontsize=16,inline=True,fmt='%1.1f')
#ax.scatter(new_data.lon,new_data.lat,edgecolors='',color='white',alpha=0.6)
plt.colorbar(mm)
plt.title("Predicted Species Richness - 2stdev")

Model Analysis


In [ ]:
model.get_parameter_dict()

Let's calculate the residuals


In [ ]:
X_ = data[['LON','LAT']]
%time Y_hat = model.predict_y(X_)

In [ ]:
pred_y = pd.DataFrame(Y_hat[0])
var_y = pd.DataFrame(Y_hat[1])

In [ ]:
new_data['pred_y'] = pred_y
new_data['var_y'] = var_y

In [ ]:
new_data= new_data.assign(error=lambda y : (y.SppN - y.pred_y)**2 )

In [ ]:
new_data.error.hist(bins=50)

In [ ]:
print(new_data.error.mean())
print(new_data.error.std())

Experiment

In this section we will bring a Raster Data from the US, using Biospytial Raster API.

  1. First select a polygon, then get A Raster FRom there, say Mean Temperature.

In [ ]:
import raster_api.tools as rt
from raster_api.models import MeanTemperature,ETOPO1,Precipitation,SolarRadiation
from sketches.models import Country

In [ ]:
## Select US
us_border = Country.objects.filter(name__contains='United States')[1]

In [ ]:
from django.db import close_old_connections

In [ ]:
close_old_connections()

In [ ]:
#Get Raster API
us_meantemp = rt.RasterData(Precipitation,us_border.geom)
us_meantemp.getRaster()

In [ ]:
us_meantemp.display_field()

In [ ]:
%time coords = us_meantemp.getCoordinates()

In [ ]:


In [ ]: