我正在处理威斯康星州的乳腺癌数据集,发现了这里。特征工程在机器学习中是很重要的,所以我的一位老师推荐了一个发现这里的库中的这里部分。dataframe看起来如下所示:

我确实将诊断特性/列更改为类别,因为其中一个错误说这可能是问题,但显然不是,因为它还没有解决。
我的意思是使用上面链接的库中的MeanEncode编码目标特性/列。下面是我尝试这样做的功能:
def MeanEncoding(self):
# Get the columns besides the target variable at the front, which is diagnosis, as recommended by teacher.
cols = self.m_df.iloc[:, 1:].columns.to_list()
# Save specifically the target variable too.
target = self.m_df.iloc[:, 0]
# Now get the object ready.
encoder = MeanEncoder(variables=cols)
print('---Fitting---')
encoder.fit(self.m_df.drop('diagnosis', axis=1), target)在此代码中:
现在,我在想,“不可能,我必须把‘radius_ to’,‘织构_均号’等数字特征转换成类别或对象?这是0的道理”。当然,我在谷歌上搜索了这个错误,它把我带到了这根线。这个人和我一样也有类似的担忧,除了有不同的功能。对他的建议是“在使用imputer之前,只需将年级列的dtype更改为object”,所以我还用以下代码更改了这些类型:
for i in range(1, len(self.m_df.columns)):
columnName = self.m_df.columns[i]
self.m_df[columnName] = self.m_df[columnName].astype('object')对我来说没有意义,因为它正在转换真正的数字列/特性的类型。我得到了这个错误,这是预料中的:
pandas.core.base.DataError: No numeric types to aggregate现在我想它只需要几个数字类型,所以我稍微修改了代码:
for i in range(1, len(self.m_df.columns) - 2):
columnName = self.m_df.columns[i]
self.m_df[columnName] = self.m_df[columnName].astype('object')这实际上只剩下最后2列为float64类型,因此所有其他列都是类型对象(除了诊断列,它是类别,但我怀疑这有什么关系)。现在出现了一些数字类型。不过,我还是会再犯错误
TypeError: Some of the variables are not categorical. Please cast
them as object or category before calling this transformer我显然遗漏了一些东西,但不确定是什么。无论我如何改变类型以满足函数,它都是错误的。
发布于 2022-01-31 14:16:27
来自Feature的MeanEncoder以及所有其他的特性引擎编码器,默认情况下只处理作为对象或类别的变量。
因此,在代码行中的列表cols中捕获的变量:cols = self.m_df.iloc[:, 1:].columns.to_list()应该只包含分类变量(对象或类别)。
当您在这里设置编码器:encoder = MeanEncoder(variables=cols)时,在variables中,您指示要编码的变量。如果您传递cols,这意味着要对cols列表中的所有变量进行编码。因此,您需要确保它们都是类型、类别或对象。
如果您得到错误:"TypeError: Some of the variables are not categorical. Please cast them as object or category before calling this transformer",这意味着cols中的某些变量不是对象或类别的类型。
如果要对数值变量进行编码,有两个选项: 1)将要编码的变量重新编码为对象。2)根据ignore_format=True设置参数变压器文件。这应该能解决你的问题。
https://stackoverflow.com/questions/70452821
复制相似问题