{ "cells": [ { "cell_type": "markdown", "source": [ "# 透過機器學習預測股市漲跌_系列2_進階資料處理\n", "\n", "作者:劉睿哲(臺灣行銷研究特邀作者)、蔡尚宏(臺灣行銷研究特邀作者)、鄭晴文(臺灣行銷研究特邀作者)\n", "\n", "[原始資料請見此](https://drive.google.com/file/d/15lnX0-B1SzXrSXEIVNMRfiA5bfK-Kfbq/view?usp=sharing),下載下來與本ipynb檔案放於同一個資料夾中,再執行下方程式即可。" ], "metadata": {} }, { "cell_type": "code", "execution_count": 28, "source": [ "import os\n", "import re\n", "import nltk\n", "import string\n", "import pandas as pd\n", "import numpy as np\n", "from nltk.corpus import stopwords\n", "from nltk.tokenize import word_tokenize\n", "from nltk.stem.wordnet import WordNetLemmatizer\n", "from sklearn.feature_extraction.text import CountVectorizer, TfidfVectorizer\n", "from sklearn.linear_model import LogisticRegression\n", "from sklearn.model_selection import train_test_split\n", "from sklearn.metrics import accuracy_score, classification_report, confusion_matrix\n", "from datetime import timedelta, datetime\n" ], "outputs": [], "metadata": {} }, { "cell_type": "code", "execution_count": 29, "source": [ "nltk.download('punkt')\n", "nltk.download('stopwords')\n", "nltk.download('wordnet')" ], "outputs": [ { "output_type": "stream", "name": "stderr", "text": [ "[nltk_data] Downloading package punkt to /Users/liuruizhe/nltk_data...\n", "[nltk_data] Package punkt is already up-to-date!\n", "[nltk_data] Downloading package stopwords to\n", "[nltk_data] /Users/liuruizhe/nltk_data...\n", "[nltk_data] Package stopwords is already up-to-date!\n", "[nltk_data] Downloading package wordnet to\n", "[nltk_data] /Users/liuruizhe/nltk_data...\n", "[nltk_data] Package wordnet is already up-to-date!\n" ] }, { "output_type": "execute_result", "data": { "text/plain": [ "True" ] }, "metadata": {}, "execution_count": 29 } ], "metadata": {} }, { "cell_type": "code", "execution_count": 47, "source": [ "data = pd.read_csv('/Users/liuruizhe/Library/Mobile Documents/com~apple~CloudDocs/TMR/Kaggle_DJIA/DJIA/after_Combined_News_DJIA.csv')\n", "data" ], "outputs": [ { "output_type": "execute_result", "data": { "text/html": [ "
\n", " | Unnamed: 0 | \n", "Date | \n", "Label | \n", "Top1 | \n", "Top2 | \n", "Top3 | \n", "Top4 | \n", "Top5 | \n", "Top6 | \n", "Top7 | \n", "... | \n", "Top19 | \n", "Top20 | \n", "Top21 | \n", "Top22 | \n", "Top23 | \n", "Top24 | \n", "Top25 | \n", "Unnamed: 28 | \n", "Unnamed: 29 | \n", "fluctuation | \n", "
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
0 | \n", "0 | \n", "2008/8/8 | \n", "0 | \n", "b georgia downs two russian warplanes as cou... | \n", "b breaking musharraf to be impeached | \n", "b russia today columns of troops roll into so... | \n", "b russian tanks are moving towards the capital... | \n", "b afghan children raped with impunity u n o... | \n", "b russian tanks have entered south ossetia wh... | \n", "b breaking georgia invades south ossetia rus... | \n", "... | \n", "b this is a busy day the european union has ... | \n", "b georgia will withdraw soldiers from iraq t... | \n", "b why the pentagon thinks attacking iran is a ... | \n", "b caucasus in crisis georgia invades south os... | \n", "b indian shoe manufactory and again in a se... | \n", "b visitors suffering from mental illnesses ban... | \n", "b no help for mexico s kidnapping surge | \n", "NaN | \n", "NaN | \n", "0.055658 | \n", "
1 | \n", "1 | \n", "2008/8/11 | \n", "1 | \n", "b why wont america and nato help us if they w... | \n", "b bush puts foot down on georgian conflict | \n", "b jewish georgian minister thanks to israeli ... | \n", "b georgian army flees in disarray as russians ... | \n", "b olympic opening ceremony fireworks faked | \n", "b what were the mossad with fraudulent new zea... | \n", "b russia angered by israeli military sale to g... | \n", "... | \n", "b china to overtake us as largest manufacturer | \n", "b war in south ossetia pics | \n", "b israeli physicians group condemns state tort... | \n", "b russia has just beaten the united states ov... | \n", "b perhaps the question about the georgia r... | \n", "b russia is so much better at war | \n", "b so this is what it s come to trading sex fo... | \n", "NaN | \n", "NaN | \n", "-1.793642 | \n", "
2 | \n", "2 | \n", "2008/8/12 | \n", "0 | \n", "b remember that adorable year old who sang at... | \n", "b russia ends georgia operation | \n", "b if we had no sexual harassment we would have... | \n", "b al qa eda is losing support in iraq because ... | \n", "b ceasefire in georgia putin outmaneuvers the... | \n", "b why microsoft and intel tried to kill the xo... | \n", "b stratfor the russo georgian war and the bal... | \n", "... | \n", "b russia georgia and nato cold war two | \n", "b remember that adorable year old who led you... | \n", "b war in georgia the israeli connection | \n", "b all signs point to the us encouraging georgi... | \n", "b christopher king argues that the us and nato... | \n", "b america the new mexico | \n", "b bbc news asia pacific extinction by man... | \n", "NaN | \n", "NaN | \n", "-0.757185 | \n", "
3 | \n", "3 | \n", "2008/8/13 | \n", "0 | \n", "b u s refuses israel weapons to attack iran ... | \n", "b when the president ordered to attack tskhinv... | \n", "b israel clears troops who killed reuters cam... | \n", "b britain s policy of being tough on drugs is ... | \n", "b body of year old found in trunk latest r... | \n", "b china has moved million quake survivors ... | \n", "b bush announces operation get all up in russi... | \n", "... | \n", "b russian convoy heads into georgia violating... | \n", "b israeli defence minister us against strike ... | \n", "b gorbachev we had no choice | \n", "b witness russian forces head towards tbilisi... | \n", "b quarter of russians blame u s for conflict... | \n", "b georgian president says us military will ta... | \n", "b nobel laureate aleksander solzhenitsyn accu... | \n", "NaN | \n", "NaN | \n", "1.207153 | \n", "
4 | \n", "4 | \n", "2008/8/14 | \n", "1 | \n", "b all the experts admit that we should legalis... | \n", "b war in south osetia pictures made by a r... | \n", "b swedish wrestler ara abrahamian throws away ... | \n", "b russia exaggerated the death toll in south o... | \n", "b missile that killed inside pakistan may ha... | \n", "b rushdie condemns random house s refusal to p... | \n", "b poland and us agree to missle defense deal ... | \n", "... | \n", "b non media photos of south ossetia georgia c... | \n", "b georgian tv reporter shot by russian sniper ... | \n", "b saudi arabia mother moves to block child ma... | \n", "b taliban wages war on humanitarian aid workers | \n", "b russia world can forget about georgia s ... | \n", "b darfur rebels accuse sudan of mounting major... | \n", "b philippines peace advocate say muslims nee... | \n", "NaN | \n", "NaN | \n", "1.795697 | \n", "
... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "
1984 | \n", "1984 | \n", "2016/6/27 | \n", "0 | \n", "barclays and rbs shares suspended from trading... | \n", "pope says church should ask forgiveness from g... | \n", "poland shocked by xenophobic abuse of poles ... | \n", "there will be no second referendum cabinet ag... | \n", "scotland welcome to join eu merkel ally says | \n", "sterling dips below friday s year low amid br... | \n", "no negative news about south african president... | \n", "... | \n", "turkey sorry for downing russian jet | \n", "edward snowden lawyer vows new push for pardon... | \n", "brexit opinion poll reveals majority don t wan... | \n", "conservative mp leave campaigner the leave c... | \n", "economists predict UNITED KINGDOM recession f... | \n", "new eu superstate plan by france germany cr... | \n", "pakistani clerics declare transgender marriage... | \n", "NaN | \n", "NaN | \n", "NaN | \n", "
1985 | \n", "1985 | \n", "2016/6/28 | \n", "1 | \n", "scientists to australia if you want to save... | \n", "the personal details of french police office... | \n", "s amp p cuts united kingdom sovereign credit r... | \n", "huge helium deposit found in africa | \n", "ceo of the south african state broadcaster qui... | \n", "brexit cost investors trillion the worst on... | \n", "hong kong democracy activists call for return ... | \n", "... | \n", "YEAR old skull from borneo reveals surprise f... | \n", "palestinians stone western wall worshipers po... | \n", "jean claude juncker asks farage why are you h... | \n", "romanians for remainians offering a new home... | \n", "brexit gibraltar in talks with scotland to st... | \n", "suicide bombers strike lebanon | \n", "mexico s security forces routinely use sexual... | \n", "NaN | \n", "NaN | \n", "NaN | \n", "
1986 | \n", "1986 | \n", "2016/6/29 | \n", "1 | \n", "explosion at airport in istanbul | \n", "yemeni former president terrorism is the offs... | \n", "UNITED KINGDOM must accept freedom of movement... | \n", "devastated scientists too late to captive bre... | \n", "british labor party leader jeremy corbyn loses... | \n", "a muslim shop in the UNITED KINGDOM was just f... | \n", "mexican authorities sexually torture women in ... | \n", "... | \n", "emaciated lions in taiz zoo are trapped in blo... | \n", "rupert murdoch describes brexit as wonderful ... | \n", "more than killed in yemen suicide attacks | \n", "google found disastrous symantec and norton vu... | \n", "extremist violence on the rise in germany dom... | \n", "bbc news labour mps pass corbyn no confidence... | \n", "tiny new zealand town with too many jobs lau... | \n", "NaN | \n", "NaN | \n", "NaN | \n", "
1987 | \n", "1987 | \n", "2016/6/30 | \n", "1 | \n", "jamaica proposes marijuana dispensers for tour... | \n", "stephen hawking says pollution and stupidity ... | \n", "boris johnson says he will not run for tory pa... | \n", "six gay men in ivory coast were abused and for... | \n", "switzerland denies citizenship to muslim immig... | \n", "palestinian terrorist stabs israeli teen girl ... | \n", "puerto rico will default on billion of debt ... | \n", "... | \n", "calls to suspend saudi arabia from un human ri... | \n", "more than nobel laureates call out greenpeac... | \n", "british pedophile sentenced to years in us f... | \n", "us permitted offshore fracks in gulf of mexi... | \n", "we will be swimming in ridicule french beach... | \n", "uefa says no minutes of silence for istanbul v... | \n", "law enforcement sources gun used in paris ter... | \n", "NaN | \n", "NaN | \n", "NaN | \n", "
1988 | \n", "1988 | \n", "2016/7/1 | \n", "1 | \n", "a year old woman in mexico city finally recei... | \n", "imf chief backs athens as permanent olympic host | \n", "the president of france says if brexit won so... | \n", "british man who must give police hours noti... | \n", "nobel laureates urge greenpeace to stop oppo... | \n", "brazil huge spike in number of police killing... | \n", "austria s highest court annuls presidential el... | \n", "... | \n", "u s sailors detained by iran spoke too much u... | \n", "mass fish kill in vietnam solved as taiwan ste... | \n", "philippines president rodrigo duterte urges pe... | \n", "spain arrests three pakistanis accused of prom... | \n", "venezuela where anger over food shortages is ... | \n", "a hindu temple worker has been killed by three... | \n", "ozone layer hole seems to be healing us amp... | \n", "NaN | \n", "NaN | \n", "NaN | \n", "
1989 rows × 31 columns
\n", "\n", " | air force | \n", "air strike | \n", "al jazeera | \n", "al qaeda | \n", "al qaida | \n", "amnesty international | \n", "anti gay | \n", "around world | \n", "australian government | \n", "barack obama | \n", "... | \n", "world biggest | \n", "world cup | \n", "world first | \n", "world largest | \n", "world news | \n", "world war | \n", "year ago | \n", "year jail | \n", "year old | \n", "year prison | \n", "
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
0 | \n", "0.0 | \n", "0.00000 | \n", "0.0 | \n", "0.361791 | \n", "0.0 | \n", "0.0 | \n", "0.0 | \n", "0.0 | \n", "0.0 | \n", "0.000000 | \n", "... | \n", "0.0 | \n", "0.000000 | \n", "0.0 | \n", "0.0 | \n", "0.000000 | \n", "0.400727 | \n", "0.0 | \n", "0.00000 | \n", "0.228241 | \n", "0.0 | \n", "
1 | \n", "0.0 | \n", "0.00000 | \n", "0.0 | \n", "0.000000 | \n", "0.0 | \n", "0.0 | \n", "0.0 | \n", "0.0 | \n", "0.0 | \n", "0.000000 | \n", "... | \n", "0.0 | \n", "0.000000 | \n", "0.0 | \n", "0.0 | \n", "0.000000 | \n", "0.671348 | \n", "0.0 | \n", "0.00000 | \n", "0.000000 | \n", "0.0 | \n", "
2 | \n", "0.0 | \n", "0.00000 | \n", "0.0 | \n", "0.000000 | \n", "0.0 | \n", "0.0 | \n", "0.0 | \n", "0.0 | \n", "0.0 | \n", "0.000000 | \n", "... | \n", "0.0 | \n", "0.000000 | \n", "0.0 | \n", "0.0 | \n", "0.000000 | \n", "0.000000 | \n", "0.0 | \n", "0.00000 | \n", "0.790220 | \n", "0.0 | \n", "
3 | \n", "0.0 | \n", "0.00000 | \n", "0.0 | \n", "0.000000 | \n", "0.0 | \n", "0.0 | \n", "0.0 | \n", "0.0 | \n", "0.0 | \n", "0.000000 | \n", "... | \n", "0.0 | \n", "0.000000 | \n", "0.0 | \n", "0.0 | \n", "0.000000 | \n", "0.000000 | \n", "0.0 | \n", "0.00000 | \n", "0.337538 | \n", "0.0 | \n", "
4 | \n", "0.0 | \n", "0.00000 | \n", "0.0 | \n", "0.000000 | \n", "0.0 | \n", "0.0 | \n", "0.0 | \n", "0.0 | \n", "0.0 | \n", "0.000000 | \n", "... | \n", "0.0 | \n", "0.000000 | \n", "0.0 | \n", "0.0 | \n", "0.375107 | \n", "0.000000 | \n", "0.0 | \n", "0.00000 | \n", "0.000000 | \n", "0.0 | \n", "
... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "
1606 | \n", "0.0 | \n", "0.00000 | \n", "0.0 | \n", "0.000000 | \n", "0.0 | \n", "0.0 | \n", "0.0 | \n", "0.0 | \n", "0.0 | \n", "0.000000 | \n", "... | \n", "0.0 | \n", "0.780427 | \n", "0.0 | \n", "0.0 | \n", "0.000000 | \n", "0.000000 | \n", "0.0 | \n", "0.00000 | \n", "0.000000 | \n", "0.0 | \n", "
1607 | \n", "0.0 | \n", "0.00000 | \n", "0.0 | \n", "0.000000 | \n", "0.0 | \n", "0.0 | \n", "0.0 | \n", "0.0 | \n", "0.0 | \n", "0.000000 | \n", "... | \n", "0.0 | \n", "0.000000 | \n", "0.0 | \n", "0.0 | \n", "0.000000 | \n", "0.000000 | \n", "0.0 | \n", "0.00000 | \n", "0.000000 | \n", "0.0 | \n", "
1608 | \n", "0.0 | \n", "0.00000 | \n", "0.0 | \n", "0.000000 | \n", "0.0 | \n", "0.0 | \n", "0.0 | \n", "0.0 | \n", "0.0 | \n", "0.607745 | \n", "... | \n", "0.0 | \n", "0.000000 | \n", "0.0 | \n", "0.0 | \n", "0.000000 | \n", "0.000000 | \n", "0.0 | \n", "0.00000 | \n", "0.000000 | \n", "0.0 | \n", "
1609 | \n", "0.0 | \n", "0.00000 | \n", "0.0 | \n", "0.000000 | \n", "0.0 | \n", "0.0 | \n", "0.0 | \n", "0.0 | \n", "0.0 | \n", "0.000000 | \n", "... | \n", "0.0 | \n", "0.000000 | \n", "0.0 | \n", "0.0 | \n", "0.412763 | \n", "0.000000 | \n", "0.0 | \n", "0.41653 | \n", "0.000000 | \n", "0.0 | \n", "
1610 | \n", "0.0 | \n", "0.38031 | \n", "0.0 | \n", "0.000000 | \n", "0.0 | \n", "0.0 | \n", "0.0 | \n", "0.0 | \n", "0.0 | \n", "0.000000 | \n", "... | \n", "0.0 | \n", "0.000000 | \n", "0.0 | \n", "0.0 | \n", "0.000000 | \n", "0.000000 | \n", "0.0 | \n", "0.00000 | \n", "0.000000 | \n", "0.0 | \n", "
1611 rows × 160 columns
\n", "