【NLP】Task2 数据读取与数据分析

Task2 数据读取与数据分析

数据读取

import pandas as pd
import matplotlib.pyplot as plt
ts_path = '/Users/gaomingjun/PycharmProjects/Datawhale/nlp/train_set.csv'
train_df = pd.read_csv(ts_path, encoding= 'unicode_escape',sep='\t',nrows= 100)
#这里加上parameter 'encoding'是因为之前UTF-8对train_set.csv解码错误了

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

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(' ')))
#新增了一行'text_len'



print(train_df.head()
   label                                               text  text_len
0      2  2967 6758 339 2021 1854 3731 4109 3792 4149 15...      1057
1     11  4464 486 6352 5619 2465 4802 1452 3137 5778 54...       486
2      3  7346 4068 5074 3747 5681 6093 1777 2226 7354 6...       764
3      2  7159 948 4866 2109 5520 2490 211 3956 5520 549...      1570
4      3  3646 3055 3055 2490 4659 6065 3370 5814 2465 5...       307
#使用describe()统计后可得出:
print(train_df['text_len'].describe())
count     100.000000
mean      872.320000
std       923.138191
min        64.000000
25%       359.500000
50%       598.000000
75%      1058.000000
max      7125.000000
#下图将句子长度绘制了直方图,可见大部分句子的长度都几种在2000以内。
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()


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

在这里插入图片描述

字符分布统计

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

'''
join() Concatenate any number of strings.
The string whose method is called is inserted in between each given string. 
The result is returned as a new string.
Example: '.'.join(['ab', 'pq', 'rs']) -> 'ab.pq.rs'
'''
all_lines = ' '.join(list(train_df['text']))

print(all_lines) 
2967 6758 339 2021 1854 3731 4109 3792 4149 1519 2058 3912 2465 2410 1219 6654... 
'''
Create a new, empty Counter object. And if given, count elements from an 
input iterable. Or, initialize the count from another mapping of elements to
their counts.
E.g
Counter({'a': 4, 'b': 2})
这里表示有4个'a',2个'b'
'''
word_count = Counter(all_lines.split(" "))


print(len(word_count)) 
=> 2405
这里得出word_count的length为2405,表示一共有2405种word
'''
sorted() Return a new list containing all items from the iterable in ascending order.
A custom key function can be supplied to customize the sort order, and the reverse 
flag can be set to request the result in descending order.
'''
sorted_wc = sorted(word_count.items(), key=lambda d:d[1], reverse = True)
'''
print(sorted_wc[0]) => ('3750', 3702)
print(sorted_wc[-1]) => ('5034', 1)
这里print出整理好的word_count(sorted_wc)的[0](第一个元素)和[-1](最后一个元素)
descending的排序,所以第一个是word(3750)出现的最多(3702次),最后一个是word(5034)出现的
最少(1次)
'''
#这里还可以根据字在每个句子的出现情况,反推出标点符号。下面代码统计了不同字符在句子中出现的次数,
#其中字符3750,字符900和字符648在20w新闻的覆盖率接近99%,很有可能是标点符号。

#这里用set就是为了去掉重复的字符
train_df['text_unique'] = train_df['text'].apply(lambda x: ' '.join(list(set(x.split(' ')))))
all_lines = ' '.join(list(train_df['text_unique']))
word_count1 = Counter(all_lines.split(" "))
sorted_wc = sorted(word_count1.items(), key=lambda d:int(d[1]), reverse = True)

'''
print(sorted_wc[0]) => ('3750', 99)
print(sorted_wc[1]) => ('900', 99)
print(sorted_wc[2]) => ('648', 96)
'''

数据分析的结论

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

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

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

  • 每个新闻平均字符个数较多,可能需要截断;

  • 由于类别不均衡,会严重影响模型的精度;

本章小结

本章对赛题数据进行读取,并新闻句子长度、类别和字符进行了可视化分析。

本章作业

  1. 假设字符3750,字符900和字符648是句子的标点符号,请分析赛题每篇新闻平均由多少个句子构成?
  2. 统计每类新闻中出现次数对多的字符

参考文献

Task2 数据读取与数据分析.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值