Lab Assignment 02: Exploring Palmer Penguins Dataset

Objective: In this lab, you will explore the Palmer Penguins dataset to:

Instructions

Provide citation for any portion that of the code that you consulted external sources. Note you cannot use genAI to generate solutions. You are ONLY allowed to debug your code with AI assistance and you must indicate clear where (which part of the code) you used the AI assitance to debug

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

  1. Import pandas and matplotlib libraries

  2. Import data and name it dataset

  3. Display the first 5 columns

  4. Identify the shape of the dataset

  5. Identify the information about features types

Questions
  • 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.

  1. Import seaborn library

  2. import seaborn as sns
  3. Seaborn's pairplot() creates a grid of pairwise scatterplots for numerical features in the penguins dataset:
  4. p = sns.pairplot(dataset)
    plt.show()
    
  5. Increase the font for all future plots, using update(). Then rerun your pairplot:
  6. plt.rcParams.update({'font.size': 14})
  7. Add categorical values to the plot. The hue parameter adds color based on the values of the island column:
  8. p = sns.pairplot(dataset, hue='island')
    plt.show()
    
  9. Replace island by species category
  10. Create a contingency table (=crosstable) for species and island
  11. pd.crosstab(index=dataset['species'], columns=dataset['island'])
  12. Create a barplot with species but instead of using barplot we will use a simplified version with seaborn
Questions
  • 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

  1. Calculate descriptive statistics using describe()
  2. 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
  3. Create histograms for numeric data (should be 4). See how to drop the last column year:
  4. To drop year column dataset.drop(columns=["year"]).hist()
  5. Adjust the figure size to fit 4 histograms better. Then rerun your plot
  6. plt.rcParams["figure.figsize"] = (12, 10)
  7. 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
  8. Add title (Come up with a good title)

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

  1. Use pd.isna() to identify missing values for bill length
  2. dataset[pd.isna(dataset['bill_length_mm'])]
  3. Display missing data counts for all columns (see In-Class Labs)
  4. Import missingno library - see In-Class Practice
  5. Use bar() amd matrix() to visualize data
Questions
  • 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)

  1. Impute bill_length_mm column with mean()
  2. #Follow this format:
    dataset['column'] = dataset['column'].fillna(dataset['column'].name()) 
    # where name is the imputation method
        
  3. Impute bill_depth_mm column with median()
  4. Impute flipper_length_mm column with your own choice of method
  5. Impute categorical missing data with the word "Unknown". HINT: it will be just the string value
  6. Make sure all missing data is imputed. Run info()
  7. Finally, display info() with dropped na dataset
Questions
  • 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

  1. Step 1 Understanding data 15pts (all questions are answered)
  2. Step 2 Exploring Relations 20pts (all questions are answered
  3. Step 3 Shape of Data 15pts
  4. Step 4 Missing data 20pts (all questions are answered)
  5. Step 5 Imputation 20pts (all questions are answered)
  6. Submission, Code, Format, Help citation (10 Points)