Doc2vecにより文章データのクラスタリングを行うため、モデルの学習とクラスタリングを行いました。その後、各クラスタの文章データを1つの文章として見立て、クラスタごとにtfidf値を求め、各クラスタの重要語を抽出し、クラスタの意味を解釈しようとしています。

その際、「各クラスタの文章データを1つの文章として見立て」の部分でつっかえてしまいました。

```

    from gensim.models.doc2vec import Doc2Vec
    from gensim.models.doc2vec import TaggedDocument
    from sklearn.cluster import KMeans
    import sys
    from collections import defaultdict
    import numpy as np
    import matplotlib.pyplot as plt
    
    #modelは1037件の文章データから作成しています。
    m = Doc2Vec.load("comment_stop2_doc2vec.model")
    
    #ベクトルをリストに格納
    vectors_list=[m.docvecs[n] for n in range(len(m.docvecs))]
    
    #ドキュメント番号のリスト
    doc_nums=range(1,1+len(m.docvecs))
    
    #クラスタリング設定
    #クラスター数を変えたい場合はn_clustersを変えてください
    n_clusters = 6
    kmeans_model = KMeans(n_clusters=n_clusters, verbose=1, random_state=1, n_jobs=-1)
    
    #クラスタリング実行
    kmeans_model.fit(vectors_list)
    
    #クラスタリングデータにラベル付け
    labels=kmeans_model.labels_
    
    #ラベルとドキュメント番号の辞書づくり
    cluster_to_docs = defaultdict(list)
    for cluster_id, doc_num in zip(labels, doc_nums):
        cluster_to_docs[cluster_id].append(doc_num)
    
    #クラスター出力
    
    for docs in cluster_to_docs.values():
        print(docs)
    
    
```

このコードにより、1037件の文章データのクラスタリングを行いました。

出力結果

