<jupyter_start><jupyter_text>Exercise 4.01: Comparing IQ Scores for Different Test Groups by Using a Box PlotIn this exercise, we will compare IQ scores among different test groups using a box plot of the Seaborn library to demonstrate how easy and efficient it is to create plots with Seaborn provided that we have a proper DataFrame. This exercise also shows how to quickly change the style and context of a Figure using the preconfigurations supplied by Seaborn.Let's compare IQ scores among different test groups using the Seaborn library: Import the necessary modules and enable plotting within a Jupyter Notebook.<jupyter_code>%matplotlib inline
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns<jupyter_output><empty_output><jupyter_text>Use the pandas read_csv() function to read the data located in the data folder:<jupyter_code>mydata = pd.read_csv("../../Datasets/iq_scores.csv")<jupyter_output><empty_output><jupyter_text>Access the data of each test group in the column. Convert them into a list using the tolist() method. Once the data of each test group has been converted into a list, assign this list to variables of each respective test group:<jupyter_code>group_a = mydata[mydata.columns[0]].tolist()
group_b = mydata[mydata.columns[1]].tolist()
group_c = mydata[mydata.columns[2]].tolist()
group_d = mydata[mydata.columns[3]].tolist()<jupyter_output><empty_output><jupyter_text>Print the values of each group to check whether the data inside it is converted into a list. This can be done with the help of the print() function:<jupyter_code>print(group_a)
print(group_b)
print(group_c)
print(group_d)<jupyter_output>[93, 99, 91, 110, 80, 113, 111, 115, 98, 74, 96, 80, 83, 102, 60, 91, 82, 90, 97, 101, 89, 89, 117, 91, 104, 104, 102, 128, 106, 111, 79, 92, 97, 101, 106, 110, 93, 93, 106, 108, 85, 83, 108, 94, 79, 87, 113, 112, 111, 111, 79, 116, 104, 84, 116, 111, 103, 103, 112, 68, 54, 80, 86, 119, 81, 84, 91, 96, 116, 125, 99, 58, 102, 77, 98, 100, 90, 106, 109, 114, 102, 102, 112, 103, 98, 96, 85, 97, 110, 131, 92, 79, 115, 122, 95, 105, 74, 85, 85, 95]<jupyter_text>Once we have the data for each test group, we need to construct a DataFrame from this given data. This can be done with the help of the pd.DataFrame() function, which is provided by pandas.<jupyter_code>data = pd.DataFrame({'Groups': ['Group A'] * len(group_a) + ['Group B'] * len(group_b) + ['Group C'] * len(group_c) + ['Group D'] * len(group_d),
                     'IQ score': group_a + group_b + group_c + group_d})<jupyter_output><empty_output><jupyter_text>If you don’t create your own DataFrame, it is often helpful to print the column names, which is done by calling print(data.columns). The output is as follows:<jupyter_code>print(data.columns)<jupyter_output>Index(['Groups', 'IQ score'], dtype='object')<jupyter_text>You can see that our DataFrame has two variables with the labels Groups and IQ score. This is especially interesting since we can use them to specify which variable to plot on the x-axis and which one on the y-axis. Now, since we have the DataFrame, we need to create a box plot using the boxplot() function provided by Seaborn. Within this function, specify the variables for both the axes along with the DataFrame. Make Groups the variable to plot on the x-axis and IQ score the variable for the y-axis. Pass data as a parameter. Here, data is the DataFrame that we obtained from the previous step. Moreover, use the whitegrid style, set the context to talk, and remove all axes splines, except the one on the bottom.<jupyter_code>plt.figure(dpi=300)
# Set style to 'whitegrid'
sns.set_style('whitegrid')
# Set context to 'talk'
sns.set_context('talk')
# Create boxplot
sns.boxplot('Groups', 'IQ score', data=data)
# Despine
sns.despine(left=True, right=True, top=True)
# Add title
plt.title('IQ scores for different test groups')
# Show plot
plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.09759797724399494, 'fraction_numerical': 0.06498103666245258, 'mean_word_length': 5.18125, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 5, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '362726', 'license': 'MIT', 'n_tokens_mistral': 1502, 'n_tokens_neox': 1222, 'n_words': 546, 'path': 'Chapter04/Exercise4.01/Exercise4.01.ipynb', 'repo_name': 'madhunikitac/The-Data-Visualization-Workshop', 'size': '3955'}