一、数据集
1.1 数据概况
数据集 | 2001-2023 年 A 股上市公司年报&管理层讨论与分析
数据名称: 2001-2023年A股上市公司年报&管理层讨论与分析
数据来源: 上海证券交易所、深圳证券交易所
数据格式: csv、txt
公司数量: 5606
MD&A数量: 60079
会计年度: 2001-2023
1.2 读取 md&a 数据
import pandas as pd
# 读取前5行数据
df = pd.read_csv('mda01-23.csv.gz', compression='gzip', nrows=5)
# gz解压后读取csv
# df = pd.read_csv('mda01-23.csv', nrows=5)
print(len(df))
df.head()
Run
60079
二、训练 Word2Vec & GloVe 模型
2.1 准备语料
从 mda01-23.csv.gz 数据中抽取出所有文本,写入到 mda01-23.txt
%%time
with open('mda01-23.txt', 'w', encoding='utf-8') as f:
text = '\n'.join(df['text'].fillna(''))
f.write(text)
最终得到 2.88G 的语料文件。
2.2 配置 cntext 环境
使用 2.1.6 版本 cntext 库(该版本暂不开源,需付费购买)。 将得到的 cntext-2.1.6-py3-none-any.whl 文件放置于电脑桌面, win 系统打开cmd(Mac 打开 terminal), 输入如下命令(将工作环境切换至桌面)
cd desktop
个别 Win 用户如无效,试试cd Desktop
。
继续在 cmd (terminal) 中执行如下命令安装 cntext2.1.6
pip3 install cntext-2.1.6-py3-none-any.whl
2.3 开始训练
%%time
%%time
import cntext as ct
w2v_model = ct.Word2Vec(corpus_file='mda01-23.txt', # 语料文件
lang='chinese', # 中文语料
vector_size=200, # 嵌入的维度数
window_size=15) # 词语上下文的窗口大小
glove_model = ct.GloVe(corpus_file='mda01-23.txt',
lang='chinese',
vector_size=200,
window_size=15)
Run
Mac(Linux) System, Enable Parallel Processing
Cache output/mda01-23_cache.txt Not Found or Empty, Preprocessing Corpus
Processing Corpus: 100%|█████████| 12437725/12437725 [03:54<00:00, 52930.57it/s]
Reading Preprocessed Corpus from output/mda01-23_cache.txt
Start Training Word2Vec
Word2Vec Training Cost 1370 s.
Output Saved To: output/mda01-23-Word2Vec.200.15.bin
Mac(Linux) System, Enable Parallel Processing
Cache output/mda01-23_cache.txt Found, Skip Preprocessing Corpus
Start Training GloVe
BUILDING VOCABULARY
Using vocabulary of size 452721.
......
04/03/25 - 04:07.06PM, iter: 001, cost: 0.112966
04/03/25 - 04:07.56PM, iter: 002, cost: 0.079845
......
04/03/25 - 04:18.06PM, iter: 014, cost: 0.048427
04/03/25 - 04:18.56PM, iter: 015, cost: 0.047962
GloVe Training Cost 1229 s.
Output Saved To: output/mda01-23-GloVe.200.15.bin
CPU times: user 1h 19min 15s, sys: 58.5 s, total: 1h 20min 14s
Wall time: 43min 20s
经过 80 分钟, 训练出的中国 A 股管理层讨论与分析的 GloVe 和 Word2Vec 词向量模型(如下截图),词汇量 914058, 模型文件 1.49G。模型可广泛用于经济管理等领域概念(情感)词典的构建或扩展。
- mda01-23_cache.txt 缓存文件
- mda01-23-Word2Vec.200.15.bin Word2Vec 模型文件
- mda01-23-GloVe.200.15.bin GloVe 模型文件
三、使用模型
3.1 导入模型
使用 ct.load_w2v(w2v_path) 来导入刚刚训练好的模型 mda01-23-GloVe.200.15.bin
import cntext as ct
print(ct.__version__)
w2v_model = ct.load_w2v('output/mda01-23-Word2Vec.200.15.bin')
glove_model = ct.load_w2v('output/mda01-23-GloVe.200.15.bin')
w2v_model
Run
2.1.6
Loading output/mda01-23-Word2Vec.200.15.bin...
Loading output/mda01-23-GloVe.200.15.bin...
<gensim.models.keyedvectors.KeyedVectors at 0x633060fe0>
3.2 评估模型
使用近义法和类比法, 判断模型的表现。详情可查看文档
以 word2vec 为例,评估模型表现
ct.evaluate_similarity(w2v_model)
ct.evaluate_analogy(w2v_model)
Run
近义测试: similarity.txt
/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/site-packages/cntext/model/evaluate_data/similarity.txt
评估结果:
+----------+------------+----------------------------+
| 发现词语 | 未发现词语 | Spearman's Rank Coeficient |
+----------+------------+----------------------------+
| 421 | 116 | 0.41 |
+----------+------------+----------------------------+
类比测试: analogy.txt
/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/site-packages/cntext/model/evaluate_data/analogy.txt
Processing Analogy Test: 100%|██████████████| 1198/1198 [00:11<00:00, 99.91it/s]
评估结果:
+--------------------+----------+------------+------------+----------+
| Category | 发现词语 | 未发现词语 | 准确率 (%) | 平均排名 |
+--------------------+----------+------------+------------+----------+
| CapitalOfCountries | 407 | 270 | 27.27 | 3.96 |
| CityInProvince | 175 | 0 | 97.14 | 1.29 |
| FamilyRelationship | 90 | 182 | 10.00 | 3.89 |
| SocialScience | 9 | 61 | 44.44 | 3.00 |
+--------------------+----------+------------+------------+----------+
近义测试: Spearman’s Rank Coeficient 系数取值[-1, 1], 取值越大, 说明模型表现越好。
类比测试:
- CapitalOfCountries 中文 md&a 语料在此项表现较差, 应该是语料中常见国家首度的提及较少。也体现了大多数企业没有国际化。盲猜美股的 CapitalOfCountries 表现应该好于 A 股。
- CityInProvince 中文 md&a 语料在此项表现如此优异,说明 A 股多数企业扎根于中国大地, 年报 md&a 中提及次数很多。
- FamilyRelationship 中文 md&a 语料中主要体现的是公司组织层面,较少提及家庭关系词语,所以类别表现一般是很容易理解的。
- SocialScience 中文 md&a 语料在此项表现一般, 应该是语料中常见的社会科学词语提及较少。
整体而言,语料训练的效果很不错,抓住了数据场景的独特性语义。
3.3 KeyedVectors 的操作方法(或属性)
方法 | 描述 |
---|---|
KeyedVectors.index_to_key | 获取词汇表中的所有单词。 |
KeyedVectors.key_to_index | 获取单词到索引的映射。 |
KeyedVectors.vector_size | 获取 GloVe 模型中任意词向量的维度。 |
KeyedVectors.get_vector(word) | 获取给定单词的词向量。 |
KeyedVectors.similar_by_word(word, topn=10) | 获取某词语最相似的 10 个近义词。 |
KeyedVectors.similar_by_vector(vector, topn=10) | 获取词向量最相似的 10 个近义词。 |
3.4 查看词汇量&维度数
# 词汇量
print('Word2Vec词汇量: ', len(w2v_model))
print('GloVe词汇量: ', len(glove_model))
print('Word2Vec维度数: ', w2v_model.vector_size)
print('GloVe维度数: ', glove_model.vector_size)
Run
Word2Vec词汇量: 779451
GloVe词汇量: 452722
Word2Vec维度数: 200
GloVe维度数: 200
3.5 词表
查看词表
w2v_model.index_to_key
Run
['公司',
'适用',
'情况',
'项目',
...
'电源',
'模块',
'治疗',
'实行',
...]
查看词汇映射表
w2v_model.key_to_index
Run
{'公司': 0,
'适用': 1,
'情况': 2,
'项目': 3,
'产品': 4,
......
'电源': 996,
'模块': 997,
'治疗': 998,
'实行': 999,
...}
3.6 查看词向量
# 查询某词的词向量
w2v_model.get_vector('创新')
Run
array([ 2.1754024 , -1.1083757 , 0.30669013, -2.3222647 , 2.037556 ,
-0.4029445 , 3.6833916 , -1.520377 , 2.046346 , -1.2697963 ,
-4.6910505 , 0.77117187, 1.1461644 , 0.44298795, 0.6784688 ,
3.3559523 , 0.24663335, -1.2482047 , -0.9346108 , -3.0777013 ,
......
-3.6354382 , 0.05906389, 0.34168765, -0.7054434 , 1.1509504 ,
1.8190739 , -1.1612972 , -1.4397353 , 1.2453864 , 2.280641 ,
0.16765192, 0.07346256, 3.5366366 , -3.6461854 , -0.9496986 ,
2.38728 , 0.20706034, 1.9512706 , 0.138616 , -1.5360951 ],
dtype=float32)
# 查询多个词的词向量
w2v_model.get_mean_vector(['创新', '研发'])
Ruj
array([ 0.09148444, -0.06439913, -0.01015558, -0.05306313, 0.06175516,
-0.06248198, 0.0741367 , -0.1192503 , 0.01363031, -0.04997339,
-0.14710814, 0.02335552, 0.02538575, 0.04013668, 0.01318196,
0.02532444, 0.04894971, -0.02153242, -0.08227678, -0.07488775,
......
-0.12517202, -0.01881655, 0.00918441, -0.0136063 , -0.00371204,
0.06221166, -0.03297246, -0.03030303, 0.0700142 , 0.0314462 ,
-0.00345534, 0.01589244, 0.08589543, -0.04257936, 0.00832741,
0.04352532, 0.0469989 , 0.02008099, 0.04311348, 0.00275607],
dtype=float32)
3.7 近义词
根据词语查找最相似的 10 个词
w2v_model.similar_by_word('创新', topn=10)
Run
[('技术创新', 0.700412929058075),
('不断创新', 0.6930481791496277),
('创新型', 0.6269345283508301),
('创新能力', 0.5974201560020447),
('引领', 0.5780265927314758),
('革新', 0.5736942291259766),
('科技进步', 0.5656147599220276),
('硬核', 0.558936357498169),
('创新性', 0.5329084992408752),
('前沿', 0.5278463959693909)]
根据某词的词向量查询最相似的 10 个词
creativeness_vector = w2v_model.get_vector('创新')
w2v_model.similar_by_vector(creativeness_vector, topn=10)
Run
[('创新', 1.0),
('技术创新', 0.700412929058075),
('不断创新', 0.6930481195449829),
('创新型', 0.6269344687461853),
('创新能力', 0.5974200963973999),
('引领', 0.5780266523361206),
('革新', 0.5736941695213318),
('科技进步', 0.5656147599220276),
('硬核', 0.558936357498169),
('创新性', 0.5329084992408752)]
多个词求得均值向量
AI_vector = w2v_model.get_mean_vector(['ai', '机器学习', '人工智能', '自然语言处理'])
w2v_model.similar_by_vector(AI_vector, topn=20)
Run
[('ai', 0.9102671146392822),
('机器学习', 0.8870545625686646),
('自然语言处理', 0.8581846356391907),
('人工智能', 0.850341260433197),
('ai模型', 0.8282461762428284),
('语言模型', 0.8115222454071045),
('深度学习', 0.8071558475494385),
('nlp', 0.798158586025238),
('自然语言理解', 0.7791630625724792),
('gpt', 0.7678513526916504),
('生成式', 0.7635747194290161),
('知识图谱', 0.7630875706672668),
('语义', 0.7626250982284546),
('模态模型', 0.7623038291931152),
('自然语言', 0.7621716856956482),
('神经网络', 0.7459751963615417),
('训练模型', 0.7420169711112976),
('ai算法', 0.7381570339202881),
('语音识别', 0.7319735884666443),
('推理', 0.7291040420532227)]
短视主义词
short_term_vector = w2v_model.get_mean_vector(['尽快', '年内', '马上'])
w2v_model.similar_by_vector(short_term_vector, topn=20)
Run
[('年内', 0.7409026026725769),
('尽快', 0.7231807112693787),
('尽早', 0.6633163094520569),
('马上', 0.654857337474823),
('早日', 0.6193257570266724),
('即将', 0.5834046602249146),
('争取早日', 0.5398548245429993),
('按期', 0.5317867994308472),
('抓紧', 0.5302024483680725),
('力争尽早', 0.5286926627159119),
('争取', 0.5223618745803833),
('今年年底', 0.5185986161231995),
('最后', 0.5065357685089111),
('后续', 0.5003851056098938),
('力争早日', 0.49779534339904785),
('争取尽早', 0.49219441413879395),
('争取尽快', 0.48603734374046326),
('力争', 0.4822418689727783),
('如期', 0.48014208674430847),
('冲刺', 0.46000415086746216)]
四、扩展词典
做词典法的文本分析,最重要的是有自己的领域词典。之前受限于技术难度,文科生的我也一直在用形容词的通用情感词典。现在依托 word2vec 技术, 可以加速人工构建的准确率和效率。
下面是在 mda01-23-Word2Vec.200.15.bin 上做的词典扩展测试,函数 ct.expand_dictionary(wv, seeddict, topn=100) 会根据种子词选取最准确的 topn 个词。
- wv 预训练模型,数据类型为 gensim.models.keyedvectors.KeyedVectors。
- seeddict 参数类似于种子词;格式为 PYTHON 字典;
- topn 返回 topn 个语义最接近 seeddict 的词,默认 100.
假设现在有种子词 seeddicts, 内含我构建的 短视词、 创新词、 竞争词, 我希望生成最终各含 30 个词的候选词表 txt 文件。
可以使用 ct.expand_dictionary 进行如下操作
seeddicts = {
'短视词': ['抓紧', '立刻', '月底', '年底', '年终', '争取', '力争'],
'创新词': ['创新', '科技', '研发', '技术', '标准'],
'竞争词': ['竞争', '竞争力'],
}
ct.expand_dictionary(wv = w2v_model,
seeddict = seeddicts,
topn=30)
Run
Finish! 短视词 candidates saved to output/短视词.txt
Finish! 创新词 candidates saved to output/创新词.txt
Finish! 竞争词 candidates saved to output/竞争词.txt
六、获取模型
内容创作不易, 本文为付费内容,
- 免费 mda01-23-Word2Vec.200.15.bin 链接: https://pan.baidu.com/s/13r8ZiwmzaiIx691vzqNKDA?pwd=vxuy 提取码: vxuy
- 免费 mda01-23-GloVe.200.15.bin 链接: https://pan.baidu.com/s/1Qi3oyE5S9OOon2GxpqP1Ew?pwd=dt3s 提取码: dt3s
- 更多免费词向量 https://cntext.readthedocs.io/zh-cn/latest/embeddings.html
- 100元 cntext-6-py3-none-any.whl 加微信 372335839, 备注「姓名-学校-专业」
相关内容
相关文献
[0]刘景江,郑畅然,洪永淼.机器学习如何赋能管理学研究?——国内外前沿综述和未来展望[J].管理世界,2023,39(09):191-216.
[1]冉雅璇,李志强,刘佳妮,张逸石.大数据时代下社会科学研究方法的拓展——基于词嵌入技术的文本分析的应用[J].南开管理评论:1-27.
[3]胡楠,薛付婧,王昊楠.管理者短视主义影响企业长期投资吗?——基于文本分析和机器学习[J].管理世界,2021,37(05):139-156+11+19-21.
[4]Kai Li, Feng Mai, Rui Shen, Xinyan Yan, Measuring Corporate Culture Using Machine Learning, *The Review of Financial Studies*,2020
- LIST | 社科(经管)文本挖掘文献汇总
- LIST | 文本分析代码汇总
- 词嵌入技术在社会科学领域进行数据挖掘常见 39 个 FAQ 汇总
- LIST | 可供社科(经管)领域使用的数据集
- Python 实证指标构建与文本分析
- 推荐 | 文本分析库 cntext2.x 使用手册
- 使用 3751w 专利申请数据集按年份(按省份)训练词向量
- 预训练模型 | 使用 1000w 专利摘要训练 word2vec 模型,可用于开发词典
- 词向量 | 使用人民网领导留言板语料训练 Word2Vec 模型
- 实验 | 使用 Stanford Glove 代码训练中文语料的 Glove 模型
- 可视化 | 人民日报语料反映七十年文化演变