kmeansとDoc2vecでクラスタリングした文章データのうち、特定のクラスタを呼び出したい
タイトルの通りです。
tsvファイルをpythonで読み込み、MeCabで分かち書きをしたのちにkmeans法でクラスタリングをすることには成功しています。
このクラスタリングによって得られた結果から、特定のクラスターの文章データを呼び出したいのですがうまくいきません。
当方、プログラミング初心者なためもしかしたら筋違いな質問かも知れませんが、どなたかご教授いただけると幸いです。
使用環境
jupyter notebook
windows10
ここから成功した部分
from gensim.models.doc2vec import Doc2Vec
from gensim.models.doc2vec import TaggedDocument
import MeCab
import csv
mt = MeCab.Tagger()
reports = []
with open("インプットファイル",mode='r',encoding='utf-8') as f:
# インプットファイルには一行に文章ID,文章がtab区切りで保存されている
reader = csv.reader(f, delimiter="\t")
for report_id, report in reader:
words = []
node = mt.parseToNode(report)
while node:
if len(node.surface) > 0:
words.append(node.surface)
node = node.next
stopword = ['あれ','それ','これ']
words2 = [token for token in words if token not in stopword]
# wordsが文章の単語のリスト,tagsには文章IDを指定
reports.append(TaggedDocument(words=words2, tags=[report_id]))
print(words2)
model = Doc2Vec(documents=reports, size=128, window=8, min_count=2, workers=8)
model.save("allcomment_stop_doc2vec.model")
m = Doc2Vec.load("allcomment_stop_doc2vec.model")
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
#ベクトルをリストに格納
vectors_list=[m.docvecs[n] for n in range(len(m.docvecs))]
#ドキュメント番号のリスト
doc_nums=range(1,1+len(m.docvecs))
#クラスタリング設定
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)
上記の出力結果(数値はtsvファイルに書かれている文章idに対応したものです)
[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] range(0, 6) 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]
ここまでは成功した部分です。
ここで、最初のクラスタの文章データを呼び出そうと
print(reports[1, 6, 7, 9,~,999])
とすると
list indices must be integers or slices, not tuple
となり複数の引数を渡すことができません。
解決策をどなたかご教授いただけると幸いです。