├── README.md ├── Data.csv ├── LICENSE └── Stock Sentiment Analysis.ipynb /README.md: -------------------------------------------------------------------------------- 1 | # Stock-Sentiment-Analysis -------------------------------------------------------------------------------- /Data.csv: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/krishnaik06/Stock-Sentiment-Analysis/HEAD/Data.csv -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [yyyy] [name of copyright owner] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /Stock Sentiment Analysis.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "## Stock Sentiment Analysis using News Headlines" 8 | ] 9 | }, 10 | { 11 | "cell_type": "code", 12 | "execution_count": 1, 13 | "metadata": {}, 14 | "outputs": [], 15 | "source": [ 16 | "import pandas as pd" 17 | ] 18 | }, 19 | { 20 | "cell_type": "code", 21 | "execution_count": 3, 22 | "metadata": {}, 23 | "outputs": [], 24 | "source": [ 25 | "df=pd.read_csv('Data.csv', encoding = \"ISO-8859-1\")" 26 | ] 27 | }, 28 | { 29 | "cell_type": "code", 30 | "execution_count": 4, 31 | "metadata": {}, 32 | "outputs": [ 33 | { 34 | "data": { 35 | "text/html": [ 36 | "
\n", 37 | "\n", 50 | "\n", 51 | " \n", 52 | " \n", 53 | " \n", 54 | " \n", 55 | " \n", 56 | " \n", 57 | " \n", 58 | " \n", 59 | " \n", 60 | " \n", 61 | " \n", 62 | " \n", 63 | " \n", 64 | " \n", 65 | " \n", 66 | " \n", 67 | " \n", 68 | " \n", 69 | " \n", 70 | " \n", 71 | " \n", 72 | " \n", 73 | " \n", 74 | " \n", 75 | " \n", 76 | " \n", 77 | " \n", 78 | " \n", 79 | " \n", 80 | " \n", 81 | " \n", 82 | " \n", 83 | " \n", 84 | " \n", 85 | " \n", 86 | " \n", 87 | " \n", 88 | " \n", 89 | " \n", 90 | " \n", 91 | " \n", 92 | " \n", 93 | " \n", 94 | " \n", 95 | " \n", 96 | " \n", 97 | " \n", 98 | " \n", 99 | " \n", 100 | " \n", 101 | " \n", 102 | " \n", 103 | " \n", 104 | " \n", 105 | " \n", 106 | " \n", 107 | " \n", 108 | " \n", 109 | " \n", 110 | " \n", 111 | " \n", 112 | " \n", 113 | " \n", 114 | " \n", 115 | " \n", 116 | " \n", 117 | " \n", 118 | " \n", 119 | " \n", 120 | " \n", 121 | " \n", 122 | " \n", 123 | " \n", 124 | " \n", 125 | " \n", 126 | " \n", 127 | " \n", 128 | " \n", 129 | " \n", 130 | " \n", 131 | " \n", 132 | " \n", 133 | " \n", 134 | " \n", 135 | " \n", 136 | " \n", 137 | " \n", 138 | " \n", 139 | " \n", 140 | " \n", 141 | " \n", 142 | " \n", 143 | " \n", 144 | " \n", 145 | " \n", 146 | " \n", 147 | " \n", 148 | " \n", 149 | " \n", 150 | " \n", 151 | " \n", 152 | " \n", 153 | " \n", 154 | " \n", 155 | " \n", 156 | " \n", 157 | " \n", 158 | " \n", 159 | " \n", 160 | " \n", 161 | " \n", 162 | " \n", 163 | " \n", 164 | " \n", 165 | " \n", 166 | " \n", 167 | " \n", 168 | " \n", 169 | " \n", 170 | " \n", 171 | " \n", 172 | " \n", 173 | " \n", 174 | " \n", 175 | " \n", 176 | " \n", 177 | " \n", 178 | " \n", 179 | " \n", 180 | " \n", 181 | " \n", 182 | " \n", 183 | " \n", 184 | " \n", 185 | " \n", 186 | " \n", 187 | " \n", 188 | " \n", 189 | " \n", 190 | " \n", 191 | " \n", 192 | " \n", 193 | " \n", 194 | " \n", 195 | " \n", 196 | " \n", 197 | " \n", 198 | " \n", 199 | "
DateLabelTop1Top2Top3Top4Top5Top6Top7Top8...Top16Top17Top18Top19Top20Top21Top22Top23Top24Top25
02000-01-030A 'hindrance to operations': extracts from the...ScorecardHughes' instant hit buoys BluesJack gets his skates on at ice-cold AlexChaos as Maracana builds up for UnitedDepleted Leicester prevail as Elliott spoils E...Hungry Spurs sense rich pickingsGunners so wide of an easy target...Flintoff injury piles on woe for EnglandHunters threaten Jospin with new battle of the...Kohl's successor drawn into scandalThe difference between men and womenSara Denver, nurse turned solicitorDiana's landmine crusade put Tories in a panicYeltsin's resignation caught opposition flat-f...Russian rouletteSold outRecovering a title
12000-01-040ScorecardThe best lake sceneLeader: German sleaze inquiryCheerio, boyoThe main recommendationsHas Cubie killed fees?Has Cubie killed fees?Has Cubie killed fees?...On the critical listThe timing of their livesDear doctorIrish court halts IRA man's extradition to Nor...Burundi peace initiative fades after rebels re...PE points the way forward to the ECBCampaigners keep up pressure on Nazi war crime...Jane RatcliffeYet more things you wouldn't know without the ...Millennium bug fails to bite
22000-01-050Coventry caught on counter by FloUnited's rivals on the road to RioThatcher issues defence before trial by videoPolice help Smith lay down the law at EvertonTale of Trautmann bears two more retellingsEngland on the rackPakistan retaliate with call for video of WalshCullinan continues his Cape monopoly...South Melbourne (Australia)Necaxa (Mexico)Real Madrid (Spain)Raja Casablanca (Morocco)Corinthians (Brazil)Tony's pet projectAl Nassr (Saudi Arabia)Ideal Holmes showPinochet leaves hospital after testsUseful links
32000-01-061Pilgrim knows how to progressThatcher facing banMcIlroy calls for Irish fighting spiritLeicester bin stadium blueprintUnited braced for Mexican waveAuntie back in fashion, even if the dress look...Shoaib appeal goes to the topHussain hurt by 'shambles' but lays blame on e......Putin admits Yeltsin quit to give him a head s...BBC worst hit as digital TV begins to biteHow much can you pay for...Christmas glitchesUpending a table, Chopping a line and Scoring ...Scientific evidence 'unreliable', defence claimsFusco wins judicial review in extradition caseRebels thwart Russian advanceBlair orders shake-up of failing NHSLessons of law's hard heart
42000-01-071Hitches and HorlocksBeckham off but United surviveBreast cancer screeningAlan ParkerGuardian readers: are you all whingers?Hollywood BeyondAshes and diamondsWhingers - a formidable minority...Most everywhere: UDIsMost wanted: Chloe lunettesReturn of the cane 'completely off the agenda'From Sleepy Hollow to GreenelandBlunkett outlines vision for over 11sEmbattled Dobson attacks 'play now, pay later'...Doom and the DomeWhat is the north-south divide?Aitken released from jailGone aloft
\n", 200 | "

