=(n−1)σxσy∑i=1n(xi−xˉ)(yi−yˉ)(3)(4)
保留四位的结果为 0.8657。
import numpy as np
x = [23, 23, 27, 27, 39, 41, 47, 49, 50, 52, 54, 54, 56, 57, 58, 58, 60, 61]
y = [9.5, 26.5, 7.8, 17.8, 31.4, 25.9, 27.4, 27.2, 31.2, 34.6, 42.5, 28.8, 33.4, 30.2, 34.1, 32.9, 41.2, 35.7]
x, y = np.array(x), np.array(y)
bar_x, bar_y = x.mean(), y.mean()
# std_x, std_y = x.std(ddof=1), y.std(ddof=1) # 样本标准差
std_x, std_y = x.std(), y.std() # 总体标准差
# r = np.sum((x - bar_x) * (y - bar_y)) / ((len(x)-1) * std_x * std_y) # 样本
r = np.sum((x - bar_x) * (y - bar_y)) / ((len(x)) * std_x * std_y) # 总体
print(r)
3.下面是一个超市某种商品连续 20 个月的销售数据(单位为百元)
21 | 16 | 19 | 24 | 27 | 23 | 22 | 21 | 20 | 17 | 16 | 20 | 23 | 22 | 18 | 24 | 26 | 25 | 20 | 26 |
对以上数据进行深度为 5 的 Equal-depth binning。
先排序!,排序后的序列为 {16,16,17,18,19,20,20,20,21,21,22,22,23,23,24,24,25,26,26,27}。
bin 1: 16, 16, 17, 18, 19
bin 2: 20, 20, 20, 21, 21
bin 3: 22, 22, 23, 23, 24
bin 4: 24, 25, 26, 26, 27
代码如下:
import numpy as np
x = [16,16,17,18,19,20,20,20,21,21,22,22,23,23,24,24,25,26,26,27]
x = np.array(x)
equal_depth_binning = np.array_split(x, [i for i in range(5, len(x), 5)])
print(equal_depth_binning)
bin 1: 17.2, 17.2, 17.2, 17.2, 17.2
bin 2: 20.4, 20.4, 20.4, 20.4, 20.4
bin 3: 20.8, 20.8, 20.8, 20.8, 20.8
bin 4: 25.6, 25.6, 25.6, 25.6, 25.6
bin_median_smooth = [np.round(i.mean(), 4) for i in equal_depth_binning]
print(bin_median_smooth)
bin 1: 16, 16, 16, 19, 19
bin 2: 20, 20, 20, 21, 21
bin 3: 22, 22, 24, 24, 24
bin 4: 24, 24, 27, 27, 27
bin_boundaries_smooth = [np.max(chunk) if np.max(chunk) - i < i - np.min(chunk) else np.min(chunk) for chunk in equal_depth_binning for i in chunk]
print(bin_boundaries_smooth)
原文地址:https://blog.csdn.net/weixin_46221946/article/details/134693167
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如若转载,请注明出处:http://www.7code.cn/show_7077.html
如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱:suwngjj01@126.com进行投诉反馈,一经查实,立即删除!