library(ggplot2)
set.seed(123)
n_size <- 300
n_binom <- 15
p_binom <- 0.4
data_binom <- rbinom(n_size, size = n_binom, prob = p_binom)
lambda_poisson <- n_binom * p_binom
# 1. Tạo data frame cho dữ liệu mô phỏng
data_df <- data.frame(val = data_binom)
# 2. Tạo data frame riêng cho Poisson lý thuyết (chỉ lấy số nguyên từ 0-15)
poisson_points <- data.frame(
x = 0:15,
y = dpois(0:15, lambda = lambda_poisson)
)
# 3. Vẽ biểu đồ
ggplot() +
# Vẽ Histogram từ dữ liệu mô phỏng
geom_histogram(data = data_df, aes(x = val, y = ..density..),
binwidth = 1, fill = "skyblue", color = "white") +
# Vẽ đường Poisson từ bảng dữ liệu đã tính sẵn (không còn lỗi non-integer)
geom_line(data = poisson_points, aes(x = x, y = y, color = "Poisson (lambda=6)"),
linetype = "dashed", linewidth = 1) +
geom_point(data = poisson_points, aes(x = x, y = y, color = "Poisson (lambda=6)"),
size = 3) +
labs(title = "So sánh phân phối Nhị thức và Poisson",
x = "Giá trị", y = "Mật độ", color = "Phân phối") +
theme_minimal()
## Warning: The dot-dot notation (`..density..`) was deprecated in ggplot2 3.4.0.
## ℹ Please use `after_stat(density)` instead.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.

import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
# 1. Đọc dữ liệu
df = pd.read_csv('iris.csv')
# Tự động tìm cột chứa tên loài hoa (thường là 'Species' hoặc 'species')
# và các cột đặc trưng (Sepal.Length, Petal.Length, ...)
cols = df.columns.tolist()
target_col = [c for c in cols if 'spec' in c.lower() or 'variety' in c.lower()][0]
# 2. Tính giá trị trung bình theo từng loại hoa
print("Giá trị trung bình của từng đặc trưng:")
## Giá trị trung bình của từng đặc trưng:
mean_values = df.groupby(target_col).mean()
print(mean_values)
## sepal.length sepal.width petal.length petal.width
## variety
## 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
# 3. Vẽ biểu đồ scatter giữa Sepal.Length (cột 0) và Petal.Length (cột 2)
plt.figure(figsize=(10, 6))
sns.scatterplot(data=df, x=cols[0], y=cols[2], hue=target_col)
plt.title('Biểu đồ Scatter Iris (Python)')
plt.xlabel(cols[0])
plt.ylabel(cols[2])
plt.show()
