Phân tích dữ liệu khám phá với các ví dụ mã Python
Phân tích dữ liệu khám phá (EDA) là một bước quan trọng trong quy trình khoa học dữ liệu giúp hiểu cấu trúc và nội dung của dữ liệu. Trong bài viết này, chúng tôi sẽ giới thiệu về EDA trong Python, cùng với một số ví dụ mã sử dụng thư viện Pandas và Matplotlib.
Nhập dữ liệu
Bước đầu tiên trong EDA là nhập dữ liệu vào Python. Chúng tôi sẽ sử dụng tập dữ liệu từ Kho lưu trữ học máy của UCI có chứa thông tin về chất lượng rượu vang. Bộ dữ liệu có thể được tải xuống từ đây .
Chúng tôi có thể sử dụng thư viện Pandas để nhập dữ liệu từ tệp CSV:
import pandas as pd
data = pd.read_csv('winequality-red.csv', sep=';')
Khi chúng tôi đã nhập dữ liệu, bước tiếp theo là hiểu cấu trúc và nội dung của nó. Chúng ta có thể bắt đầu bằng cách xem một vài hàng đầu tiên của dữ liệu bằng phương head()pháp:
print(data.head())
print(data.info())
Trực quan hóa dữ liệu
EDA cũng liên quan đến việc trực quan hóa dữ liệu để xác định các mẫu và mối quan hệ. Chúng ta có thể sử dụng thư viện Matplotlib để tạo biểu đồ xếp hạng chất lượng rượu:pythonCopy cod
import matplotlib.pyplot as plt
plt.hist(data['quality'])
plt.xlabel('Quality')
plt.ylabel('Count')
plt.show()
Chúng ta cũng có thể tạo một biểu đồ phân tán của hai biến để trực quan hóa mối quan hệ của chúng. Ví dụ: chúng ta có thể tạo biểu đồ phân tán nồng độ cồn so với chất lượng:pythonCopy code
plt.scatter(data['alcohol'], data['quality'])
plt.xlabel('Alcohol')
plt.ylabel('Quality')
plt.show()
Làm sạch dữ liệu
EDA cũng liên quan đến việc làm sạch dữ liệu để loại bỏ mọi giá trị bị thiếu hoặc sai. Chúng ta có thể sử dụng thư viện Pandas để xác định và xóa bất kỳ hàng nào thiếu giá trị:pythonCopy code
data.dropna(inplace=True)
Dưới đây là một số ví dụ khác về EDA trong Python:
Ví dụ 1: Bộ dữ liệu Titanic
Bộ dữ liệu Titanic là một ví dụ cổ điển được sử dụng để dạy máy học. Bộ dữ liệu chứa thông tin về hành khách trên tàu Titanic, bao gồm tuổi tác, giới tính, hạng vé và liệu họ có sống sót sau thảm họa hay không.
Đây là cách chúng tôi có thể thực hiện EDA trên bộ dữ liệu Titanic:pythonCopy code
import pandas as pd
import seaborn as sns
titanic = pd.read_csv('titanic.csv')
# Display summary statistics of the numerical columns
print(titanic.describe())
# Visualize the distribution of passenger ages
sns.histplot(data=titanic, x="Age", hue="Survived", bins=20)
# Visualize the relationship between fare and survival rate
sns.boxplot(data=titanic, x="Survived", y="Fare")
Ví dụ 2: Bộ dữ liệu Iris
The Iris dataset is another classic example used for teaching machine learning. The dataset contains information about different species of Iris flowers, including the length and width of their petals and sepals.
Here’s how we can perform EDA on the Iris dataset:
import pandas as pd
import seaborn as sns
iris = sns.load_dataset('iris')
# Display summary statistics of the numerical columns
print(iris.describe())
# Visualize the relationship between petal length and width
sns.scatterplot(data=iris, x="petal_length", y="petal_width", hue="species")
# Visualize the distribution of sepal length for each species
sns.kdeplot(data=iris, x="sepal_length", hue="species", fill=True)
Example 3: Ames Housing Dataset
The Ames Housing dataset is a real-world example used for predicting housing prices. The dataset contains information about various attributes of houses in Ames, Iowa, including their size, location, and condition.
Here’s how we can perform EDA on the Ames Housing dataset:
import pandas as pd
import seaborn as sns
housing = pd.read_csv('housing.csv')
# Display summary statistics of the numerical columns
print(housing.describe())
# Visualize the distribution of sale prices
sns.histplot(data=housing, x="SalePrice")
# Visualize the relationship between living area and sale price
sns.scatterplot(data=housing, x="GrLivArea", y="SalePrice", hue="OverallQual")
By performing EDA on these datasets, data scientists can gain valuable insights into the data, identify potential issues, and make informed decisions about which machine learning models to use.
Conclusion
EDA là một bước quan trọng trong quy trình khoa học dữ liệu giúp hiểu được cấu trúc và nội dung của dữ liệu. Trong bài viết này, chúng tôi đã giới thiệu về EDA trong Python, cùng với một số ví dụ mã sử dụng thư viện Pandas và Matplotlib. Chúng tôi đã đề cập đến cách nhập và hiểu dữ liệu, cách trực quan hóa dữ liệu và cách làm sạch dữ liệu.
Bằng cách thực hiện EDA, các nhà khoa học dữ liệu có thể xác định các vấn đề tiềm ẩn với dữ liệu có thể ảnh hưởng đến độ chính xác của kết quả cuối cùng.

![Dù sao thì một danh sách được liên kết là gì? [Phần 1]](https://post.nghiatu.com/assets/images/m/max/724/1*Xokk6XOjWyIGCBujkJsCzQ.jpeg)



































