题目
已知2015级物联网工程专业1班、2019级物联网工程专业1班《数据挖掘》课程期末成绩分别如下:
2015级物联网工程专业1班:82,92,77,62,70,36,80,100,74,64,63,56,72,78,68,65,72,80,58,92,79,92,65,56,85,73,61,71,42,89.
2019级物联网工程专业1班:
57,67,64,54,77,65,71,58,59,69,67,84,63,95,81 ,46,49,60,64,66,74,55,58,63,65,68,76,72,48,72,87,85,76,90,65,88,74,92,83,67,76,65,83,93,67.
试用代码(Matlab/Python/C等)实现求解上述两个班期末成绩的五数概况,并画出五数盒图。
个人答案
使用Python实现结果如下:
import matplotlib.pyplot as plt #绘图库
import numpy as np #数组库
# 1.创建成绩数组
data1 = [82,92,77,62,70,36,80,100,74,64,63,56,72,78,68,65,72,80,58,92,79,92,65,56,85,73,61,71,42,89]
data2 = [57,67,64,54,77,65,71,58,59,69,67,84,63,95,81 ,46,49,60,64,66,74,55,58,63,65,68,76,72,48,72,87,85,76,90,65,88,74,92,83,67,76,65,83,93,67]
# 2.求出最小值,Q1,中位数,Q3,最大值
min_2015 = np.min(data_2015) #2015级成绩的最小值
max_2015 = np.max(data_2015) #2015级成绩的最大值
median_2015 = np.median(data_2015) #2015级成绩的中位数
q1_2015 = np.percentile(data_2015,25) #2015级成绩的Q1
q3_2015 = np.percentile(data_2015,75) #2015级成绩的Q3
min_2019 = np.min(data_2019) #2019级成绩的最小值
max_2019 = np.max(data_2019) #2019级成绩的最大值
median_2019 = np.median(data_2019) #2019级成绩的中位数
q1_2019 = np.percentile(data_2019,25) #2019级成绩的Q1
q3_2019 = np.percentile(data_2019,75) #2019级成绩的Q3
print("2015级:",min_2015,max_2015,median_2015,q1_2015,q3_2015)
print("2019级:",min_2019,max_2019,median_2019,q1_2019,q3_2019)
# 3.绘制五数盒图
data = [data_2015,data_2019]
plt.boxplot(data)
labels = ['2015','2019'] #设置特征名称
plt.xticks(np.arange(1, len(labels) + 1), labels)
plt.show() #显示图表