網(wǎng)站首頁 編程語言 正文
Python之sklearn數(shù)據(jù)預(yù)處理中fit(),transform()與fit_transform()的區(qū)別_python
作者:anshuai_aw1 ? 更新時間: 2023-03-29 編程語言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é)
原文鏈接:https://blog.csdn.net/anshuai_aw1/article/details/82498374
相關(guān)推薦
- 2022-07-12 使用docker搭建Redis-cluster偽集群
- 2022-02-17 antv g2設(shè)置chart圖例的legend為一條線與一個圓的組合
- 2022-06-22 C++深度探索運(yùn)算符重載和返回值優(yōu)化_C 語言
- 2022-06-26 C#實現(xiàn)數(shù)組元素的數(shù)據(jù)類型轉(zhuǎn)換方法詳解_C#教程
- 2022-06-12 C#集合之可觀察集合的用法_C#教程
- 2022-12-04 關(guān)于SQL查詢語句關(guān)鍵字方法_MsSql
- 2022-11-01 Android串口通訊SerialPort的使用詳情_Android
- 2022-08-19 Android開發(fā)自定義實時圖表控件實現(xiàn)示例_Android
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支