pandas 在sklearn中将文本列转换为数字

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34915813/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 00:32:35  来源:igfitidea点击:

convert text columns into numbers in sklearn

pythonnumpypandasscikit-learn

提问by Selva Saravana Er

I'm new to data analytics. I'm trying some models in python Sklearn. I have a dataset in which some of the columns have text columns. Like below,

我是数据分析的新手。我正在 python Sklearn 中尝试一些模型。我有一个数据集,其中一些列有文本列。像下面,

Dataset

数据集

Is there a way to convert these column values into numbers in pandas or Sklearn?. Assigning numbers to these values will be right?. And what if a new string pops out in test data?.

有没有办法将这些列值转换为 Pandas 或 Sklearn 中的数字?为这些值分配数字是否正确?。如果测试数据中弹出一个新字符串怎么办?

Please advice.

请指教。

回答by Amir F

Consider using Label Encoding - it transforms the categorical data by assigning each category an integer between 0 and the num_of_categories-1:

考虑使用标签编码 - 它通过为每个类别分配一个介于 0 和 num_of_categories-1 之间的整数来转换分类数据:

from sklearn.preprocessing import LabelEncoder
df = pd.DataFrame(['a','b','c','d','a','c','a','d'], columns=['letter'])

  letter
0      a
1      b
2      c
3      d
4      a
5      c
6      a

Applying:

申请:

le = LabelEncoder()
encoded_series = df[df.columns[:]].apply(le.fit_transform)

encoded_series:

编码系列:

    letter
0   0
1   1
2   2
3   3
4   0
5   2
6   0
7   3

回答by maxymoo

You can convert them into integer codes by using the categorical datatype.

您可以使用分类数据类型将它们转换为整数代码。

column = column.astype('category')
column_encoded = column.cat.codes

As long as use use a tree based model with deep enough trees, eg GradientBoostingClassifier(max_depth=10), your model should be able to split out the categories again.

只要使用具有足够深树的基于树的模型,例如GradientBoostingClassifier(max_depth=10),您的模型就应该能够再次拆分类别。