#1.Sinh 600 số ngẫu nhiên từ phân phối nhị thức
set.seed(123)
n <- 20
p <- 0.5
data <- rbinom(600, n , p )
# 2.Vẽ biểu đồ histogram
hist(data, col="blue", main="Histogram", xlab="Giá trị", ylab="Tần suất", probability=TRUE)
#Vẽ đường cong phân phối chuẩn
mean_val <- n * p
sd_val <- sqrt(n * p * (1 - p))
x <- seq(min(data), max(data), length=100)
y <- dnorm(x, mean=mean_val, sd=sd_val)
lines(x, y, col="red", lwd=2)

# 1. Đọc tập dữ liệu Iris
data(iris)
# 2. Tính giá trị trung bình của từng đặc trưng theo từng loại hoa
mean_values <- aggregate(. ~ Species, data=iris, FUN=mean)
print(mean_values)
## Species Sepal.Length Sepal.Width Petal.Length Petal.Width
## 1 setosa 5.006 3.428 1.462 0.246
## 2 versicolor 5.936 2.770 4.260 1.326
## 3 virginica 6.588 2.974 5.552 2.026
# 3. Tính độ lệch chuẩn của từng đặc trưng theo từng loại hoa
sd_values <- aggregate(. ~ Species, data=iris, FUN=sd)
print(sd_values)
## Species Sepal.Length Sepal.Width Petal.Length Petal.Width
## 1 setosa 0.3524897 0.3790644 0.1736640 0.1053856
## 2 versicolor 0.5161711 0.3137983 0.4699110 0.1977527
## 3 virginica 0.6358796 0.3224966 0.5518947 0.2746501
# 4. Vẽ biểu đồ Boxplot cho chiều rộng đài hoa (Sepal.Width)
boxplot(Sepal.Width ~ Species, data=iris, col="lightblue",
main="Boxplot của Sepal Width theo loài",
xlab="Loài hoa", ylab="Chiều rộng đài hoa (Sepal Width)")

import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
# 1. Đọc tập dữ liệu Iris bằng Pandas
iris = sns.load_dataset("iris") # Dữ liệu có sẵn trong seaborn
# 2. Tính giá trị trung bình và độ lệch chuẩn của từng đặc trưng theo từng loại hoa
mean_values = iris.groupby("species").mean()
std_values = iris.groupby("species").std()
print("Giá trị trung bình:\n", mean_values)
## Giá trị trung bình:
## sepal_length sepal_width petal_length petal_width
## species
## setosa 5.006 3.428 1.462 0.246
## versicolor 5.936 2.770 4.260 1.326
## virginica 6.588 2.974 5.552 2.026
print("\nĐộ lệch chuẩn:\n", std_values)
##
## Độ lệch chuẩn:
## sepal_length sepal_width petal_length petal_width
## species
## setosa 0.352490 0.379064 0.173664 0.105386
## versicolor 0.516171 0.313798 0.469911 0.197753
## virginica 0.635880 0.322497 0.551895 0.274650
# 3. Vẽ biểu đồ Boxplot của chiều rộng đài hoa (Sepal.Width)
plt.boxplot([iris[iris["species"] == sp]["sepal_width"] for sp in iris["species"].unique()],
tick_labels=iris["species"].unique())
## {'whiskers': [<matplotlib.lines.Line2D object at 0x00000234C70B6290>, <matplotlib.lines.Line2D object at 0x00000234C707B650>, <matplotlib.lines.Line2D object at 0x00000234C70B6D10>, <matplotlib.lines.Line2D object at 0x00000234C70B7910>, <matplotlib.lines.Line2D object at 0x00000234C70C02D0>, <matplotlib.lines.Line2D object at 0x00000234C70C0F50>], 'caps': [<matplotlib.lines.Line2D object at 0x00000234C6FFBD50>, <matplotlib.lines.Line2D object at 0x00000234C6FFB150>, <matplotlib.lines.Line2D object at 0x00000234C700FA90>, <matplotlib.lines.Line2D object at 0x00000234C700EE90>, <matplotlib.lines.Line2D object at 0x00000234C70C1B90>, <matplotlib.lines.Line2D object at 0x00000234C70C2790>], 'boxes': [<matplotlib.lines.Line2D object at 0x00000234C6B1A510>, <matplotlib.lines.Line2D object at 0x00000234C6FF8910>, <matplotlib.lines.Line2D object at 0x00000234C700C990>], 'medians': [<matplotlib.lines.Line2D object at 0x00000234C6FFA490>, <matplotlib.lines.Line2D object at 0x00000234C700E210>, <matplotlib.lines.Line2D object at 0x00000234C70C32D0>], 'fliers': [<matplotlib.lines.Line2D object at 0x00000234C6FF9850>, <matplotlib.lines.Line2D object at 0x00000234C700D5D0>, <matplotlib.lines.Line2D object at 0x00000234C70C3E90>], 'means': []}
plt.title("Boxplot của Sepal Width theo loài")
plt.xlabel("Loài hoa")
plt.ylabel("Chiều rộng đài hoa (Sepal Width)")
plt.show()
