【NLP入门】赛题1-新闻文本分类-Task02-数据读取及初步分析

赛题1-新闻文本分类-Task02-数据读取及初步分析

今天主要是代码向


# 引入 Pandas
import pandas as pd
# 读取数据
train_set = pd.read_csv('./data/1/train_set.csv', sep='\t')
# 看一下数据的前几行
train_set.head()

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

# 句子长度分析
train_set['text_len'] = train_set['text'].map(lambda x: len(x.split(' ')))
train_set['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

train_set.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

# 引入绘图包
import matplotlib.pyplot as plt

_ = plt.hist(train_set['text_len'])
plt.xlabel('text_len')
plt.title("count")

_ = plt.hist(train_set['text_len'], range=(0, 5921), bins=200)
plt.xlabel('Text char count')
plt.title("Histogram of char count")

在这里插入图片描述

# 新闻类别分布
train_set['label'].value_counts().plot(kind='bar')
plt.title('News class count')
plt.xlabel("category")

在这里插入图片描述

# 字符分布统计


from collections import Counter
all_lines = ' '.join(list(train_set['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)


# 这里还可以根据字在每个句子的出现情况,反推出标点符号。
# 下面代码统计了不同字符在句子中出现的次数

from collections import Counter
train_set['text_unique'] = train_set['text'].apply(lambda x: ' '.join(list(set(x.split(' ')))))
all_lines = ' '.join(list(train_set['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)

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

# 导入支持多分隔符包
import re
train_set['sentence'] = train_set['text'].map(lambda x: len(re.split('3750|900|648', x)))
train_set['sentence'].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
# 每篇新闻平均由81个句子构成


train_set.groupby(['label'])

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值