gpt4 book ai didi

python - 如何制作用于电影评论数据集分类的数据框?

转载 作者:太空宇宙 更新时间:2023-11-03 18:01:39 25 4
gpt4 key购买 nike

我是 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 (部分)。

我的目标是通过数字数据映射从该数据集中形成一个数据帧,以便我可以使用该数据帧对情绪进行分类。

最佳答案

使用纯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

关于python - 如何制作用于电影评论数据集分类的数据框?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27590796/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com