【发布时间】:2019-07-22 15:46:36
【问题描述】:
我有一个这样的数据框:
Name subname Feature1 Feature2 ...
AAA a 0.123 0.345 ...
AAA b 0.123 0.345 ...
BBB a 0.123 0.345 ...
BBB b 0.123 0.345 ...
我要创建标签(添加新列):
Name subname Feature1 Feature2 ...Class
AAA a 0.123 0.345 ... 1
AAA b 0.123 0.345 ... 1
BBB a 0.123 0.345 ... 2
BBB b 0.123 0.345 ... 2
为了让数据适合分类模型,有没有一种方法可以有效地创建这些标签?我得到了超过 5000 行,非常感谢。
【问题讨论】:
-
你在编码什么,只是
Name?如果是df['Class'] = pd.factorize(df['Name'])[0] + 1,如果是2D分解,您可以使用np.unique和return_inverse -
您有单独的数据框或系列中的标签还是什么?
-
是的,仅根据“名称”,在最后一列中有标签,以便我可以适应 GDBT 模型来选择特征,这是正确的方法吗?
-
欺骗中的两个答案都是完全有效的,你可以选择使用哪个
-
@user3483203 我在 Google 上查看了一些教程,我可以使用“from sklearn.preprocessing import LabelEncoder”之类的东西吗?这会一样吗?非常感谢。
标签: python pandas encoding encode