5 rows × 27 columns

\n", 201 | "
" 202 | ], 203 | "text/plain": [ 204 | " Date Label Top1 \\\n", 205 | "0 2000-01-03 0 A 'hindrance to operations': extracts from the... \n", 206 | "1 2000-01-04 0 Scorecard \n", 207 | "2 2000-01-05 0 Coventry caught on counter by Flo \n", 208 | "3 2000-01-06 1 Pilgrim knows how to progress \n", 209 | "4 2000-01-07 1 Hitches and Horlocks \n", 210 | "\n", 211 | " Top2 \\\n", 212 | "0 Scorecard \n", 213 | "1 The best lake scene \n", 214 | "2 United's rivals on the road to Rio \n", 215 | "3 Thatcher facing ban \n", 216 | "4 Beckham off but United survive \n", 217 | "\n", 218 | " Top3 \\\n", 219 | "0 Hughes' instant hit buoys Blues \n", 220 | "1 Leader: German sleaze inquiry \n", 221 | "2 Thatcher issues defence before trial by video \n", 222 | "3 McIlroy calls for Irish fighting spirit \n", 223 | "4 Breast cancer screening \n", 224 | "\n", 225 | " Top4 \\\n", 226 | "0 Jack gets his skates on at ice-cold Alex \n", 227 | "1 Cheerio, boyo \n", 228 | "2 Police help Smith lay down the law at Everton \n", 229 | "3 Leicester bin stadium blueprint \n", 230 | "4 Alan Parker \n", 231 | "\n", 232 | " Top5 \\\n", 233 | "0 Chaos as Maracana builds up for United \n", 234 | "1 The main recommendations \n", 235 | "2 Tale of Trautmann bears two more retellings \n", 236 | "3 United braced for Mexican wave \n", 237 | "4 Guardian readers: are you all whingers? \n", 238 | "\n", 239 | " Top6 \\\n", 240 | "0 Depleted Leicester prevail as Elliott spoils E... \n", 241 | "1 Has Cubie killed fees? \n", 242 | "2 England on the rack \n", 243 | "3 Auntie back in fashion, even if the dress look... \n", 244 | "4 Hollywood Beyond \n", 245 | "\n", 246 | " Top7 \\\n", 247 | "0 Hungry Spurs sense rich pickings \n", 248 | "1 Has Cubie killed fees? \n", 249 | "2 Pakistan retaliate with call for video of Walsh \n", 250 | "3 Shoaib appeal goes to the top \n", 251 | "4 Ashes and diamonds \n", 252 | "\n", 253 | " Top8 \\\n", 254 | "0 Gunners so wide of an easy target \n", 255 | "1 Has Cubie killed fees? \n", 256 | "2 Cullinan continues his Cape monopoly \n", 257 | "3 Hussain hurt by 'shambles' but lays blame on e... \n", 258 | "4 Whingers - a formidable minority \n", 259 | "\n", 260 | " ... \\\n", 261 | "0 ... \n", 262 | "1 ... \n", 263 | "2 ... \n", 264 | "3 ... \n", 265 | "4 ... \n", 266 | "\n", 267 | " Top16 \\\n", 268 | "0 Flintoff injury piles on woe for England \n", 269 | "1 On the critical list \n", 270 | "2 South Melbourne (Australia) \n", 271 | "3 Putin admits Yeltsin quit to give him a head s... \n", 272 | "4 Most everywhere: UDIs \n", 273 | "\n", 274 | " Top17 \\\n", 275 | "0 Hunters threaten Jospin with new battle of the... \n", 276 | "1 The timing of their lives \n", 277 | "2 Necaxa (Mexico) \n", 278 | "3 BBC worst hit as digital TV begins to bite \n", 279 | "4 Most wanted: Chloe lunettes \n", 280 | "\n", 281 | " Top18 \\\n", 282 | "0 Kohl's successor drawn into scandal \n", 283 | "1 Dear doctor \n", 284 | "2 Real Madrid (Spain) \n", 285 | "3 How much can you pay for... \n", 286 | "4 Return of the cane 'completely off the agenda' \n", 287 | "\n", 288 | " Top19 \\\n", 289 | "0 The difference between men and women \n", 290 | "1 Irish court halts IRA man's extradition to Nor... \n", 291 | "2 Raja Casablanca (Morocco) \n", 292 | "3 Christmas glitches \n", 293 | "4 From Sleepy Hollow to Greeneland \n", 294 | "\n", 295 | " Top20 \\\n", 296 | "0 Sara Denver, nurse turned solicitor \n", 297 | "1 Burundi peace initiative fades after rebels re... \n", 298 | "2 Corinthians (Brazil) \n", 299 | "3 Upending a table, Chopping a line and Scoring ... \n", 300 | "4 Blunkett outlines vision for over 11s \n", 301 | "\n", 302 | " Top21 \\\n", 303 | "0 Diana's landmine crusade put Tories in a panic \n", 304 | "1 PE points the way forward to the ECB \n", 305 | "2 Tony's pet project \n", 306 | "3 Scientific evidence 'unreliable', defence claims \n", 307 | "4 Embattled Dobson attacks 'play now, pay later'... \n", 308 | "\n", 309 | " Top22 \\\n", 310 | "0 Yeltsin's resignation caught opposition flat-f... \n", 311 | "1 Campaigners keep up pressure on Nazi war crime... \n", 312 | "2 Al Nassr (Saudi Arabia) \n", 313 | "3 Fusco wins judicial review in extradition case \n", 314 | "4 Doom and the Dome \n", 315 | "\n", 316 | " Top23 \\\n", 317 | "0 Russian roulette \n", 318 | "1 Jane Ratcliffe \n", 319 | "2 Ideal Holmes show \n", 320 | "3 Rebels thwart Russian advance \n", 321 | "4 What is the north-south divide? \n", 322 | "\n", 323 | " Top24 \\\n", 324 | "0 Sold out \n", 325 | "1 Yet more things you wouldn't know without the ... \n", 326 | "2 Pinochet leaves hospital after tests \n", 327 | "3 Blair orders shake-up of failing NHS \n", 328 | "4 Aitken released from jail \n", 329 | "\n", 330 | " Top25 \n", 331 | "0 Recovering a title \n", 332 | "1 Millennium bug fails to bite \n", 333 | "2 Useful links \n", 334 | "3 Lessons of law's hard heart \n", 335 | "4 Gone aloft \n", 336 | "\n", 337 | "[5 rows x 27 columns]" 338 | ] 339 | }, 340 | "execution_count": 4, 341 | "metadata": {}, 342 | "output_type": "execute_result" 343 | } 344 | ], 345 | "source": [ 346 | "df.head()" 347 | ] 348 | }, 349 | { 350 | "cell_type": "code", 351 | "execution_count": 6, 352 | "metadata": {}, 353 | "outputs": [], 354 | "source": [ 355 | "train = df[df['Date'] < '20150101']\n", 356 | "test = df[df['Date'] > '20141231']" 357 | ] 358 | }, 359 | { 360 | "cell_type": "code", 361 | "execution_count": 7, 362 | "metadata": {}, 363 | "outputs": [ 364 | { 365 | "data": { 366 | "text/html": [ 367 | "
\n", 368 | "\n", 381 | "\n", 382 | " \n", 383 | " \n", 384 | " \n", 385 | " \n", 386 | " \n", 387 | " \n", 388 | " \n", 389 | " \n", 390 | " \n", 391 | " \n", 392 | " \n", 393 | " \n", 394 | " \n", 395 | " \n", 396 | " \n", 397 | " \n", 398 | " \n", 399 | " \n", 400 | " \n", 401 | " \n", 402 | " \n", 403 | " \n", 404 | " \n", 405 | " \n", 406 | " \n", 407 | " \n", 408 | " \n", 409 | " \n", 410 | " \n", 411 | " \n", 412 | " \n", 413 | " \n", 414 | " \n", 415 | " \n", 416 | " \n", 417 | " \n", 418 | " \n", 419 | " \n", 420 | " \n", 421 | " \n", 422 | " \n", 423 | " \n", 424 | " \n", 425 | " \n", 426 | " \n", 427 | " \n", 428 | " \n", 429 | " \n", 430 | " \n", 431 | " \n", 432 | " \n", 433 | " \n", 434 | " \n", 435 | " \n", 436 | " \n", 437 | " \n", 438 | " \n", 439 | " \n", 440 | " \n", 441 | " \n", 442 | " \n", 443 | " \n", 444 | " \n", 445 | " \n", 446 | " \n", 447 | " \n", 448 | " \n", 449 | " \n", 450 | " \n", 451 | " \n", 452 | " \n", 453 | " \n", 454 | " \n", 455 | " \n", 456 | " \n", 457 | " \n", 458 | " \n", 459 | " \n", 460 | " \n", 461 | " \n", 462 | " \n", 463 | " \n", 464 | " \n", 465 | " \n", 466 | " \n", 467 | " \n", 468 | " \n", 469 | " \n", 470 | " \n", 471 | " \n", 472 | " \n", 473 | " \n", 474 | " \n", 475 | " \n", 476 | " \n", 477 | " \n", 478 | " \n", 479 | " \n", 480 | " \n", 481 | " \n", 482 | " \n", 483 | " \n", 484 | " \n", 485 | " \n", 486 | " \n", 487 | " \n", 488 | " \n", 489 | " \n", 490 | " \n", 491 | " \n", 492 | " \n", 493 | " \n", 494 | " \n", 495 | " \n", 496 | " \n", 497 | " \n", 498 | " \n", 499 | " \n", 500 | " \n", 501 | " \n", 502 | " \n", 503 | " \n", 504 | " \n", 505 | " \n", 506 | " \n", 507 | " \n", 508 | " \n", 509 | " \n", 510 | " \n", 511 | " \n", 512 | " \n", 513 | " \n", 514 | " \n", 515 | " \n", 516 | " \n", 517 | " \n", 518 | " \n", 519 | " \n", 520 | " \n", 521 | " \n", 522 | " \n", 523 | " \n", 524 | " \n", 525 | " \n", 526 | " \n", 527 | " \n", 528 | " \n", 529 | " \n", 530 | "
0123456789...15161718192021222324
0A hindrance to operations extracts from the...ScorecardHughes instant hit buoys BluesJack gets his skates on at ice cold AlexChaos as Maracana builds up for UnitedDepleted Leicester prevail as Elliott spoils E...Hungry Spurs sense rich pickingsGunners so wide of an easy targetDerby raise a glass to Strupar s debut doubleSouthgate strikes Leeds pay the penalty...Flintoff injury piles on woe for EnglandHunters threaten Jospin with new battle of the...Kohl s successor drawn into scandalThe difference between men and womenSara Denver nurse turned solicitorDiana s landmine crusade put Tories in a panicYeltsin s resignation caught opposition flat f...Russian rouletteSold outRecovering a title
1ScorecardThe best lake sceneLeader German sleaze inquiryCheerio boyoThe main recommendationsHas Cubie killed feesHas Cubie killed feesHas Cubie killed feesHopkins furious at Foster s lack of Hannibal...Has Cubie killed fees...On the critical listThe timing of their livesDear doctorIrish court halts IRA man s extradition to Nor...Burundi peace initiative fades after rebels re...PE points the way forward to the ECBCampaigners keep up pressure on Nazi war crime...Jane RatcliffeYet more things you wouldn t know without the ...Millennium bug fails to bite
2Coventry caught on counter by FloUnited s rivals on the road to RioThatcher issues defence before trial by videoPolice help Smith lay down the law at EvertonTale of Trautmann bears two more retellingsEngland on the rackPakistan retaliate with call for video of WalshCullinan continues his Cape monopolyMcGrath puts India out of their miseryBlair Witch bandwagon rolls on...South Melbourne AustraliaNecaxa MexicoReal Madrid SpainRaja Casablanca MoroccoCorinthians BrazilTony s pet projectAl Nassr Saudi ArabiaIdeal Holmes showPinochet leaves hospital after testsUseful links
3Pilgrim knows how to progressThatcher facing banMcIlroy calls for Irish fighting spiritLeicester bin stadium blueprintUnited braced for Mexican waveAuntie back in fashion even if the dress look...Shoaib appeal goes to the topHussain hurt by shambles but lays blame on e...England s decade of disastersRevenge is sweet for jubilant Cronje...Putin admits Yeltsin quit to give him a head s...BBC worst hit as digital TV begins to biteHow much can you pay forChristmas glitchesUpending a table Chopping a line and Scoring ...Scientific evidence unreliable defence claimsFusco wins judicial review in extradition caseRebels thwart Russian advanceBlair orders shake up of failing NHSLessons of law s hard heart
4Hitches and HorlocksBeckham off but United surviveBreast cancer screeningAlan ParkerGuardian readers are you all whingersHollywood BeyondAshes and diamondsWhingers a formidable minorityAlan Parker part twoThuggery Toxins and Ties...Most everywhere UDIsMost wanted Chloe lunettesReturn of the cane completely off the agendaFrom Sleepy Hollow to GreenelandBlunkett outlines vision for over sEmbattled Dobson attacks play now pay later ...Doom and the DomeWhat is the north south divideAitken released from jailGone aloft
\n", 531 | "

