S = [s_1, s_2, ..., s_n]
样本中的每个值都是真值与误差的和。
mean = \frac{(s_1 + s_2 + ... + s_n) }{n}
算数平均值表示对真值的无偏估计。
m = np.mean(array)
m = array.mean()
m = df.mean(axis=0)
案例:针对电影评分数据做均值分析:
mean = ratings['John Carson'].mean()
mean = np.mean(ratings['John Carson'])
means = ratings.mean(axis=1)
求平均值时,考虑不同样本的重要性,可以为不同的样本赋予不同的权重。
样本:S = [s_1, s_2, s_3 ... s_n]
权重:W =[w_1, w_2, w_3 ... w_n]
加权平均值:
a = \frac{s_1w_1 + s_2w_2 + ... + s_nw_n}{w_1+w_2+...+w_n}
代码实现:
a = np.average(array, weights=volumes)
案例:自定义权重,求加权平均。
# 加权均值
w = np.array([3,1,1,1,1,1,1])
np.average(ratings.loc['Inception'], weights=w)
mask = ~pd.isna(ratings.loc['Inception'])
np.average(ratings.loc['Inception'][mask], weights=w[mask])
np.max() / np.min() / np.ptp()
:返回一个数组中最大值/最小值/极差(最大值减最小值)
import numpy as np
# 产生9个介于[10, 100)区间的随机数
a = np.random.randint(10, 100, 9)
print(a)
print(np.max(a), np.min(a), np.ptp(a))
np.argmax() np.argmin()
和 pd.idxmax() pd.idxmin()
: 返回一个数组中最大/最小元素的下标
# 在np中,使用argmax获取到最大值的下标
print(np.argmax(a), np.argmin(a))
# 在pandas中,使用idxmax获取到最大值的下标
print(series.idxmax(), series.idxmin())
print(dataframe.idxmax(), dataframe.idxmin())
将多个样本按照大小排序,取中间位置的元素。
若样本数量为奇数,中位数为最中间的元素
[1, 2000, 3000, 4000, 10000000]
若样本数量为偶数,中位数为最中间的两个元素的平均值
[1,2000,3000,4000,5000,10000000]
案例:分析中位数的算法,测试numpy提供位数API
np.median() 中位数
import numpy as np
closing_prices = np.loadtxt('../../data/aapl.csv',
delimiter=',', usecols=(6), unpack=True)
size = closing_prices.size
sorted_prices = np.msort(closing_prices)
median = (sorted_prices[int((size - 1) / 2)] +
sorted_prices[int(size / 2)]) / 2
print(median)
median = np.median(closing_prices)
print(median)
可以评估一组数据的震荡幅度,到底稳定不稳定
样本(sample): S = [s_1, s_2, s_3, ..., s_n]
平均值: m = \frac{s_1 + s_2 + s_3 + ... + s_n}{n}
离差(deviation):表示某组数据距离某个中心点的偏离程度 D = [d_1, d_2, d_3, ..., d_n] d_i = S_i-m 用每一个数据,减去均值,得到离差 如果离差的绝对值比较大,距离中心点比较远,所以震荡幅度大 离差有正有负,做离差方,将其全都变成正数
离差方: Q = [q_1, q_2, q_3, ..., q_n] q_i=d_i^2
总体方差(variance): v = \frac{(q_1+q_2+q_3 + ... + q_n)}{n} 最整组离差方 , /n 得到离差的均值,为方差 方差越大,震荡越剧烈 方差越小,震荡越平缓
总体标准差(standard deviation): s = \sqrt{v}
样本方差: v' = \frac{(q_1+q_2+q_3 + ... + q_n)}{n-1} , v = \frac{(q_1+q_2+q_3 + ... + q_n)}{n} 其中,n-1称之为“贝塞尔校正”,这是因为抽取样本时候,采集的样本主要是落在中心值附近,那么通过这些样本计算的方差会小于等于对总体数据集方差的无偏估计值。为了能弥补这方面的缺陷,那么我们把公式的n改为n-1,以此来提高方差的数值。称为贝塞尔校正系数。
样本标准差: s' = \sqrt{v'}
案例: 根据标准差理论,针对评分数据进行方差分析:
ratings.std(axis=0)
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有