0%

[阿里云天池] 自然语言处理训练营 1

数据读取

1
2
3
import pandas as pd
train_df = pd.read_csv('./data/train_set.csv', sep='\t', nrows=100)
train_df.head()
label text
0 2 2967 6758 339 2021 1854 3731 4109 3792 4149 15…
1 11 4464 486 6352 5619 2465 4802 1452 3137 5778 54…
2 3 7346 4068 5074 3747 5681 6093 1777 2226 7354 6…
3 2 7159 948 4866 2109 5520 2490 211 3956 5520 549…
4 3 3646 3055 3055 2490 4659 6065 3370 5814 2465 5…

数据分析

在读取完成数据集后,我们还可以对数据集进行数据分析的操作。虽然对于非结构数据并不需要做很多的数据分析,但通过数据分析还是可以找出一些规律的。

此步骤我们读取了所有的训练集数据,在此我们通过数据分析希望得出以下结论:

  • 赛题数据中,新闻文本的长度是多少?
  • 赛题数据的类别分布是怎么样的,哪些类别比较多?
  • 赛题数据中,字符分布是怎么样的?

    句子长度分析

在赛题数据中每行句子的字符使用空格进行隔开,所以可以直接统计单词的个数来得到每个句子的长度。统计并如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
%pylab inline
train_df['text_len'] = train_df['text'].apply(lambda x: len(x.split(' ')))
print(train_df['text_len'].describe())

Populating the interactive namespace from numpy and matplotlib
count 100.000000
mean 872.320000
std 923.138191
min 64.000000
25% 359.500000
50% 598.000000
75% 1058.000000
max 7125.000000
Name: text_len, dtype: float64

新闻类别分布

1
2
3
train_df['label'].value_counts().plot(kind='bar')
plt.title('News class count')
plt.xlabel("category")

在数据集中标签的对应的关系如下:{‘科技’: 0, ‘股票’: 1, ‘体育’: 2, ‘娱乐’: 3, ‘时政’: 4, ‘社会’: 5, ‘教育’: 6, ‘财经’: 7, ‘家居’: 8, ‘游戏’: 9, ‘房产’: 10, ‘时尚’: 11, ‘彩票’: 12, ‘星座’: 13}

从统计结果可以看出,赛题的数据集类别分布存在较为不均匀的情况。在训练集中科技类新闻最多,其次是股票类新闻,最少的新闻是星座新闻。

字符分布统计

接下来可以统计每个字符出现的次数,首先可以将训练集中所有的句子进行拼接进而划分为字符,并统计每个字符的个数。

从统计结果中可以看出,在训练集中总共包括6869个字,其中编号3750的字出现的次数最多,编号3133的字出现的次数最少。

1
2
3
4
5
6
7
8
9
10
from collections import Counter
all_lines = ' '.join(list(train_df['text']))
word_count = Counter(all_lines.split(" "))
word_count = sorted(word_count.items(), key=lambda d:d[1], reverse = True)

print(len(word_count)) # 2405

print(word_count[0]) # ('3750', 3702)

print(word_count[-1]) # ('5034', 1)

这里还可以根据字在每个句子的出现情况,反推出标点符号。下面代码统计了不同字符在句子中出现的次数,其中字符3750,字符900和字符648在20w新闻的覆盖率接近99%,很有可能是标点符号。

1
2
3
4
5
6
7
from collections import Counter
train_df['text_unique'] = train_df['text'].apply(lambda x: ' '.join(list(set(x.split(' ')))))
all_lines = ' '.join(list(train_df['text_unique']))
word_count = Counter(all_lines.split(" "))
word_count = sorted(word_count.items(), key=lambda d:int(d[1]), reverse = True)

print(word_count[:5]) # [('900', 99), ('3750', 99), ('648', 96), ('7399', 87), ('2109', 86)]

数据分析的结论

通过上述分析我们可以得出以下结论:

  1. 赛题中每个新闻包含的字符个数平均为1000个,还有一些新闻字符较长;
  2. 赛题中新闻类别分布不均匀,科技类新闻样本量接近4w,星座类新闻样本量不到1k;
  3. 赛题总共包括7000-8000个字符;

通过数据分析,我们还可以得出以下结论:

  1. 每个新闻平均字符个数较多,可能需要截断;
  2. 由于类别不均衡,会严重影响模型的精度;

本章作业

  1. 假设字符3750,字符900和字符648是句子的标点符号,请分析赛题每篇新闻平均由多少个句子构成?
    1
    2
    train_df['length'] = train_df['text'].apply(lambda x: x.count('3750') + x.count('900') + x.count('648'))
    print(sum(train_df['length']) / 100)
  2. 统计每类新闻中出现次数对多的字符
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    lst = [[] for _ in range(14)]
    for i in range(len(train_df)):
    lst[train_df['label'][i]].extend(train_df['text'][i].split())
    lst_freq = list(map(Counter, lst))
    lst_freq = [sorted(lst.items(), key=lambda d:int(d[1]), reverse = True) for lst in lst_freq]
    for i in range(len(lst_freq)):
    print(lst_freq[i][0])

    ('3750', 610)
    ('3750', 531)
    ('3750', 956)
    ('3750', 239)
    ('3750', 78)
    ('3750', 193)
    ('3750', 491)
    ('3750', 214)
    ('3750', 68)
    ('3750', 51)
    ('3750', 152)
    ('3750', 102)
    ('4464', 59)
    ('648', 6)

欢迎关注我的其它发布渠道