如何在机器学习中打印分类特征?

假设我有一个训练数据集

r1: 便宜, 昂贵 -> 价格

r2: 兴奋 -> 娱乐

r3: 热, 夏天 -> 天气

r4: 钱 -> 价格

r5: 雨 -> 天气


然后我想按以下模式显示:

价格 -> 便宜, 昂贵, 钱

娱乐 -> 兴奋

天气 -> 热, 夏天, 雨

有人知道吗?我正在进行一项自然语言处理研究。谢谢。


回答:

import pandas as pd# Dictionary of itemsd = {'words' : [ [ 'cheap', 'expensive'], ['excited'], ['hot', 'summer'], ['money'], ['rain'] ],      'category': ['price', 'entertainment', 'weather', 'price', 'weather']}# Convert dictionary to dataframedf = 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

Related Posts

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注