Python数据分析:蔬菜店销售额数据分析示例
导入numpy库
import numpy as np import matplotlib.pyplot as plt
定义销售额数据
sales = np.array([257, 276, 297, 252, 228, 310, 240, 228, 265, 278, 271, 292, 261, 281, 301, 274, 267, 280, 291, 258, 272, 284, 268, 303, 273, 282, 263, 322, 249, 269, 290])
(1) 计算平均值、标准差和中位数
mean_sales = np.mean(sales) std_sales = np.std(sales) median_sales = np.median(sales) print('平均销售收入为:', mean_sales, '元') print('销售收入的标准差为:', std_sales, '元') print('销售收入的中位数为:', median_sales, '元')
(2) 以10元为组距,分为10组,绘制直方图
bins = np.arange(220, 330, 10) plt.hist(sales, bins=bins, color='steelblue', edgecolor='black') plt.xlabel('销售额(元)') plt.ylabel('频数') plt.title('蔬菜店三月份每日销售额分布直方图') plt.show()
(3) 以10元为组距,分为11组,绘制茎叶图
stem_leaf = plt.stem(np.round((sales-min(sales))/10), use_line_collection=True) plt.setp(stem_leaf, 'marker', ' ', 'linecolor', 'blue', 'linewidth', 2) plt.xlabel('茎') plt.ylabel('叶') plt.title('蔬菜店三月份每日销售额茎叶图') plt.show()
(4) 计算四分位数
q1 = np.percentile(sales, 25) q3 = np.percentile(sales, 75) print('第一四分位数为:', q1, '元') print('第三四分位数为:', q3, '元')
原文地址: https://www.cveoy.top/t/topic/njYM 著作权归作者所有。请勿转载和采集!