<jupyter_start><jupyter_text>PMF of ageDo people tend to gain weight as they get older? We can answer this question by visualizing the relationship between weight and age. But before we make a scatter plot, it is a good idea to visualize distributions one variable at a time. Here, you'll visualize age using a bar chart first. Recall that all PMF objects have a `.bar()` method to make a bar chart.The BRFSS dataset includes a variable, `'AGE'` (note the capitalization!), which represents each respondent's age. To protect respondents' privacy, ages are rounded off into 5-year bins. `'AGE'` contains the midpoint of the bins.Instructions1. Extract the variable `'AGE'` from the DataFrame `brfss` and assign it to `age`.2. Get the PMF of `age` and plot it as a bar chart.<jupyter_code>import pandas as pd
import matplotlib.pyplot as plt
from empiricaldist import Pmf

brfss = pd.read_hdf('Behavioral_Risk_Factor_Surveillance_System_BRFSS.hdf5')
# Extract age
age = brfss['AGE']

# Plot the PMF
# pmf_age = Pmf(age)
pmf_age = Pmf.from_seq(age)
pmf_age.bar()

# Label the axes
plt.xlabel('Age in years')
plt.ylabel('PMF')
plt.show()<jupyter_output><empty_output><jupyter_text>Scatter plotNow let's make a scatterplot of `weight` versus `age`. To make the code run faster, I've selected only the first 1000 rows from the `brfss` DataFrame.`weight` and `age` have already been extracted for you. Your job is to use `plt.plot()` to make a scatter plot.Instructions1. Make a scatter plot of `weight` and `age` with format string `'o'` and `alpha=0.1`.<jupyter_code># Select the first 1000 respondents
brfss_1000 = brfss[:1000]

# Extract age and weight
age = brfss_1000['AGE']
weight = brfss_1000['WTKG3']

# Make a scatter plot
plt.plot(age, weight, 'o', alpha=0.1)

plt.xlabel('Age in years')
plt.ylabel('Weight in kg')

plt.show()<jupyter_output><empty_output><jupyter_text>JitteringIn the previous exercise, the ages fall in columns because they've been rounded into 5-year bins. If we jitter them, the scatter plot will show the relationship more clearly. Recall how Allen jittered `height` and `weight` in the video:```height_jitter = height + np.random.normal(0, 2, size=len(brfss))weight_jitter = weight + np.random.normal(0, 2, size=len(brfss))```Instructions1. Add random noise to `age` with mean `0` and standard deviation `2.5`.2. Make a scatter plot between `weight` and `age` with marker size `5` and `alpha=0.2`. Be sure to also specify `'o'`.<jupyter_code>import numpy as np
# Select the first 1000 respondents
brfss_1000 = brfss[:1000]

# Add jittering to age
age = brfss_1000['AGE'] + np.random.normal(0, 2.5, size=len(brfss_1000))
# Extract weight
weight = brfss_1000['WTKG3']

# Make a scatter plot
plt.plot(age, weight, 'o', markersize=5, alpha=0.2)

plt.xlabel('Age in years')
plt.ylabel('Weight in kg')
plt.show()<jupyter_output><empty_output><jupyter_text>Height and weightPreviously we looked at a scatter plot of height and weight, and saw that taller people tend to be heavier. Now let's take a closer look using a box plot. The `brfss` DataFrame contains a variable `'_HTMG10'` that represents height in centimeters, binned into 10 cm groups.Recall how Allen created the box plot of `'AGE'` and `'WTKG3'` in the video, with the y-axis on a logarithmic scale:```sns.boxplot(x='AGE', y='WTKG3', data=data, whis=10)plt.yscale('log')```Instructions1. Fill in the parameters of `.boxplot()` to plot the distribution of weight (`'WTKG3'`) in each height (`'_HTMG10'`) group. Specify `whis=10`, just as was done in the video.2. Add a line to plot the y-axis on a logarithmic scale.<jupyter_code>import seaborn as sns

plt.rcParams['figure.figsize'] = [10, 5]
# Drop rows with missing data
data = brfss.dropna(subset=['_HTMG10', 'WTKG3'])

# Make a box plot
sns.boxplot(x='_HTMG10', y='WTKG3', data=data, whis=10)

# Plot the y-axis on a log scale
plt.yscale('log')