5 rows × 25 columns

\n", 532 | "
" 533 | ], 534 | "text/plain": [ 535 | " 0 \\\n", 536 | "0 A hindrance to operations extracts from the... \n", 537 | "1 Scorecard \n", 538 | "2 Coventry caught on counter by Flo \n", 539 | "3 Pilgrim knows how to progress \n", 540 | "4 Hitches and Horlocks \n", 541 | "\n", 542 | " 1 \\\n", 543 | "0 Scorecard \n", 544 | "1 The best lake scene \n", 545 | "2 United s rivals on the road to Rio \n", 546 | "3 Thatcher facing ban \n", 547 | "4 Beckham off but United survive \n", 548 | "\n", 549 | " 2 \\\n", 550 | "0 Hughes instant hit buoys Blues \n", 551 | "1 Leader German sleaze inquiry \n", 552 | "2 Thatcher issues defence before trial by video \n", 553 | "3 McIlroy calls for Irish fighting spirit \n", 554 | "4 Breast cancer screening \n", 555 | "\n", 556 | " 3 \\\n", 557 | "0 Jack gets his skates on at ice cold Alex \n", 558 | "1 Cheerio boyo \n", 559 | "2 Police help Smith lay down the law at Everton \n", 560 | "3 Leicester bin stadium blueprint \n", 561 | "4 Alan Parker \n", 562 | "\n", 563 | " 4 \\\n", 564 | "0 Chaos as Maracana builds up for United \n", 565 | "1 The main recommendations \n", 566 | "2 Tale of Trautmann bears two more retellings \n", 567 | "3 United braced for Mexican wave \n", 568 | "4 Guardian readers are you all whingers \n", 569 | "\n", 570 | " 5 \\\n", 571 | "0 Depleted Leicester prevail as Elliott spoils E... \n", 572 | "1 Has Cubie killed fees \n", 573 | "2 England on the rack \n", 574 | "3 Auntie back in fashion even if the dress look... \n", 575 | "4 Hollywood Beyond \n", 576 | "\n", 577 | " 6 \\\n", 578 | "0 Hungry Spurs sense rich pickings \n", 579 | "1 Has Cubie killed fees \n", 580 | "2 Pakistan retaliate with call for video of Walsh \n", 581 | "3 Shoaib appeal goes to the top \n", 582 | "4 Ashes and diamonds \n", 583 | "\n", 584 | " 7 \\\n", 585 | "0 Gunners so wide of an easy target \n", 586 | "1 Has Cubie killed fees \n", 587 | "2 Cullinan continues his Cape monopoly \n", 588 | "3 Hussain hurt by shambles but lays blame on e... \n", 589 | "4 Whingers a formidable minority \n", 590 | "\n", 591 | " 8 \\\n", 592 | "0 Derby raise a glass to Strupar s debut double \n", 593 | "1 Hopkins furious at Foster s lack of Hannibal... \n", 594 | "2 McGrath puts India out of their misery \n", 595 | "3 England s decade of disasters \n", 596 | "4 Alan Parker part two \n", 597 | "\n", 598 | " 9 ... \\\n", 599 | "0 Southgate strikes Leeds pay the penalty ... \n", 600 | "1 Has Cubie killed fees ... \n", 601 | "2 Blair Witch bandwagon rolls on ... \n", 602 | "3 Revenge is sweet for jubilant Cronje ... \n", 603 | "4 Thuggery Toxins and Ties ... \n", 604 | "\n", 605 | " 15 \\\n", 606 | "0 Flintoff injury piles on woe for England \n", 607 | "1 On the critical list \n", 608 | "2 South Melbourne Australia \n", 609 | "3 Putin admits Yeltsin quit to give him a head s... \n", 610 | "4 Most everywhere UDIs \n", 611 | "\n", 612 | " 16 \\\n", 613 | "0 Hunters threaten Jospin with new battle of the... \n", 614 | "1 The timing of their lives \n", 615 | "2 Necaxa Mexico \n", 616 | "3 BBC worst hit as digital TV begins to bite \n", 617 | "4 Most wanted Chloe lunettes \n", 618 | "\n", 619 | " 17 \\\n", 620 | "0 Kohl s successor drawn into scandal \n", 621 | "1 Dear doctor \n", 622 | "2 Real Madrid Spain \n", 623 | "3 How much can you pay for \n", 624 | "4 Return of the cane completely off the agenda \n", 625 | "\n", 626 | " 18 \\\n", 627 | "0 The difference between men and women \n", 628 | "1 Irish court halts IRA man s extradition to Nor... \n", 629 | "2 Raja Casablanca Morocco \n", 630 | "3 Christmas glitches \n", 631 | "4 From Sleepy Hollow to Greeneland \n", 632 | "\n", 633 | " 19 \\\n", 634 | "0 Sara Denver nurse turned solicitor \n", 635 | "1 Burundi peace initiative fades after rebels re... \n", 636 | "2 Corinthians Brazil \n", 637 | "3 Upending a table Chopping a line and Scoring ... \n", 638 | "4 Blunkett outlines vision for over s \n", 639 | "\n", 640 | " 20 \\\n", 641 | "0 Diana s landmine crusade put Tories in a panic \n", 642 | "1 PE points the way forward to the ECB \n", 643 | "2 Tony s pet project \n", 644 | "3 Scientific evidence unreliable defence claims \n", 645 | "4 Embattled Dobson attacks play now pay later ... \n", 646 | "\n", 647 | " 21 \\\n", 648 | "0 Yeltsin s resignation caught opposition flat f... \n", 649 | "1 Campaigners keep up pressure on Nazi war crime... \n", 650 | "2 Al Nassr Saudi Arabia \n", 651 | "3 Fusco wins judicial review in extradition case \n", 652 | "4 Doom and the Dome \n", 653 | "\n", 654 | " 22 \\\n", 655 | "0 Russian roulette \n", 656 | "1 Jane Ratcliffe \n", 657 | "2 Ideal Holmes show \n", 658 | "3 Rebels thwart Russian advance \n", 659 | "4 What is the north south divide \n", 660 | "\n", 661 | " 23 \\\n", 662 | "0 Sold out \n", 663 | "1 Yet more things you wouldn t know without the ... \n", 664 | "2 Pinochet leaves hospital after tests \n", 665 | "3 Blair orders shake up of failing NHS \n", 666 | "4 Aitken released from jail \n", 667 | "\n", 668 | " 24 \n", 669 | "0 Recovering a title \n", 670 | "1 Millennium bug fails to bite \n", 671 | "2 Useful links \n", 672 | "3 Lessons of law s hard heart \n", 673 | "4 Gone aloft \n", 674 | "\n", 675 | "[5 rows x 25 columns]" 676 | ] 677 | }, 678 | "execution_count": 7, 679 | "metadata": {}, 680 | "output_type": "execute_result" 681 | } 682 | ], 683 | "source": [ 684 | "# Removing punctuations\n", 685 | "data=train.iloc[:,2:27]\n", 686 | "data.replace(\"[^a-zA-Z]\",\" \",regex=True, inplace=True)\n", 687 | "\n", 688 | "# Renaming column names for ease of access\n", 689 | "list1= [i for i in range(25)]\n", 690 | "new_Index=[str(i) for i in list1]\n", 691 | "data.columns= new_Index\n", 692 | "data.head(5)\n", 693 | "\n" 694 | ] 695 | }, 696 | { 697 | "cell_type": "code", 698 | "execution_count": 8, 699 | "metadata": {}, 700 | "outputs": [ 701 | { 702 | "data": { 703 | "text/html": [ 704 | "
\n", 705 | "\n", 718 | "\n", 719 | " \n", 720 | " \n", 721 | " \n", 722 | " \n", 723 | " \n", 724 | " \n", 725 | " \n", 726 | " \n", 727 | " \n", 728 | " \n", 729 | " \n", 730 | " \n", 731 | " \n", 732 | " \n", 733 | " \n", 734 | " \n", 735 | " \n", 736 | " \n", 737 | " \n", 738 | " \n", 739 | " \n", 740 | " \n", 741 | " \n", 742 | " \n", 743 | " \n", 744 | " \n", 745 | " \n", 746 | " \n", 747 | " \n", 748 | " \n", 749 | " \n", 750 | " \n", 751 | " \n", 752 | " \n", 753 | " \n", 754 | " \n", 755 | " \n", 756 | " \n", 757 | " \n", 758 | " \n", 759 | " \n", 760 | " \n", 761 | " \n", 762 | " \n", 763 | " \n", 764 | " \n", 765 | " \n", 766 | " \n", 767 | " \n", 768 | " \n", 769 | " \n", 770 | " \n", 771 | "
0123456789...15161718192021222324
0a hindrance to operations extracts from the...scorecardhughes instant hit buoys bluesjack gets his skates on at ice cold alexchaos as maracana builds up for uniteddepleted leicester prevail as elliott spoils e...hungry spurs sense rich pickingsgunners so wide of an easy targetderby raise a glass to strupar s debut doublesouthgate strikes leeds pay the penalty...flintoff injury piles on woe for englandhunters threaten jospin with new battle of the...kohl s successor drawn into scandalthe difference between men and womensara denver nurse turned solicitordiana s landmine crusade put tories in a panicyeltsin s resignation caught opposition flat f...russian roulettesold outrecovering a title
\n", 772 | "

