Modeling species’ geographic distributions is an important
problem in conservation biology. In this example, we
model the geographic distribution of two South American
mammals given past observations and 14 environmental
variables. Since we have only positive examples (there are
no unsuccessful observations), we cast this problem as a
density estimation problem and use the OneClassSVM
as our modeling tool. The dataset is provided by Phillips et. al. (2006).
If available, the example uses
basemap
to plot the coast lines and national boundaries of South America.
________________________________________________________________________________
Modeling distribution of species 'bradypus variegatus'
- fit OneClassSVM ... done.
- plot coastlines from coverage
- predict species distribution
Area under the ROC curve : 0.868443
________________________________________________________________________________
Modeling distribution of species 'microryzomys minutus'
- fit OneClassSVM ... done.
- plot coastlines from coverage
- predict species distribution
Area under the ROC curve : 0.993919
time elapsed: 6.66s
# Authors: The scikit-learn developers# SPDX-License-Identifier: BSD-3-Clausefromtimeimporttimeimportmatplotlib.pyplotaspltimportnumpyasnpfromsklearnimportmetrics,svmfromsklearn.datasetsimportfetch_species_distributionsfromsklearn.utilsimportBunch# if basemap is available, we'll use it.# otherwise, we'll improvise later...try:frommpl_toolkits.basemapimportBasemapbasemap=TrueexceptImportError:basemap=Falsedefconstruct_grids(batch):"""Construct the map grid from the batch object Parameters ---------- batch : Batch object The object returned by :func:`fetch_species_distributions` Returns ------- (xgrid, ygrid) : 1-D arrays The grid corresponding to the values in batch.coverages """# x,y coordinates for corner cellsxmin=batch.x_left_lower_corner+batch.grid_sizexmax=xmin+(batch.Nx*batch.grid_size)ymin=batch.y_left_lower_corner+batch.grid_sizeymax=ymin+(batch.Ny*batch.grid_size)# x coordinates of the grid cellsxgrid=np.arange(xmin,xmax,batch.grid_size)# y coordinates of the grid cellsygrid=np.arange(ymin,ymax,batch.grid_size)return(xgrid,ygrid)defcreate_species_bunch(species_name,train,test,coverages,xgrid,ygrid):"""Create a bunch with information about a particular organism This will use the test/train record arrays to extract the data specific to the given species name. """bunch=Bunch(name=" ".join(species_name.split("_")[:2]))species_name=species_name.encode("ascii")points=dict(test=test,train=train)forlabel,ptsinpoints.items():# choose points associated with the desired speciespts=pts[pts["species"]==species_name]bunch["pts_%s"%label]=pts# determine coverage values for each of the training & testing pointsix=np.searchsorted(xgrid,pts["dd long"])iy=np.searchsorted(ygrid,pts["dd lat"])bunch["cov_%s"%label]=coverages[:,-iy,ix].Treturnbunchdefplot_species_distribution(species=("bradypus_variegatus_0","microryzomys_minutus_0"),):""" Plot the species distribution. """iflen(species)>2:print("Note: when more than two species are provided,"" only the first two will be used")t0=time()# Load the compressed datadata=fetch_species_distributions()# Set up the data gridxgrid,ygrid=construct_grids(data)# The grid in x,y coordinatesX,Y=np.meshgrid(xgrid,ygrid[::-1])# create a bunch for each speciesBV_bunch=create_species_bunch(species[0],data.train,data.test,data.coverages,xgrid,ygrid)MM_bunch=create_species_bunch(species[1],data.train,data.test,data.coverages,xgrid,ygrid)# background points (grid coordinates) for evaluationnp.random.seed(13)background_points=np.c_[np.random.randint(low=0,high=data.Ny,size=10000),np.random.randint(low=0,high=data.Nx,size=10000),].T# We'll make use of the fact that coverages[6] has measurements at all# land points. This will help us decide between land and water.land_reference=data.coverages[6]# Fit, predict, and plot for each species.fori,speciesinenumerate([BV_bunch,MM_bunch]):print("_"*80)print("Modeling distribution of species '%s'"%species.name)# Standardize featuresmean=species.cov_train.mean(axis=0)std=species.cov_train.std(axis=0)train_cover_std=(species.cov_train-mean)/std# Fit OneClassSVMprint(" - fit OneClassSVM ... ",end="")clf=svm.OneClassSVM(nu=0.1,kernel="rbf",gamma=0.5)clf.fit(train_cover_std)print("done.")# Plot map of South Americaplt.subplot(1,2,i+1)ifbasemap:print(" - plot coastlines using basemap")m=Basemap(projection="cyl",llcrnrlat=Y.min(),urcrnrlat=Y.max(),llcrnrlon=X.min(),urcrnrlon=X.max(),resolution="c",)m.drawcoastlines()m.drawcountries()else:print(" - plot coastlines from coverage")plt.contour(X,Y,land_reference,levels=[-9998],colors="k",linestyles="solid")plt.xticks([])plt.yticks([])print(" - predict species distribution")# Predict species distribution using the training dataZ=np.ones((data.Ny,data.Nx),dtype=np.float64)# We'll predict only for the land points.idx=(land_reference>-9999).nonzero()coverages_land=data.coverages[:,idx[0],idx[1]].Tpred=clf.decision_function((coverages_land-mean)/std)Z*=pred.min()Z[idx[0],idx[1]]=predlevels=np.linspace(Z.min(),Z.max(),25)Z[land_reference==-9999]=-9999# plot contours of the predictionplt.contourf(X,Y,Z,levels=levels,cmap=plt.cm.Reds)plt.colorbar(format="%.2f")# scatter training/testing pointsplt.scatter(species.pts_train["dd long"],species.pts_train["dd lat"],s=2**2,c="black",marker="^",label="train",)plt.scatter(species.pts_test["dd long"],species.pts_test["dd lat"],s=2**2,c="black",marker="x",label="test",)plt.legend()plt.title(species.name)plt.axis("equal")# Compute AUC with regards to background pointspred_background=Z[background_points[0],background_points[1]]pred_test=clf.decision_function((species.cov_test-mean)/std)scores=np.r_[pred_test,pred_background]y=np.r_[np.ones(pred_test.shape),np.zeros(pred_background.shape)]fpr,tpr,thresholds=metrics.roc_curve(y,scores)roc_auc=metrics.auc(fpr,tpr)plt.text(-35,-70,"AUC: %.3f"%roc_auc,ha="right")print("\n Area under the ROC curve : %f"%roc_auc)print("\ntime elapsed: %.2fs"%(time()-t0))plot_species_distribution()plt.show()
Total running time of the script: (0 minutes 6.859 seconds)