【问题标题】:How to print Categorical features in Machine Learning?如何在机器学习中打印分类特征?
【发布时间】:2020-05-11 12:45:28
【问题描述】:

假设我有一个火车数据集

r1:便宜,昂贵 -> 价格

r2: 兴奋 -> 娱乐

r3:炎热,夏天 -> 天气

r4: 钱 -> 价格

r5: 下雨 -> 天气


那么我想以这种模式显示它:

价格 -> 便宜,昂贵,金钱

娱乐 -> 兴奋

天气 -> 炎热,夏天,下雨

有人知道吗?我正在做 NLP 研究。谢谢你。

【问题讨论】:

  • 您能否添加几行数据集作为样本?

标签: python machine-learning nlp nltk data-science


【解决方案1】:
import pandas as pd
# Dictionary of items
d = {'words' : [ [ 'cheap', 'expensive'], ['excited'], ['hot', 'summer'], ['money'], ['rain'] ], 
     'category': ['price', 'entertainment', 'weather', 'price', 'weather']}
# Convert dictionary to dataframe
df = pd.DataFrame(d)
# Unpack the list of 'words' by joining with ','
df.words = df.words.str.join(',')
# Groupby and aggregate to get the unique 'words' for each 'category'
new_df = df.groupby('category').agg({'words':'unique'})
# Since the groupby results in a list of items, unpack by joining with ','
new_df.words = new_df.words.str.join(',')
# reset_index() to convert the groupby object to a dataframe
# This is optional. If not used, 'category' will the index of the dataframe.
new_df.reset_index(inplace=True)
new_df

【讨论】:

    猜你喜欢
    • 2013-03-25
    • 2015-12-11
    • 2019-07-14
    • 2012-03-28
    • 1970-01-01
    • 2013-06-23
    • 2019-06-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多