```

    
    [1, 6, 7, 9, 10, 17, 25, 35, 47, 104, 129, 131, 147, 150, 151, 152, 154, 155, 156, 157, 160, 162, 163, 164, 166, 168, 170, 171, 172, 174, 176, 177, 178, 180, 183, 191, 194, 199, 210, 219, 222, 233, 239, 240, 242, 253, 255, 256, 257, 268, 269, 272, 275, 294, 301, 303, 307, 311, 312, 314, 315, 320, 321, 322, 324, 330, 340, 347, 355, 357, 368, 369, 371, 373, 374, 376, 378, 380, 381, 385, 387, 405, 407, 409, 414, 417, 418, 419, 421, 427, 431, 432, 433, 436, 439, 442, 443, 444, 445, 446, 447, 448, 455, 458, 459, 460, 463, 465, 466, 468, 471, 490, 493, 500, 507, 536, 552, 565, 566, 568, 573, 575, 577, 579, 581, 583, 596, 604, 611, 612, 618, 619, 625, 630, 634, 640, 644, 646, 648, 649, 654, 655, 663, 667, 668, 670, 675, 681, 682, 684, 685, 687, 695, 696, 697, 698, 702, 703, 708, 710, 711, 736, 755, 762, 763, 766, 771, 779, 791, 799, 807, 810, 813, 814, 817, 824, 829, 842, 843, 847, 856, 860, 870, 879, 889, 894, 895, 897, 904, 911, 919, 920, 922, 924, 925, 926, 927, 928, 930, 931, 932, 933, 934, 936, 937, 949, 951, 952, 956, 957, 958, 959, 962, 963, 965, 968, 979, 985, 989, 996, 997, 999]
    [2, 11, 21, 22, 23, 26, 27, 30, 34, 36, 38, 43, 45, 50, 51, 52, 53, 54, 55, 57, 58, 60, 61, 63, 64, 65, 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, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 105, 106, 107, 108, 109, 110, 111, 113, 114, 116, 121, 123, 124, 126, 127, 128, 130, 132, 134, 137, 138, 139, 140, 141, 142, 143, 144, 145, 153, 159, 167, 193, 200, 201, 204, 205, 206, 209, 211, 212, 214, 215, 217, 218, 224, 225, 234, 235, 238, 241, 243, 249, 250, 251, 254, 258, 259, 264, 266, 270, 271, 276, 277, 278, 279, 281, 282, 283, 284, 285, 286, 287, 288, 289, 290, 291, 345, 346, 349, 350, 353, 359, 360, 363, 364, 365, 366, 367, 370, 372, 377, 379, 423, 424, 425, 449, 451, 452, 453, 454, 469, 475, 477, 479, 481, 483, 486, 494, 502, 506, 510, 511, 513, 514, 516, 523, 526, 527, 528, 530, 531, 532, 533, 537, 541, 544, 547, 548, 551, 555, 558, 559, 560, 561, 562, 584, 585, 586, 588, 590, 591, 593, 594, 595, 598, 600, 601, 605, 606, 617, 636, 645, 669, 689, 705, 706, 712, 714, 715, 716, 721, 722, 723, 724, 725, 726, 727, 728, 729, 731, 732, 733, 734, 738, 739, 741, 742, 744, 745, 747, 749, 751, 752, 754, 758, 760, 761, 764, 765, 767, 768, 769, 770, 773, 774, 775, 776, 781, 783, 785, 787, 789, 790, 794, 796, 802, 803, 812, 815, 816, 819, 825, 832, 848, 853, 855, 857, 861, 862, 864, 865, 866, 869, 871, 872, 873, 876, 888, 898, 899, 900, 905, 906, 907, 909, 915, 917, 938, 940, 943, 948, 964, 1000, 1001, 1002, 1003, 1004, 1005, 1006, 1007, 1008, 1010, 1011, 1012, 1013, 1015, 1016, 1017, 1018, 1020, 1022, 1023, 1025, 1026, 1028, 1030, 1033, 1034, 1035]
    [3, 4, 5, 8, 12, 13, 14, 18, 19, 20, 24, 28, 29, 31, 32, 33, 37, 39, 40, 41, 42, 44, 46, 48, 49, 56, 59, 62, 66, 93, 112, 115, 117, 118, 119, 120, 122, 125, 133, 135, 136, 146, 149, 158, 161, 165, 173, 179, 185, 202, 203, 207, 208, 213, 216, 220, 221, 223, 226, 227, 228, 229, 230, 231, 232, 236, 237, 244, 245, 246, 247, 248, 252, 261, 262, 263, 265, 267, 273, 274, 280, 342, 343, 344, 348, 351, 352, 356, 358, 361, 362, 382, 384, 386, 388, 404, 406, 408, 410, 411, 412, 413, 415, 416, 420, 422, 426, 428, 429, 434, 435, 437, 438, 450, 456, 462, 464, 467, 472, 473, 474, 476, 478, 480, 482, 484, 485, 487, 488, 489, 491, 492, 495, 496, 497, 498, 499, 501, 503, 504, 505, 508, 509, 512, 515, 517, 518, 519, 520, 521, 522, 524, 525, 529, 534, 535, 538, 539, 540, 542, 545, 546, 549, 550, 553, 554, 556, 557, 563, 564, 569, 578, 582, 587, 589, 592, 597, 599, 602, 607, 613, 614, 616, 624, 651, 657, 658, 660, 661, 662, 671, 672, 674, 678, 686, 688, 694, 699, 707, 713, 717, 718, 719, 720, 730, 735, 737, 740, 743, 746, 748, 750, 753, 756, 757, 759, 772, 777, 778, 780, 782, 784, 786, 788, 792, 793, 795, 797, 798, 800, 801, 804, 805, 806, 808, 809, 811, 818, 823, 826, 827, 828, 830, 831, 833, 834, 835, 836, 837, 838, 839, 840, 841, 844, 845, 846, 849, 850, 851, 852, 854, 858, 859, 863, 867, 868, 874, 875, 877, 878, 880, 881, 882, 883, 884, 885, 886, 887, 890, 891, 892, 893, 896, 901, 902, 903, 908, 910, 912, 913, 914, 916, 941, 942, 944, 946, 947, 950, 953, 954, 960, 961, 966, 967, 994, 995, 1009, 1014, 1019, 1021, 1024, 1027, 1029, 1031, 1032, 1036, 1037]
    [15, 16, 148, 169, 175, 181, 182, 184, 186, 187, 188, 189, 190, 192, 195, 196, 197, 198, 260, 292, 295, 296, 297, 299, 300, 304, 306, 309, 310, 313, 316, 317, 327, 332, 337, 338, 339, 354, 375, 383, 389, 400, 430, 440, 441, 457, 461, 470, 567, 570, 574, 580, 603, 609, 610, 615, 620, 622, 623, 626, 627, 628, 629, 631, 632, 633, 635, 638, 647, 650, 652, 653, 656, 659, 673, 676, 677, 679, 680, 690, 691, 693, 700, 709, 918, 921, 923, 929, 935, 939, 945, 955, 969, 971, 972, 973, 974, 975, 976, 977, 978, 980, 981, 982, 983, 984, 986, 987, 988, 990, 991, 992, 993, 998]
    [293, 298, 302, 305, 308, 318, 319, 323, 325, 326, 328, 329, 331, 333, 334, 335, 341, 390, 391, 395, 396, 397, 398, 399, 401, 402, 403, 543, 571, 572, 576, 608, 621, 637, 639, 641, 642, 643, 692, 701, 704, 820, 821, 822, 970]
    [336, 392, 393, 394, 664, 665, 666, 683]
    [222, 331, 317, 114, 45, 8]
    
```

