如何为sklearn正确编码类别-决策树的内存错误

时间:2020-04-09 14:17:03

标签: python encoding scikit-learn decision-tree

这是我第一次访问sklearn库,说实话,由于我在互联网上发现了许多“做事方式”,因此我脑子里一片混乱。 因此,我有一个如下所示的清理数据库:

<class 'pandas.core.frame.DataFrame'>
Int64Index: 246858 entries, 2 to 371527
Data columns (total 11 columns):
name                  246858 non-null object
price                 246858 non-null int64
vehicleType           246858 non-null object
yearOfRegistration    246858 non-null int64
gearbox               246858 non-null object
powerPS               246858 non-null int64
model                 246858 non-null object
kilometer             246858 non-null int64
fuelType              246858 non-null object
brand                 246858 non-null object
notRepairedDamage     246858 non-null object
dtypes: int64(4), object(7)
memory usage: 22.6+ MB

因此,我要继续对价格变量进行分类。显然,我必须对以下类别进行编码:

categorical = ['name', 'vehicleType', 'gearbox', 'model', 'fuelType', 'brand', 'notRepairedDamage']

这就是问题所在。我总是遇到内存错误。我尝试使用数据框映射器:

encoding = DataFrameMapper([
    (['name', 'vehicleType', 'gearbox', 'model', 'fuelType', 'brand', 'notRepairedDamage'], 
      OneHotEncoder(handle_unknown='ignore')),    
    (["price", "yearOfRegistration", "powerPS", "kilometer"], None)
])
encoding_target = DataFrameMapper([
    (['price'], none)
])

现在这很好,假设我要尝试分类树,必须创建训练并进行测试,但是在必须应用转换之前:

X = encoding.transform(data.loc[:, data.columns != "price"])

在这一点上,我得到一个内存错误。我不知道

1 个答案:

答案 0 :(得分:0)

尝试一下:

apply