站点数据驱动的遥感参数机器学习反演方法
软件环境配置
Anaconda安装与配置
1. 从清华镜像源获取安装包:
https://mirrors.tuna.tsinghua.edu.cn/anaconda/archive/
2. 管理员权限执行安装,选择默认配置路径
PyCharm环境设置
1. 下载社区版安装包
2. 自定义安装路径(避免中文目录)
3. 通过插件市场安装中文语言包
Python环境管理
1. 在Anaconda中创建新环境:
conda create --name rs_ml python=3.9
2. 安装必需库:
conda install -c conda-forge gdal scikit-learn xgboost numpy
3. PyCharm配置解释器路径:
~/anaconda3/envs/rs_ml/bin/python
数据预处理流程
遥感数据获取
1. 从地理空间数据云获取Landsat8数据
2. 使用ENVI进行波段合成:
File → Open → Layer Stacking<br>
选择波段1-7 → 设置输出路径
3. 导出GeoTIFF格式:
File → Save As → GeoTIFF
站点数据处理
1. 转换表格坐标为空间数据:
gdal.VectorTranslate('stations.shp', 'input.csv', format='ESRI Shapefile')
2. 坐标系转换(WGS84转UTM):
osr.CoordinateTransformation(src_srs, target_srs)
机器学习模型实现
数据准备
import pandas as pd
import numpy as np
from sklearn.preprocessing import StandardScaler
# 加载站点观测数据
obs_data = pd.read_csv('ground_stations.csv')
# 提取特征和标签
features = obs_data[['band1', 'band2', 'band3', 'band4',
'band5', 'band6', 'band7']].values
labels = obs_data['temperature'].values
# 数据标准化
scaler = StandardScaler()
scaled_features = scaler.fit_transform(features)
模型训练与优化
from xgboost import XGBRegressor
from sklearn.model_selection import GridSearchCV, train_test_split
# 数据集划分
X_train, X_test, y_train, y_test = train_test_split(
scaled_features, labels, test_size=0.2, random_state=42
)
# 参数网格配置
param_grid = {
'max_depth': [3, 5, 7],
'n_estimators': [100, 150, 200],
'learning_rate': [0.01, 0.1, 0.2]
}
# 网格搜索优化
model = XGBRegressor()
grid_search = GridSearchCV(model, param_grid, cv=3, scoring='neg_mean_squared_error')
grid_search.fit(X_train, y_train)
# 输出最优参数
print(f"最优参数: {grid_search.best_params_}")
print(f"最佳RMSE: {np.sqrt(-grid_search.best_score_)}")
模型应用
import rasterio
from rasterio.plot import reshape_as_image
# 加载遥感影像
with rasterio.open('landsat_composite.tif') as src:
img_data = src.read()
meta = src.meta
# 数据预处理
reshaped_img = reshape_as_image(img_data).reshape(-1, 7)
scaled_img = scaler.transform(reshaped_img)
# 全图预测
predicted = grid_search.predict(scaled_img)
result = predicted.reshape(img_data.shape[1], img_data.shape[2])
# 保存结果
meta.update(dtype=rasterio.float32, count=1)
with rasterio.open('lst_prediction.tif', 'w', **meta) as dst:
dst.write(result.astype(np.float32), 1)
精度验证
from sklearn.metrics import r2_score, mean_squared_error
test_pred = grid_search.predict(X_test)
print(f"R²: {r2_score(y_test, test_pred):.4f}")
print(f"RMSE: {np.sqrt(mean_squared_error(y_test, test_pred)):.4f}")