Spaces:
Runtime error
Runtime error
File size: 41,201 Bytes
d83e0b5 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 | {
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"id": "407898b3",
"metadata": {},
"outputs": [],
"source": [
"import pandas as pd\n",
"books=pd.read_csv('books_with_categories.csv')"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "5590fdf2",
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"c:\\Users\\KRISH\\miniconda3\\Lib\\site-packages\\tqdm\\auto.py:21: TqdmWarning: IProgress not found. Please update jupyter and ipywidgets. See https://ipywidgets.readthedocs.io/en/stable/user_install.html\n",
" from .autonotebook import tqdm as notebook_tqdm\n",
"Device set to use cuda:0\n"
]
},
{
"data": {
"text/plain": [
"[[{'label': 'joy', 'score': 0.9778217077255249},\n",
" {'label': 'anger', 'score': 0.00690877391025424},\n",
" {'label': 'neutral', 'score': 0.004429477732628584},\n",
" {'label': 'sadness', 'score': 0.0038780963514000177},\n",
" {'label': 'fear', 'score': 0.0034606074914336205},\n",
" {'label': 'disgust', 'score': 0.002587498864158988},\n",
" {'label': 'surprise', 'score': 0.0009138151071965694}]]"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Use a pipeline as a high-level helper\n",
"from transformers import pipeline\n",
"\n",
"classifier = pipeline(\"text-classification\", model=\"j-hartmann/emotion-english-distilroberta-base\",top_k=None,device=0)\n",
"classifier(\"I love this \")"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "7435bb0a",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'A NOVEL THAT READERS and critics have been eagerly anticipating for over a decade, Gilead is an astonishingly imagined story of remarkable lives. John Ames is a preacher, the son of a preacher and the grandson (both maternal and paternal) of preachers. It’s 1956 in Gilead, Iowa, towards the end of the Reverend Ames’s life, and he is absorbed in recording his family’s story, a legacy for the young son he will never see grow up. Haunted by his grandfather’s presence, John tells of the rift between his grandfather and his father: the elder, an angry visionary who fought for the abolitionist cause, and his son, an ardent pacifist. He is troubled, too, by his prodigal namesake, Jack (John Ames) Boughton, his best friend’s lost son who returns to Gilead searching for forgiveness and redemption. Told in John Ames’s joyous, rambling voice that finds beauty, humour and truth in the smallest of life’s details, Gilead is a song of celebration and acceptance of the best and the worst the world has to offer. At its heart is a tale of the sacred bonds between fathers and sons, pitch-perfect in style and story, set to dazzle critics and readers alike.'"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"books[\"description\"][0]"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "833fe654",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[[{'label': 'fear', 'score': 0.654841423034668},\n",
" {'label': 'neutral', 'score': 0.16985194385051727},\n",
" {'label': 'sadness', 'score': 0.11640876531600952},\n",
" {'label': 'surprise', 'score': 0.02070068195462227},\n",
" {'label': 'disgust', 'score': 0.019100716337561607},\n",
" {'label': 'joy', 'score': 0.0151612414047122},\n",
" {'label': 'anger', 'score': 0.003935152664780617}]]"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"classifier(books[\"description\"][0])"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "39ac9c2b",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[[{'label': 'surprise', 'score': 0.7296032905578613},\n",
" {'label': 'neutral', 'score': 0.14038528501987457},\n",
" {'label': 'fear', 'score': 0.06816212832927704},\n",
" {'label': 'joy', 'score': 0.04794234782457352},\n",
" {'label': 'anger', 'score': 0.00915635097771883},\n",
" {'label': 'disgust', 'score': 0.002628469606861472},\n",
" {'label': 'sadness', 'score': 0.002122158883139491}],\n",
" [{'label': 'neutral', 'score': 0.449370801448822},\n",
" {'label': 'disgust', 'score': 0.27359163761138916},\n",
" {'label': 'joy', 'score': 0.10908281058073044},\n",
" {'label': 'sadness', 'score': 0.0936271920800209},\n",
" {'label': 'anger', 'score': 0.04047831892967224},\n",
" {'label': 'surprise', 'score': 0.02697017416357994},\n",
" {'label': 'fear', 'score': 0.006879049353301525}],\n",
" [{'label': 'neutral', 'score': 0.6462163925170898},\n",
" {'label': 'sadness', 'score': 0.24273289740085602},\n",
" {'label': 'disgust', 'score': 0.043422624468803406},\n",
" {'label': 'surprise', 'score': 0.028300542384386063},\n",
" {'label': 'joy', 'score': 0.014211481437087059},\n",
" {'label': 'fear', 'score': 0.014084117487072945},\n",
" {'label': 'anger', 'score': 0.011031893081963062}],\n",
" [{'label': 'fear', 'score': 0.928167998790741},\n",
" {'label': 'anger', 'score': 0.03219102695584297},\n",
" {'label': 'neutral', 'score': 0.012808704748749733},\n",
" {'label': 'sadness', 'score': 0.008756889030337334},\n",
" {'label': 'surprise', 'score': 0.008597930893301964},\n",
" {'label': 'disgust', 'score': 0.008431846275925636},\n",
" {'label': 'joy', 'score': 0.0010455820010975003}],\n",
" [{'label': 'sadness', 'score': 0.9671574234962463},\n",
" {'label': 'neutral', 'score': 0.015104176476597786},\n",
" {'label': 'disgust', 'score': 0.0064806039445102215},\n",
" {'label': 'fear', 'score': 0.005394001957029104},\n",
" {'label': 'surprise', 'score': 0.0022869459353387356},\n",
" {'label': 'anger', 'score': 0.0018428926123306155},\n",
" {'label': 'joy', 'score': 0.0017338803736492991}],\n",
" [{'label': 'joy', 'score': 0.9327973127365112},\n",
" {'label': 'disgust', 'score': 0.03771765157580376},\n",
" {'label': 'neutral', 'score': 0.015891950577497482},\n",
" {'label': 'sadness', 'score': 0.006444534286856651},\n",
" {'label': 'anger', 'score': 0.005025016609579325},\n",
" {'label': 'surprise', 'score': 0.0015812088968232274},\n",
" {'label': 'fear', 'score': 0.0005423093680292368}],\n",
" [{'label': 'joy', 'score': 0.6528706550598145},\n",
" {'label': 'neutral', 'score': 0.2542746365070343},\n",
" {'label': 'surprise', 'score': 0.06808312982320786},\n",
" {'label': 'sadness', 'score': 0.009908992797136307},\n",
" {'label': 'disgust', 'score': 0.006512203253805637},\n",
" {'label': 'anger', 'score': 0.004821316804736853},\n",
" {'label': 'fear', 'score': 0.003529016859829426}],\n",
" [{'label': 'neutral', 'score': 0.549476683139801},\n",
" {'label': 'sadness', 'score': 0.11169017851352692},\n",
" {'label': 'disgust', 'score': 0.10400667041540146},\n",
" {'label': 'surprise', 'score': 0.07876553386449814},\n",
" {'label': 'anger', 'score': 0.06413363665342331},\n",
" {'label': 'fear', 'score': 0.051362816244363785},\n",
" {'label': 'joy', 'score': 0.04056441783905029}]]"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"classifier(books[\"description\"][0].split(\".\"))"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "67fbf665",
"metadata": {},
"outputs": [],
"source": [
"sentences = books[\"description\"][0].split(\".\")\n",
"predictions=classifier(sentences)"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "c622277d",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'A NOVEL THAT READERS and critics have been eagerly anticipating for over a decade, Gilead is an astonishingly imagined story of remarkable lives'"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"sentences[0]"
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "11a6fb6d",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[{'label': 'fear', 'score': 0.928167998790741},\n",
" {'label': 'anger', 'score': 0.03219102695584297},\n",
" {'label': 'neutral', 'score': 0.012808704748749733},\n",
" {'label': 'sadness', 'score': 0.008756889030337334},\n",
" {'label': 'surprise', 'score': 0.008597930893301964},\n",
" {'label': 'disgust', 'score': 0.008431846275925636},\n",
" {'label': 'joy', 'score': 0.0010455820010975003}]"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"predictions[3]"
]
},
{
"cell_type": "code",
"execution_count": 10,
"id": "03e4b938",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[{'label': 'anger', 'score': 0.00915635097771883},\n",
" {'label': 'disgust', 'score': 0.002628469606861472},\n",
" {'label': 'fear', 'score': 0.06816212832927704},\n",
" {'label': 'joy', 'score': 0.04794234782457352},\n",
" {'label': 'neutral', 'score': 0.14038528501987457},\n",
" {'label': 'sadness', 'score': 0.002122158883139491},\n",
" {'label': 'surprise', 'score': 0.7296032905578613}]"
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"sorted(predictions[0],key=lambda x:x[\"label\"])"
]
},
{
"cell_type": "code",
"execution_count": 11,
"id": "494af118",
"metadata": {},
"outputs": [],
"source": [
"import numpy as np\n",
"emotion_labels= [x[\"label\"] for x in predictions[0]]\n",
"emotion_scores = {label:[] for label in emotion_labels}\n",
"isbn=[]\n",
"\n",
"def calculate_max_emotion_scores(predictions):\n",
" per_emotion_scores={label:[] for label in emotion_labels}\n",
" for prediction in predictions:\n",
" sorted_predictions=sorted(prediction,key = lambda x:x[\"label\"])\n",
" for index,label in enumerate(emotion_labels):\n",
" per_emotion_scores[label].append(sorted_predictions[index][\"score\"])\n",
"\n",
" return {label:np.max(scores) for label,scores in per_emotion_scores.items()}"
]
},
{
"cell_type": "code",
"execution_count": 12,
"id": "50eabfd2",
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"You seem to be using the pipelines sequentially on GPU. In order to maximize efficiency please use a dataset\n"
]
}
],
"source": [
"for i in range(10):\n",
" isbn.append(books[\"isbn13\"][i])\n",
" sentences=books[\"description\"][i].split(\".\")\n",
" predictions=classifier(sentences)\n",
" max_scores= calculate_max_emotion_scores(predictions)\n",
" for label in emotion_labels:\n",
" emotion_scores[label].append(max_scores[label])\n",
" "
]
},
{
"cell_type": "code",
"execution_count": 13,
"id": "35410108",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'surprise': [0.00915635097771883,\n",
" 0.005966869182884693,\n",
" 0.041300997138023376,\n",
" 0.016036199405789375,\n",
" 0.01362438965588808,\n",
" 0.009158486500382423,\n",
" 0.003973892889916897,\n",
" 0.023656249046325684,\n",
" 0.3006698787212372,\n",
" 0.01765839010477066],\n",
" 'neutral': [0.002628469606861472,\n",
" 0.002887075301259756,\n",
" 0.024568455293774605,\n",
" 0.06069517880678177,\n",
" 0.12224282324314117,\n",
" 0.012228667736053467,\n",
" 0.0038164728321135044,\n",
" 0.009091983549296856,\n",
" 0.2794811427593231,\n",
" 0.1779269576072693],\n",
" 'fear': [0.06816212832927704,\n",
" 0.0038098874501883984,\n",
" 0.1040615662932396,\n",
" 0.001691634999588132,\n",
" 0.09504348784685135,\n",
" 0.03679506108164787,\n",
" 0.0012676806654781103,\n",
" 0.4044956862926483,\n",
" 0.08446498215198517,\n",
" 0.04945705831050873],\n",
" 'joy': [0.04794234782457352,\n",
" 0.7044203877449036,\n",
" 0.7672369480133057,\n",
" 0.1617567539215088,\n",
" 0.0083356574177742,\n",
" 0.043375857174396515,\n",
" 0.8725652098655701,\n",
" 0.01337516214698553,\n",
" 0.0013904988300055265,\n",
" 0.032197605818510056],\n",
" 'anger': [0.14038528501987457,\n",
" 0.21776098012924194,\n",
" 0.042176082730293274,\n",
" 0.7326855063438416,\n",
" 0.27261340618133545,\n",
" 0.6213923692703247,\n",
" 0.07678427547216415,\n",
" 0.29216688871383667,\n",
" 0.029392102733254433,\n",
" 0.662406325340271],\n",
" 'disgust': [0.002122158883139491,\n",
" 0.004508530721068382,\n",
" 0.010859863832592964,\n",
" 0.020988158881664276,\n",
" 0.4758804738521576,\n",
" 0.0051463996060192585,\n",
" 0.004653005860745907,\n",
" 0.022726479917764664,\n",
" 0.2327764630317688,\n",
" 0.013540088199079037],\n",
" 'sadness': [0.7296032905578613,\n",
" 0.060646187514066696,\n",
" 0.009796091355383396,\n",
" 0.006146553438156843,\n",
" 0.012259832583367825,\n",
" 0.2719031274318695,\n",
" 0.036939460784196854,\n",
" 0.23448744416236877,\n",
" 0.07182495296001434,\n",
" 0.04681351035833359]}"
]
},
"execution_count": 13,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"emotion_scores"
]
},
{
"cell_type": "code",
"execution_count": 15,
"id": "c99e6dec",
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"100%|██████████| 5197/5197 [06:10<00:00, 14.02it/s] \n"
]
}
],
"source": [
"from tqdm import tqdm\n",
"emotion_labels= [x[\"label\"] for x in predictions[0]]\n",
"emotion_scores = {label:[] for label in emotion_labels}\n",
"isbn=[]\n",
"for i in tqdm(range(len(books))):\n",
" isbn.append(books[\"isbn13\"][i])\n",
" sentences=books[\"description\"][i].split(\".\")\n",
" predictions=classifier(sentences)\n",
" max_scores= calculate_max_emotion_scores(predictions)\n",
" for label in emotion_labels:\n",
" emotion_scores[label].append(max_scores[label])\n",
" "
]
},
{
"cell_type": "code",
"execution_count": 16,
"id": "da04acb1",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>fear</th>\n",
" <th>anger</th>\n",
" <th>sadness</th>\n",
" <th>neutral</th>\n",
" <th>disgust</th>\n",
" <th>joy</th>\n",
" <th>surprise</th>\n",
" <th>isbn13</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>0.009156</td>\n",
" <td>0.002628</td>\n",
" <td>0.068162</td>\n",
" <td>0.047942</td>\n",
" <td>0.140385</td>\n",
" <td>0.002122</td>\n",
" <td>0.729603</td>\n",
" <td>9780002005883</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>0.005967</td>\n",
" <td>0.002887</td>\n",
" <td>0.003810</td>\n",
" <td>0.704420</td>\n",
" <td>0.217761</td>\n",
" <td>0.004509</td>\n",
" <td>0.060646</td>\n",
" <td>9780002261982</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>0.041301</td>\n",
" <td>0.024568</td>\n",
" <td>0.104062</td>\n",
" <td>0.767237</td>\n",
" <td>0.042176</td>\n",
" <td>0.010860</td>\n",
" <td>0.009796</td>\n",
" <td>9780006178736</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>0.016036</td>\n",
" <td>0.060695</td>\n",
" <td>0.001692</td>\n",
" <td>0.161757</td>\n",
" <td>0.732686</td>\n",
" <td>0.020988</td>\n",
" <td>0.006147</td>\n",
" <td>9780006280897</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>0.013624</td>\n",
" <td>0.122243</td>\n",
" <td>0.095043</td>\n",
" <td>0.008336</td>\n",
" <td>0.272613</td>\n",
" <td>0.475880</td>\n",
" <td>0.012260</td>\n",
" <td>9780006280934</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" fear anger sadness neutral disgust joy surprise \\\n",
"0 0.009156 0.002628 0.068162 0.047942 0.140385 0.002122 0.729603 \n",
"1 0.005967 0.002887 0.003810 0.704420 0.217761 0.004509 0.060646 \n",
"2 0.041301 0.024568 0.104062 0.767237 0.042176 0.010860 0.009796 \n",
"3 0.016036 0.060695 0.001692 0.161757 0.732686 0.020988 0.006147 \n",
"4 0.013624 0.122243 0.095043 0.008336 0.272613 0.475880 0.012260 \n",
"\n",
" isbn13 \n",
"0 9780002005883 \n",
"1 9780002261982 \n",
"2 9780006178736 \n",
"3 9780006280897 \n",
"4 9780006280934 "
]
},
"execution_count": 16,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"emotions_df=pd.DataFrame(emotion_scores)\n",
"emotions_df[\"isbn13\"]=isbn\n",
"emotions_df.head()"
]
},
{
"cell_type": "code",
"execution_count": 17,
"id": "5f693d61",
"metadata": {},
"outputs": [],
"source": [
"books=pd.merge(books,emotions_df,on=\"isbn13\")"
]
},
{
"cell_type": "code",
"execution_count": 18,
"id": "ee09e5e5",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>isbn13</th>\n",
" <th>isbn10</th>\n",
" <th>title</th>\n",
" <th>authors</th>\n",
" <th>categories</th>\n",
" <th>thumbnail</th>\n",
" <th>description</th>\n",
" <th>published_year</th>\n",
" <th>average_rating</th>\n",
" <th>num_pages</th>\n",
" <th>...</th>\n",
" <th>title_and_subtitle</th>\n",
" <th>tagged_description</th>\n",
" <th>simple_categories</th>\n",
" <th>fear</th>\n",
" <th>anger</th>\n",
" <th>sadness</th>\n",
" <th>neutral</th>\n",
" <th>disgust</th>\n",
" <th>joy</th>\n",
" <th>surprise</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>9780002005883</td>\n",
" <td>0002005883</td>\n",
" <td>Gilead</td>\n",
" <td>Marilynne Robinson</td>\n",
" <td>Fiction</td>\n",
" <td>http://books.google.com/books/content?id=KQZCP...</td>\n",
" <td>A NOVEL THAT READERS and critics have been eag...</td>\n",
" <td>2004.0</td>\n",
" <td>3.85</td>\n",
" <td>247.0</td>\n",
" <td>...</td>\n",
" <td>Gilead</td>\n",
" <td>9780002005883 A NOVEL THAT READERS and critics...</td>\n",
" <td>Fiction</td>\n",
" <td>0.009156</td>\n",
" <td>0.002628</td>\n",
" <td>0.068162</td>\n",
" <td>0.047942</td>\n",
" <td>0.140385</td>\n",
" <td>0.002122</td>\n",
" <td>0.729603</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>9780002261982</td>\n",
" <td>0002261987</td>\n",
" <td>Spider's Web</td>\n",
" <td>Charles Osborne;Agatha Christie</td>\n",
" <td>Detective and mystery stories</td>\n",
" <td>http://books.google.com/books/content?id=gA5GP...</td>\n",
" <td>A new 'Christie for Christmas' -- a full-lengt...</td>\n",
" <td>2000.0</td>\n",
" <td>3.83</td>\n",
" <td>241.0</td>\n",
" <td>...</td>\n",
" <td>Spider's Web: A Novel</td>\n",
" <td>9780002261982 A new 'Christie for Christmas' -...</td>\n",
" <td>Fiction</td>\n",
" <td>0.005967</td>\n",
" <td>0.002887</td>\n",
" <td>0.003810</td>\n",
" <td>0.704420</td>\n",
" <td>0.217761</td>\n",
" <td>0.004509</td>\n",
" <td>0.060646</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>9780006178736</td>\n",
" <td>0006178731</td>\n",
" <td>Rage of angels</td>\n",
" <td>Sidney Sheldon</td>\n",
" <td>Fiction</td>\n",
" <td>http://books.google.com/books/content?id=FKo2T...</td>\n",
" <td>A memorable, mesmerizing heroine Jennifer -- b...</td>\n",
" <td>1993.0</td>\n",
" <td>3.93</td>\n",
" <td>512.0</td>\n",
" <td>...</td>\n",
" <td>Rage of angels</td>\n",
" <td>9780006178736 A memorable, mesmerizing heroine...</td>\n",
" <td>Fiction</td>\n",
" <td>0.041301</td>\n",
" <td>0.024568</td>\n",
" <td>0.104062</td>\n",
" <td>0.767237</td>\n",
" <td>0.042176</td>\n",
" <td>0.010860</td>\n",
" <td>0.009796</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>9780006280897</td>\n",
" <td>0006280897</td>\n",
" <td>The Four Loves</td>\n",
" <td>Clive Staples Lewis</td>\n",
" <td>Christian life</td>\n",
" <td>http://books.google.com/books/content?id=XhQ5X...</td>\n",
" <td>Lewis' work on the nature of love divides love...</td>\n",
" <td>2002.0</td>\n",
" <td>4.15</td>\n",
" <td>170.0</td>\n",
" <td>...</td>\n",
" <td>The Four Loves</td>\n",
" <td>9780006280897 Lewis' work on the nature of lov...</td>\n",
" <td>Nonfiction</td>\n",
" <td>0.016036</td>\n",
" <td>0.060695</td>\n",
" <td>0.001692</td>\n",
" <td>0.161757</td>\n",
" <td>0.732686</td>\n",
" <td>0.020988</td>\n",
" <td>0.006147</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>9780006280934</td>\n",
" <td>0006280935</td>\n",
" <td>The Problem of Pain</td>\n",
" <td>Clive Staples Lewis</td>\n",
" <td>Christian life</td>\n",
" <td>http://books.google.com/books/content?id=Kk-uV...</td>\n",
" <td>\"In The Problem of Pain, C.S. Lewis, one of th...</td>\n",
" <td>2002.0</td>\n",
" <td>4.09</td>\n",
" <td>176.0</td>\n",
" <td>...</td>\n",
" <td>The Problem of Pain</td>\n",
" <td>9780006280934 \"In The Problem of Pain, C.S. Le...</td>\n",
" <td>Nonfiction</td>\n",
" <td>0.013624</td>\n",
" <td>0.122243</td>\n",
" <td>0.095043</td>\n",
" <td>0.008336</td>\n",
" <td>0.272613</td>\n",
" <td>0.475880</td>\n",
" <td>0.012260</td>\n",
" </tr>\n",
" <tr>\n",
" <th>...</th>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>5192</th>\n",
" <td>9788172235222</td>\n",
" <td>8172235224</td>\n",
" <td>Mistaken Identity</td>\n",
" <td>Nayantara Sahgal</td>\n",
" <td>Indic fiction (English)</td>\n",
" <td>http://books.google.com/books/content?id=q-tKP...</td>\n",
" <td>On A Train Journey Home To North India After L...</td>\n",
" <td>2003.0</td>\n",
" <td>2.93</td>\n",
" <td>324.0</td>\n",
" <td>...</td>\n",
" <td>Mistaken Identity</td>\n",
" <td>9788172235222 On A Train Journey Home To North...</td>\n",
" <td>Fiction</td>\n",
" <td>0.025156</td>\n",
" <td>0.001939</td>\n",
" <td>0.094667</td>\n",
" <td>0.002254</td>\n",
" <td>0.010511</td>\n",
" <td>0.857255</td>\n",
" <td>0.008218</td>\n",
" </tr>\n",
" <tr>\n",
" <th>5193</th>\n",
" <td>9788173031014</td>\n",
" <td>8173031010</td>\n",
" <td>Journey to the East</td>\n",
" <td>Hermann Hesse</td>\n",
" <td>Adventure stories</td>\n",
" <td>http://books.google.com/books/content?id=rq6JP...</td>\n",
" <td>This book tells the tale of a man who goes on ...</td>\n",
" <td>2002.0</td>\n",
" <td>3.70</td>\n",
" <td>175.0</td>\n",
" <td>...</td>\n",
" <td>Journey to the East</td>\n",
" <td>9788173031014 This book tells the tale of a ma...</td>\n",
" <td>Nonfiction</td>\n",
" <td>0.005602</td>\n",
" <td>0.003775</td>\n",
" <td>0.018216</td>\n",
" <td>0.400263</td>\n",
" <td>0.338892</td>\n",
" <td>0.005487</td>\n",
" <td>0.227765</td>\n",
" </tr>\n",
" <tr>\n",
" <th>5194</th>\n",
" <td>9788179921623</td>\n",
" <td>817992162X</td>\n",
" <td>The Monk Who Sold His Ferrari: A Fable About F...</td>\n",
" <td>Robin Sharma</td>\n",
" <td>Health & Fitness</td>\n",
" <td>http://books.google.com/books/content?id=c_7mf...</td>\n",
" <td>Wisdom to Create a Life of Passion, Purpose, a...</td>\n",
" <td>2003.0</td>\n",
" <td>3.82</td>\n",
" <td>198.0</td>\n",
" <td>...</td>\n",
" <td>The Monk Who Sold His Ferrari: A Fable About F...</td>\n",
" <td>9788179921623 Wisdom to Create a Life of Passi...</td>\n",
" <td>Fiction</td>\n",
" <td>0.008463</td>\n",
" <td>0.009147</td>\n",
" <td>0.013295</td>\n",
" <td>0.620452</td>\n",
" <td>0.329754</td>\n",
" <td>0.010788</td>\n",
" <td>0.008101</td>\n",
" </tr>\n",
" <tr>\n",
" <th>5195</th>\n",
" <td>9788185300535</td>\n",
" <td>8185300534</td>\n",
" <td>I Am that</td>\n",
" <td>Sri Nisargadatta Maharaj;Sudhakar S. Dikshit</td>\n",
" <td>Philosophy</td>\n",
" <td>http://books.google.com/books/content?id=Fv_JP...</td>\n",
" <td>This collection of the timeless teachings of o...</td>\n",
" <td>1999.0</td>\n",
" <td>4.51</td>\n",
" <td>531.0</td>\n",
" <td>...</td>\n",
" <td>I Am that: Talks with Sri Nisargadatta Maharaj</td>\n",
" <td>9788185300535 This collection of the timeless ...</td>\n",
" <td>Nonfiction</td>\n",
" <td>0.005475</td>\n",
" <td>0.034544</td>\n",
" <td>0.003970</td>\n",
" <td>0.258353</td>\n",
" <td>0.648011</td>\n",
" <td>0.017372</td>\n",
" <td>0.032275</td>\n",
" </tr>\n",
" <tr>\n",
" <th>5196</th>\n",
" <td>9789027712059</td>\n",
" <td>9027712050</td>\n",
" <td>The Berlin Phenomenology</td>\n",
" <td>Georg Wilhelm Friedrich Hegel</td>\n",
" <td>History</td>\n",
" <td>http://books.google.com/books/content?id=Vy7Sk...</td>\n",
" <td>Since the three volume edition ofHegel's Philo...</td>\n",
" <td>1981.0</td>\n",
" <td>0.00</td>\n",
" <td>210.0</td>\n",
" <td>...</td>\n",
" <td>The Berlin Phenomenology</td>\n",
" <td>9789027712059 Since the three volume edition o...</td>\n",
" <td>Nonfiction</td>\n",
" <td>0.002837</td>\n",
" <td>0.003137</td>\n",
" <td>0.001166</td>\n",
" <td>0.958549</td>\n",
" <td>0.028252</td>\n",
" <td>0.002916</td>\n",
" <td>0.003142</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"<p>5197 rows × 21 columns</p>\n",
"</div>"
],
"text/plain": [
" isbn13 isbn10 \\\n",
"0 9780002005883 0002005883 \n",
"1 9780002261982 0002261987 \n",
"2 9780006178736 0006178731 \n",
"3 9780006280897 0006280897 \n",
"4 9780006280934 0006280935 \n",
"... ... ... \n",
"5192 9788172235222 8172235224 \n",
"5193 9788173031014 8173031010 \n",
"5194 9788179921623 817992162X \n",
"5195 9788185300535 8185300534 \n",
"5196 9789027712059 9027712050 \n",
"\n",
" title \\\n",
"0 Gilead \n",
"1 Spider's Web \n",
"2 Rage of angels \n",
"3 The Four Loves \n",
"4 The Problem of Pain \n",
"... ... \n",
"5192 Mistaken Identity \n",
"5193 Journey to the East \n",
"5194 The Monk Who Sold His Ferrari: A Fable About F... \n",
"5195 I Am that \n",
"5196 The Berlin Phenomenology \n",
"\n",
" authors \\\n",
"0 Marilynne Robinson \n",
"1 Charles Osborne;Agatha Christie \n",
"2 Sidney Sheldon \n",
"3 Clive Staples Lewis \n",
"4 Clive Staples Lewis \n",
"... ... \n",
"5192 Nayantara Sahgal \n",
"5193 Hermann Hesse \n",
"5194 Robin Sharma \n",
"5195 Sri Nisargadatta Maharaj;Sudhakar S. Dikshit \n",
"5196 Georg Wilhelm Friedrich Hegel \n",
"\n",
" categories \\\n",
"0 Fiction \n",
"1 Detective and mystery stories \n",
"2 Fiction \n",
"3 Christian life \n",
"4 Christian life \n",
"... ... \n",
"5192 Indic fiction (English) \n",
"5193 Adventure stories \n",
"5194 Health & Fitness \n",
"5195 Philosophy \n",
"5196 History \n",
"\n",
" thumbnail \\\n",
"0 http://books.google.com/books/content?id=KQZCP... \n",
"1 http://books.google.com/books/content?id=gA5GP... \n",
"2 http://books.google.com/books/content?id=FKo2T... \n",
"3 http://books.google.com/books/content?id=XhQ5X... \n",
"4 http://books.google.com/books/content?id=Kk-uV... \n",
"... ... \n",
"5192 http://books.google.com/books/content?id=q-tKP... \n",
"5193 http://books.google.com/books/content?id=rq6JP... \n",
"5194 http://books.google.com/books/content?id=c_7mf... \n",
"5195 http://books.google.com/books/content?id=Fv_JP... \n",
"5196 http://books.google.com/books/content?id=Vy7Sk... \n",
"\n",
" description published_year \\\n",
"0 A NOVEL THAT READERS and critics have been eag... 2004.0 \n",
"1 A new 'Christie for Christmas' -- a full-lengt... 2000.0 \n",
"2 A memorable, mesmerizing heroine Jennifer -- b... 1993.0 \n",
"3 Lewis' work on the nature of love divides love... 2002.0 \n",
"4 \"In The Problem of Pain, C.S. Lewis, one of th... 2002.0 \n",
"... ... ... \n",
"5192 On A Train Journey Home To North India After L... 2003.0 \n",
"5193 This book tells the tale of a man who goes on ... 2002.0 \n",
"5194 Wisdom to Create a Life of Passion, Purpose, a... 2003.0 \n",
"5195 This collection of the timeless teachings of o... 1999.0 \n",
"5196 Since the three volume edition ofHegel's Philo... 1981.0 \n",
"\n",
" average_rating num_pages ... \\\n",
"0 3.85 247.0 ... \n",
"1 3.83 241.0 ... \n",
"2 3.93 512.0 ... \n",
"3 4.15 170.0 ... \n",
"4 4.09 176.0 ... \n",
"... ... ... ... \n",
"5192 2.93 324.0 ... \n",
"5193 3.70 175.0 ... \n",
"5194 3.82 198.0 ... \n",
"5195 4.51 531.0 ... \n",
"5196 0.00 210.0 ... \n",
"\n",
" title_and_subtitle \\\n",
"0 Gilead \n",
"1 Spider's Web: A Novel \n",
"2 Rage of angels \n",
"3 The Four Loves \n",
"4 The Problem of Pain \n",
"... ... \n",
"5192 Mistaken Identity \n",
"5193 Journey to the East \n",
"5194 The Monk Who Sold His Ferrari: A Fable About F... \n",
"5195 I Am that: Talks with Sri Nisargadatta Maharaj \n",
"5196 The Berlin Phenomenology \n",
"\n",
" tagged_description simple_categories \\\n",
"0 9780002005883 A NOVEL THAT READERS and critics... Fiction \n",
"1 9780002261982 A new 'Christie for Christmas' -... Fiction \n",
"2 9780006178736 A memorable, mesmerizing heroine... Fiction \n",
"3 9780006280897 Lewis' work on the nature of lov... Nonfiction \n",
"4 9780006280934 \"In The Problem of Pain, C.S. Le... Nonfiction \n",
"... ... ... \n",
"5192 9788172235222 On A Train Journey Home To North... Fiction \n",
"5193 9788173031014 This book tells the tale of a ma... Nonfiction \n",
"5194 9788179921623 Wisdom to Create a Life of Passi... Fiction \n",
"5195 9788185300535 This collection of the timeless ... Nonfiction \n",
"5196 9789027712059 Since the three volume edition o... Nonfiction \n",
"\n",
" fear anger sadness neutral disgust joy surprise \n",
"0 0.009156 0.002628 0.068162 0.047942 0.140385 0.002122 0.729603 \n",
"1 0.005967 0.002887 0.003810 0.704420 0.217761 0.004509 0.060646 \n",
"2 0.041301 0.024568 0.104062 0.767237 0.042176 0.010860 0.009796 \n",
"3 0.016036 0.060695 0.001692 0.161757 0.732686 0.020988 0.006147 \n",
"4 0.013624 0.122243 0.095043 0.008336 0.272613 0.475880 0.012260 \n",
"... ... ... ... ... ... ... ... \n",
"5192 0.025156 0.001939 0.094667 0.002254 0.010511 0.857255 0.008218 \n",
"5193 0.005602 0.003775 0.018216 0.400263 0.338892 0.005487 0.227765 \n",
"5194 0.008463 0.009147 0.013295 0.620452 0.329754 0.010788 0.008101 \n",
"5195 0.005475 0.034544 0.003970 0.258353 0.648011 0.017372 0.032275 \n",
"5196 0.002837 0.003137 0.001166 0.958549 0.028252 0.002916 0.003142 \n",
"\n",
"[5197 rows x 21 columns]"
]
},
"execution_count": 18,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"books"
]
},
{
"cell_type": "code",
"execution_count": 19,
"id": "4f4407d2",
"metadata": {},
"outputs": [],
"source": [
"books.to_csv(\"books_with_emotions.csv\",index=False)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "base",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.12.3"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
|