Đề bài:
set.seed(123)
x <- rbinom(100, size = 10, prob = 0.3)
hist(x, breaks = seq(-0.5, 10.5, 1),
col = "lightblue",
main = "Histogram dữ liệu phân phối nhị thức",
xlab = "Giá trị",
ylab = "Tần suất")
##Kiểm định Kolmogorov–Smirnov
ks.test(x, "pbinom", size = 10, prob = 0.3)
## Warning in ks.test.default(x, "pbinom", size = 10, prob = 0.3): ties should not
## be present for the one-sample Kolmogorov-Smirnov test
##
## Asymptotic one-sample Kolmogorov-Smirnov test
##
## data: x
## D = 0.26961, p-value = 9.711e-07
## alternative hypothesis: two-sided
Lưu ý: Kiểm định Kolmogorov–Smirnov giả định phân phối liên tục. Trong trường hợp này dữ liệu tuân theo phân phối nhị thức (rời rạc) nên xuất hiện cảnh báo do các giá trị trùng lặp. Tuy nhiên, kiểm định vẫn được sử dụng để tham khảo mức độ phù hợp của phân phối.
#CÂU 2 (5 điểm) Đề bài
Đọc tập dữ liệu Iris bằng thư viện Pandas
Tính trung bình, trung vị, độ lệch chuẩn của từng đặc trưng (Sepal.Length, Sepal.Width, Petal.Length, Petal.Width)
Vẽ biểu đồ boxplot cho từng đặc trưng theo từng loài hoa (Species)
##Đọc dữ liệu Iris
import pandas as pd
import matplotlib.pyplot as plt
# Đọc dữ liệu Iris
df = pd.read_csv(
"https://raw.githubusercontent.com/mwaskom/seaborn-data/master/iris.csv"
)
##Tính các đặc trưng thống kê
df.groupby("species").agg(
mean_sepal_length=("sepal_length", "mean"),
median_sepal_length=("sepal_length", "median"),
std_sepal_length=("sepal_length", "std"),
mean_sepal_width=("sepal_width", "mean"),
median_sepal_width=("sepal_width", "median"),
std_sepal_width=("sepal_width", "std"),
mean_petal_length=("petal_length", "mean"),
median_petal_length=("petal_length", "median"),
std_petal_length=("petal_length", "std"),
mean_petal_width=("petal_width", "mean"),
median_petal_width=("petal_width", "median"),
std_petal_width=("petal_width", "std")
)
| mean_sepal_length | median_sepal_length | std_sepal_length | mean_sepal_width | median_sepal_width | std_sepal_width | mean_petal_length | median_petal_length | std_petal_length | mean_petal_width | median_petal_width | std_petal_width | |
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| species | ||||||||||||
| setosa | 5.006 | 5.0 | 0.352490 | 3.428 | 3.4 | 0.379064 | 1.462 | 1.50 | 0.173664 | 0.246 | 0.2 | 0.105386 |
| versicolor | 5.936 | 5.9 | 0.516171 | 2.770 | 2.8 | 0.313798 | 4.260 | 4.35 | 0.469911 | 1.326 | 1.3 | 0.197753 |
| virginica | 6.588 | 6.5 | 0.635880 | 2.974 | 3.0 | 0.322497 | 5.552 | 5.55 | 0.551895 | 2.026 | 2.0 | 0.274650 |
##Vẽ boxplot theo Species
import matplotlib.pyplot as plt
df.boxplot(column=["sepal_length", "sepal_width",
"petal_length", "petal_width"],
by="species",
figsize=(10, 6))
## array([[<Axes: title={'center': 'sepal_length'}, xlabel='species'>,
## <Axes: title={'center': 'sepal_width'}, xlabel='species'>],
## [<Axes: title={'center': 'petal_length'}, xlabel='species'>,
## <Axes: title={'center': 'petal_width'}, xlabel='species'>]],
## dtype=object)
plt.suptitle("")
plt.title("Boxplot các đặc trưng theo loài hoa")
plt.show()