【问题标题】:How can i make Dataframe for classification from movie review dataset?如何从电影评论数据集中制作用于分类的 Dataframe?
【发布时间】:2015-02-19 20:24:27
【问题描述】:

我是 pandas 的新手,正在尝试使用一些数据进行练习。我得到以下格式的训练数据集。
这是电影评论的数据集。如何从此类数据中制作 DataFrame 以用于 SVM 分类。我已经练习过使用 [12000*12] 大小的数据进行分类,其中每一行具有相同数量的属性。但是在这里,属性的长度不相等。我该如何修改。

PhraseId    SentenceId  Phrase  Sentiment
1   1   Wanker Goths are on the loose ! 2
2   1   Wanker Goths    2
3   1   Wanker  2
4   1   Goths   2
5   1   are on the loose !  2
6   1   are on the loose    2
7   1   on the loose    2
8   1   the loose   2
9   2   made Eddie Murphy a movie star and the man has n't aged a day . 3
10  2   made Eddie Murphy a movie star and the man  3
11  2   Eddie Murphy a movie star and the man   2
12  2   a movie star and the man    2
13  2   a movie star and    2
14  2   has n't aged a day .    2
15  2   has n't aged a day  3
16  2   aged a day  2

这是实际训练dataset(部分)。

我的目标是用数字数据映射从这个 Dataset 形成一个 DataFrame,以便我可以使用该 DataFrame 对 Sentiment 进行分类。

【问题讨论】:

  • 我知道 pandas 有一个 get_dummies() 但我从未在文本分类问题中使用过它

标签: python pandas dataframe svm sentiment-analysis


【解决方案1】:

用纯python:

t = """PhraseId    SentenceId  Phrase  Sentiment
1   1   Wanker Goths are on the loose ! 2
2   1   Wanker Goths    2
3   1   Wanker  2
4   1   Goths   2
5   1   are on the loose !  2"""

用换行符分割字符串:

t = t.split('\n')

然后获取拆分后的字符串列表:

s = [i.split() for i in t]

然后合并短语并得到一个数据框:

import pandas as pd
df = pd.DataFrame([(i[0],i[1],' '.join(i[2:-1]),i[-1]) for i in s],columns=s[0])
df = df.ix[1:]
print df

【讨论】:

  • 对不起,我只知道如何将它转换成DataFrame。
  • 谢谢您,先生。我知道这部分。我实际上需要进一步转换
猜你喜欢
  • 1970-01-01
  • 2020-10-20
  • 2020-04-08
  • 2021-06-23
  • 1970-01-01
  • 2015-07-03
  • 2017-12-13
  • 2021-09-15
  • 2020-04-25
相关资源
最近更新 更多