第1回 データ分析の基礎――データの可視化――
プログラム(Pythonスクリプト)の完全なリストを以下に上げる。スクリプトのファイルは圧縮ファイルfiles1.zipとしてまとめたので、クリックしてダウンロード解凍すればスクリプトファイルを開くことができる。
リスト1 2つのクラスの学力のヒストグラム描画スクリプト
import matplotlib.pyplot as plt
A = [71, 71, 70, 77, 79, 82, 71, 89,
91, 84,
75, 76, 86, 75, 89,
76, 73, 82, 74, 88,
48, 57, 53, 56, 59,
44, 53, 52, 48, 46,
57, 49, 42, 45, 56,
42, 45, 43, 49, 54]
B = [71, 54, 62, 61, 60, 67, 62, 71,
59, 65,
59, 63, 51, 55, 67, 58, 69, 66, 69,
69,
58, 63, 65, 63, 58,
62, 63, 69, 68, 55,
64, 64, 59, 70, 64,
57, 63, 65, 69, 73]
mean_A = sum(A) / len(A)
print('Aの平均値 = ', mean_A)
mean_B = sum(B) / len(B)
print('Bの平均値 = ', mean_B)
plt.hist(A, alpha = 0.5, label =
'Class A')
plt.hist(B, alpha = 0.5, label =
'Class B')
plt.legend()
plt.show()
リスト2 A社とB社の社員の年収の分布グラフの描画スクリプト
import matplotlib.pyplot as plt
import numpy as np
import seaborn as sb
import pandas as pd
A = [
572, 583, 1089, 381, 428, 354, 434, 554,
595, 392,
335, 444, 400, 388, 712, 485, 336, 386,
411, 523,
490, 584, 359, 505, 314, 339, 384, 737,
400, 557,
395, 340, 366, 340, 382, 675, 396, 398,
486, 773,
375, 371, 593, 507, 1113, 441, 330, 436,
578, 348,
372, 558, 599, 512, 495, 560, 595, 494,
357, 313,
912, 403, 318, 438, 464, 618, 331, 586,
427, 347,
692, 373, 806, 394, 368, 768, 383, 313,
1234, 967,
648, 546, 354, 317, 460, 337, 394, 313,
459, 534,
725, 416, 401, 313, 364, 465, 504, 420,
486, 312,
922, 318, 533, 387, 417, 789, 410, 387,
710, 412,
339, 503, 468, 384, 467, 895, 360, 627,
634, 551,
546, 570, 329, 376, 366, 438, 810, 561,
424, 405,
360, 469, 929, 1076, 558, 376, 410, 431,
703, 335,
412, 314, 558, 456, 507, 350, 372, 1447,
522, 577,
323, 706, 503, 356, 810, 805, 669, 444,
392, 1288,
366, 623, 318, 523, 336, 366, 479, 484,
466, 439,
336, 405, 498, 319, 735, 381, 421, 659,
535, 543,
319, 435, 414, 552, 317, 421, 403, 450,
318, 820,
1059, 669, 555, 424, 400, 389, 528, 630,
450, 338,
653, 613, 342, 492, 463, 890, 382, 617,
679, 378,
816, 376, 395, 1213, 482, 768, 573, 487,
541, 355,
466, 521, 381, 372, 471, 367, 600, 534,
796, 372,
394, 482, 414, 540, 350, 848, 361, 371,
1249, 391,
901, 367, 594, 471, 420, 315, 902, 405, 578,
325,
357, 330, 722, 416, 386, 504, 356, 488,
1157, 357,
406, 870, 464, 560, 525, 632, 333, 315,
529, 625,
328, 736, 414, 388, 354, 713, 372, 543,
361, 675,
748, 414, 483, 434, 1267, 441, 317, 315,
556, 392,
407, 326, 737, 369, 399, 425, 457, 491,
355, 455,
420, 316, 536, 951, 435, 526, 556, 381,
450, 482,
543, 327, 561, 518, 525, 420, 434, 551,
880, 546,
519, 327, 521, 362, 352, 563, 326, 329,
315, 570,
1239, 536, 486, 370, 342, 537, 471, 386,
341, 647,
349, 634, 341, 320, 585, 411, 335, 527,
808, 532,
1160, 315, 330, 319, 342, 570, 451, 359,
405, 542,
324, 382, 326, 379, 315, 569, 317, 1191,
632, 490,
544, 313, 393, 367, 653, 441, 378, 742,
558, 847,
320, 660, 403, 759, 463, 352, 354, 868,
645, 336,
1118, 507, 462, 377, 468, 715, 329, 727,
389, 565,
357, 381, 349, 647, 562, 440, 315, 336,
854, 736,
375, 503, 569, 766, 382, 618, 960, 363,
357, 422,
1066, 567, 482, 430, 333, 400, 403, 659,
801, 369,
622, 387, 339, 340, 576, 327, 312, 446,
330, 377,
367, 544, 322, 539, 555, 319, 468, 345,
370, 433,
384, 509, 336, 884, 483, 359, 886, 897,
463, 697,
357, 485, 710, 411, 911, 378, 766, 337,
494, 514,
665, 595, 326, 494, 386, 383, 645, 419,
562, 615,
357, 463, 541, 339, 925, 421, 1622, 380,
436, 454,
326, 405, 719, 328, 363, 568, 430, 408,
556, 341,
373, 437, 360, 490, 627, 588, 605, 689,
651, 821,
539, 419, 314, 522, 348, 324, 483, 1142,
383, 540,
357, 392, 365, 460, 313, 372, 617, 380,
333, 401,
645, 678, 475, 462, 315, 341, 361, 364,
331, 830,
336, 343, 367, 429, 350, 525, 989, 573,
332, 1328,
601, 325, 383, 376, 336, 325, 493, 574,
340, 539,
557, 335, 359, 421, 350, 350, 627, 388,
492, 412,
352, 515, 315, 463, 699, 586, 566, 406,
970, 314,
318, 551, 385, 321, 424, 533, 474, 496,
747, 371,
558, 338, 496, 428, 513, 376, 412, 372,
345, 335,
520, 326, 418, 730, 416, 542, 633, 547,
569, 320,
521, 438, 343, 651, 541, 681, 382, 481,
519, 823,
408, 470, 315, 744, 867, 533, 541, 385,
370, 357,
608, 587, 643, 340, 315, 397, 428, 315,
614, 540,
464, 433, 365, 820, 385, 534, 427, 404,
1391, 378,
416, 559, 369, 348, 550, 483, 537, 415,
391, 329,
385, 745, 678, 588, 353, 535, 436, 654,
427, 481,
351, 349, 386, 344, 813, 731, 387, 425,
377, 911,
334, 683, 386, 369, 337, 915, 393, 419,
475, 315,
508, 441, 913, 399, 341, 395, 323, 580,
572, 489,
325, 434, 337, 604, 408, 325, 1519, 562,
783, 1120,
350, 441, 398, 334, 704, 355, 585, 312,
661, 483,
364, 483, 377, 318, 446, 358, 316, 428,
466, 504,
317, 317, 338, 380, 351, 339, 1045, 380,
382, 541,
491, 749, 845, 314, 486, 355, 465, 747,
438, 516,
327, 334, 554, 390, 592, 387, 334, 487,
322, 452,
356, 327, 323, 465, 970, 420, 317, 698,
414, 432,
442, 582, 563, 463, 866, 386, 331, 376,
342, 452,
587, 518, 325, 484, 320, 384, 355, 614,
600, 478,
468, 337, 470, 408, 458, 581, 414, 477,
595, 874,
574, 435, 622, 342, 359, 390, 606, 568,
459, 452,
326, 649, 325, 348, 715, 845, 912, 491,
339, 443,
582, 637, 324, 341, 333, 338, 443, 1163,
408, 507,
579, 510, 429, 359, 312, 316, 906, 497,
550, 524,
404, 358, 331, 369, 1020, 465, 374, 526,
510, 359,
390, 568, 506, 460, 346, 549, 433, 494,
420, 949,
695, 515, 594, 609, 795, 352, 547, 614,
443, 648,
356, 925, 381, 1288, 404, 412, 517, 427,
762, 1118,
313, 364, 317, 320, 451, 979, 492, 457,
418, 538,
504, 439, 498, 344, 759, 334, 442, 334,
332, 460,
357, 339, 379, 350, 367, 777, 314, 347,
321, 471,
623, 429, 377, 373, 499, 1088, 352, 629,
364, 358,
369, 333, 774, 440, 630, 446, 1057, 443,
395, 326,
412, 840, 328, 593, 419, 389, 391, 678,
340, 391,
366, 472, 354, 553, 469, 423, 397, 730,
553, 431,
478, 705, 391, 414, 748, 383, 392, 590,
392, 396,
406, 400, 472, 571, 382, 544, 379, 683,
617, 348,
492, 584, 453, 749, 587, 376, 444, 466,
392, 413,
518, 324, 521, 529, 434, 366, 322, 333,
338, 900,
410, 600, 1050, 406, 572, 485, 468, 445,
487, 508
]
B = [
501, 507, 508, 501, 513, 487, 510, 495,
506, 498,
488, 495, 499, 501, 495, 497, 480, 500,
491, 508,
503, 510, 507, 500, 503, 503, 493, 492,
505, 501,
494, 482, 509, 497, 482, 501, 496, 495,
493, 504,
477, 498, 505, 516, 522, 506, 491, 504,
513, 494,
500, 493, 501, 482, 498, 498, 505, 488,
495, 485,
489, 486, 498, 493, 479, 521, 510, 488,
491, 513,
509, 490, 489, 503, 506, 495, 488, 512,
488, 499,
506, 496, 493, 521, 494, 478, 481, 507,
493, 494,
491, 504, 483, 514, 507, 501, 506, 502,
489, 499,
503, 507, 496, 493, 490, 501, 490, 487,
501, 510,
503, 501, 504, 497, 510, 508, 487, 501,
506, 506,
502, 502, 482, 493, 517, 516, 492, 509,
505, 504,
495, 501, 495, 495, 484, 505, 497, 486,
490, 508,
501, 491, 484, 501, 493, 515, 505, 488,
496, 486,
494, 493, 526, 518, 489, 494, 505, 513,
514, 488,
511, 497, 498, 492, 505, 505, 487, 517,
534, 497,
498, 508, 497, 504, 488, 499, 496, 493,
501, 483,
497, 511, 505, 503, 487, 505, 499, 503,
503, 499,
500, 505, 493, 500, 493, 483, 493, 474,
492, 491,
498, 486, 485, 513, 499, 518, 519, 500,
499, 505,
506, 513, 493, 508, 500, 499, 481, 490,
498, 501,
496, 484, 495, 484, 494, 497, 503, 518,
500, 506,
494, 499, 497, 502, 489, 499, 490, 498,
513, 499,
484, 486, 509, 489, 485, 489, 491, 484,
511, 504,
505, 499, 481, 519, 502, 500, 502, 512,
494, 495,
504, 499, 505, 510, 496, 490, 505, 495,
504, 494,
511, 490, 505, 496, 484, 507, 509, 501,
513, 518,
505, 503, 501, 504, 507, 496, 476, 493,
498, 498,
493, 497, 494, 491, 490, 501, 501, 520,
487, 504,
512, 505, 527, 499, 494, 519, 497, 508,
492, 504,
519, 500, 494, 488, 503, 487, 524, 492,
506, 480,
491, 507, 514, 486, 501, 514, 498, 486,
498, 491,
506, 504, 511, 484, 477, 493, 508, 500,
479, 476,
504, 494, 500, 509, 499, 516, 496, 484,
487, 486,
491, 496, 498, 482, 513, 505, 479, 510,
506, 499,
497, 520, 498, 515, 490, 499, 501, 503,
493, 487,
503, 495, 513, 506, 499, 504, 494, 484,
524, 509,
503, 507, 500, 510, 493, 492, 494, 514,
496, 478,
489, 493, 517, 501, 501, 497, 484, 491,
481, 506,
516, 518, 497, 499, 511, 499, 510, 504,
505, 500,
490, 497, 500, 508, 492, 504, 515, 499,
499, 503,
473, 495, 510, 495, 487, 502, 510, 504,
502, 488,
488, 496, 507, 501, 506, 516, 497, 501,
490, 513,
499, 496, 504, 495, 510, 511, 498, 497,
504, 511,
503, 509, 512, 489, 508, 512, 496, 485,
524, 502,
476, 505, 490, 496, 507, 507, 510, 500,
500, 497,
504, 508, 469, 509, 513, 501, 485, 490,
516, 516,
504, 499, 499, 508, 513, 520, 494, 503,
493, 496,
489, 507, 487, 505, 496, 493, 496, 519,
510, 502,
493, 500, 496, 502, 502, 491, 496, 502,
495, 509,
510, 504, 506, 495, 501, 506, 475, 484,
489, 488,
506, 492, 488, 491, 516, 500, 491, 490,
506, 497,
510, 510, 498, 492, 503, 517, 504, 505,
486, 496,
500, 507, 501, 505, 507, 498, 504, 491,
507, 501,
506, 493, 519, 505, 497, 505, 484, 501,
509, 481,
498, 510, 501, 489, 485, 516, 503, 509,
476, 484,
493, 488, 510, 507, 501, 498, 493, 491,
503, 490,
505, 507, 493, 504, 516, 481, 509, 490,
485, 520,
497, 499, 512, 499, 495, 478, 487, 502,
492, 487,
509, 495, 504, 528, 500, 507, 500, 510,
492, 507,
501, 492, 483, 492, 504, 498, 498, 499,
498, 502,
504, 491, 497, 491, 498, 496, 509, 497,
501, 485,
510, 496, 523, 514, 512, 506, 494, 491,
501, 503,
498, 503, 504, 499, 494, 526, 509, 498,
491, 509,
513, 507, 507, 502, 484, 489, 484, 490,
513, 500,
505, 489, 506, 496, 501, 515, 500, 507,
491, 490,
512, 494, 502, 497, 506, 479, 494, 496,
491, 505,
492, 517, 493, 493, 499, 477, 515, 508,
507, 506,
486, 523, 480, 495, 494, 501, 514, 486,
495, 504,
493, 490, 495, 506, 487, 501, 519, 496,
493, 497,
497, 512, 501, 488, 494, 495, 504, 497,
494, 503,
486, 495, 507, 506, 508, 504, 503, 488,
511, 506,
505, 493, 501, 507, 509, 488, 487, 508,
499, 497,
515, 497, 505, 506, 502, 515, 497, 500,
494, 495,
515, 490, 498, 500, 486, 513, 510, 493,
496, 506,
509, 498, 489, 508, 492, 501, 499, 491,
503, 477,
494, 495, 504, 509, 509, 500, 509, 499,
503, 502,
498, 499, 502, 514, 508, 508, 505, 490,
504, 496,
511, 494, 479, 503, 475, 495, 492, 506,
511, 493,
507, 513, 497, 483, 497, 497, 498, 499,
494, 484,
486, 501, 502, 497, 482, 503, 495, 502,
507, 499,
503, 504, 489, 491, 500, 513, 509, 496,
491, 495,
518, 480, 514, 514, 505, 501, 487, 503,
504, 506,
507, 483, 491, 485, 516, 511, 514, 505,
495, 503,
502, 494, 507, 491, 511, 510, 497, 520,
491, 505,
502, 510, 489, 504, 501, 505, 503, 520,
498, 511,
504, 518, 492, 494, 494, 503, 496, 487,
499, 522,
495, 500, 503, 509, 479, 497, 502, 514,
497, 501,
502, 495, 498, 503, 510, 504, 482, 497,
497, 496,
495, 509, 509, 489, 498, 496, 503, 499,
490, 505,
487, 520, 476, 517, 505, 499, 487, 525,
508, 497,
516, 481, 506, 490, 511, 507, 496, 493,
509, 508,
498, 504, 507, 488, 507, 504, 512, 480,
504, 500,
497, 504, 494, 508, 491, 504, 475, 497,
505, 501,
477, 513, 507, 505, 510, 494, 494, 508,
515, 496,
513, 501, 497, 482, 489, 506, 504, 494,
507, 502,
511, 496, 484, 506, 501, 491, 496, 495,
496, 502,
495, 490, 499, 493, 511, 506, 500, 494,
511, 494,
509, 491, 508, 518, 488, 493, 489, 494,
512, 494
]
print('A社のデータ数 = ', len(A))
print('B社のデータ数 = ', len(B))
a = np.array(A)
print('A社の平均 = ', a.mean())
b = np.array(B)
print('B社の平均 = ', b.mean())
df = pd.DataFrame({'Company': ['A'] *
len(A) + ['B'] * len(B),
'Income': A + B})
sb.stripplot(x = 'Company', y =
'Income', data = df, alpha = 0.5)
sb.violinplot(x = 'Company', y =
'Income', data = df, alpha = 0.5)
plt.show()