1037件のデータについてクラスタリングされていることが分かります。  
ここで、各クラスタの文章を1つの文章のように見立てるため、文章データのみを抽出するため

```

    clu0 =([reports[i].words for i in (1, 5, 19, 27, 31, 37, 43, 47, 48, 57, 87, 90, 93, 112, 114, 117, 128, 129, 133, 149, 151, 153, 162, 163, 170, 171, 173, 174, 176, 180, 185, 199, 202, 209, 212, 218, 227, 228, 229, 230, 236, 238, 244, 252, 254, 261, 262, 263, 268, 269, 270, 271, 272, 274, 277, 278, 348, 350, 352, 355, 360, 361, 362, 363, 364, 366, 371, 372, 380, 382, 385, 429, 434, 437, 439, 450, 452, 456, 462, 465, 468, 473, 476, 482, 485, 490, 491, 493, 496, 501, 506, 508, 512, 519, 521, 522, 523, 524, 525, 529, 531, 534, 539, 546, 550, 551, 552, 555, 563, 568, 573, 578, 582, 587, 599, 605, 607, 623, 624, 644, 650, 654, 661, 668, 674, 681, 682, 686, 687, 688, 696, 708, 712, 726, 727, 733, 754, 760, 765, 771, 782, 786, 797, 803, 805, 806, 808, 811, 819, 823, 824, 830, 833, 834, 841, 843, 844, 845, 846, 858, 860, 870, 871, 873, 875, 887, 893, 897, 898, 908, 912, 932, 941, 946, 947, 951, 967, 968, 989, 995, 996, 1002, 1017, 1028)])
    clu1 =([reports[i].words for i in (2, 3, 8, 11, 21, 22, 23, 28, 29, 30, 36, 38, 39, 45, 50, 51, 52, 53, 55, 58, 60, 61, 63, 64, 65, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 83, 84, 85, 86, 89, 91, 92, 95, 96, 98, 99, 100, 101, 102, 103, 105, 107, 108, 109, 110, 111, 113, 115, 116, 121, 122, 123, 124, 126, 127, 130, 132, 134, 136, 137, 138, 139, 140, 141, 144, 145, 152, 167, 193, 203, 204, 205, 206, 208, 211, 214, 216, 219, 220, 221, 223, 224, 225, 226, 237, 243, 248, 249, 250, 258, 259, 264, 265, 266, 276, 279, 281, 283, 284, 285, 286, 287, 288, 289, 290, 291, 342, 343, 345, 346, 367, 370, 379, 388, 449, 453, 454, 469, 475, 477, 479, 481, 483, 484, 486, 488, 494, 497, 502, 503, 504, 505, 507, 509, 514, 515, 518, 520, 526, 527, 528, 530, 532, 533, 535, 536, 537, 541, 542, 544, 545, 547, 554, 556, 557, 558, 559, 561, 562, 584, 585, 586, 588, 590, 591, 593, 594, 595, 597, 598, 600, 601, 614, 617, 645, 660, 669, 672, 705, 707, 715, 724, 725, 747, 758, 767, 769, 770, 773, 774, 775, 776, 777, 779, 784, 785, 788, 789, 790, 792, 794, 795, 796, 798, 800, 802, 804, 809, 812, 816, 825, 827, 828, 836, 838, 839, 853, 854, 855, 856, 857, 859, 861, 862, 863, 864, 866, 867, 868, 869, 872, 876, 877, 880, 881, 883, 884, 888, 890, 899, 900, 905, 907, 909, 910, 911, 913, 914, 915, 917, 940, 943, 948, 964, 966, 1004, 1010)])
    clu2 =([reports[i].words for i in (4, 6, 12, 18, 26, 32, 33, 34, 40, 41, 42, 44, 46, 49, 54, 56, 59, 62, 66, 82, 88, 94, 97, 106, 118, 119, 135, 142, 143, 156, 159, 160, 161, 164, 165, 166, 178, 179, 201, 207, 210, 213, 215, 217, 232, 234, 235, 242, 245, 246, 247, 251, 255, 280, 282, 324, 349, 353, 356, 359, 368, 376, 377, 378, 384, 386, 423, 433, 438, 440, 446, 447, 451, 455, 458, 460, 463, 464, 470, 472, 478, 480, 487, 489, 492, 495, 498, 499, 510, 513, 516, 517, 540, 548, 553, 560, 564, 581, 592, 596, 602, 604, 606, 611, 613, 618, 634, 636, 648, 651, 658, 659, 667, 670, 671, 679, 689, 697, 699, 706, 710, 711, 714, 728, 752, 768, 772, 778, 780, 781, 783, 787, 793, 801, 807, 810, 813, 814, 815, 818, 826, 829, 832, 835, 840, 847, 848, 849, 850, 852, 865, 874, 878, 879, 885, 886, 889, 891, 892, 896, 901, 902, 903, 906, 916, 927, 933, 938, 942, 944, 949, 952, 953, 954, 956, 957, 958, 959, 960, 961, 962, 963, 974, 977, 978, 993, 994, 1001, 1003, 1005, 1006)])
    clu3 =([reports[i].words for i in (7, 9, 13, 14, 16, 17, 20, 24, 25, 35, 120, 125, 146, 147, 148, 150, 155, 157, 158, 168, 169, 172, 177, 181, 182, 183, 188, 189, 195, 200, 222, 231, 233, 239, 240, 241, 256, 260, 273, 294, 297, 301, 307, 309, 311, 312, 313, 314, 315, 316, 321, 322, 327, 339, 340, 344, 347, 351, 357, 358, 374, 381, 387, 389, 431, 432, 435, 436, 442, 443, 444, 445, 448, 457, 466, 467, 471, 474, 500, 511, 538, 549, 565, 566, 569, 570, 574, 575, 577, 579, 580, 583, 589, 610, 612, 615, 616, 619, 625, 630, 635, 638, 640, 646, 649, 652, 653, 655, 656, 657, 662, 663, 677, 684, 685, 691, 695, 698, 700, 702, 703, 713, 716, 718, 723, 731, 732, 734, 735, 738, 741, 742, 745, 746, 749, 751, 761, 764, 791, 799, 817, 831, 837, 842, 851, 882, 894, 895, 904, 920, 921, 922, 923, 924, 925, 926, 928, 929, 930, 931, 934, 935, 936, 937, 939, 950, 955, 965, 971, 976, 979, 981, 982, 983, 984, 985, 987, 990, 992, 999, 1000, 1007, 1008, 1009, 1011, 1012, 1013, 1020, 1023, 1024, 1025, 1026, 1029, 1030, 1031, 1035)])
    clu4 =([reports[i].words for i in (10, 15, 104, 131, 154, 175, 184, 186, 187, 190, 191, 192, 194, 196, 197, 198, 253, 257, 267, 275, 292, 293, 296, 299, 303, 304, 306, 308, 310, 320, 329, 330, 331, 332, 337, 338, 354, 365, 369, 373, 375, 383, 404, 408, 411, 413, 424, 425, 426, 428, 430, 441, 459, 461, 543, 567, 576, 603, 609, 620, 622, 626, 627, 628, 629, 631, 632, 633, 637, 639, 641, 642, 643, 647, 673, 675, 676, 678, 680, 690, 692, 693, 694, 709, 717, 719, 720, 721, 722, 729, 737, 739, 740, 743, 744, 748, 750, 756, 757, 759, 821, 822, 918, 919, 945, 969, 970, 972, 973, 975, 980, 986, 988, 991, 997, 998, 1014, 1015, 1016, 1018, 1019, 1021, 1022, 1027, 1032, 1033, 1034, 1037)])
    clu5 =([reports[i].words for i in (295, 298, 300, 302, 305, 317, 318, 319, 323, 325, 326, 328, 333, 334, 335, 336, 341, 390, 391, 392, 393, 394, 395, 396, 397, 398, 399, 400, 401, 402, 403, 405, 406, 407, 409, 410, 412, 414, 415, 416, 417, 418, 419, 420, 421, 422, 427, 571, 572, 608, 621, 664, 665, 666, 683, 701, 704, 730, 736, 753, 755, 762, 763, 766, 820, 1036)])
    
```

としたところ、list index out of rangeと言われてしまいました。  
また、最後のコードのイケてなさは初心者の自分でも分かるので、何かいい書き方があれば併せてご教授願いたいです。。。
CC BY-SA 4.0
2019-11-14T18:31:52.123
60524
2019-11-15T02:50:47.143
2019-11-15T02:50:47.143
2238
36375
question
python 自然言語処理
List index out of rangeによりリストが作れない