如何根据频率对文本列进行分类

时间:2017-10-08 10:11:34

标签: python pandas dataframe scikit-learn

我有一个citie名单列表,我想根据它们的频率对其进行分类。我首先想要使用binning,但由于这需要单调间距我放弃了。下一个甚至可能更好的方法是使用pandas.qcut基于频率创建基于分位数的类别。但是有了分位数,我不知道如何根据分位数创建一个额外的列。例如:

import numpy as np
import pandas as pd

np.random.seed(0)
cities = np.random.choice(['Ontario', 'Ottawa', 'Vancouver','Edmonton',
                           'Winnipeg', 'Churchill'], 500)
# Create fake data and their frequencies
df = pd.DataFrame (cities, columns=['City'])
freq = df['City'].value_counts()
print (freq)
# Create quantiles
qc = pd.qcut (freq, 3)
print (qc)
# And now? I have the quantiles but how to assign a categorie to each City?
category_for_each_city = df['City'] in qc # does not work, but many other things neither

我尝试了很多东西,但没有一个有效。我应该能够为此编写一个循环,但我无法想象这是Python的方式。我试过寻找一些sklearn变形金刚,但找不到这个特定的解决方案。任何帮助将不胜感激。

此外,我有许多偏差的发行版,一个可以扩展到例如日志转换的解决方案将会有很大的帮助。

1 个答案:

答案 0 :(得分:1)

你差不多......

In [106]: category_for_each_city = df['City'].map(qc)

In [107]: category_for_each_city
Out[107]:
0      (77.333, 84.667]
1      (72.999, 77.333]
2       (84.667, 100.0]
3       (84.667, 100.0]
4       (84.667, 100.0]
5       (84.667, 100.0]
6      (77.333, 84.667]
             ...
493     (84.667, 100.0]
494    (72.999, 77.333]
495    (77.333, 84.667]
496     (84.667, 100.0]
497    (77.333, 84.667]
498    (77.333, 84.667]
499    (77.333, 84.667]
Name: City, Length: 500, dtype: category
Categories (3, interval[float64]): [(72.999, 77.333] < (77.333, 84.667] < (84.667, 100.0]]

<强>更新

In [114]: qc = pd.qcut (freq, 3, labels=[0,1,2])

In [115]: category_for_each_city = df['City'].map(qc)

In [116]: category_for_each_city
Out[116]:
0      1
1      0
2      2
3      2
4      2
5      2
6      1
      ..
493    2
494    0
495    1
496    2
497    1
498    1
499    1
Name: City, Length: 500, dtype: category
Categories (3, int64): [0 < 1 < 2]