新闻文本分类 Task2 数据读取与数据分析

1.处理后的赛题训练数据如下:

import pandas as pd
import numpy as np
import os
import tensorflow as tf
import math
import tqdm
import json
import zipfile
import matplotlib.pyplot as plt
%matplotlib inline
def zip2file(zip_file_name: str, extract_path: str, members=None, pwd=None):
    """ 压缩文件内容提取值指定的文件夹

    :param zip_file_name: 待解压的文件  .zip          r'D:\Desktop\tst.zip'
    :param extract_path:  提取文件保存的目录           r'D:\Desktop\tst\test\test'
    :param members:       指定提取的文件,默认全部
    :param pwd:           解压文件的密码
    :return:
    """
    with zipfile.ZipFile(zip_file_name) as zf:
        zf.extractall(extract_path, members=members, pwd=pwd)

zip2file('./train_set.csv.zip',None)
zip2file('./test_a.csv.zip',None)
train_df = pd.read_csv('./train_set.csv',sep='\t')
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…

2.数据分析

1.赛题数据中,新闻文本长度是多少

2.赛题数据的类别分布式什么样的,哪些类别比较多

3.赛题数据中,字符分布式什么样的

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

查看数据分布情况

train_df['text_len'].value_counts()
321      220
500      214
452      213
316      211
252      208
333      206
461      205
478      205
254      204
375      203
415      201
495      200
389      200
298      200
308      200
405      199
334      199
347      199
339      198
417      197
545      197
289      196
411      196
385      196
327      196
451      196
395      196
292      196
494      195
348      195
        ... 
9701       1
4900       1
4645       1
12389      1
6112       1
5156       1
5856       1
5540       1
5668       1
10018      1
6052       1
4261       1
4832       1
4704       1
6692       1
6625       1
5731       1
4773       1
10599      1
6497       1
5029       1
4448       1
5157       1
33847      1
3622       1
5797       1
5859       1
5925       1
6053       1
6247       1
Name: text_len, Length: 5633, dtype: int64
_ = plt.hist(train_df['text_len'],bins = 2000,range = (0,10000))
plt.xlabel('text char count')
plt.title('histogram of char count')

新闻文本分类 Task2 数据读取与数据分析_第1张图片

由上可知大部分句子长度都在2000以内
查看一下label的分布

train_df['label'].value_counts().plot(kind='bar')
plt.title('news class count')
plt.xlabel('category')

新闻文本分类 Task2 数据读取与数据分析_第2张图片

你可能感兴趣的:(NLP)