5 python machine-learning scikit-learn
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])
Run Code Online (Sandbox Code Playgroud)
你能帮我知道上面代码的作用吗?我对Imputer知之甚少.请帮忙!
小智 6
令人困惑的部分是适合和变换.
#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
Run Code Online (Sandbox Code Playgroud)
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])
Run Code Online (Sandbox Code Playgroud)
我为您注释掉了代码,希望这会更有意义。您需要将 X 视为一个矩阵,必须对其进行转换才能不再有 NaN(缺失值)。
请参阅文档以获取更多信息。