物种分布模型分析

在生物多样性保护领域,地理分布模型是了解物种生态习性和保护策略的重要工具。本文以南美洲两种哺乳动物为例,展示了如何利用OneClassSVM模型和14个环境变量来预测它们的地理分布。由于观测数据中只包含成功记录,将这一问题转化为密度估计问题,并使用OneClassSVM作为建模工具。数据集由Phillips等人在2006年提供。如果条件允许,会使用basemap库来绘制南美洲的海岸线和国界。

本文研究的两种物种分别是:

  • Bradypus variegatus,即棕喉树懒,一种生活在树上的哺乳动物。
  • Microryzomys minutus,也称为森林小稻鼠,一种生活在秘鲁、哥伦比亚、厄瓜多尔和委内瑞拉的啮齿类动物。

在模型构建过程中,首先使用OneClassSVM对数据进行拟合,然后绘制海岸线,并预测物种的分布。通过计算ROC曲线下面积(AUC),可以评估模型的预测性能。以下是模型分析的详细步骤和结果。

模型分析步骤

首先,需要构建地图网格。这可以通过从数据集中获取的坐标点来实现。接着,为每个物种创建一个数据集,包括训练和测试数据。然后,使用OneClassSVM模型对标准化后的特征进行拟合,并预测物种的分布。最后,计算ROC曲线下面积,以评估模型的性能。

from time import time import matplotlib.pyplot as plt import numpy as np from sklearn import metrics, svm from sklearn.datasets import fetch_species_distributions from sklearn.utils import Bunch try: from mpl_toolkits.basemap import Basemap basemap = True except ImportError: basemap = False def construct_grids(batch): # 构建地图网格 xmin = batch.x_left_lower_corner + batch.grid_size xmax = xmin + (batch.Nx * batch.grid_size) ymin = batch.y_left_lower_corner + batch.grid_size ymax = ymin + (batch.Ny * batch.grid_size) xgrid = np.arange(xmin, xmax, batch.grid_size) ygrid = np.arange(ymin, ymax, batch.grid_size) return xgrid, ygrid def create_species_bunch(species_name, train, test, coverages, xgrid, ygrid): # 创建物种数据集 bunch = Bunch(name=" ".join(species_name.split("_")[:2])) species_name = species_name.encode("ascii") points = dict(test=test, train=train) for label, pts in points.items(): pts = pts[pts["species"] == species_name] bunch["pts_%s" % label] = pts ix = np.searchsorted(xgrid, pts["dd long"]) iy = np.searchsorted(ygrid, pts["dd lat"]) bunch["cov_%s" % label] = coverages[:, -iy, ix].T return bunch def plot_species_distribution(species=("bradypus_variegatus_0", "microryzomys_minutus_0")): # 绘制物种分布图 t0 = time() data = fetch_species_distributions() xgrid, ygrid = construct_grids(data) X, Y = np.meshgrid(xgrid, ygrid[::-1]) BV_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 = np.c_[np.random.randint(low=0, high=data.Ny, size=10000), np.random.randint(low=0, high=data.Nx, size=10000)].T land_reference = data.coverages[6] for i, species in enumerate([BV_bunch, MM_bunch]): print("Modeling distribution of species '%s'" % species.name) mean = species.cov_train.mean(axis=0) std = species.cov_train.std(axis=0) train_cover_std = (species.cov_train - mean) / std clf = svm.OneClassSVM(nu=0.1, kernel="rbf", gamma=0.5) clf.fit(train_cover_std) plt.subplot(1, 2, i+1) if basemap: m = Basemap(projection="cyl", llcrnrlat=Y.min(), urcrnrlat=Y.max(), llcrnrlon=X.min(), urcrnrlon=X.max(), resolution="c") m.drawcoastlines() m.drawcountries() else: plt.contour(X, Y, land_reference, levels=[-9998], colors="k", linestyles="solid") plt.xticks([]) plt.yticks([]) Z = np.ones((data.Ny, data.Nx), dtype=np.float64) idx = np.where(land_reference > -9999) coverages_land = data.coverages[:, idx[0], idx[1]].T pred = clf.decision_function((coverages_land - mean) / std) Z *= pred.min() Z[idx[0], idx[1]] = pred levels = np.linspace(Z.min(), Z.max(), 25) Z[land_reference == -9999] = -9999 plt.contourf(X, Y, Z, levels=levels, cmap=plt.cm.Reds) plt.colorbar(format="%.2f") plt.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") pred_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("\nArea under the ROC curve : %f" % roc_auc) print("\ntime elapsed: %.2f s" % (time() - t0)) plot_species_distribution() plt.show()

通过上述代码,可以看到模型的运行结果。对于Bradypus variegatus,ROC曲线下面积为0.868443,而对于Microryzomys minutus,该值为0.993919,显示出较高的预测准确性。整个脚本的运行时间为10.610秒。

相关示例

以下是一些与本模型相关的其他示例,它们也涉及到物种分布的建模和分析:

  • Kernel Density Estimate of Species Distributions
  • SGD: Weighted samples
  • Plot multi-class SGD on the iris dataset
  • SVM: Weighted samples
沪ICP备2024098111号-1
上海秋旦网络科技中心:上海市奉贤区金大公路8218号1幢 联系电话:17898875485