GitHub:如何構建一個股票市場知識圖譜?(附代碼&鏈接)


<div class="rich_media_content" id="js_content">
                

                

                
                
                <p style="min-height:1em;font-family:'-apple-system-font', BlinkMacSystemFont, 'Helvetica Neue', 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei UI', 'Microsoft YaHei', Arial, sans-serif;letter-spacing:.544px;line-height:27.2px;"><img class="rich_pages" style="font-size:16px;letter-spacing:.544px;text-align:center;" src="https://img2018.cnblogs.com/blog/1112483/201908/1112483-20190813161236583-1770923137.png" alt="640?wx_fmt=png"></p><p style="min-height:1em;line-height:1.5em;">來源:專知</p><p style="min-height:1em;line-height:1.5em;"><span style="letter-spacing:1px;">本文約<strong><span style="color:rgb(166,91,203);">2600</span></strong><span style="color:rgb(166,91,203);"><strong>字,</strong></span>建議閱讀<span style="color:rgb(166,91,203);"><strong>5</strong></span><strong><span style="color:rgb(166,91,203);">分鍾。</span></strong></span></p><p>如何利用網絡上的公開數據構建一個自己的股票證券知識圖譜呢?</p><p style="line-height:1.5em;text-align:left;"><br></p><p style="line-height:1.5em;text-align:left;"><strong><span style="font-size:15px;">[ 導讀 ]</span></strong><span style="font-size:15px;">你不理財財不理你,理財是大家早就達成共識的事情。作為新時代的五好青年,又身具機器學習、深度學習、知識圖譜的能力,以自己的專業知識創造財富,應該更有意思。但是,股票證券市場信息繁雜,如何利用網絡上的公開數據構建一個自己的股票證券知識圖譜呢?GitHub的lemonhu同學,開源了一套解決方案。</span></p><p style="line-height:1.5em;text-align:left;"><br></p><blockquote><p style="line-height:1.5em;text-align:left;">github地址:</p><p style="line-height:1.5em;text-align:left;">https://github.com/lemonhu/stock-knowledge-graph</p><p style="line-height:1.5em;text-align:left;">作者:lemonhu</p></blockquote><p><br></p><h2 style="line-height:1.5em;text-align:center;"><a name="t0"></a><strong><span style="font-size:15px;color:rgb(166,91,203);">結果預覽</span></strong></h2><p><br></p><p style="line-height:1.5em;text-align:left;"><img class="rich_pages" src="https://img2018.cnblogs.com/blog/1112483/201908/1112483-20190813161257981-144727933.png" alt="640?wx_fmt=png"></p><p><br></p><h2 style="line-height:1.5em;text-align:center;"><a name="t1"></a><strong><span style="font-size:15px;color:rgb(166,91,203);">數據源</span></strong></h2><p><br></p><p style="line-height:1.5em;text-align:left;"><span style="font-size:15px;">本項目需要用到兩種數據源:一種是公司董事信息,另一種是股票的行業以及概念信息。</span></p><p style="line-height:1.5em;text-align:left;"><br></p><ul class="list-paddingleft-2"><li><p style="line-height:1.5em;text-align:left;"><span style="font-size:15px;">公司董事信息</span></p><p style="line-height:1.5em;text-align:left;"><span style="font-size:15px;">這部分數據包含在data目錄下的stockpage壓縮文件中,⾥面的每一個文件是以XXXXXX.html命名,其中XXXXXX是股票代碼。這部分數據是由同花順個股的⽹頁爬取而來的,執行解壓縮命令unzip stockpage.zip即可獲取。比如對於600007.html,這部分內容來自於:</span><span style="font-size:14px;color:rgb(136,136,136);">http://stockpage.10jqka.com.cn/600007/company/#manager</span><span style="font-size:15px;"></span></p></li><li><p style="line-height:1.5em;text-align:left;"><span style="font-size:15px;">股票行業以及概念信息</span></p></li><li><p style="line-height:1.5em;text-align:left;"><span style="font-size:15px;">這部分信息也可以通過⽹上公開的信息得到。在這里,我們使用Tushare工具來獲得,詳細細節見之后具體的任務部分。</span></p></li></ul><p><br></p><h2 style="line-height:1.5em;text-align:center;"><a name="t2"></a><strong><span style="font-size:15px;color:rgb(166,91,203);">從⽹頁中抽取董事會的信息</span></strong></h2><p><br></p><p style="line-height:1.5em;text-align:left;"><span style="font-size:15px;">在我們給定的html文件中,需要對每一個股票/公司抽取董事會成員的信息,這部分信息包括董事會成員“姓名”、“職務”、“性別”、“年齡”共四個字段。首先,姓名和職務的字段來自於:</span></p><p style="line-height:1.5em;text-align:left;"><br></p><p style="line-height:1.5em;text-align:left;"><img class="rich_pages" src="https://img2018.cnblogs.com/blog/1112483/201908/1112483-20190813161411830-773823730.png" alt="640?wx_fmt=png"></p><p style="line-height:1.5em;text-align:left;"><br></p><p style="line-height:1.5em;text-align:left;"><span style="font-size:15px;">在這里總共有12位董事成員的信息,都需要抽取出來。另外,性別和年齡字段也可以從下附圖里抽取出來:</span></p><p style="line-height:1.5em;text-align:left;"><br></p><p style="line-height:1.5em;text-align:left;"><img class="rich_pages" src="https://img2018.cnblogs.com/blog/1112483/201908/1112483-20190813161440588-624461431.png" alt="640?wx_fmt=png"></p><p style="line-height:1.5em;text-align:left;"><br></p><p style="line-height:1.5em;text-align:left;"><span style="font-size:15px;">最后,生成一個 executive_prep.csv文件,格式如下:</span></p><p><br></p><p><br></p><div class="table-box"><table width="556"><thead><tr style="border-top:1px solid rgb(198,203,209);"><th align="center" style="border-top-width:1px;border-color:rgb(223,226,229);"><span style="font-size:15px;">高管姓名</span></th><th align="center" style="border-top-width:1px;border-color:rgb(223,226,229);"><span style="font-size:15px;">性別</span></th><th align="center" style="border-top-width:1px;border-color:rgb(223,226,229);"><span style="font-size:15px;">年齡</span></th><th align="center" style="border-top-width:1px;border-color:rgb(223,226,229);"><span style="font-size:15px;">股票代碼</span></th><th align="center" style="border-top-width:1px;border-color:rgb(223,226,229);"><span style="font-size:15px;">職位</span></th></tr></thead><tbody><tr style="border-top:1px solid rgb(198,203,209);"><td align="center" style="border-color:rgb(223,226,229);"><span style="font-size:15px;">朴明志</span></td><td align="center" style="border-color:rgb(223,226,229);"><span style="font-size:15px;">男</span></td><td align="center" style="border-color:rgb(223,226,229);"><span style="font-size:15px;">51</span></td><td align="center" style="border-color:rgb(223,226,229);"><span style="font-size:15px;">600007</span></td><td align="center" style="border-color:rgb(223,226,229);"><span style="font-size:15px;">董事⻓/董事</span></td></tr><tr style="border-top:1px solid rgb(198,203,209);"><td align="center" style="border-color:rgb(223,226,229);"><span style="font-size:15px;">高燕</span></td><td align="center" style="border-color:rgb(223,226,229);"><span style="font-size:15px;">女</span></td><td align="center" style="border-color:rgb(223,226,229);"><span style="font-size:15px;">60</span></td><td align="center" style="border-color:rgb(223,226,229);"><span style="font-size:15px;">600007</span></td><td align="center" style="border-color:rgb(223,226,229);"><span style="font-size:15px;">執⾏董事</span></td></tr><tr style="border-top:1px solid rgb(198,203,209);"><td align="center" style="border-color:rgb(223,226,229);"><span style="font-size:15px;">劉永政</span></td><td align="center" style="border-color:rgb(223,226,229);"><span style="font-size:15px;">男</span></td><td align="center" style="border-color:rgb(223,226,229);"><span style="font-size:15px;">50</span></td><td align="center" style="border-color:rgb(223,226,229);"><span style="font-size:15px;">600008</span></td><td align="center" style="border-color:rgb(223,226,229);"><span style="font-size:15px;">董事⻓/董事</span></td></tr><tr style="border-top:1px solid rgb(198,203,209);"><td align="center" style="border-color:rgb(223,226,229);"><span style="font-size:15px;">···</span></td><td align="center" style="border-color:rgb(223,226,229);"><span style="font-size:15px;">···</span></td><td align="center" style="border-color:rgb(223,226,229);"><span style="font-size:15px;">···</span></td><td align="center" style="border-color:rgb(223,226,229);"><span style="font-size:15px;">···</span></td><td align="center" style="border-color:rgb(223,226,229);"><span style="font-size:15px;">···</span></td></tr></tbody></table></div><p style="line-height:1.5em;"><br></p><p style="line-height:1.5em;"><span style="font-size:15px;">注:建議表頭最好用相應的英文表示。</span></p><p style="line-height:1.5em;text-align:center;"><br></p><p style="line-height:1.5em;text-align:center;"><strong><span style="font-size:15px;color:rgb(166,91,203);">獲取股票行業和概念的信息</span></strong></p><p style="line-height:1.5em;"><br></p><p style="line-height:1.5em;"><span style="font-size:15px;">對於這部分信息,我們可以利⽤工具Tushare來獲取,官網為</span><span style="color:rgb(136,136,136);font-size:12px;">http://tushare.org/</span><span style="font-size:15px;">&nbsp;,使用pip命令進行安裝即可。下載完之后,在python里即可調用股票行業和概念信息。</span></p><p style="line-height:1.5em;"><span style="font-size:15px;"><br></span></p><p style="line-height:1.5em;"><span style="font-size:15px;"></span><span style="font-size:14px;color:rgb(136,136,136);">參考鏈接:</span></p><p style="line-height:1.5em;"><span style="font-size:14px;color:rgb(136,136,136);"></span><span style="font-size:14px;color:rgb(136,136,136);">http://tushare.org/classifying.html#id2</span><span style="font-size:12px;color:rgb(136,136,136);"></span><span style="font-size:15px;"></span></p><p style="line-height:1.5em;"><span style="font-size:12px;color:rgb(136,136,136);"><br></span></p><p style="line-height:1.5em;"><span style="font-size:15px;">通過以下的代碼獲得股票行業信息,並把返回的信息直接存儲在stock_industry_prep.csv文件里。</span></p><p style="line-height:1.5em;"><br></p><p style="line-height:1.5em;"><img class="rich_pages" src="https://img2018.cnblogs.com/blog/1112483/201908/1112483-20190813162018660-1290145485.png"></p><p style="line-height:1.5em;"><br></p><p style="line-height:1.5em;"><span style="font-size:15px;">類似的,可以通過以下代碼即可獲得股票概念信息,並把它們存儲在stock_concept_prep.csv文件里。</span><br></p><p style="line-height:1.5em;"><br></p><p style="line-height:1.5em;"><img class="rich_pages" src="https://img2018.cnblogs.com/blog/1112483/201908/1112483-20190813162038171-1072692412.png" alt="640?wx_fmt=png"></p><p><br></p><h2 style="line-height:1.5em;text-align:center;"><a name="t3"></a><strong><span style="font-size:15px;color:rgb(166,91,203);">設計知識圖譜</span></strong></h2><p><br></p><p style="line-height:1.5em;"><span style="font-size:15px;">設計一個這樣的圖譜:</span></p><p style="line-height:1.5em;"><br></p><ul class="list-paddingleft-2"><li><p style="line-height:1.5em;"><span style="font-size:15px;">創建“人”實體,這個人擁有姓名、性別、年齡</span></p></li><li><p style="line-height:1.5em;"><span style="font-size:15px;">創建“公司”實體,除了股票代碼,還有股票名稱</span></p></li><li><p style="line-height:1.5em;"><span style="font-size:15px;">創建“概念”實體,每個概念都有概念名</span></p></li><li><p style="line-height:1.5em;"><span style="font-size:15px;">創建“行業”實體,每個行業都有⾏業名</span></p></li><li><p style="line-height:1.5em;"><span style="font-size:15px;">給“公司”實體添加“ST”的標記,這個由LABEL來實現</span></p></li><li><p style="line-height:1.5em;"><span style="font-size:15px;">創建“人”和“公司”的關系,這個關系有董事長、執行董事等等</span></p></li><li><p style="line-height:1.5em;"><span style="font-size:15px;">創建“公司”和“概念”的關系</span></p></li><li><p style="line-height:1.5em;"><span style="font-size:15px;">創建“公司”和“行業”的關系</span></p></li></ul><p style="line-height:1.5em;"><br></p><p style="line-height:1.5em;"><span style="font-size:15px;">把設計圖存儲為design.png文件。</span></p><p style="line-height:1.5em;"><br></p><p style="line-height:1.5em;"><span style="font-size:15px;">注:實體名字和關系名字需要易懂,對於上述的要求,並不一定存在唯一的設計,只要能夠覆蓋上面這些要求即可。“ST”標記是用來刻畫⼀個股票嚴重虧損的狀態,這個可以從給定的股票名字前綴來判斷,背景知識可參考百科ST股票,“ST”股票對應列表為['*ST', 'ST', 'S*ST', 'SST']。</span></p><p style="line-height:1.5em;"><br></p><p style="line-height:1.5em;text-align:center;"><strong><span style="font-size:15px;color:rgb(166,91,203);">創建可以導⼊Neo4j的csv文件</span></strong></p><p style="line-height:1.5em;text-align:left;"><br></p><p style="line-height:1.5em;text-align:left;"><span style="font-size:15px;">在前兩個任務里,我們已經分別生成了 executive_prep.csv, stock_industry_prep.csv, stock_concept_prep.csv,但這些文件不能直接導入到Neo4j數據庫。</span></p><p style="line-height:1.5em;"><br></p><p style="line-height:1.5em;"><span style="font-size:15px;">所以需要做⼀些處理,並生成能夠直接導入Neo4j的csv格式。</span><span style="font-size:15px;">我們需要生成這⼏個文件:</span></p><p style="line-height:1.5em;text-align:left;"><br></p><p style="line-height:1.5em;text-align:left;"><span style="font-size:15px;">executive.csv, stock.csv, concept.csv, industry.csv, executive_stock.csv, stock_industry.csv, stock_concept.csv。</span></p><p style="line-height:1.5em;"><br></p><p style="line-height:1.5em;text-align:left;"><span style="font-size:15px;">對於格式的要求,請參考:</span><span style="font-size:14px;color:rgb(136,136,136);">https://neo4j.com/docs/operations-manual/current/tutorial/import-tool/</span><span style="font-size:15px;"></span></p><p style="line-height:1.5em;"><br></p><p style="line-height:1.5em;text-align:center;"><strong><span style="font-size:15px;color:rgb(166,91,203);">利用上面的csv文件生成數據庫</span></strong></p><p style="line-height:1.5em;"><br></p><p style="min-height:1em;text-align:center;"><img class="rich_pages" src="https://img2018.cnblogs.com/blog/1112483/201908/1112483-20190813162057051-1134500933.png" alt="640?wx_fmt=png"></p><p><br></p><p style="line-height:1.5em;"><span style="font-size:15px;">這個命令會把所有的數據導入到Neo4j中,數據默認存放在 graph.db 文件夾里。</span><span style="font-size:15px;">如果graph.db文件夾之前已經有數據存在,則可以選擇先刪除再執行命令。</span></p><p style="line-height:1.5em;"><br></p><p style="line-height:1.5em;"><span style="font-size:15px;">把Neo4j服務重啟之后,就可以通過localhost:7474觀察到知識圖譜了。</span></p><p><br></p><p style="line-height:1.5em;text-align:center;"><strong><span style="font-size:15px;color:rgb(166,91,203);">查詢分析</span></strong></p><p style="line-height:1.5em;"><br></p><p style="line-height:1.5em;"><span style="font-size:15px;">基於構建好的知識圖譜,通過編寫Cypher語句回答如下問題:</span><br></p><p style="line-height:1.5em;"><br></p><ul class="list-paddingleft-2"><li><p style="line-height:1.5em;"><span style="font-size:15px;">有多少個公司目前是屬於“ST”類型的?</span></p></li><li><p style="line-height:1.5em;"><span style="font-size:15px;">“600519”公司的所有獨立董事人員中,有多少人同時也擔任別的公司的獨立董事職位?</span></p></li><li><p style="line-height:1.5em;"><span style="font-size:15px;">有多少公司既屬於環保行業,又有外資背景?</span></p></li><li><p style="line-height:1.5em;"><span style="font-size:15px;">對於有鋰電池概念的所有公司,獨⽴董事中女性⼈員⽐例是多少?</span></p></li></ul><p style="line-height:1.5em;"><br></p><p><span style="font-size:15px;">請提供對應的Cypher語句以及答案,並把結果寫在result.txt。</span></p><p><br></p><p style="line-height:1.5em;text-align:center;"><strong><span style="font-size:15px;color:rgb(166,91,203);">實施問題</span></strong></p><p style="line-height:1.5em;text-align:center;"><br></p><ul class="list-paddingleft-2"><li><p style="line-height:1.5em;"><span style="font-size:15px;">構建人的實體時,重名問題具體怎么解決?</span><br></p></li><li><p style="line-height:1.5em;"><span style="font-size:15px;">把簡單思路寫在result.txt文件中。</span></p></li></ul><p><br></p><p style="line-height:1.5em;text-align:center;"><strong><span style="font-size:15px;color:rgb(166,91,203);">工程目錄</span></strong></p><p style="line-height:1.5em;text-align:center;"><br></p><p style="line-height:1.5em;"><img class="rich_pages" src="https://img2018.cnblogs.com/blog/1112483/201908/1112483-20190813162118733-275139296.png" alt="640?wx_fmt=png"></p><p style="min-height:1em;text-align:center;"><br></p><blockquote style="color:rgba(0,0,0,.498);"><p style="min-height:1em;">具體內容,請到Github庫進行查看。</p></blockquote><p><br></p><p style="min-height:1em;font-family:'-apple-system-font', BlinkMacSystemFont, 'Helvetica Neue', 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei UI', 'Microsoft YaHei', Arial, sans-serif;letter-spacing:.544px;text-align:right;"><span style="color:rgb(136,136,136);font-size:14px;">編輯:王菁</span></p><p style="min-height:1em;font-family:'-apple-system-font', BlinkMacSystemFont, 'Helvetica Neue', 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei UI', 'Microsoft YaHei', Arial, sans-serif;letter-spacing:.544px;text-align:right;"><span style="color:rgb(136,136,136);font-size:14px;">校對:洪舒越</span></p><p style="min-height:1em;font-family:'-apple-system-font', BlinkMacSystemFont, 'Helvetica Neue', 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei UI', 'Microsoft YaHei', Arial, sans-serif;letter-spacing:.544px;text-align:right;"><span style="color:rgb(136,136,136);font-size:14px;"><br></span></p><p style="min-height:1em;font-family:'-apple-system-font', BlinkMacSystemFont, 'Helvetica Neue', 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei UI', 'Microsoft YaHei', Arial, sans-serif;letter-spacing:.544px;text-align:right;"><span style="color:rgb(136,136,136);font-size:14px;"><strong style="font-size:15px;text-align:center;color:rgb(60,60,60);line-height:25.5px;letter-spacing:1px;"><strong style="line-height:25.5px;"><strong style="line-height:25.5px;"><strong style="line-height:25.5px;"><strong style="line-height:25.5px;"><strong style="line-height:25.5px;"><strong style="line-height:25.5px;"><strong style="line-height:25.5px;"><strong style="line-height:25.5px;"><strong style="line-height:25.5px;"></strong></strong></strong></strong></strong></strong></strong></strong></strong></strong></span></p>
            </div>原文地址:https://blog.csdn.net/eNohtZvQiJxo00aTz3y8/article/details/96055187
                                  </div>


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM