Task2 数据读取与数据分析
使用Pandas库完成数据读取操作,并对赛题数据进行分析构成。
数据读取
import pandas as pd
train_df = pd.read_csv('./train_set.csv', sep='\t', nrows=100)
#读取行数nrows,为此次读取文件的函数,由于数据集比较大,先设置为100
print(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...
第一列为新闻的类别,第二列为新闻的字符。
数据分析
在读取完成数据集后,我们对数据集进行数据分析的操作,通过数据分析还来找出一些规律。
句子长度分析
直接统计单词的个数来得到每个句子的长度
train_df['text_len'] = train_df['text'].apply(lambda x: len(x.split(' ')))
print(train_df['text_len'].describe())
count 200000.000000
mean 907.207110
std 996.029036
min 2.000000
25% 374.000000
50% 676.000000
75% 1131.000000
max 57921.000000
Name: text_len, dtype: float64
对新闻句子的统计可以得出,本次赛题给定的文本比较长,每个句子平均由907个字符构成,最短的句子长度为2,最长的句子长度为57921。
下图将句子长度绘制了直方图,可见大部分句子的长度都几种在2000以内。
import pandas as pd
import matplotlib.pyplot as plt
train_df = pd.read_csv('./train_set.csv', sep='\t')
# print(train_df.head())
train_df['text_len'] = train_df['text'].apply(lambda x: len(x.split(' ')))
# print(train_df['text_len'].describe())
_ = plt.hist(train_df['text_len'], bins=200)
plt.xlabel('Text char count')
plt.title("Histogram of char count")
plt.show()
新闻类别分布
对数据集的类别进行分布统计,具体统计每类新闻的样本个数
train_df['label'].value_counts().plot(kind='bar')
plt.title('News class count')
plt.xlabel("category")
plt.show()
在数据集中标签的对应的关系如下:{‘科技’: 0, ‘股票’: 1, ‘体育’: 2, ‘娱乐’: 3, ‘时政’: 4, ‘社会’: 5, ‘教育’: 6, ‘财经’: 7, ‘家居’: 8, ‘游戏’: 9, ‘房产’: 10, ‘时尚’: 11, ‘彩票’: 12, ‘星座’: 13}
从统计结果可以看出,赛题的数据集类别分布存在较为不均匀的情况。在训练集中科技类新闻最多,其次是股票类新闻,最少的新闻是星座新闻。
字符分布统计
统计每个字符出现的次数,首先可以将训练集中所有的句子进行拼接进而划分为字符,并统计每个字符的个数。
import pandas as pd
from collections import Counter
train_df = pd.read_csv('./train_set.csv', sep='\t')
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))
print(word_count[0])
print(word_count[-1])
6869
('3750', 7482224)
('3133', 1)
从统计结果中可以看出,在训练集中总共包括6869个字,其中编号3750的字出现的次数最多,编号3133的字出现的次数最少。
根据字在每个句子的出现情况,反推出标点符号
import pandas as pd
from collections import Counter
train_df = pd.read_csv('./train_set.csv', sep='\t')
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[0])
print(word_count[1])
print(word_count[2])
('3750', 197997)
('900', 197653)
('648', 191975)
可以看到不同字符在句子中出现的次数,其中字符3750,字符900和字符648在20w新闻的覆盖率接近99%,很有可能是标点符号。
数据分析的结论
通过上述分析我们可以得出以下结论:
- 赛题中每个新闻包含的字符个数平均为1000个,还有一些新闻字符较长;
- 赛题中新闻类别分布不均匀,科技类新闻样本量接近4w,星座类新闻样本量不到1k;
- 赛题总共包括7000-8000个字符;
通过数据分析,我们还可以得出以下结论:
- 每个新闻平均字符个数较多,可能需要截断;
- 由于类别不均衡,会严重影响模型的精度;
本章作业
-
作业1:假设字符3750,字符900和字符648是句子的标点符号,分析赛题每篇新闻平均由多少个句子构成
-
作业2:统计每类新闻中出现次数对多的字符
作业1
将每篇新闻用字符3750,字符900和字符648划分为句子,统计每篇新闻的句子个数
import pandas as pd
import re
train_df = pd.read_csv('./train_set.csv', sep='\t')
train_df['sentence_num'] = train_df['text'].apply(lambda x: len(re.split('3750|900|648', x)))
print(train_df['sentence_num'].describe())
count 200000.000000
mean 80.802370
std 86.955448
min 1.000000
25% 29.000000
50% 57.000000
75% 103.000000
max 3460.000000
Name: sentence_num, dtype: float64
我们可以看到平均每篇新闻由80.8个句子构成
作业2
将训练集中所有句子去掉标点符号后,按label进行拼接,统计每个字符的个数。
from collections import Counter
import pandas as pd
train_df = pd.read_csv('./train_set.csv', sep='\t')
class_count = []
for i in range(14):
line = ' '.join(train_df[train_df['label'] == i]['text'])
class_count.append(re.sub('3750|900|648', '', line))
for i, line in enumerate(class_count):
line = filter(lambda x: x, line.split(' '))
word_count = Counter(line)
word_count = sorted(word_count.items(), key=lambda x: x[1], reverse=True)
print(i, ":", word_count[0][0])
0 : 3370
1 : 3370
2 : 7399
3 : 6122
4 : 4411
5 : 6122
6 : 6248
7 : 3370
8 : 6122
9 : 7328
10 : 3370
11 : 4939
12 : 4464
13 : 4939