ML反演叶面积指数(LAI), 关于scikit-learn中的fit_transform()和transform()理解

机器学习遥感领域相关应用

最近使用了几种机器学习的算法来估算棉花的叶面积指数(Leaf Area Index, LAI),模型训练好了,使用单独的test data验证,几个模型的精度还不错,但是最后用训练好的模型和3期(2018.6.6、2018.7.11、2018.8.20)Sentinel-2遥感影像数据来反演LAI的时候,发现3期反演出来的LAI结果值分布非常接近(按照实测数据,正常结果应该是3期LAI均值分别在0.3、3.6、5.5左右),大部分值都在1.5~2.5之间,这让我感觉特别不能理解,明明模型已经训练好了,用他们去predict测试集表现也很好,可为什么最后拿遥感影像数据来算的时候就不对呢??然后我想了很久没有想出问题出在哪儿,后来我就想既然模型调参训练过程没有什么问题,那问题可能出在后面处理Sentinel-2影像这里,仔细看了下这部分的代码,突然发现一个小问题,模型训练的时候train data进行了Feature Scaling(主要是standardization),然后我发现Sentinel-2影像数据和train data都是用了fit_transform(),而模型测试的时候使用的是transform(),这让我很奇怪,又回去仔细翻看Hands-On Machine Learning with Scikit-Learn and TensorFlow这本书(当时构建模型的时候主要参考的这本书)发现scikit-learn中确实有2种变换方法fit_transform()和transform(),但是书中对这2种方法的解释不是特别理解,于是在网上查询了下2者的区别,终于明白,fit_transform()是用于train data(其中的fit主要是为了计算待标准化数据的均值和方差等参数),而transform()是用于test data,所以最后拿Sentinel-2影像反演LAI的时候不能再使用fit_transform()了,立马改成transform(),再运行模型,结果跟预期的一致。好了,问题终于解决了,可以回去安心改论文了。

*上别人的解答:

from sklearn.preprocessing import StandardScaler
sc = StandardScaler()
sc.fit_tranform(X_train)
sc.tranform(X_test)

ML反演叶面积指数(LAI), 关于scikit-learn中的fit_transform()和transform()理解

https://*.com/questions/23838056/what-is-the-difference-between-transform-and-fit-transform-in-sklearn

 

 

 

参考网址:

https://blog.csdn.net/jobs010/article/details/79032454

https://*.com/questions/23838056/what-is-the-difference-between-transform-and-fit-transform-in-sklearn

https://*.com/questions/48692500/fit-transform-on-training-data-and-transform-on-test-data?noredirect=1

https://blog.csdn.net/lufangbo/article/details/79308348