Nhập môn Thống kê với Python
Maggie Matsui
Content Developer, DataCamp

Khoảng cách trung bình từ mỗi điểm dữ liệu đến trung bình của dữ liệu

Khoảng cách trung bình từ mỗi điểm dữ liệu đến trung bình của dữ liệu

1. Trừ trung bình khỏi từng điểm dữ liệu
dists = msleep['sleep_total'] -
np.mean(msleep['sleep_total'])
print(dists)
0 1.666265
1 6.566265
2 3.966265
3 4.466265
4 -6.433735
...
2. Bình phương từng khoảng cách
sq_dists = dists ** 2
print(sq_dists)
0 2.776439
1 43.115837
2 15.731259
3 19.947524
4 41.392945
...
3. Tổng bình phương khoảng cách
sum_sq_dists = np.sum(sq_dists)
print(sum_sq_dists)
1624.065542
4. Chia cho số điểm dữ liệu - 1
variance = sum_sq_dists / (83 - 1)
print(variance)
19.805677
Dùng np.var()
np.var(msleep['sleep_total'], ddof=1)
19.805677
Khi không có ddof=1, sẽ tính phương sai tổng thể thay vì phương sai mẫu:
np.var(msleep['sleep_total'])
19.567055
np.sqrt(np.var(msleep['sleep_total'], ddof=1))
4.450357
np.std(msleep['sleep_total'], ddof=1)
4.450357
dists = msleep['sleep_total'] - np.mean(msleep['sleep_total'])np.mean(np.abs(dists))
3.566701
Độ lệch chuẩn vs. độ lệch tuyệt đối trung bình
np.quantile(msleep['sleep_total'], 0.5)
10.1
$$
phân vị 0.5 = trung vị
Tứ phân vị:
np.quantile(msleep['sleep_total'], [0, 0.25, 0.5, 0.75, 1])
array([ 1.9 , 7.85, 10.1 , 13.75, 19.9 ])
import matplotlib.pyplot as plt
plt.boxplot(msleep['sleep_total'])
plt.show()

np.quantile(msleep['sleep_total'], [0, 0.2, 0.4, 0.6, 0.8, 1])
array([ 1.9 , 6.24, 9.48, 11.14, 14.4 , 19.9 ])
np.linspace(start, stop, num)
np.quantile(msleep['sleep_total'], np.linspace(0, 1, 5))
array([ 1.9 , 7.85, 10.1 , 13.75, 19.9 ])
Chiều cao hộp trong boxplot
np.quantile(msleep['sleep_total'], 0.75) - np.quantile(msleep['sleep_total'], 0.25)
5.9
from scipy.stats import iqr
iqr(msleep['sleep_total'])
5.9
Ngoại lệ (outlier): điểm dữ liệu khác biệt đáng kể so với phần còn lại
Xác định khác biệt đáng kể như thế nào? Một điểm là ngoại lệ nếu:
from scipy.stats import iqr iqr = iqr(msleep['bodywt'])lower_threshold = np.quantile(msleep['bodywt'], 0.25) - 1.5 * iqr upper_threshold = np.quantile(msleep['bodywt'], 0.75) + 1.5 * iqr
msleep[(msleep['bodywt'] < lower_threshold) | (msleep['bodywt'] > upper_threshold)]
name vore sleep_total bodywt
4 Cow herbi 4.0 600.000
20 Asian elephant herbi 3.9 2547.000
22 Horse herbi 2.9 521.000
...
msleep['bodywt'].describe()
count 83.000000
mean 166.136349
std 786.839732
min 0.005000
25% 0.174000
50% 1.670000
75% 41.750000
max 6654.000000
Name: bodywt, dtype: float64
Nhập môn Thống kê với Python