Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Stock Prediction Using Facebook Sentiment with Python

tlyu0419
April 28, 2022

Stock Prediction Using Facebook Sentiment with Python

Stock market prediction has been a vital area of research for a long time. And there are many abroad study found that public mood sentiment on social media, such as Twitter, can predict the stock price effectively.
But when it comes to the situation of Taiwan, due to the difference in popular social media and the languages, both of them bring many problems and difficulties to building a stock price prediction model.
To localize the technic with previous papers and give suggestions to investors, I develop a Python project about predicting the stock price through Facebook sentiment. This Tutorial is going to share the five vital modules: data collection, data preprocessing, sentiment analysis, prediction model, and portfolio suggestion. I hope this will help the investors get some valuable indicators and make better investment decisions.

tlyu0419

April 28, 2022
Tweet

More Decks by tlyu0419

Other Decks in Research

Transcript

  1. Mail: [email protected] | Slido: 636922 NCCU PyDay | Stock Prediction

    Using Facebook Sentiment with Python 歡迎來到 PyDay 2022!
  2. Mail: [email protected] | Slido: 636922 NCCU PyDay | Stock Prediction

    Using Facebook Sentiment with Python 今天是一日的 Python 課程 3
  3. Mail: [email protected] | Slido: 636922 NCCU PyDay | Stock Prediction

    Using Facebook Sentiment with Python 今天的重點會在 Facebook爬蟲 和 情感分析 4
  4. Mail: [email protected] | Slido: 636922 NCCU PyDay | Stock Prediction

    Using Facebook Sentiment with Python 有問題可以 舉手發問 或在 Slido 留言 5
  5. Mail: [email protected] | Slido: 636922 NCCU PyDay | Stock Prediction

    Using Facebook Sentiment with Python Github: https://github.com/tlyu0419/Stock-Prediction-Using- Facebook-Sentiment-with-Python SpeakerDeck: https://speakerdeck.com/tlyu0419 6
  6. Mail: [email protected] | Slido: 636922 NCCU PyDay | Stock Prediction

    Using Facebook Sentiment with Python 游騰林 國泰世華銀行 | 數據部 | 資料科學家 PyConTW 產業、應用與待遇 - Python 在勞動市場中的職缺分析 PyData Taipei 誰識KOL? 2020台灣總統大選在 Facebook 上的社群網絡分析(II) AI Academy Chatbot In Cathay: Service Journey and Social Network Analysis MOPCON 讓機器學習服務 - 數據驅動客服的創新與實踐 7
  7. Mail: [email protected] | Slido: 636922 NCCU PyDay | Stock Prediction

    Using Facebook Sentiment with Python 故事要從這篇論文開始說起 8
  8. Mail: [email protected] | Slido: 636922 NCCU PyDay | Stock Prediction

    Using Facebook Sentiment with Python 什麼論文居然如此厲害? 11
  9. Mail: [email protected] | Slido: 636922 NCCU PyDay | Stock Prediction

    Using Facebook Sentiment with Python 論文分析流程 12
  10. Mail: [email protected] | Slido: 636922 NCCU PyDay | Stock Prediction

    Using Facebook Sentiment with Python DJIA • Dow Jones Industrial Average • Yahoo! Finance • Daily DJIA closing-values 分析流程 13 Twitter feed • 2008.02 ~ 12 • 9,853,498 tweets • posted by 2.7M users • Filter data by specific ketwords Mood indicators • Opinion Finder • G-POMS with six dimensions Normalization • To Z score SOFNN • Regression • Classification Granger Causality • Find out how much predictive information one signal has about another over a given lag period.
  11. Mail: [email protected] | Slido: 636922 NCCU PyDay | Stock Prediction

    Using Facebook Sentiment with Python 情感分析效度 • Sentiment Analysis • Opinion Finder: Identify the emotional polarity of sentences (positive or negative) • G-POMS: 1. CALM 2. ALERT 3. SURE 4. VITAL 5. KIND 6. HAPPY • Events • Presidential election • Thanksgiving Day 14
  12. Mail: [email protected] | Slido: 636922 NCCU PyDay | Stock Prediction

    Using Facebook Sentiment with Python 情感與股價的關聯 15 • 股價會跟過去 2~5 天的 Calm 情緒相關 • “We are not testing actual causation but whether one time series has predictive information about the other or not.” (p.4) • 許多時候 Calm 情緒分數的變化 真的早於股價變化
  13. Mail: [email protected] | Slido: 636922 NCCU PyDay | Stock Prediction

    Using Facebook Sentiment with Python 股價預測模型效度 16 IOF :前3天股價 + 正向/負向情感 I0 :前3天股價 I1 :前3天股價 + 前3天 Calm 情感 I1, 3 :前3天股價 + 前3天 Calm & Sure 情感 I1, 4 :前3天股價 + 前3天 Calm & Vital 情感 I1, 5 :前3天股價 + 前3天 Calm & Kind 情感 I1, 2 :前3天股價 + 前3天 Calm & Alert 情感 I1, 6 :前3天股價 + 前3天 Calm & Happy 情感
  14. Mail: [email protected] | Slido: 636922 NCCU PyDay | Stock Prediction

    Using Facebook Sentiment with Python 17 論文說 預測股價準確度有 87%! P.S. 是真的 87 %!
  15. Mail: [email protected] | Slido: 636922 NCCU PyDay | Stock Prediction

    Using Facebook Sentiment with Python 我嘗試把這個研究在地化 18
  16. Mail: [email protected] | Slido: 636922 NCCU PyDay | Stock Prediction

    Using Facebook Sentiment with Python DJIA • 改收集台灣證券交易所 的資料 • 對遺漏值進行填補 (週末、假日) 分析流程在地化 19 Twitter feed • 改收集 Facebook 的資料 • 收集 19 個財經相關 的粉絲專頁資料 201912 ~ 202204 Mood indicators • 中文資料的情感分析 Normalization • To Z score SOFNN • Regression • Classification Granger Causality • Find out how much predictive information one signal has about another over a given lag period.
  17. Mail: [email protected] | Slido: 636922 NCCU PyDay | Stock Prediction

    Using Facebook Sentiment with Python 在地化的實驗結果 • 模型效度 • 預測漲的準確度: 3 / 11 ~= 0.27 • 預測跌的準確度: 14/18 ~= 0.78 • 整體的準確度 (14+3) / 29 ~= 0.58 • 應用 • 獲利機會: 3 / 7 ~= 42.9% • 降損機會: 14/22 ~= 63.6% • 你覺得模型好用嗎? • 優點和缺點分別有什麼? 20 實際 總計 漲 跌 預測 漲 3 8 11 跌 4 14 18 總計 7 22 29
  18. Mail: [email protected] | Slido: 636922 NCCU PyDay | Stock Prediction

    Using Facebook Sentiment with Python 誰來翻譯一下這個數據? 21
  19. Mail: [email protected] | Slido: 636922 NCCU PyDay | Stock Prediction

    Using Facebook Sentiment with Python 假設我在第 1 天買 x 元 最後 1 天賣 y 元 y – x = -1033元 22 本金: 17, 625
  20. Mail: [email protected] | Slido: 636922 NCCU PyDay | Stock Prediction

    Using Facebook Sentiment with Python 但在 大數據情感分析 幫助下 你還是會賠 847 元! 23 本金: 35,258
  21. Mail: [email protected] | Slido: 636922 NCCU PyDay | Stock Prediction

    Using Facebook Sentiment with Python 怎麼通過 大數據模型 還是賠錢? 24
  22. Mail: [email protected] | Slido: 636922 NCCU PyDay | Stock Prediction

    Using Facebook Sentiment with Python 今天想跟大家一起做研究 看看建模流程哪裡還可以再優化 25
  23. Mail: [email protected] | Slido: 636922 NCCU PyDay | Stock Prediction

    Using Facebook Sentiment with Python 大綱 • Facebook 爬蟲 • 情感分析 • 股價資料處理 • Granger Causality • 股價預測模型建置 • 股價預測模型應用 • 後續優化 26
  24. Mail: [email protected] | Slido: 636922 NCCU PyDay | Stock Prediction

    Using Facebook Sentiment with Python 網路爬蟲是 從網站自動收集資料的技術 27
  25. Mail: [email protected] | Slido: 636922 NCCU PyDay | Stock Prediction

    Using Facebook Sentiment with Python 什麼是網路爬蟲 • 網路爬蟲是透過程式取代瀏覽器來瀏覽網頁的技術 • 藉由爬蟲程式能讓我們在收集/剖析/整理資料的工作變得更省力 28 request response
  26. Mail: [email protected] | Slido: 636922 NCCU PyDay | Stock Prediction

    Using Facebook Sentiment with Python 爬蟲眼中的世界 29
  27. Mail: [email protected] | Slido: 636922 NCCU PyDay | Stock Prediction

    Using Facebook Sentiment with Python 我們要做的事情就是 透過 Python 向伺服器發出請求 透過Python剖析伺服器回傳的資料 30
  28. Mail: [email protected] | Slido: 636922 NCCU PyDay | Stock Prediction

    Using Facebook Sentiment with Python 爬蟲界流傳 「開發一時爽,_______________」 31
  29. Mail: [email protected] | Slido: 636922 NCCU PyDay | Stock Prediction

    Using Facebook Sentiment with Python 爬蟲界流傳 「開發一時爽,一直開發一直爽」 32 維護火葬場
  30. Mail: [email protected] | Slido: 636922 NCCU PyDay | Stock Prediction

    Using Facebook Sentiment with Python 在開發爬蟲程式之前 33 評估開發成本與 是否有替代網站? 是不是已經有人 做過並開源了? 查詢目標網站是 不是有提供API? 請當個有禮貌的爬蟲 開發爬蟲程式
  31. Mail: [email protected] | Slido: 636922 NCCU PyDay | Stock Prediction

    Using Facebook Sentiment with Python 在正式開發爬蟲程式之前 先來了解 Facebook 的網站架構! 34
  32. Mail: [email protected] | Slido: 636922 NCCU PyDay | Stock Prediction

    Using Facebook Sentiment with Python First of All: 無痕模式 • 請開啟瀏覽器的無痕模式! (如果你不想被鎖帳號… • 開啟後連結至 Yahoo奇摩 股市理財粉絲專頁 • https://www.facebook.c om/YahooTWFinance/ 35
  33. Mail: [email protected] | Slido: 636922 NCCU PyDay | Stock Prediction

    Using Facebook Sentiment with Python 加載更多貼文 • 動態加載更多資料 • 透過往下滑動網頁,伺服器會 回傳更多貼文資料 • Question: 為什麼不一次回傳所有資料? • 瀏覽器在背後做了什麼? • 打開開發者工具的 Network頁籤 進行監測 36
  34. Mail: [email protected] | Slido: 636922 NCCU PyDay | Stock Prediction

    Using Facebook Sentiment with Python 開發者工具 37
  35. Mail: [email protected] | Slido: 636922 NCCU PyDay | Stock Prediction

    Using Facebook Sentiment with Python 搜尋資料 38 • 透過關鍵字(CTRL + F)查詢資料在哪個 request • 建議找英文/數字,因為中文會有編碼的問題 查詢內容 查詢結果
  36. Mail: [email protected] | Slido: 636922 NCCU PyDay | Stock Prediction

    Using Facebook Sentiment with Python 發送請求 39 • 向 Graphql 請求資料 • 請求的方式是 POST • 確認請求(request) 的 headers 和 Payload 內 容 • 通過 Python 模擬這些資 訊請求資料
  37. Mail: [email protected] | Slido: 636922 NCCU PyDay | Stock Prediction

    Using Facebook Sentiment with Python 請求內容 40 Headers: 跟伺服器說你的身份 Payload: 跟伺服器說你需要的資料
  38. Mail: [email protected] | Slido: 636922 NCCU PyDay | Stock Prediction

    Using Facebook Sentiment with Python 回傳的資料結構 • 通過 Json 格式做資料交換 • 每個請求中包含了 3 則貼文 • 每則貼文中包含 • 貼文人姓名 • 貼文人ID • 貼文時間 • 貼文內容 • 貼文ID • 按讚、留言、分享數 • … 41
  39. Mail: [email protected] | Slido: 636922 NCCU PyDay | Stock Prediction

    Using Facebook Sentiment with Python 加載更多資料 42 • 在 Payload 中有個 cursor 參數, 記錄的是貼文的編號 • 這個編號藏在從上一個請求而來 (把上一次的編號放進這次的請求 中,就能加載更多資料) • 如果不放這個參數,會回傳目前 最新的貼文
  40. Mail: [email protected] | Slido: 636922 NCCU PyDay | Stock Prediction

    Using Facebook Sentiment with Python 爬蟲架構設計 • Request 是從開發者工具觀察而來 • 透過Python將Request 參數發送給 Facebook Server • 剖析Facebook Server回傳的 Json 資料,當中包含貼文資訊與編號 • 將貼文資料整理成結構化的資料並儲存 • 將貼文編號更新至Request參數 • 重新向Facebook Server發送請求, 請求更多貼文資料 43 Request 參數 Python Facebook Server Parsing 1 2 3 4 5 6
  41. Mail: [email protected] | Slido: 636922 NCCU PyDay | Stock Prediction

    Using Facebook Sentiment with Python 請大家打開 Jupyter 吧! 44 有問題可以尋求助教的協助~
  42. Mail: [email protected] | Slido: 636922 NCCU PyDay | Stock Prediction

    Using Facebook Sentiment with Python 還沒安裝的同學請到這邊下載 Python 45 1. Google 搜尋 miniconda 2. 下載 Python 3.8版
  43. Mail: [email protected] | Slido: 636922 NCCU PyDay | Stock Prediction

    Using Facebook Sentiment with Python Miniconda安裝教學 46 打開安裝檔看到的畫面 環境變數的選項要打鉤
  44. Mail: [email protected] | Slido: 636922 NCCU PyDay | Stock Prediction

    Using Facebook Sentiment with Python 下載到你的電腦並解壓縮 https://github.com/TLYu0419/Stock-Prediction-Using-Facebook-Sentiment-with-Python 47 如果沒有想法的人可以解壓縮到 「Documents」的資料夾
  45. Mail: [email protected] | Slido: 636922 NCCU PyDay | Stock Prediction

    Using Facebook Sentiment with Python 切換工作路徑 • 打開 miniconda 的 prompt 並 cd 至解壓縮到的資料夾 48
  46. Mail: [email protected] | Slido: 636922 NCCU PyDay | Stock Prediction

    Using Facebook Sentiment with Python 安裝使用套件 • 輸入下方的指令 pip install –r requirements.txt --timeout 30 --retries 10 49
  47. Mail: [email protected] | Slido: 636922 NCCU PyDay | Stock Prediction

    Using Facebook Sentiment with Python 開啟 Jupyter • 輸入 jupyter lab 即可成功開啟 jupyter 50
  48. Mail: [email protected] | Slido: 636922 NCCU PyDay | Stock Prediction

    Using Facebook Sentiment with Python Jupyter 開啟畫面 51
  49. Mail: [email protected] | Slido: 636922 NCCU PyDay | Stock Prediction

    Using Facebook Sentiment with Python Stock-Prediction-Using-Facebook-Sentiment-with- Python/01_facebook_crawler.ipynb 52
  50. Mail: [email protected] | Slido: 636922 NCCU PyDay | Stock Prediction

    Using Facebook Sentiment with Python 糟糕,我聽不太懂怎麼辦? 53
  51. Mail: [email protected] | Slido: 636922 NCCU PyDay | Stock Prediction

    Using Facebook Sentiment with Python 開發網路爬蟲的心法 • (也適用在模型的建置與優化) • 成長性思維 • 堅信沒有解不開的問題! • 切割問題 • 問題出在 headers? payload? 請求頻率? 鎖定IP? … • 做很多的假設,然後逐一排除 • 放 A 參數能取得資料嗎?拿掉 B 會怎麼樣? • 做有意義的實驗 (錯的事情做100次也不會變好) • 真的解不開時 • 尋求協助 (有 1 次 hint 的機會,當然看廣告就可以更多) • 猜看看可能的答案 (有3次的犯錯機會) 54
  52. Mail: [email protected] | Slido: 636922 NCCU PyDay | Stock Prediction

    Using Facebook Sentiment with Python facebook_crawler 套件 • 不需登入: 不用擔心被鎖定帳號 • 簡單: 僅需要粉絲頁/社團的網址 和跳脫的日期 • 高效: 透過 request 直接請求資 料,不需通過 Selenium 55 facebook-crawler · PyPI
  53. Mail: [email protected] | Slido: 636922 NCCU PyDay | Stock Prediction

    Using Facebook Sentiment with Python Facebook爬蟲 56
  54. Mail: [email protected] | Slido: 636922 NCCU PyDay | Stock Prediction

    Using Facebook Sentiment with Python 想怎麼爬就怎麼爬 57
  55. Mail: [email protected] | Slido: 636922 NCCU PyDay | Stock Prediction

    Using Facebook Sentiment with Python 情感分析是 從文本中萃取情緒的分析技術 58
  56. Mail: [email protected] | Slido: 636922 NCCU PyDay | Stock Prediction

    Using Facebook Sentiment with Python 什麼是情感分析 59 唯一支持韓總統 烙跑市長 最終的勝利是屬於我們的! 讓我們一起把韓市長 送進總統府~ 凍蒜凍蒜凍蒜~~
  57. Mail: [email protected] | Slido: 636922 NCCU PyDay | Stock Prediction

    Using Facebook Sentiment with Python 情感分析應用案例 60 監測品牌的網路聲量, 即時掌握市場動態並調整行銷策略
  58. Mail: [email protected] | Slido: 636922 NCCU PyDay | Stock Prediction

    Using Facebook Sentiment with Python 情感分析應用案例 61 通過情感分析從新聞自動萃取企 業的犯罪風險
  59. Mail: [email protected] | Slido: 636922 NCCU PyDay | Stock Prediction

    Using Facebook Sentiment with Python 情感分析應用案例 Ref: Study: Twitter Sentiment Mirrored Facebook’s Stock Price Today 62 透過社群網絡上的正負向情感資訊 預測短時間後的股價走勢 (投資理財有賺有賠,使用前請詳閱說明書)
  60. Mail: [email protected] | Slido: 636922 NCCU PyDay | Stock Prediction

    Using Facebook Sentiment with Python 情感分析應用案例 63 寫 Mail 時自動提醒信件背後傳遞 的語氣與態度
  61. Mail: [email protected] | Slido: 636922 NCCU PyDay | Stock Prediction

    Using Facebook Sentiment with Python 情感分析的潛在應用 • 改進 拍拍機器人2.0 • 通過情感分析剖析情緒, 不用列舉一系列的關鍵詞 64
  62. Mail: [email protected] | Slido: 636922 NCCU PyDay | Stock Prediction

    Using Facebook Sentiment with Python 那情感分析有哪些方法? 65
  63. Mail: [email protected] | Slido: 636922 NCCU PyDay | Stock Prediction

    Using Facebook Sentiment with Python 常見的情感分析方法 66 統計值法 藉由資料源的評分來統計情感分數 詞典法 建立情感詞典,統計文本中正負向詞的 出現次數 情感分析套件 藉由其他資料源來建立情感評分 模型,並套用在新資料源上 訓練模型 將資料標註評分後再建置ML/DL模型
  64. Mail: [email protected] | Slido: 636922 NCCU PyDay | Stock Prediction

    Using Facebook Sentiment with Python 統計值法 Ref:如何爬取 GooglePlay 的APP留言與評分 67 如果資料源已經蒐集滿意度分數與意見回饋 就建議直接使用滿意度分數來監測網路輿情
  65. Mail: [email protected] | Slido: 636922 NCCU PyDay | Stock Prediction

    Using Facebook Sentiment with Python 詞典法 68 評分詞典 改良 出色 努力 卓越 順利 … 不足 不法 中止 不明 代價 … 銀行界爆發大規模詐貸案!老牌貿易商潤 寅實業以虛假應收帳款詐貸…損失最大 的苦主為台企銀,曝險金額高達 13.38 億元, 因放款多屬無擔保品,損失比重高達 9 成, 為此案的重災戶。 建立標註正/負向詞彙的詞典 並藉此統計文章中出現正/負向詞彙的數量
  66. Mail: [email protected] | Slido: 636922 NCCU PyDay | Stock Prediction

    Using Facebook Sentiment with Python 情感評分套件法 69 藉由現成的套件直接預測文本的情感分數 中文:snowNLP(2015) 英文:NLTK(2020)
  67. Mail: [email protected] | Slido: 636922 NCCU PyDay | Stock Prediction

    Using Facebook Sentiment with Python 訓練模型法 70 收集語料 標註資料 特徵工程 訓練模型 • 社群網站 • 新聞文本 • 商品評論 • … • 自行標註 • 外包 • 詞頻矩陣 • TF-IDF • 詞向量 • … • 機器學習 • 深度學習 訓練模型的精度最高,但需要先標註資料>“<
  68. Mail: [email protected] | Slido: 636922 NCCU PyDay | Stock Prediction

    Using Facebook Sentiment with Python 情感分析策略 71 N 小 大 統計值法 借模型法 詞典法 爬蟲法 翻譯+套件法 START 已標註? 想標註? 詞庫 需求量 Y N Y N Y N
  69. Mail: [email protected] | Slido: 636922 NCCU PyDay | Stock Prediction

    Using Facebook Sentiment with Python 實務上在做情感分析 72
  70. Mail: [email protected] | Slido: 636922 NCCU PyDay | Stock Prediction

    Using Facebook Sentiment with Python 借模型法 Ref: 百度 / AI 開放平台 / 情感傾向分析 73 借用 百度/ Google 的情感傾向分析 來幫我們的資料標註情感分數 (註冊帳號後就能免費體驗50萬次)
  71. Mail: [email protected] | Slido: 636922 NCCU PyDay | Stock Prediction

    Using Facebook Sentiment with Python 借模型法 74 # !pip install baidu-aip from aip import AipNlp # 輸入 APP 的資料 APP_ID = '17584307' API_KEY = 'tvqsXXXXXXXXXnwQm' SECRET_KEY = 'X4XksoWpBXXrKMG5Sk3bPmNvPpIgckHy' client = AipNlp(APP_ID, API_KEY, SECRET_KEY) # 想做情感分析的文本 text = "希望从情感的角度來看服务平台谁可以成为未來的标准" client.sentimentClassify(text) client.emotion(text) 在網站上申請後 取得的資料
  72. Mail: [email protected] | Slido: 636922 NCCU PyDay | Stock Prediction

    Using Facebook Sentiment with Python # !pip install baidu-aip from aip import AipNlp # 輸入 APP 的資料 APP_ID = '17584307' API_KEY = 'tvqsXXXXXXXXXnwQm' SECRET_KEY = 'X4XksoWpBXXrKMG5Sk3bPmNvPpIgckHy' client = AipNlp(APP_ID, API_KEY, SECRET_KEY) # 想做情感分析的文本 text = "希望从情感的角度來看服务平台谁可以成为未來的标准" client.sentimentClassify(text) client.emotion(text) 借模型法 75 {'log_id': 4045458584401201752, 'text': '希望从情感的角度來看服务平台谁可以成为未來的标准', 'items': [{'subitems': [], 'replies': [], 'prob': 0.997689, 'label': 'neutral'}, {'subitems': [], 'replies': [], 'prob': 0.00117633, 'label': 'optimistic'}, {'subitems': [], 'replies': [], 'prob': 0.00113456, 'label': 'pessimistic'}]} 在網站上申請後 取得的資料
  73. Mail: [email protected] | Slido: 636922 NCCU PyDay | Stock Prediction

    Using Facebook Sentiment with Python 詞典法 76 業務部門 洗錢防制 風險審查 可以依產業/公司建立專屬詞庫 但需留意不同部門的想法不盡相同
  74. Mail: [email protected] | Slido: 636922 NCCU PyDay | Stock Prediction

    Using Facebook Sentiment with Python 爬蟲法 Ref: 1. 百度 / AI 開放平台 / 情感傾向分析 2. wisers.ai 77 資料量不大的時候可以「試用」別人的產品 但要注意別把別人的網站用壞了>”<
  75. Mail: [email protected] | Slido: 636922 NCCU PyDay | Stock Prediction

    Using Facebook Sentiment with Python 爬蟲法 78 import requests url = 'https://www.wisers.ai/?api=ailab-demo-apilb.wisers.com:8000/senti/api/processtext' data = { 'text' : '幾十年來從沒像此時這麼的風雨飄搖', 'model' :'dl', 'output_level' : 'subject', 'show_scores' :'true'} resp = requests.post(url, json=data) resp.json() {'docid': None, 'is_successful': True, 'message': '', 'overall_res': {'sentiment': -1, 'sentiment_score': 0.29}, 'detail_res': []}
  76. Mail: [email protected] | Slido: 636922 NCCU PyDay | Stock Prediction

    Using Facebook Sentiment with Python 翻譯 + 套件法 79 透過 Google 翻譯將文本翻譯成英文 接著就可以放入 nltk 模型中XD
  77. Mail: [email protected] | Slido: 636922 NCCU PyDay | Stock Prediction

    Using Facebook Sentiment with Python 翻譯 + 套件法 80 from googletrans import Translator import nltk translator = Translator() sid = nltk.sentiment.vader.SentimentIntensityAnalyzer() #zh-tw to en translations = translator.translate(['對的事情做,不對的事情不要做', '不要為了選舉而選舉', '心存善念,盡力而為'], dest='en') for translation in translations: print(translation.origin, ' -> ', translation.text) # 翻譯 print(sid.polarity_scores(translation.text)) # 獲取情感分數 print('=================')
  78. Mail: [email protected] | Slido: 636922 NCCU PyDay | Stock Prediction

    Using Facebook Sentiment with Python from googletrans import Translator import nltk translator = Translator() sid = nltk.sentiment.vader.SentimentIntensityAnalyzer() #zh-tw to en translations = translator.translate(['對的事情做,不對的事情不要做', '不要為了選舉而選舉', '心存善念,盡力而為'], dest='en') for translation in translations: print(translation.origin, ' -> ', translation.text) # 翻譯 print(sid.polarity_scores(translation.text)) # 獲取情感分數 print('=================') 翻譯 + 套件法 81 對的事情做,不對的事情不要做 -> Do the right things, don't do the wrong things {'neg': 0.0, 'neu': 0.758, 'pos': 0.242, 'compound': 0.3724} ================= 不要為了選舉而選舉 -> Don't vote for election {'neg': 0.0, 'neu': 1.0, 'pos': 0.0, 'compound': 0.0} ================= 心存善念,盡力而為 -> Do your best {'neg': 0.0, 'neu': 0.323, 'pos': 0.677, 'compound': 0.6369}
  79. Mail: [email protected] | Slido: 636922 NCCU PyDay | Stock Prediction

    Using Facebook Sentiment with Python 經過翻譯後再丟入NLTK抽取情感分數 82
  80. Mail: [email protected] | Slido: 636922 NCCU PyDay | Stock Prediction

    Using Facebook Sentiment with Python 翻譯 / 爬蟲法看起來很厲害 我們是不是不用自己Train模型了? 83
  81. Mail: [email protected] | Slido: 636922 NCCU PyDay | Stock Prediction

    Using Facebook Sentiment with Python BUT 試用通常會有次數限制 ! 84
  82. Mail: [email protected] | Slido: 636922 NCCU PyDay | Stock Prediction

    Using Facebook Sentiment with Python 偷模型法 85 1. 先從語料庫中抽一定數量的樣本(ex: 2000) 2. 通過 翻譯 / 爬蟲 / AI公司的情感分析產品 判斷這些樣本的情感 3. 根據落地場景修正別人的判斷結果 4. 將別人的判斷結果作為目標變數,訓練模型學習別人如何判斷 5. 將模型套用在原先語料庫中的大量資料
  83. Mail: [email protected] | Slido: 636922 NCCU PyDay | Stock Prediction

    Using Facebook Sentiment with Python 如何訓練情感分類模型 86 訓練模型的關鍵在於如何處理這些非結構化的文本資料(Content)
  84. Mail: [email protected] | Slido: 636922 NCCU PyDay | Stock Prediction

    Using Facebook Sentiment with Python CountVector • 優點 • 簡單 • 有效 • 缺點 • 資料稀疏: 大多數的值都是0 • 資料表很大張: 消耗運算時間和記憶體 • 資訊離散: 大多都是1 / 0 ,不利模型學習 87
  85. Mail: [email protected] | Slido: 636922 NCCU PyDay | Stock Prediction

    Using Facebook Sentiment with Python Word2Vector 介紹 • Efficient estimation of word representations in vector space • Google 2013的研究,被引用 2.9萬次 • 藉由大量文本訓練模型,幫每個詞在 K 維空間中找到位置 88 透過大量的文本資料讓模型學習每個詞的意義(向量) 取得向量後就能夠用來推薦或推論 EX: (中國) - (北京) ~= (台灣) – (??) >> 模型會回答 ?? 是 台北
  86. Mail: [email protected] | Slido: 636922 NCCU PyDay | Stock Prediction

    Using Facebook Sentiment with Python Word2Vec 訓練方式 • 假設電腦能知道每個詞的意思 • 理想上「應數系」要跟「AI」、「機器學習」的距離比較接近 • 跟「昆蟲」、「賽車」的距離比較遠 • 模型如何進行訓練 • 透過 window size 的方式訓練詞的意思 • CBOW: 前後的詞預測中間的詞 • SKIP-GRAM: 中間預測前後的詞 • 實務上SKIP-GRAM的效果較好 89
  87. Mail: [email protected] | Slido: 636922 NCCU PyDay | Stock Prediction

    Using Facebook Sentiment with Python Word2Vector 結果 90 K 個維度的數值資料,default = 128 Embedding 的目標回顧 • 自動提取特徵 • 創造 K個虛擬的維度來取代 原始數百/千萬維的資料 • 資訊稠密有助於模型學習 [0.676655, 0.298118, …, -0.422986, -0.763698] 清華大學 [0.118514, -0.183278, …, -0.528604, 0.131890] 應數系 [0.093297, -0.163998, …, -0.526928, 0.076224] 網路爬蟲 [0.093297, -0.163998, …, -0.526928, 0.076224] 數理資訊社 在 Spacy 中是將先找出每個詞的向量 接著再將這些向量取平均來代表文檔
  88. Mail: [email protected] | Slido: 636922 NCCU PyDay | Stock Prediction

    Using Facebook Sentiment with Python 文本特徵處理 • Sentiment Analysis of Twitter Data: A Survey of Techniq (arxiv.org) • 預處理 • Mail • Hashtag • Emoji • URL • Stop words 91
  89. Mail: [email protected] | Slido: 636922 NCCU PyDay | Stock Prediction

    Using Facebook Sentiment with Python 情感分析策略 92 N 小 大 統計值法 借模型法 詞典法 爬蟲法 翻譯+套件法 START 已標註? 想標註? 詞庫 需求量 Y N Y N Y N 沒錢、沒人的話可以「借」別人的模型 借完再拿來當目標變數 Train 自己的模型XD 解釋力最強,但需要花時間溝通>”< 小心別用壞別人的網站 有點吃翻譯的效度XD
  90. Mail: [email protected] | Slido: 636922 NCCU PyDay | Stock Prediction

    Using Facebook Sentiment with Python 以天為單位聚合情感資料集 93 透過不同方式抽取文本的情感, 接著 group by 天後聚合資料
  91. Mail: [email protected] | Slido: 636922 NCCU PyDay | Stock Prediction

    Using Facebook Sentiment with Python 文字亂碼的處理方式 • Matplot 預設的字體為 DejaVuSans.ttf ,該字體無法顯示中文 • 正常的處理方式(如底下連結) 1. 將中文字體放入 Matplotlib 套件的字體資料夾 2. 調整預設的字體為中文字體 94 Fur: 解決Python 3 Matplotlib與Seaborn視覺化套件中文顯示問題
  92. Mail: [email protected] | Slido: 636922 NCCU PyDay | Stock Prediction

    Using Facebook Sentiment with Python 文字亂碼的處理方式 • Matplot 預設的字體為 DejaVuSans.ttf ,該字體無法顯示中文 • 正常的處理方式(如底下連結) • 將中文字體放入 Matplotlib 套件的字體資料夾 • 調整預設的字體為中文字體 • 不正常的處理方式 • 直接將中文字體重命名為 DejaVuSans.ttf • 將檔案複製進 Matplotlib 套件的字體資料夾 • C:\Windows\Fonts\微軟正黑體 95 Fur: 解決Python 3 Matplotlib與Seaborn視覺化套件中文顯示問題
  93. Mail: [email protected] | Slido: 636922 NCCU PyDay | Stock Prediction

    Using Facebook Sentiment with Python Stock-Prediction-Using-Facebook-Sentiment-with- Python/02_Sectiment_analysis.ipynb 96
  94. Mail: [email protected] | Slido: 636922 NCCU PyDay | Stock Prediction

    Using Facebook Sentiment with Python 股價資料爬蟲 97
  95. Mail: [email protected] | Slido: 636922 NCCU PyDay | Stock Prediction

    Using Facebook Sentiment with Python 網站介紹與操作 98
  96. Mail: [email protected] | Slido: 636922 NCCU PyDay | Stock Prediction

    Using Facebook Sentiment with Python 股價爬蟲程式開發 99
  97. Mail: [email protected] | Slido: 636922 NCCU PyDay | Stock Prediction

    Using Facebook Sentiment with Python 如何處理股價的遺漏值? • Stock Prediction Using Twitter Sentiment Analysis • 股價的資料不乏因週末、假期導致的遺漏值 • 許多研究會直接使用遺漏值前後的平均值來填補 >> 股價通常遵循 concave function,應用 遞迴 的方式補 該遺漏值之前以及之後的第一個非遺漏資料的均值 • [ 2 , 4 , NA1 , NA2 , NA3 , 16 ] • NA1 = (4 + 16) / 2 >> 10 • NA2 = (NA1 +16) / 2 >> 13 • NA3 = (NA2 + 16) / 2 >> 14.5 100
  98. Mail: [email protected] | Slido: 636922 NCCU PyDay | Stock Prediction

    Using Facebook Sentiment with Python 如何處理股價的遺漏值? 101 掃描收盤價的資料 如果遇到遺漏值往 前後找非 NA 的值 將找到的前後值相 加平均
  99. Mail: [email protected] | Slido: 636922 NCCU PyDay | Stock Prediction

    Using Facebook Sentiment with Python Stock-Prediction-Using-Facebook-Sentiment-with- Python/03_Stock_Price.ipynb 102
  100. Mail: [email protected] | Slido: 636922 NCCU PyDay | Stock Prediction

    Using Facebook Sentiment with Python 所以 股價 和 社群情感 的關係是? 103
  101. Mail: [email protected] | Slido: 636922 NCCU PyDay | Stock Prediction

    Using Facebook Sentiment with Python 格蘭傑因果關係(Granger Causality) • Granger Causality 是一種假說檢定的統計方法,檢定一組時間序列 x是否為 另一組時間序列 y 的原因 • Granger Causality 的結論只是一種統計估計,不是真正意義上的因果關係, 不能作爲肯定或否定因果關係的根據 104
  102. Mail: [email protected] | Slido: 636922 NCCU PyDay | Stock Prediction

    Using Facebook Sentiment with Python 格蘭傑因果關係(Granger Causality) • 將股價和分析出的情感分數整 理成 by天 的結果 • 逐一跑 Granger Causality 檢 定,檢視情感分數與股價是否 存在關聯 • 因為網路的訊息傳遞較快,檢 定時可以一次檢定1 ~ 5天的結 果 105
  103. Mail: [email protected] | Slido: 636922 NCCU PyDay | Stock Prediction

    Using Facebook Sentiment with Python Granger Causality in Python 106 要留意pandas dataframe 中 x 跟 y 的位置 目標變數(y)放前面,解釋變數(x)放後面 Ref: Granger Causality in Time Series Explained with Chicken and Egg problem
  104. Mail: [email protected] | Slido: 636922 NCCU PyDay | Stock Prediction

    Using Facebook Sentiment with Python Granger Causality Limitation • Granger causality does not provide any insight on the relationship between the variable hence it is not true causality unlike ’cause and effect’ analysis. >> 因果是指統計上的因果,不是人理解的因果 • Granger causality fails to forecast when there is an interdependency between two or more variables. • Granger causality test can’t be performed on non-stationary data. >> 使用時要先檢查資料是不是平穩 107 Ref: Granger Causality in Time Series Explained with Chicken and Egg problem 1. 吃冰 >> 被鯊魚攻擊 2. 基地台數量 >> 癌症數量
  105. Mail: [email protected] | Slido: 636922 NCCU PyDay | Stock Prediction

    Using Facebook Sentiment with Python 圖中的時間序列有哪些是平穩的? 108 Ref: 8.1 平稳性和差分 | 预测: 方法与实践 a. 連續292天的Google的 股價 b. 連續292天Google股價 的每日變化量 c. 美國各年的罷工總次數 d. 美國獨立家庭住宅的每 月價格 e. 按不變美元計算的美國 的雞蛋價格 f. 每月在澳大利亞維多利 亞州被屠宰的豬的數量 g. 每年在加拿大西北的麥 肯齊河停留的猞猁數量 h. 澳大利亞每月啤酒產量 i. 澳大利亞每月發電量
  106. Mail: [email protected] | Slido: 636922 NCCU PyDay | Stock Prediction

    Using Facebook Sentiment with Python 圖中的時間序列有哪些是平穩的? 109 Ref: 8.1 平稳性和差分 | 预测: 方法与实践 a. 連續292天的Google的 股價 b. 連續292天Google股價 的每日變化量 c. 美國各年的罷工總次數 d. 美國獨立家庭住宅的每 月價格 e. 按不變美元計算的美國 的雞蛋價格 f. 每月在澳大利亞維多利 亞州被屠宰的豬的數量 g. 每年在加拿大西北的麥 肯齊河停留的猞猁數量 h. 澳大利亞每月啤酒產量 i. 澳大利亞每月發電量
  107. Mail: [email protected] | Slido: 636922 NCCU PyDay | Stock Prediction

    Using Facebook Sentiment with Python 檢定數列是否平穩 (Augmented Dickey-Fuller Test 110
  108. Mail: [email protected] | Slido: 636922 NCCU PyDay | Stock Prediction

    Using Facebook Sentiment with Python 數列不平穩的處理方式 111 當數列不平穩時需要先對其做差分 如果一階差分還是不平穩則繼續做二階差分
  109. Mail: [email protected] | Slido: 636922 NCCU PyDay | Stock Prediction

    Using Facebook Sentiment with Python Granger Causality 檢定結果 Lag LIKE HAHA LOVE WOW CARE ANGRY SAD SNOW NLP NLTK_ NEG NLTK_P OS BAIDU_ NEG BAIDU_ POS 1 day .1802 .5059 .0976+ .1867 .0488* .3493 .1196 .3085 .4748 .9278 .5429 .8614 2 days .3763 .6636 .2515 .4488 .1248 .6062 .0280* .5840 .3980 .8537 .2749 .9584 3 days .3586 .6789 .1814 .4463 .0912+ .7695 .0540+ .6952 .2957 .0371* .0172* .4780 4 days .2881 .7972 .2896 .5509 .1156 .6831 .0204* .6414 .0426* .0385* .0138* .1407 5 days .4430 .7595 .3887 .5674 .1727 .5510 .0230* .6410 .0283* .0199* .0106* .0446* 112 Note: + < 0.10, *<0.05, **<0.01, ***<0.001
  110. Mail: [email protected] | Slido: 636922 NCCU PyDay | Stock Prediction

    Using Facebook Sentiment with Python 模型建置 • 目標變數設置 • 直接預測明天的股價 • 預測明天會漲跌多少元 • 預測明天的漲跌幅度 • 預測明天的漲或跌 • pandas.DataFrame.shift • https://pandas.pydata.org/d ocs/reference/api/pandas.Da taFrame.shift.html 113
  111. Mail: [email protected] | Slido: 636922 NCCU PyDay | Stock Prediction

    Using Facebook Sentiment with Python 模型建置2 • 根據 Granger Causality 的結果決定放入的解釋變數 • 嘗試不同的模型與參數 • Linear Regression • Logistic Regression • SVM • SOFNN 114 看論文的好處是可以模仿別人嘗試過的流程 不用自己從頭開始摸索,重複撞別人撞過的牆!
  112. Mail: [email protected] | Slido: 636922 NCCU PyDay | Stock Prediction

    Using Facebook Sentiment with Python Stock-Prediction-Using-Facebook-Sentiment-with- Python/04.Modeling.ipynb 115
  113. Mail: [email protected] | Slido: 636922 NCCU PyDay | Stock Prediction

    Using Facebook Sentiment with Python 模型應用 • 暫時先不理會實際的股價XD • 最初階的應用方式 • 預測隔天會漲,今天趕快買 • 預測明天會跌,今天趕快賣 • 進階的應用方式 • 預測是否高/低與過去7, 15天的 均值,如是才進行買賣決策,否 則不動 116
  114. Mail: [email protected] | Slido: 636922 NCCU PyDay | Stock Prediction

    Using Facebook Sentiment with Python Stock-Prediction-Using-Facebook-Sentiment-with- Python/05_Portfolio_Management.ipynb 117
  115. Mail: [email protected] | Slido: 636922 NCCU PyDay | Stock Prediction

    Using Facebook Sentiment with Python 做研究本來就是這樣 118
  116. Mail: [email protected] | Slido: 636922 NCCU PyDay | Stock Prediction

    Using Facebook Sentiment with Python 要經過很多嘗試 還不一定能得到理想的結果 119
  117. Mail: [email protected] | Slido: 636922 NCCU PyDay | Stock Prediction

    Using Facebook Sentiment with Python 後續還有許多的優化空間 120
  118. Mail: [email protected] | Slido: 636922 NCCU PyDay | Stock Prediction

    Using Facebook Sentiment with Python 資料科學界名言「特徵為王」 優化模型的效度不會只有演算法! 121
  119. Mail: [email protected] | Slido: 636922 NCCU PyDay | Stock Prediction

    Using Facebook Sentiment with Python DJIA • 改收集台灣證券交易所 的資料 • 對遺漏值進行填補 (週末、假日) 分析流程在地化 122 Twitter feed • 改收集 Facebook 的資料 • 收集 19 個財經相關 的粉絲專頁資料 201912 ~ 202204 • 收集更多粉絲專頁資料 • 清理與財經無關的貼文 • 收集留言資料 • 評估粉絲專頁的影響力 Mood indicators • 中文的情感分析 • 嘗試不同的情感分析方法 Normalization • To Z score Model • Regression • Classification Granger Causality • Find out how much predictive information one signal has about another over a given lag period.
  120. Mail: [email protected] | Slido: 636922 NCCU PyDay | Stock Prediction

    Using Facebook Sentiment with Python 如何收集更多粉絲專頁? • 設定種子頁面,透過Facebook推薦相關粉絲專頁的功能, 以滾雪球的方式收集更多粉絲專頁 123
  121. Mail: [email protected] | Slido: 636922 NCCU PyDay | Stock Prediction

    Using Facebook Sentiment with Python 清理與財經無關的貼文 • 為什麼大家要這麼生氣? 124
  122. Mail: [email protected] | Slido: 636922 NCCU PyDay | Stock Prediction

    Using Facebook Sentiment with Python 清理與財經無關的貼文2 • 如何清理無關的貼文資料? • 訓練一個分類器幫我們分類 • 那分類器的 文本 和 Label 要從哪裡來? 125
  123. Mail: [email protected] | Slido: 636922 NCCU PyDay | Stock Prediction

    Using Facebook Sentiment with Python 收集留言資料 • Stock-Prediction-Using-Facebook-Sentiment-with-Python/06_Bonus.ipynb 126
  124. Mail: [email protected] | Slido: 636922 NCCU PyDay | Stock Prediction

    Using Facebook Sentiment with Python 情感分析面 127 SnowNLP 認為這是負向新聞 SnowNLP 認為這是正向新聞
  125. Mail: [email protected] | Slido: 636922 NCCU PyDay | Stock Prediction

    Using Facebook Sentiment with Python 評估粉絲專頁的影響力 • 有些粉絲頁每天發很多貼文但沒有多少LIKE和留言 • 有些則數量相對不多,但往往數千/萬個LIKE和留言 128
  126. Mail: [email protected] | Slido: 636922 NCCU PyDay | Stock Prediction

    Using Facebook Sentiment with Python 應用面 • 直接預測股價 • 預測波動量 • 預測波動幅度 • 單純預測漲跌 • 根據漲跌的機率值調整買賣決策 • 如果信心程度不夠,就維持原本的持有情況 129
  127. Mail: [email protected] | Slido: 636922 NCCU PyDay | Stock Prediction

    Using Facebook Sentiment with Python 附錄 • Pagolu, V. S., Reddy, K. N., Panda, G., & Majhi, B. (2016, October). Sentiment analysis of Twitter data for predicting stock market movements. In 2016 international conference on signal processing, communication, power and embedded system (SCOPES) (pp. 1345-1350). IEEE. • Mittal, A., & Goel, A. (2012). Stock prediction using Twitter sentiment analysis. Standford University, CS229 (2011 http://cs229.Stanford. edu/proj2011/GoelMittal- StockMarketPredictionUsingTwitterSentimentAnalysis.pdf), 15, 2352. • Bollen, J., & Mao, H. (2011). Twitter mood as a stock market predictor. Computer, 44(10), 91-94. 130
  128. Mail: [email protected] | Slido: 636922 NCCU PyDay | Stock Prediction

    Using Facebook Sentiment with Python 人才招募中 131 【數位數據】資料科學分析師-數據分析 (數數發中 心,DDT)|國泰世華商業銀行股份有限公司_人力資源部| 台北市信義區-104 人力銀行 【數位數據】商業數據分析師(數數發中心, DDT)|國泰世 華商業銀行股份有限公司_人力資源部|台北市信義區- 104 人力銀行
  129. Mail: [email protected] | Slido: 636922 NCCU PyDay | Stock Prediction

    Using Facebook Sentiment with Python 2020總統大選的社群網絡分析 132 帶職參選 博士論文 庶民總統 國家認同 政績評論 香港民主 小英加油 韓的負面議題 藍綠都一樣 韓國瑜 蔡英文 宋楚瑜
  130. Mail: [email protected] | Slido: 636922 NCCU PyDay | Stock Prediction

    Using Facebook Sentiment with Python 2020總統大選的社群網絡分析(II) 133 透過 SNA 幫候選人/公司找到正確的 KOL 來幫忙行銷、傳達理念 每位候選人身邊都不乏有正/負向的聲 音 可以再進一步找出應該關注/經營的對 象 也可以觀察候選人身邊的人附近的情感 態度 我們可以知道他的聲音應該被重視或者 忽略
  131. Mail: [email protected] | Slido: 636922 NCCU PyDay | Stock Prediction

    Using Facebook Sentiment with Python 程式語言的流行程度(TIOBE) 134
  132. Mail: [email protected] | Slido: 636922 NCCU PyDay | Stock Prediction

    Using Facebook Sentiment with Python 產業、應用與待遇 待遇在4萬 以上的比例 135
  133. Mail: [email protected] | Slido: 636922 NCCU PyDay | Stock Prediction

    Using Facebook Sentiment with Python Python在勞動市場的職缺類型 136 1: 雲端架構架構 2: 數據分析/機器學習 3: 雲端服務(英) 4: 產品/自動化測試 5: 數據分析/機器學習(英) 6: 演算法設計/開發 7: 軟體設計/測試(英) 0: 資料庫設計/開發 1261 347 388 642 629 876 988 1169