Lab Assignment 02: Exploring Palmer Penguins Dataset
Objective: In this lab, you will explore the Palmer Penguins dataset to:
- identify missing data
- visualize data
- impute values
- uncover interesting insights
Instructions
Make sure to answer any questions marked as QUESTION (in bold)
Submission: code cells should be executed and show the output and graphs. If you have collaborated with another student, state their name, but the submission and the notebook must be individual (no shared URL allowed). Set the Shared URL Link to Viewable publicly and paste the link to Gradescope assignment Lab02
Step 1. Understanding Penguin dataset (15pts)
The first step of exploratory data analysis is to explore the number of instances, number of features, and type of each feature
Download palmer_penguin.csv data from the course schedule
Create a new colab notebook. Make sure you add your name
-
Import pandas and matplotlib libraries
-
Import data and name it dataset
Display the first 5 columns
Identify the shape of the dataset
Identify the information about features types
- How many instances are in the penguins dataset?
- How many features are in the penguins dataset?
- Which columns contain the categorical features in the dataset?
Step 2 Exploring Relationships (20 Points)
Once the dataset's size and features are understood, we can explore the relationships between features. Data visualizations like scatter plots give a visual representation of how pairs of features change together. Instead of creating individual scatteplots, you will learn how to use pair plot with seaborn library.
Import seaborn library
- Seaborn's pairplot() creates a grid of pairwise scatterplots for numerical features in the penguins dataset:
- Increase the font for all future plots, using update(). Then rerun your pairplot:
- Add categorical values to the plot. The hue parameter adds color based on the values of the island column:
- Replace island by species category
- Create a contingency table (=crosstable) for species and island
- Create a barplot with species but instead of using barplot we will use a simplified version with seaborn
- Take a look at https://seaborn.pydata.org/generated/seaborn.countplot.html
- Create a countplot for species
- Create a countplot for species but color-coded with island
import seaborn as sns
p = sns.pairplot(dataset)
plt.show()
plt.rcParams.update({'font.size': 14})
p = sns.pairplot(dataset, hue='island')
plt.show()
pd.crosstab(index=dataset['species'], columns=dataset['island'])
- Which island has only Adelie penguins?
- What is the relationship between bill length and body mass?
- Look at the pairplot histogram of bill lengths (top left), and then examine the same plot with hues for species and island. Describe any interesting observation(s)
Step 3 Describe the shape of data (15 Points)
The shape of a feature's distribution is important way to examine if data is normally distributed, or skewed
- Calculate descriptive statistics using describe()
- Take a look at the documentation https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.describe.html and include all data. The default is only for numerical
- Create histograms for numeric data (should be 4). See how to drop the last column year: To drop year column dataset.drop(columns=["year"]).hist()
- Adjust the figure size to fit 4 histograms better. Then rerun your plot
- Change the size of the bins, try different numbers. Check out how to change bins: https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.hist.html
- Add title (Come up with a good title)
plt.rcParams["figure.figsize"] = (12, 10)
Step 4 Detect missing data (20pts)
If only a few instances have missing data, those instances may be set aside without having a negative impact on a model. if many instances have missing data, techniques like imputation may be needed
- Use pd.isna() to identify missing values for bill length
- Display missing data counts for all columns (see In-Class Labs)
- Import missingno library - see In-Class Practice
- Use bar() amd matrix() to visualize data
dataset[pd.isna(dataset['bill_length_mm'])]
- Which columns have missing values?
- How many penguins are missing information about bill length?
- What type of missing data are the numerical measurements for these two penguins? HINT: are there any observable patterns for these penguins (HINT:if yes -> MAR; if no -> MCAR)
Step 5 Impute missing data (20 Points)
- Impute bill_length_mm column with mean()
- Impute bill_depth_mm column with median()
- Impute flipper_length_mm column with your own choice of method
- Impute categorical missing data with the word "Unknown". HINT: it will be just the string value
- Make sure all missing data is imputed. Run info()
- Finally, display info() with dropped na dataset
#Follow this format:
dataset['column'] = dataset['column'].fillna(dataset['column'].name())
# where name is the imputation method
- Currently, you used a placeholder "Unknown" for categorical data. How this method can be useful? What other methods could you apply to categorical data?
- What did you learn in this lab? Do you feel prepared now to explore your own data for homework next week?
Grading Rubrics
- Step 1 Understanding data 15pts (all questions are answered)
- Step 2 Exploring Relations 20pts (all questions are answered
- Step 3 Shape of Data 15pts
- Step 4 Missing data 20pts (all questions are answered)
- Step 5 Imputation 20pts (all questions are answered)
- Submission, Code, Format, Help citation (10 Points)