1 rows × 25 columns

\n", 773 | "
" 774 | ], 775 | "text/plain": [ 776 | " 0 1 \\\n", 777 | "0 a hindrance to operations extracts from the... scorecard \n", 778 | "\n", 779 | " 2 3 \\\n", 780 | "0 hughes instant hit buoys blues jack gets his skates on at ice cold alex \n", 781 | "\n", 782 | " 4 \\\n", 783 | "0 chaos as maracana builds up for united \n", 784 | "\n", 785 | " 5 \\\n", 786 | "0 depleted leicester prevail as elliott spoils e... \n", 787 | "\n", 788 | " 6 7 \\\n", 789 | "0 hungry spurs sense rich pickings gunners so wide of an easy target \n", 790 | "\n", 791 | " 8 \\\n", 792 | "0 derby raise a glass to strupar s debut double \n", 793 | "\n", 794 | " 9 ... \\\n", 795 | "0 southgate strikes leeds pay the penalty ... \n", 796 | "\n", 797 | " 15 \\\n", 798 | "0 flintoff injury piles on woe for england \n", 799 | "\n", 800 | " 16 \\\n", 801 | "0 hunters threaten jospin with new battle of the... \n", 802 | "\n", 803 | " 17 18 \\\n", 804 | "0 kohl s successor drawn into scandal the difference between men and women \n", 805 | "\n", 806 | " 19 \\\n", 807 | "0 sara denver nurse turned solicitor \n", 808 | "\n", 809 | " 20 \\\n", 810 | "0 diana s landmine crusade put tories in a panic \n", 811 | "\n", 812 | " 21 22 \\\n", 813 | "0 yeltsin s resignation caught opposition flat f... russian roulette \n", 814 | "\n", 815 | " 23 24 \n", 816 | "0 sold out recovering a title \n", 817 | "\n", 818 | "[1 rows x 25 columns]" 819 | ] 820 | }, 821 | "execution_count": 8, 822 | "metadata": {}, 823 | "output_type": "execute_result" 824 | } 825 | ], 826 | "source": [ 827 | "# Convertng headlines to lower case\n", 828 | "for index in new_Index:\n", 829 | " data[index]=data[index].str.lower()\n", 830 | "data.head(1)" 831 | ] 832 | }, 833 | { 834 | "cell_type": "code", 835 | "execution_count": 14, 836 | "metadata": {}, 837 | "outputs": [ 838 | { 839 | "data": { 840 | "text/plain": [ 841 | "'scorecard the best lake scene leader german sleaze inquiry cheerio boyo the main recommendations has cubie killed fees has cubie killed fees has cubie killed fees hopkins furious at foster s lack of hannibal appetite has cubie killed fees a tale of two tails i say what i like and i like what i say elbows eyes and nipples task force to assess risk of asteroid collision how i found myself at last on the critical list the timing of their lives dear doctor irish court halts ira man s extradition to northern ireland burundi peace initiative fades after rebels reject mandela as mediator pe points the way forward to the ecb campaigners keep up pressure on nazi war crimes suspect jane ratcliffe yet more things you wouldn t know without the movies millennium bug fails to bite'" 842 | ] 843 | }, 844 | "execution_count": 14, 845 | "metadata": {}, 846 | "output_type": "execute_result" 847 | } 848 | ], 849 | "source": [ 850 | "' '.join(str(x) for x in data.iloc[1,0:25])" 851 | ] 852 | }, 853 | { 854 | "cell_type": "code", 855 | "execution_count": 15, 856 | "metadata": {}, 857 | "outputs": [], 858 | "source": [ 859 | "headlines = []\n", 860 | "for row in range(0,len(data.index)):\n", 861 | " headlines.append(' '.join(str(x) for x in data.iloc[row,0:25]))" 862 | ] 863 | }, 864 | { 865 | "cell_type": "code", 866 | "execution_count": 41, 867 | "metadata": {}, 868 | "outputs": [ 869 | { 870 | "data": { 871 | "text/plain": [ 872 | "'a hindrance to operations extracts from the leaked reports scorecard hughes instant hit buoys blues jack gets his skates on at ice cold alex chaos as maracana builds up for united depleted leicester prevail as elliott spoils everton s party hungry spurs sense rich pickings gunners so wide of an easy target derby raise a glass to strupar s debut double southgate strikes leeds pay the penalty hammers hand robson a youthful lesson saints party like it s wear wolves have turned into lambs stump mike catches testy gough s taunt langer escapes to hit flintoff injury piles on woe for england hunters threaten jospin with new battle of the somme kohl s successor drawn into scandal the difference between men and women sara denver nurse turned solicitor diana s landmine crusade put tories in a panic yeltsin s resignation caught opposition flat footed russian roulette sold out recovering a title'" 873 | ] 874 | }, 875 | "execution_count": 41, 876 | "metadata": {}, 877 | "output_type": "execute_result" 878 | } 879 | ], 880 | "source": [ 881 | "headlines[0]" 882 | ] 883 | }, 884 | { 885 | "cell_type": "code", 886 | "execution_count": 17, 887 | "metadata": {}, 888 | "outputs": [], 889 | "source": [ 890 | "from sklearn.feature_extraction.text import CountVectorizer\n", 891 | "from sklearn.ensemble import RandomForestClassifier" 892 | ] 893 | }, 894 | { 895 | "cell_type": "code", 896 | "execution_count": 33, 897 | "metadata": {}, 898 | "outputs": [], 899 | "source": [ 900 | "## implement BAG OF WORDS\n", 901 | "countvector=CountVectorizer(ngram_range=(2,2))\n", 902 | "traindataset=countvector.fit_transform(headlines)" 903 | ] 904 | }, 905 | { 906 | "cell_type": "code", 907 | "execution_count": 34, 908 | "metadata": {}, 909 | "outputs": [ 910 | { 911 | "data": { 912 | "text/plain": [ 913 | "RandomForestClassifier(bootstrap=True, class_weight=None, criterion='entropy',\n", 914 | " max_depth=None, max_features='auto', max_leaf_nodes=None,\n", 915 | " min_impurity_decrease=0.0, min_impurity_split=None,\n", 916 | " min_samples_leaf=1, min_samples_split=2,\n", 917 | " min_weight_fraction_leaf=0.0, n_estimators=200, n_jobs=None,\n", 918 | " oob_score=False, random_state=None, verbose=0,\n", 919 | " warm_start=False)" 920 | ] 921 | }, 922 | "execution_count": 34, 923 | "metadata": {}, 924 | "output_type": "execute_result" 925 | } 926 | ], 927 | "source": [ 928 | "# implement RandomForest Classifier\n", 929 | "randomclassifier=RandomForestClassifier(n_estimators=200,criterion='entropy')\n", 930 | "randomclassifier.fit(traindataset,train['Label'])" 931 | ] 932 | }, 933 | { 934 | "cell_type": "code", 935 | "execution_count": 35, 936 | "metadata": {}, 937 | "outputs": [], 938 | "source": [ 939 | "## Predict for the Test Dataset\n", 940 | "test_transform= []\n", 941 | "for row in range(0,len(test.index)):\n", 942 | " test_transform.append(' '.join(str(x) for x in test.iloc[row,2:27]))\n", 943 | "test_dataset = countvector.transform(test_transform)\n", 944 | "predictions = randomclassifier.predict(test_dataset)" 945 | ] 946 | }, 947 | { 948 | "cell_type": "code", 949 | "execution_count": 36, 950 | "metadata": {}, 951 | "outputs": [], 952 | "source": [ 953 | "## Import library to check accuracy\n", 954 | "from sklearn.metrics import classification_report,confusion_matrix,accuracy_score" 955 | ] 956 | }, 957 | { 958 | "cell_type": "code", 959 | "execution_count": 37, 960 | "metadata": {}, 961 | "outputs": [ 962 | { 963 | "name": "stdout", 964 | "output_type": "stream", 965 | "text": [ 966 | "[[139 47]\n", 967 | " [ 13 179]]\n", 968 | "0.8412698412698413\n", 969 | " precision recall f1-score support\n", 970 | "\n", 971 | " 0 0.91 0.75 0.82 186\n", 972 | " 1 0.79 0.93 0.86 192\n", 973 | "\n", 974 | " micro avg 0.84 0.84 0.84 378\n", 975 | " macro avg 0.85 0.84 0.84 378\n", 976 | "weighted avg 0.85 0.84 0.84 378\n", 977 | "\n" 978 | ] 979 | } 980 | ], 981 | "source": [ 982 | "matrix=confusion_matrix(test['Label'],predictions)\n", 983 | "print(matrix)\n", 984 | "score=accuracy_score(test['Label'],predictions)\n", 985 | "print(score)\n", 986 | "report=classification_report(test['Label'],predictions)\n", 987 | "print(report)" 988 | ] 989 | }, 990 | { 991 | "cell_type": "code", 992 | "execution_count": null, 993 | "metadata": {}, 994 | "outputs": [], 995 | "source": [] 996 | } 997 | ], 998 | "metadata": { 999 | "kernelspec": { 1000 | "display_name": "Python 3", 1001 | "language": "python", 1002 | "name": "python3" 1003 | }, 1004 | "language_info": { 1005 | "codemirror_mode": { 1006 | "name": "ipython", 1007 | "version": 3 1008 | }, 1009 | "file_extension": ".py", 1010 | "mimetype": "text/x-python", 1011 | "name": "python", 1012 | "nbconvert_exporter": "python", 1013 | "pygments_lexer": "ipython3", 1014 | "version": "3.6.7" 1015 | } 1016 | }, 1017 | "nbformat": 4, 1018 | "nbformat_minor": 2 1019 | } 1020 | --------------------------------------------------------------------------------