簡體   English   中英

使用sklearn時python中的fit,transform和fit_transform有什么區別?

[英]What is difference between fit, transform and fit_transform in python when using sklearn?

from sklearn.preprocessing import Imputer
imputer = Imputer(missing_values='NaN', strategy='mean',axis=0)
imputer = imputer.fit(X[:, 1:3])
X[:, 1:3]=imputer.transform(X[:, 1:3]) 

你能幫我知道上面的代碼是做什么的嗎? 我對 Imputer 了解不多。 請幫助!

令人困惑的部分是適合和轉換。

 #here fit method will calculate the required parameters (In this case mean)
 #and store it in the impute object
 imputer = imputer.fit(X[:, 1:3])
 X[:, 1:3]=imputer.transform(X[:, 1:3]) 
 #imputer.transform will actually do the work of replacement of nan with mean.
 #This can be done in one step using fit_transform

Imputer 用於替換缺失值。 fit 方法計算參數,而 fit_transform 方法更改數據以用均值替換那些 NaN 並輸出新矩陣 X。

# Imports library
from sklearn.preprocessing import Imputer

# Create a new instance of the Imputer object
# Missing values are replaced with NaN
# Missing values are replaced by the mean later on
# The axis determines whether you want to move column or row wise
imputer = Imputer(missing_values='NaN', strategy='mean',axis=0)

# Fit the imputer to X
imputer = imputer.fit(X[:, 1:3])

# Replace in the original matrix X
# with the new values after the transformation of X
X[:, 1:3]=imputer.transform(X[:, 1:3]) 

我為你注釋掉了代碼,我希望這會更有意義。 您需要將 X 視為一個矩陣,您必須對其進行轉換才能不再有 NaN(缺失值)。

有關詳細信息,請參閱文檔。

你的評論告訴你區別。 這是說,如果你不使用 imputer.fit,你就不能用某種方法替換 nan,例如用均值或中值。 要應用此過程,您需要在 imputer.fit 之后使用 imputer.transform,然后您將擁有一個沒有 nan 值的新數據集。

據我所知,從庫中​​導入一個特定的類

from sklearn.preprocessing import Imputer

創建一個類的對象,根據我們的個性化數據處理數據

imputer = Imputer(missing_values='NaN', strategy='mean',axis=0)

應用(如在數據上應用函數)到矩陣 x

例如讓一個操作符 e 應用於數據 d Imputer.fit返回 ed imputer = imputer.fit(X[:, 1:3])

現在Imputer.transform計算 ed 的值並將其分配給給定的矩陣

X[:, 1:3]=imputer.transform(X[:, 1:3])

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM