<jupyter_start><jupyter_text>Observations and Insights<jupyter_code># Dependencies and Setup
import matplotlib.pyplot as plt
import pandas as pd
import scipy.stats as st
from scipy.stats import linregress

# Study data files
mouse_metadata_path = "data/Mouse_metadata.csv"
study_results_path = "data/Study_results.csv"

# Read the mouse data and the study results
mouse_metadata = pd.read_csv(mouse_metadata_path)
study_results = pd.read_csv(study_results_path)

# Combine the data into a single dataset
mice_df = pd.merge(study_results, mouse_metadata, on='Mouse ID')

# Display the data table for preview
mice_df
# Checking the number of mice.
mice_number = len(mice_df['Mouse ID'].unique())
print(f"The number of mice is {mice_number}")
# Getting the duplicate mice by ID number that shows up for Mouse ID and Timepoint. 
duplicate_mice = mice_df[mice_df.duplicated(subset=['Mouse ID','Timepoint'], keep=False)]
# Optional: Get all the data for the duplicate mouse ID. 
duplicate_mice
# Create a clean DataFrame by dropping the duplicate mouse by its ID.
clean_mice_df = mice_df[mice_df['Mouse ID'] != duplicate_mice.iloc[0]['Mouse ID']]
# Checking the number of mice in the clean DataFrame.
clean_mice_number = len(clean_mice_df['Mouse ID'].unique())
print(f"The number of mice in the clean DataFrame is {clean_mice_number}")<jupyter_output>The number of mice in the clean DataFrame is 248<jupyter_text>Summary Statistics<jupyter_code># Generate a summary statistics table of mean, median, variance, standard deviation, and SEM of the tumor volume for each regimen
stats = pd.DataFrame(clean_mice_df.groupby("Drug Regimen")["Tumor Volume (mm3)"].mean())
stats.rename(columns={'Tumor Volume (mm3)': 'Mean'}, inplace=True)
stats['Median'] = clean_mice_df.groupby("Drug Regimen")["Tumor Volume (mm3)"].median()
stats['Variance'] = clean_mice_df.groupby("Drug Regimen")["Tumor Volume (mm3)"].var()
stats['Standard Deviation'] = clean_mice_df.groupby("Drug Regimen")["Tumor Volume (mm3)"].std()
stats['Standard Error'] = clean_mice_df.groupby("Drug Regimen")["Tumor Volume (mm3)"].sem()
stats
# Generate a summary statistics table of mean, median, variance, standard deviation, and SEM of the tumor volume for each regimen
# This method produces everything in a single groupby function
stats_2 = clean_mice_df.groupby('Drug Regimen').agg(
    Mean = ('Tumor Volume (mm3)', 'mean'),
    Median = ('Tumor Volume (mm3)', 'median'),
    Variance = ('Tumor Volume (mm3)', 'var'),
    Standard_Deviation = ('Tumor Volume (mm3)', 'std'),
    Standard_Error = ('Tumor Volume (mm3)', 'sem'))
stats_2<jupyter_output><empty_output><jupyter_text>Bar and Pie Charts<jupyter_code># Generate a bar plot showing the total number of mice for each treatment throughout the course of the study using pandas. 
mice_byRegimen = pd.DataFrame(clean_mice_df.groupby("Drug Regimen")['Mouse ID'].nunique())
mice_byRegimen = mice_byRegimen.rename(columns = {'Mouse ID': 'Count'}, inplace = False)
plot = mice_byRegimen.plot(kind='bar', ylim=(20,27), title='Mice Number by Drug Regimen', width=0.8, figsize=(8,3))
plot.set(ylabel="Total Mice Number")
plt.show()
# Generate a bar plot showing the total number of mice for each treatment throughout the course of the study using pyplot.
x_axis = mice_byRegimen.index
y_axis = mice_byRegimen['Count']
plt.figure(figsize=(8,3))
plt.bar(x_axis, y_axis, label='Count')
plt.title('Mice Number by Drug Regimen')
plt.xlabel("Drug Regimen")
plt.ylabel("Total Mice Number")
plt.ylim(20,27)
plt.xticks(rotation='vertical')
plt.legend()
plt.show()
# Generate a pie plot showing the distribution of female versus male mice using pandas
mice_sex = pd.DataFrame(clean_mice_df.groupby("Sex")['Mouse ID'].nunique())
mice_sex = mice_sex.rename(columns = {'Mouse ID': 'Count'}, inplace = False)

mice_sex.plot(kind='pie',subplots=True, autopct='%1.2f%%', startangle=90, explode=[0.05,0], shadow=True, 
              title='Mice Gender Distribution')
plt.ylabel ('')
plt.show()
# Generate a pie plot showing the distribution of female versus male mice using pyplot
values = mice_sex['Count']
labels = mice_sex.index

plt.pie(values, labels=labels, autopct='%1.2f%%', startangle=90, explode=[0.05,0], shadow=True)
plt.title('Mice Gender Distribution')
plt.legend()
plt.show()<jupyter_output><empty_output><jupyter_text>Quartiles, Outliers and Boxplots<jupyter_code># Calculate the final tumor volume of each mouse across four of the treatment regimens:  
# Capomulin, Ramicane, Infubinol, and Ceftamin

# Start by getting the last (greatest) timepoint for each mouse
max_tumor_vol = clean_mice_df.groupby(['Mouse ID', 'Drug Regimen']).agg(Timepoint = ('Timepoint', 'max'))

# Merge this group df with the original dataframe to get the tumor volume at the last timepoint
max_tumor_vol = max_tumor_vol.merge(clean_mice_df, how = 'inner', on = ['Mouse ID', 'Timepoint'])
max_tumor_vol.head()
# Put treatments into a list for for loop (and later for plot labels)
regimens = ['Capomulin', 'Ramicane', 'Infubinol', 'Ceftamin']

# Create empty list to fill with tumor vol data (for plotting)
tumor_vol_list = []

# Calculate the IQR and quantitatively determine if there are any potential outliers. 
for regimen in regimens:
    
    # Locate the rows which contain mice on each drug and get the tumor volumes
    tumor_vol = max_tumor_vol['Tumor Volume (mm3)'].loc[max_tumor_vol['Drug Regimen'] == regimen]
    
    # add subset 
    tumor_vol_list.append(tumor_vol)
    
    # Determine outliers using upper and lower bounds
    quartiles = tumor_vol.quantile([.25,.5,.75])
    lowerq = quartiles[0.25]
    upperq = quartiles[0.75]
    iqr = upperq-lowerq
    
    print(f"For {regimen} the interquartile range is: {iqr}")

    lower_bound = lowerq - (1.5*iqr)
    upper_bound = upperq + (1.5*iqr)
    print(f"For {regimen} values below {lower_bound} are potential outliers.")
    print(f"For {regimen} values above {upper_bound} are potential outliers.\n")
# Generate a box plot of the final tumor volume of each mouse across four regimens of interest
red_square = dict(markerfacecolor='r', marker='s')
fig, ax = plt.subplots()
ax.boxplot(tumor_vol_list, flierprops=red_square)
ax.set_title('Final Tumor Volume Across Different Drug Regimens')
ax.set_ylabel('Tumor Volume (mm3)')
ax.set_xlabel('Drug Regimen')
ax.set_xticklabels(regimens)
plt.show()<jupyter_output><empty_output><jupyter_text>Line and Scatter Plots<jupyter_code># Generate a line plot of time point versus tumor volume for a mouse treated with Capomulin
capomulin_mice = pd.DataFrame(clean_mice_df[clean_mice_df['Drug Regimen'] == 'Capomulin'])
capomulin_mouse = capomulin_mice[capomulin_mice['Mouse ID'] == capomulin_mice.iloc[0]['Mouse ID']]
capomulin_mouse.plot(x='Timepoint',y='Tumor Volume (mm3)')
plt.title("Timepoint vs Tumor Volume for a Capomulin Treated Mouse")
plt.ylabel('Tumor Volume (mm3)')
plt.show()
# Generate a scatter plot of mouse weight versus average tumor volume for the Capomulin regimen
mean_values = ['Weight (g)', 'Tumor Volume (mm3)']
capomulin_df = capomulin_mice.groupby('Mouse ID', as_index=False)[mean_values].mean()
mean_weight = capomulin_df['Weight (g)']
mean_volume = capomulin_df['Tumor Volume (mm3)']
plt.scatter(mean_weight, mean_volume)
plt.title("Mouse Weight vs. Average Tumor Volume for the Capomulin Regimen")
plt.ylabel('Tumor Volume (mm3)')
plt.xlabel('Weight (g)')
plt.show()<jupyter_output><empty_output><jupyter_text>Correlation and Regression<jupyter_code># Calculate the correlation coefficient and linear regression model 
# for mouse weight and average tumor volume for the Capomulin regimen
x_values = mean_weight
y_values = mean_volume

(slope, intercept, rvalue, pvalue, stderr) = linregress(x_values, y_values)
regress_values = x_values * slope + intercept
line_eq = "y = " + str(round(slope,2)) + "x + " + str(round(intercept,2))
plt.scatter(x_values,y_values)
plt.plot(x_values,regress_values,"r-")
plt.annotate(line_eq,(15,44),fontsize=15,color="red")
plt.xlabel('Weight')
plt.ylabel('Tummor Volume')
plt.title('Mouse Weight vs. Average Tumor Volume for the Capomulin Regimen')
plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12088452088452088, 'fraction_numerical': 0.009582309582309583, 'mean_word_length': 6.134969325153374, 'pattern_counts': {'":': 0, '<': 24, '<?xml version=': 0, '>': 24, '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': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '348549', 'license': 'ADSL', 'n_tokens_mistral': 2864, 'n_tokens_neox': 2675, 'n_words': 788, 'path': 'Pymaceuticals/pymaceuticals.ipynb', 'repo_name': 'AlfredoOcegueda/matplotlib-challenge', 'size': '8140'}