繁体   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