数据读取
import pandas as pd
#train_df=pd.read_csv('./train_set.csv',sep='\t',nrows=100)
train_df=pd.read_csv('./train_set.csv',sep='\t')
- 分隔符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... |
句子长度分析
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
绘制句子长度统计直方图
import matplotlib.pyplot as plt
plt.hist(train_df['text_len'],bins=200)
plt.xlabel('Text char count')
plt.title('Histogram of char count')
绘制新闻类别直方图
train_df['label'].value_counts().plot(kind='bar')
plt.title('New class count')
plt.xlabel('category')
字符分布统计
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))
print(word_count[0])
print(word_count[-1])
6869
('3750', 7482224)
('3133', 1)
训练集一共包括6869个字,其中编号为3750的字出现的次数最多,编号为3133的字出现的次数最少。
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在文章的覆盖率接近99%,很有可能是标点符号。
数据分析结论
- 每个字符个数平均为1000个;
- 科技新闻样本最多接近4w,星座类的不到1k;
- 文章一共可能包括6000-7000个字符。