Python之sklearn數(shù)據(jù)預(yù)處理中fit(),transform()與fit_transform()的區(qū)別
sklearn數(shù)據(jù)預(yù)處理中fit(),transform()與fit_transform()的區(qū)別
概述
注意這是數(shù)據(jù)預(yù)處理中的方法:
Fit(): Method calculates the parameters μ and σ and saves them as internal objects.
解釋:簡單來說,就是求得訓(xùn)練集X的均值啊,方差啊,最大值啊,最小值啊這些訓(xùn)練集X固有的屬性。可以理解為一個訓(xùn)練過程
Transform(): Method using these calculated parameters apply the transformation to a particular dataset.
解釋:在Fit的基礎(chǔ)上,進(jìn)行標(biāo)準(zhǔn)化,降維,歸一化等操作(看具體用的是哪個工具,如PCA,StandardScaler等)。
Fit_transform(): joins the fit() and transform() method for transformation of dataset.
解釋:
fit_transform
是fit和transform的組合,既包括了訓(xùn)練又包含了轉(zhuǎn)換。transform()
和fit_transform()二者的功能都是對數(shù)據(jù)進(jìn)行某種統(tǒng)一處理(比如標(biāo)準(zhǔn)化~N(0,1),將數(shù)據(jù)縮放(映射)到某個固定區(qū)間,歸一化,正則化等)fit_transform(trainData)
對部分?jǐn)?shù)據(jù)先擬合fit,找到該part的整體指標(biāo),如均值、方差、最大值最小值等等(根據(jù)具體轉(zhuǎn)換的目的),然后對該trainData進(jìn)行轉(zhuǎn)換transform,從而實現(xiàn)數(shù)據(jù)的標(biāo)準(zhǔn)化、歸一化等等。
根據(jù)對之前部分trainData進(jìn)行fit的整體指標(biāo),對剩余的數(shù)據(jù)(testData)使用同樣的均值、方差、最大最小值等指標(biāo)進(jìn)行轉(zhuǎn)換transform(testData),從而保證train、test處理方式相同。
所以,一般都是這么用:
from sklearn.preprocessing import StandardScaler sc = StandardScaler() sc.fit_tranform(X_train) sc.tranform(X_test)
Note:
- 必須先用fit_transform(trainData),之后再transform(testData)
- 如果直接transform(testData),程序會報錯
- 如果fit_transfrom(trainData)后,使用fit_transform(testData)而不transform(testData),雖然也能歸一化,但是兩個結(jié)果不是在同一個“標(biāo)準(zhǔn)”下的,具有明顯差異。(一定要避免這種情況)
舉例
以PCA預(yù)處理,舉個栗子:
import pandas as pd import numpy as np ? from sklearn.decomposition import PCA? ? #========================================================================================== X1=pd.DataFrame(np.arange(9).reshape((3,3)),index=['a','b','c'], ? ? ? ? ? ? ? columns=['one','two','three']) ? ? pca=PCA(n_components=1) ? newData1=pca.fit_transform(X1) ? pca.fit(X1) newData12=pca.transform(X1) ? """ newData1和newData2結(jié)果一致 """ #========================================================================================== a=[[1,2,3],[5,6,7],[4,5,8]] ? X2=pd.DataFrame(np.array(a),index=['a','b','c'], ? ? ? ? ? ? ? columns=['one','two','three']) ? pca_new=PCA(n_components=1) pca_new.transform(X2) """ 沒有fit,直接transform報錯: NotFittedError: This PCA instance is not fitted yet. Call 'fit' with appropriate arguments before using this method. """
sklearn中歸一化的坑
This MinMaxScaler instance is not fitted yet. Call 'fit' with appropriate arguments before using this method.
原因
歸一化時,fit() 和transform() 兩個方法要分開.
sc_x = MinMaxScaler(feature_range=(0, 1)).fit(X) X=sc_x.transform(X)
sc_y = MinMaxScaler(feature_range=(0, 1)).fit(Y) Y = sc_y.transform(Y)
總結(jié)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
淺析python實現(xiàn)scrapy定時執(zhí)行爬蟲
這篇文章主要介紹了淺析python實現(xiàn)scrapy定時執(zhí)行爬蟲的相關(guān)資料,需要的朋友可以參考下2018-03-03python 用pandas實現(xiàn)數(shù)據(jù)透視表功能
這篇文章主要介紹了python 用pandas實現(xiàn)數(shù)據(jù)透視表功能的方法,幫助大家更好的理解和使用python,感興趣的朋友可以了解下2020-12-12python對站點數(shù)據(jù)做EOF且做插值繪制填色圖
這篇文章主要介紹了python對站點數(shù)據(jù)做EOF且做插值繪制填色圖,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價值,,需要的小伙伴可以參考一下2022-09-09Python使用wxpy模塊實現(xiàn)微信兩兩群組消息同步功能(推薦)
這篇文章主要介紹了Python使用wxpy模塊實現(xiàn)微信兩兩群組消息同步,本文通過示例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-06-06