- 数据分析
- 数据治理
- 产品解决方案
- 行业解决方案
- 案例
- 数据资产入表
- 赋能中心
- 伙伴
- 关于
时间:2022-07-05来源:和你遇见浏览数:104次
数据离散程度的度量,接近 0,离散度小,越大,离散程度也大;极差就是最大最小值之间的差值;一个包含 0 或 100 的数据集,与一个 [0,50,...,50,100] 的极差相同;一种更复杂的离散度的度量方式为方差(variance);因为方差很难理解,是原始值的平方,所以一般习惯使用标准差
1.1描述单个数据集
描述数据集简单的方式就是用列表去描述:num_friends = [100, 99, 41, 25]
对于足够量小的数据集上述描述已经足够明确,但是数据集较大时,该方法既不实用,也不直观,盯着 100 万的数看显然不够直观,就需要用统计学来提取和表达数据的相关特征;第一种方式就是使用 Counter 和 plt.bar 将数量放入直方图中;利用 Counter 统计每一个数字出现的次数; # -*- coding: utf-8 -*-"""Spyder EditorAThis is a temporary script file."""from collections import Counterimport matplotlib.pyplot as pltnum_friends = [100, 55, 99, 24, 24, 55]friend_counts = Counter(num_friends)xs = range(101)ys = [friend_counts[x] for x in xs]plt.bar(xs, ys)plt.axis([, 101, , 5])plt.title("Histogram of Friend Counts")plt.xlabel("# of freinds")plt.ylabel("# of people")plt.show()但是这样的图依然难与人沟通,需要计算一些统计量,比如计算样本大小,最大最小值等等;
1.2中心倾向
通常了解数据中心,一般采用均值;如果有两个数据点,均值就是它们的中间点;当添加更多数据点时,均值也会随之移动;有时候也会对中位数(median)感兴趣,是中间的点值或者中间两个点的均值,取决于数据集是奇数还是偶数;中位数的一个泛化概念是中位数(quantile),标识在排序后的数据中某个百分比位置的值(中位数表示在 50% 位置的数据的值); # -*- coding: utf-8 -*-"""Spyder EditorAThis is a temporary script file."""from typing import Listdef quantile(xs:List[float], p:float) -> float:p_index = int(p * len(xs))return sorted(xs)[p_index]print(quantile([1, 3, 4, 1, 2], 0.25)) #1众数(mode):出现次数最多的一个或多个值; # -*- coding: utf-8 -*-"""Spyder EditorAThis is a temporary script file."""from typing import List,Counterdef mode(xs: List[float]) -> List[float]:"""因为众数可能有多个,所以需要返回一个列表"""counts = Counter(xs)max_counts = max(counts.values())return [x_i for x_i, count in counts.items() if count == max_counts]print(mode([1,2,3,41,1,2])) #[1, 2]1.3离散度
离散度(dispersion):数据离散程度的度量,接近 0,离散度小,越大,离散程度也大;极差就是最大最小值之间的差值;一个包含 0 或 100 的数据集,与一个 [0,50,...,50,100] 的极差相同;一种更复杂的离散度的度量方式为方差(variance);因为方差很难理解,是原始值的平方,所以一般习惯使用标准差;# -*- coding: utf-8 -*-"""Spyder EditorAThis is a temporary script file."""from typing import Listfrom statistics import meanimport mathdef decline_mean(x: List[float]) -> List[float]:x_mean = mean(x)return [x_i - x_mean for x_i in x]def sum_of_squares(x: List[float]) -> float:list_squares = [x_i * x_i for x_i in x]return sum(list_squares)# 计算方差def variance(xs: List[float]) -> float:n = len(xs)deviations = decline_mean(xs)return sum_of_squares(deviations) / (n - 1)# 计算标准差def standard_variance(xs: List[float]) -> float:return math.sqrt(variance(xs))print(variance([1,2,3,4])) #1.6666666666666667 方差print(standard_variance([1,2,3,4])) #1.2909944487358056 标准差极差和标准差都有异常值问题,更稳健的替代方案是计算 75% 和 25% 的分位数之差:这样不会受到一小部分异常值的影响; # -*- coding: utf-8 -*-"""Spyder EditorAThis is a temporary script file."""from typing import Listdef quantile(xs: List[float], p: float) -> float:p_index = int(p * len(xs))return sorted(xs)[p_index]def interquartile_range(xs: List[float]) -> float:return quantile(xs, 0.75) - quantile(xs, 0.25)print(interquartile_range([1, 3, 4, 1, 2])) #2
1.4相关
比如想要看用户在网站上花费的时间与其在该网站上拥有的朋友数量相关;命名一个为 daily_minutes 的列表,该列表中的元素与之前 num_friends 列表的元素对应,以进一步探索关系;协方差:方差的孪生兄弟;方差衡量单个变量对其均值的偏离程度,协方差衡量两个变量对其均值的共同偏离程度;from typing import Listfrom statistics import meanVector = List[float]def covariance(xs: List[float], ys: List[float]) -> float:assert len(xs) == len(ys), "must have same number of elements"return dot(decline_mean(xs), decline_mean(ys)) / (len(xs) - 1)def dot(v: Vector, w: Vector) -> Vector:# 判定长度是否相同assert len(v) == len(w), "vector have same length"return sum(v_i * w_i for v_i, w_i in zip(v, w))def decline_mean(x: List[float]) -> List[float]:x_mean = mean(x)return [x_i - x_mean for x_i in x]print(covariance([1,2], [2,3])) #0.5协方差很难解释的原因: 协方差的单位是 朋友量/分钟/天,这很难理解; 如果每个用户朋友数是之前两倍,分钟数不变;但从某种意义上,变量的相关度是一样的; 由于以上原因,相关性系数(correlation)是更常用的概念,是协方差除以两个变量的标准差的值;相关系数没有单位,取值范围是 -1(完全负相关)~1(完全正相关),0.25 就是比较弱的正相关;
上一篇:教练式管理工具与技术...
下一篇:智能制造10步走...