天池nlp入门竞赛-新闻文本分类-数据分析

  1. 什么是可哈希(hashable)?

Anything that is not mutable (mutable means, likely to change) can be hashed. Besides the hash function to look for, if a class has it, by eg. dir(tuple) and looking for the hash method, here are some examples

#x = hash(set([1,2])) #set unhashable
x = hash(frozenset([1,2])) #hashable
#x = hash(([1,2], [2,3])) #tuple of mutable objects, unhashable
x = hash((1,2,3)) #tuple of immutable objects, hashable
#x = hash()
#x = hash({1,2}) #list of mutable objects, unhashable
#x = hash([1,2,3]) #list of immutable objects, unhashable

List of immutable types:

int, float, decimal, complex, bool, string, tuple, range, frozenset, bytes

List of mutable types:

list, dict, set, bytearray, user-defined classes

  1. Counter

Dict subclass for counting hashable items. Sometimes called a bag or multiset. Elements are stored as dictionary keys and their counts are stored as dictionary values.

from collections import Counter
all_lines = ' '.join(list(train_df['text']))
word_count = Counter(all_lines.split(' ')) # word_count是一个dict,{key: value}。key是word,value是count
  1. Sorted

sorted(iterable, /, *, key=None, reverse=False) returns 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.

word_count.items() returns a list of a given dictionary’s (key, value) tuple pair.如:dict_items([(‘A’, ‘Geeks’), (‘B’, 4), (‘C’, ‘Geeks’)]) # Return a new list containing all items from the iterable in ascending order.
key=lambda d: d[1] 按value排序,reverse=True 从大到小

word_count = sorted(word_count.items(), key=lambda d: d[1], reverse=True)

word_count.items()是一个list which is iterable,因此可以一次迭代处理一个(key, value) tuple pair,d代指当前处理的一个(key, value) tuple pair,d[1]是value,所以意思是按value排序。

  1. 如何统计词数?
from collections import Counter
all_lines = ' '.join(list(train_df['text']))
word_count = Counter(all_lines.split(' ')) # word_count是一个dict,{key: value}。key是word,value是count
word_count = sorted(word_count.items(), key=lambda d: d[1], reverse=True) # word_count.items() returns a list of a given dictionary’s (key, value) tuple pair.如:dict_items([('A', 'Geeks'), ('B', 4), ('C', 'Geeks')]) # Return a new list containing all items from the iterable in ascending order.key=lambda d: d[1] 按value排序,reverse=True 从大到小
print(len(word_count))
  1. 如何统计每个词出现的句子数?
 # set使得每个词如‘3750’在一句话中只出现1次,即统计每句话都由哪些词组成,因为我们不关心一句话中某个词出现几次,
 # 我们关心某个词出现在哪些句子中,统计每个词出现句子的数量。
train_df['text_unique'] = train_df['text'].apply(lambda x: ' '.join(list(set(x.split(' ')))))
print(train_df['text_unique'])
all_lines = ' '.join(list(train_df['text_unique'])) # make the values of a Series (Strings) a list, and join the list to a long string
word_count = Counter(all_lines.split(' ')) #统计每个词的出现次数,=出现句子数
print(type(word_count)) # type:<class 'collections.Counter'>
word_count = sorted(word_count.items(), key=lambda d:d[1], reverse=True) #
print(type(word_count)) # type:<class 'list'>
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
天池大赛是国内知名的数据科学竞赛平台,零基础入门NLP - 新闻文本分类是其中的一项比赛任务。这个任务的目标是利用机器学习和自然语言处理的方法,对给定的新闻文本进行分类,即根据新闻内容判断其所属的类别。这个任务对于初学者来说是一个很好的入门项目。 在解决这个问题的过程中,我们需要首先对提供的训练数据进行探索性数据分析,了解数据的分布,词频以及类别的平衡情况。然后,我们可以进行文本预处理,包括分词、去除停用词、词干化等。接下来,可以构建特征表示,可以使用TF-IDF、Word2Vec或者其他词嵌入模型来提取文本的向量表示。在构建特征表示后,可以选择合适的机器学习算法,如朴素贝叶斯、支持向量机、深度学习等,来训练分类模型。 在进行模型训练之前,可以将数据集分为训练集和验证集,用于模型的评估和调优。我们可以使用交叉验证,调整模型的超参数,选择表现最好的模型。在模型训练完成后,可以使用测试集对模型进行评估,计算准确率、召回率和F1值等指标。最后,我们可以利用模型对给定的未知新闻文本进行分类预测。 在解决这个问题的过程中,还可以进行一些方法的优化和改进。比如,可以使用集成学习的方法,如随机森林、XGBoost或者LightGBM等,结合多个分类器的结果来提高整体的分类准确率。此外,可以尝试使用预训练的模型,如BERT等,来获得更好的特征表示。此外,还可以尝试使用深度学习网络,如卷积神经网络或者循环神经网络,来提取文本的高级语义特征。 总之,零基础入门NLP - 新闻文本分类是一个很好的机会,可以学习和应用自然语言处理的知识和技术。通过解决这个问题,我们可以深入了解文本分类的基本概念和方法,提升自己在数据科学领域的能力和竞争力。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值