問題:數(shù)據(jù)標(biāo)準(zhǔn)化,應(yīng)該在訓(xùn)練集和測試集劃分前還是后?
答:數(shù)據(jù)標(biāo)準(zhǔn)化,應(yīng)該在訓(xùn)練集和測試集劃分后,分別對訓(xùn)練集和測試集進(jìn)行數(shù)據(jù)標(biāo)準(zhǔn)化處理。不應(yīng)該是數(shù)據(jù)標(biāo)準(zhǔn)化后,再進(jìn)行劃分。雖然從模型測試的結(jié)果看,可能出現(xiàn)的差距不大。
解釋:當(dāng)你手頭已經(jīng)有一份訓(xùn)練和測試數(shù)據(jù)。在真正的部署過程中,測試數(shù)據(jù)實際上就是那些源源不斷剛剛出現(xiàn)的數(shù)據(jù),你不知道它什么分布,也不知道它出現(xiàn)什么樣的數(shù)值。所以你要用訓(xùn)練數(shù)據(jù)得到的均值和標(biāo)準(zhǔn)偏差,去轉(zhuǎn)換它。這更加貼近部署的實際。測試集的歸一化的均值和標(biāo)準(zhǔn)偏差應(yīng)該來源于訓(xùn)練集
代碼:
from sklearn import preprocessing
Standard = preprocessing.StandardScaler()
x_train = Standard.fit_transform(x_train)
x_test = Standard.transform(x_test)
注意事項:
1.必須現(xiàn)在train上使用fit_transform,再在test數(shù)據(jù)集上使用transform
2.? ? x_train = preprocessing.StandardScaler().fit_transform(x_train)
? ? ? ?x_test = preprocessing.StandardScaler().transform(x_test)
????????會報錯:文章來源:http://www.zghlxwxcb.cn/news/detail-404067.html
This StandardScaler instance is not fitted yet. Call 'fit' with appropriate arguments before using this estimator.
解決辦法:需要Standard = preprocessing.StandardScaler(),定義一個對象后,再進(jìn)行標(biāo)準(zhǔn)化處理。文章來源地址http://www.zghlxwxcb.cn/news/detail-404067.html
到了這里,關(guān)于訓(xùn)練集和測試集的標(biāo)準(zhǔn)化中的坑!??!的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!