ArcGIS Pro随机森林分类实战:从数据准备到模型评估全流程指南

1次阅读
没有评论

共计 2528 个字符,预计需要花费 7 分钟才能阅读完成。

image.webp

背景痛点

在传统遥感影像分类中,最大似然法(Maximum Likelihood Classification)等方法存在几个明显的局限性:

ArcGIS Pro 随机森林分类实战:从数据准备到模型评估全流程指南

  • 对数据分布假设严格(如要求正态分布)
  • 难以处理高维特征(如多波段组合)
  • 缺乏特征重要性评估能力

随机森林(Random Forest)则表现出显著优势:

  • 抗噪声能力强,适合处理遥感数据中的异常值
  • 自动评估特征重要性(Feature Importance)
  • 支持非参数化数据,无需严格的数据分布假设

环境配置

推荐使用以下环境配置:

  • ArcGIS Pro 2.8+(需确保已安装 Python 3.7+)
  • 必需 Python 库:
  • arcpy(ArcGIS Pro 自带)
  • scikit-learn>=0.24(用于随机森林实现)
  • numpy>=1.20(数组运算支持)

安装命令:

conda install -c esri arcgis scikit-learn numpy

核心实现

数据预处理

关键步骤是将栅格数据转换为训练样本格式。通过 ArcPy 实现:

import arcpy
from sklearn.ensemble import RandomForestClassifier

# 将训练样本点转换为特征数组
train_points = "C:/data/train_samples.shp"  # 训练样本点矢量
raster_stack = "C:/data/imagery.tif"  # 多波段影像

# 提取样本点对应栅格值
arcpy.sa.ExtractMultiValuesToPoints(train_points, raster_stack, "BILINEAR")

# 将属性表转换为 NumPy 数组
fields = ["Band1", "Band2", "Band3", "Class"]  # 假设有 3 个波段和 1 个分类字段
data = arcpy.da.TableToNumPyArray(train_points, fields)
X = data[['Band1', 'Band2', 'Band3']]  # 特征矩阵
y = data['Class']  # 标签向量 

特征工程

可以构建衍生特征增强模型表现:

# 计算 NDVI(假设 Band3 是近红外,Band2 是红光)X['NDVI'] = (X['Band3'] - X['Band2']) / (X['Band3'] + X['Band2'] + 1e-10)

模型训练与保存

完整训练脚本示例:

from sklearn.model_selection import train_test_split
import joblib

# 划分训练集 / 测试集
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3)

# 初始化随机森林
rf = RandomForestClassifier(
    n_estimators=100,  # 决策树数量
    max_depth=15,      # 树的最大深度
    random_state=42    # 随机种子
)

# 训练模型
rf.fit(X_train, y_train)

# 保存模型
joblib.dump(rf, 'random_forest_model.pkl')

模型优化

参数调优

使用网格搜索(Grid Search)寻找最优参数组合:

from sklearn.model_selection import GridSearchCV

param_grid = {'n_estimators': [50, 100, 200],
    'max_depth': [10, 15, 20],
    'min_samples_split': [2, 5]
}

grid_search = GridSearchCV(
    estimator=rf,
    param_grid=param_grid,
    cv=3,  # 3 折交叉验证
    n_jobs=-1  # 使用所有 CPU 核心
)
grid_search.fit(X_train, y_train)

print("最佳参数:", grid_search.best_params_)

精度验证

生成混淆矩阵和 Kappa 系数:

from sklearn.metrics import confusion_matrix, cohen_kappa_score

y_pred = rf.predict(X_test)

# 混淆矩阵
cm = confusion_matrix(y_test, y_pred)
print("混淆矩阵:\n", cm)

# Kappa 系数
kappa = cohen_kappa_score(y_test, y_pred)
print("Kappa 系数:", round(kappa, 3))

生产建议

内存管理

处理大区域影像时建议分块处理:

# 分块处理函数示例
def block_classification(input_raster, model, block_size=1024):
    raster = arcpy.Raster(input_raster)

    # 获取影像范围
    extent = raster.extent
    width = int(extent.width)
    height = int(extent.height)

    # 分块处理
    for x in range(0, width, block_size):
        for y in range(0, height, block_size):
            # 读取当前块数据...
            # 预测分类...
            # 写入结果...

结果后处理

使用多数滤波(Majority Filter)消除小斑块:

# 分类后处理
classified_raster = "C:/output/classification.tif"
smoothed_raster = arcpy.sa.FocalStatistics(
    classified_raster, 
    "Rectangle 3 3 CELL", 
    "MAJORITY"
)
smoothed_raster.save("C:/output/classification_smoothed.tif")

延伸思考

对于超大规模数据分类,可以考虑:

  1. 将模型部署到 ArcGIS Enterprise 进行分布式计算
  2. 使用 Dask 或 Spark 扩展处理能力
  3. 尝试集成深度学习模型(如 U -Net)提升精度

通过以上流程,即使是 GIS 新手也能快速构建可投入生产的随机森林分类模型。建议从中小区域开始实践,逐步扩展到更复杂的应用场景。

正文完
 0
评论(没有评论)