# Remove unneeded lines and label axes
sns.despine(left=True, bottom=True)
plt.xlabel('Height in cm')
plt.ylabel('Weight in kg')
plt.show()<jupyter_output><empty_output><jupyter_text>Distribution of incomeIn the next two exercises we'll look at relationships between income and other variables. In the BRFSS, income is represented as a categorical variable; that is, respondents are assigned to one of 8 income categories. The variable name is `'INCOME2'`. Before we connect income with anything else, let's look at the distribution by computing the PMF. Recall that all `Pmf` objects have a `.bar()` method.Instructions1. Extract `'INCOME2'` from the `brfss` DataFrame and assign it to `income`.2. Plot the PMF of `income` as a bar chart.<jupyter_code># Extract income
income = brfss['INCOME2']

# Plot the PMF
# Pmf(income).bar()
Pmf.from_seq(income).bar()

# Label the axes
plt.xlabel('Income level')
plt.ylabel('PMF')
plt.show()<jupyter_output><empty_output><jupyter_text>ncome and heightLet's now use a violin plot to visualize the relationship between income and height.Instructions1. Create a violin plot to plot the distribution of height (`'HTM4'`) in each income (`'INCOME2'`) group. Specify `inner=None` to simplify the plot.<jupyter_code># Drop rows with missing data
data = brfss.dropna(subset=['INCOME2', 'HTM4'])

# Make a violin plot
sns.violinplot(x='INCOME2', y='HTM4', data=data, inner=None)

# Remove unneeded lines and label axes
sns.despine(left=True, bottom=True)
plt.xlabel('Income level')
plt.ylabel('Height in cm')
plt.show()<jupyter_output><empty_output><jupyter_text>Computing correlationsThe purpose of the BRFSS is to explore health risk factors, so it includes questions about diet. The variable `'_VEGESU1'` represents the number of servings of vegetables respondents reported eating per day.Let's see how this variable relates to age and income.Instructions1. From the `brfss` DataFrame, select the columns `'AGE'`, `'INCOME2'`, and `'_VEGESU1'`.2. Compute the correlation matrix for these variables.<jupyter_code># Select columns
columns = ['AGE', 'INCOME2', '_VEGESU1']
subset = brfss[columns]

# Compute the correlation matrix
subset.corr()<jupyter_output><empty_output><jupyter_text>Interpreting correlationsIn the previous exercise, the correlation between income and vegetable consumption is about `0.12`. The correlation between age and vegetable consumption is about `-0.01`.Which of the following are correct interpretations of these results:- _A_: People with higher incomes eat more vegetables.- _B_: The relationship between income and vegetable consumption is linear.- _C_: Older people eat more vegetables.- _D_: There could be a strong nonlinear relationship between age and vegetable consumption. A and D only.  Income and vegetablesAs we saw in a previous exercise, the variable `'_VEGESU1'` represents the number of vegetable servings respondents reported eating per day.Let's estimate the slope of the relationship between vegetable consumption and income.Instructions1. Extract the columns `'INCOME2'` and `'_VEGESU1'` from `subset` into `xs` and `ys` respectively.2. Compute the simple linear regression of these variables.<jupyter_code>from scipy.stats import linregress

# Extract the variables
subset = brfss.dropna(subset=['INCOME2', '_VEGESU1'])
xs = subset['INCOME2']
ys = subset['_VEGESU1']

# Compute the linear regression
res = linregress(xs, ys)
res<jupyter_output><empty_output><jupyter_text>Fit a lineContinuing from the previous exercise:- Assume that `xs` and `ys` contain income codes and daily vegetable consumption, respectively, and- `res` contains the results of a simple linear regression of `ys` onto `xs`.Now, you're going to compute the line of best fit. NumPy has been imported for you as `np`.Instructions1. Set `fx` to the minimum and maximum of `xs`, stored in a NumPy array.2. Set `fy` to the points on the fitted line that correspond to the `fx`.<jupyter_code># Plot the scatter plot
plt.clf()
x_jitter = xs + np.random.normal(0, 0.15, len(xs))
plt.plot(x_jitter, ys, 'o', alpha=0.2)

# Plot the line of best fit
fx = np.array([xs.min(), xs.max()])
fy = res.intercept + res.slope * fx
plt.plot(fx, fy, '-', alpha=0.7)

plt.xlabel('Income code')
plt.ylabel('Vegetable servings per day')
plt.ylim([0, 6])
plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.10598124010232672, 'fraction_numerical': 0.01839444512120843, 'mean_word_length': 5.4798737174427785, 'pattern_counts': {'":': 0, '<': 37, '<?xml version=': 0, '>': 37, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '9', 'dir': 'jupyter-structured-clean-dedup', 'id': '653585', 'license': 'MIT', 'n_tokens_mistral': 2778, 'n_tokens_neox': 2564, 'n_words': 1026, 'path': 'exploratory_data_analysis_in_python/3_relationships.ipynb', 'repo_name': 'vhsenna/datacamp-courses', 'size': '8209'}