您好,登錄后才能下訂單哦!
這篇文章將為大家詳細(xì)講解有關(guān)如何用Python進(jìn)行金融市場(chǎng)文本數(shù)據(jù)的情感計(jì)算,文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個(gè)參考,希望大家閱讀完這篇文章后對(duì)相關(guān)知識(shí)有一定的了解。
一、tushare介紹
tushare庫是目前比較流行的開源免費(fèi)的經(jīng)濟(jì)數(shù)據(jù)庫,tushare有普通版和高級(jí)版,其中普通版無需積分就可以使用,而高級(jí)版需要使用積分才可使用。
tushare基礎(chǔ)班提供了包括:
交易數(shù)據(jù),如歷史行情、復(fù)權(quán)數(shù)據(jù)、實(shí)時(shí)行情等
投資參考數(shù)據(jù),如分配的方案、業(yè)績(jī)預(yù)告、限售股解禁、基金持股、新浪數(shù)據(jù)、融資融券
股票分類數(shù)據(jù)、行業(yè)、概念、地域、中小板、創(chuàng)業(yè)板、封校警示板生
基本面數(shù)據(jù)、股票列表、業(yè)績(jī)報(bào)告(主表)、盈利能力、營運(yùn)能力、償債能力等
宏觀經(jīng)濟(jì)數(shù)據(jù),如存款利率、貸款利率、GDP數(shù)據(jù)、工業(yè)品出場(chǎng)價(jià)格指數(shù)、居民消費(fèi)節(jié)各直屬
新聞事件數(shù)據(jù),如新浪股吧
龍虎榜數(shù)據(jù)
銀行間同業(yè)拆放理論
電影票房
安裝
!pip3 install tushare
Run
Collecting tushare [? 25l Downloading https: //files.pythonhosted.org/packages/a9/8b/2695ad38548d474f4ffb9c95548df126e82adb24e56e2c4f7ce1ef9fbd01/tushare-1.2.43.tar.gz (168kB) [K 100 % |████████████████████████████████| 174kB 162kB /s ta : 00 : 01 [? 25hBuilding wheels for collected packages: tushare Running setup.py bdist_wheel for tushare ... [? 25ldone [? 25h Stored in directory: /Users/ thunderhit/ Library / Caches /pip/wheels/ 4b / 28 / 7b / 62d7a4155b34be251c1840e7cecfa4c374812819c59edba760 Successfully built tushare Installing collected packages: tushare Successfully installed tushare- 1.2 . 43 [ 33mYou are using pip version 18.1 , however version 19.2 . 3 is available. You should consider upgrading via the 'pip install --upgrade pip' command.[ 0m
二、新聞數(shù)據(jù)
新聞事件接口主要提供國內(nèi)財(cái)經(jīng)、證券、港股和期貨方面的滾動(dòng)新聞,以及個(gè)股的信息地lei數(shù)據(jù)。但目前只有新浪股吧api的接口可用,其他的需要使用tushare高級(jí)版。
獲取sina財(cái)經(jīng)股吧首頁的重點(diǎn)消息。股吧數(shù)據(jù)目前獲取大概17條重點(diǎn)數(shù)據(jù),可根據(jù)參數(shù)設(shè)置是否顯示消息內(nèi)容,默認(rèn)情況是不顯示。
參數(shù)說明:
show_content:boolean,是否顯示內(nèi)容,默認(rèn)False
返回值說明:
title, 消息標(biāo)題
content, 消息內(nèi)容(show_content=True的情況下)
ptime, 發(fā)布時(shí)間
rcounts,閱讀次數(shù)
調(diào)用方法
import tushare as ts #顯示詳細(xì)內(nèi)容 newsdata = ts.guba_sina(show_content= True ) newsdata.head( 10 )
三、讀取詞典
之前制作的中文金融情感詞典是csv文件格式,我們使用pandas讀取
import pandas as pd df = pd.read_csv( 'CFSD/pos.csv' , encoding= 'gbk' ) df.head()
我們將讀取詞典定義成函數(shù)
def read_dict(file, header): """ file: 詞典路徑 header: csv文件內(nèi)字段名,如postive 讀取csv詞典,返回詞語列表 """ df = pd.read_csv(file, encoding= 'gbk' ) return list(df[header]) poswords = read_dict(file= 'CFSD/pos.csv' , header = 'postive' ) negwords = read_dict(file= 'CFSD/neg.csv' , header = 'negative' ) negwords[: 5 ]
run
[ '閉門造車' , '閉塞' , '云里霧里' , '拖累' , '過熱' ]
三、情感分析方法
這里我們對(duì)新聞content內(nèi)容進(jìn)行情感分析,分析的思路是統(tǒng)計(jì)content中正、負(fù)詞的占比。我們會(huì)用到pandas的 df.agg(func)方法對(duì)content列進(jìn)行文本計(jì)算。這需要先定義一個(gè)待調(diào)用的情感計(jì)算函數(shù),注意有可能出現(xiàn)分母為0,所以定義的函數(shù)使用了try except捕捉0除異常,返回0.
import jieba def pos_senti(content): """ content: 待分析文本內(nèi)容 返回正面詞占文本總詞語數(shù)的比例 """ try : pos_word_num = words = jieba.lcut(content) for kw in poswords: pos_word_num += words.count(kw) return pos_word_num/len(words) except : return def neg_senti(content): """ content: 待分析文本內(nèi)容 返回負(fù)面詞占文本總詞語數(shù)的比例 """ try : neg_word_num = words = jieba.lcut(content) for kw in negwords: neg_word_num += words.count(kw) return neg_word_num/len(words) except : return 0
對(duì)content列分別施行情感計(jì)算函數(shù)possenti,negsenti,將得到的得分賦值給pos、neg列
newsdata[ 'pos' ]=newsdata[ 'content' ].agg(pos_senti) newsdata[ 'neg' ]=newsdata[ 'content' ].agg(neg_senti) newsdata.head( 10 )
我們的數(shù)據(jù)中出現(xiàn)了pos和neg兩個(gè)得分,我們還可以定義一個(gè)判斷函數(shù),判斷文本的情緒分類。
當(dāng)pos比neg大,判斷為'正'
當(dāng)pos比neg小,判斷為'負(fù)'
這里不嚴(yán)謹(jǐn),為了教程簡(jiǎn)單,沒考慮相等的情況
newsdata[ 'senti_classification' ] = newsdata[ 'pos' ]>newsdata[ 'neg' ] newsdata[ 'senti_classification' ] = newsdata[ 'senti_classification' ].map({ True : "正" , False : "負(fù)" }) newsdata.head( 10 )
總結(jié)
其實(shí)到這兒,簡(jiǎn)單的情感計(jì)算就實(shí)現(xiàn)了。
另外,大家在使用本文時(shí),一定要注意:
本篇Python學(xué)習(xí)教程使用的情感詞典是CFSD中文金融情感詞典,大家可以用自己領(lǐng)域的詞典,得到poswords和negwords
還有要注意的是情感計(jì)算函數(shù)(possenti和negsenti),有不同的算法就有不同的結(jié)果
正負(fù)面傾向判斷,我這里比較粗糙,沒有考慮相等的中性問題。
注意以上幾點(diǎn),本篇Python學(xué)習(xí)教程代碼就可復(fù)用。不過再好的代碼,前提是得會(huì)python,會(huì)懂編程思維,知道如何寫代碼改代碼,不然大家用起來也比較困難。
關(guān)于如何用Python進(jìn)行金融市場(chǎng)文本數(shù)據(jù)的情感計(jì)算就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。