<jupyter_start><jupyter_text>Observations and Insights 1 The distribution of the female and male mice is relatively even, with males being 2% higher.2 For the Capomulin regimen, mouse weight and average tumor volume has a positive linear regression.3 Capomulin has the highest number of total measurements.4 Infubinol is the only drug regimen that has an outlier (out of Capomulin, Ramicane, Infubinol, and Ceftamin).5 The tumor volume of Mouse l509 was highest on the 19th day.<jupyter_code># Dependencies and Setup
import matplotlib.pyplot as plt
import pandas as pd
import scipy.stats as st
import numpy as np 
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 and display the data table for preview
combined_mouse_data_df = pd.merge(mouse_metadata, study_results,
                              how="outer", on="Mouse ID")
combined_mouse_data_df.head()

# Checking the number of mice.
total_mice = combined_mouse_data_df["Mouse ID"].nunique()
total_mice
# Getting the duplicate mice by ID number that shows up for Mouse ID and Timepoint. 
duplicate_mice = combined_mouse_data_df.loc[combined_mouse_data_df.duplicated(subset=['Mouse ID', 'Timepoint',]),'Mouse ID'].unique()
duplicate_mice

# Optional: Get all the data for the duplicate mouse ID. 
duplicate_mouse_data = combined_mouse_data_df.loc[combined_mouse_data_df["Mouse ID"] == "g989"]
duplicate_mouse_data

# Create a clean DataFrame by dropping the duplicate mouse by its ID.
clean_data_df = combined_mouse_data_df[combined_mouse_data_df['Mouse ID'].isin(duplicate_mice) == False]
clean_data_df.head()
# Checking the number of mice in the clean DataFrame.
len(clean_data_df["Mouse ID"].unique())<jupyter_output><empty_output><jupyter_text>Summary Statistics<jupyter_code># Calculate the  mean, median, variance, standard deviation, and SEM of the tumor volume for each drug regimen: 
summary_df = clean_data_df.groupby("Drug Regimen")
summary_df = summary_df["Tumor Volume (mm3)"]

summary_mean = summary_df.mean().round(2)
summary_median = summary_df.median().round(2)
summary_variance = summary_df.var().round(2)
summary_std = summary_df.std().round(2)
summary_sem = summary_df.sem().round(2)

# Assemble single summary dataframe.
summary_statistics_df = pd.DataFrame({"Average": summary_mean,
                                     "Median": summary_median,
                                     "Variance": summary_variance,
                                     "Standard Deviation": summary_std,
                                     "SEM": summary_sem})
summary_statistics_df

# Generate a summary statistics table in a single line
summary_statistics_df = clean_data_df.groupby("Drug Regimen").agg({"Tumor Volume (mm3)": ["mean", "median", "var", "std", "sem"]})
summary_statistics_df.round(2)<jupyter_output><empty_output><jupyter_text>Bar and Pie Charts<jupyter_code># Calculate total number of measurements per regimen 
counts = clean_data_df["Drug Regimen"].value_counts()

# Generate a bar plot showing the total number of measurements taken on each drug regimen using pandas.
counts.plot(kind= "bar")
plt.xlabel("Drug Regimen")
plt.xticks(rotation = 60)
plt.ylabel("Number of Measurements")
plt.title("Total Measurements Per Drug Regimen")
plt.show()



# Generate a bar plot showing the total number of measurements taken on each drug regimen using pyplot.
counts = clean_data_df["Drug Regimen"].value_counts()
plt.bar(counts.index.values,counts.values, width = .5)
plt.xlabel("Drug Regimen")
plt.xticks(rotation = 60)
plt.ylabel("Number of Measurements")
plt.title("Total Measurements Per Drug Regimen")
plt.show()

# Generate a pie plot showing the distribution of female versus male mice using pandas
counts = clean_data_df.Sex.value_counts()
counts.plot(kind='pie', autopct='%1.1f%%', shadow=True, startangle=140)
plt.axis('equal')
plt.title('Distribution of Female vs Male Mice')
plt.show

# Generate a pie plot showing the distribution of female versus male mice using pyplot
counts = clean_data_df.Sex.value_counts()
plt.pie(counts.values, labels = counts.index.values, autopct='%1.1f%%', shadow=True, startangle=140)
plt.ylabel('Sex')
plt.axis('equal')
plt.title('Distribution of Female vs Male Mice')
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 = clean_data_df.groupby(["Mouse ID"])['Timepoint'].max()
max_tumor = max_tumor.reset_index()

# Merge this group df with the original dataframe to get the tumor volume at the last timepoint
merge_data = max_tumor.merge(clean_data_df, on = ['Mouse ID', 'Timepoint'], how = "left")
# Create a treatments list for a for loop (and later for plot labels)
treatments_list = ["Capomulin", "Ramicane", "Infubinol", "Ceftamin"]

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

# Calculate the IQR and quantitatively determine if there are any potential outliers. 
for drugs in treatments_list:
    
    # Locate the rows which contain mice on each drug and get the tumor volumes
    final_tumor_volume = merge_data.loc[merge_data["Drug Regimen"] == drugs,'Tumor Volume (mm3)']
    
    # add subset 
    tumor_volume_list.append(final_tumor_volume)
    
    # Determine outliers using upper and lower bounds
    quartiles = final_tumor_volume.quantile([.25,.5,.75])
    lowerq = quartiles[0.25]
    upperq = quartiles[0.75]
    iqr = upperq-lowerq
    lower_bound = lowerq - (1.5*iqr)
    upper_bound = upperq + (1.5*iqr)
    outliers = final_tumor_volume.loc[(final_tumor_volume < lower_bound) | (final_tumor_volume > upper_bound)]
    if outliers.empty:
        print(f"{drugs}'s potential outliers: None")
    else:
        print(f"{drugs}'s potential outliers: {outliers}")
    
# Generate a box plot of the final tumor volume of each mouse across four regimens of interest
volume = final_tumor_volume 
fig1, ax1 = plt.subplots()
ax1.set_title('Final Tumor Volume Across Four Regimens')
plt.boxplot(tumor_volume_list, labels = treatments_list)
ax1.set_ylabel('Tumor Volume (mm3)')
plt.show()<jupyter_output><empty_output><jupyter_text>Line and Scatter Plots<jupyter_code># Generate a line plot of tumor volume vs. time point for a mouse treated with Capomulin
capomulin_table = clean_data_df.loc[clean_data_df['Drug Regimen'] == "Capomulin"]
mousedata = capomulin_table.loc[capomulin_table['Mouse ID']== 'l509']
plt.plot(mousedata['Timepoint'],mousedata['Tumor Volume (mm3)'])
plt.xlabel('Timepoint (days)')
plt.ylabel('Tumor Volume (mm3)')
plt.title('Capomulin treatment of mouse l509')
plt.show()

# Generate a scatter plot of average tumor volume vs. mouse weight for the Capomulin regimen
capomulin_table = clean_data_df.loc[clean_data_df['Drug Regimen'] == "Capomulin"]
average_volume = capomulin_table.groupby(['Mouse ID']).mean()
plt.scatter(average_volume['Weight (g)'], average_volume['Tumor Volume (mm3)'])
plt.xlabel('Weight (g)')
plt.ylabel('Tumor Volume (mm3)')
plt.title('Comparison of Average Turmor Volume vs Mouse Weight for Capomulin')
plt.show()<jupyter_output><empty_output><jupyter_text>Correlation and Regression<jupyter_code># Calculate the correlation coefficient for mouse weight and average tumor volume for the Capomulin regimen
correlation = st.pearsonr(average_volume['Weight (g)'], average_volume['Tumor Volume (mm3)'])
print(f"The correlation between the mouse weight and average tumor volume is {round(correlation[0],2)}")

# Find the linear regression for mouse weight and average tumor volume for the Capomulin regimen
x_values = average_volume['Weight (g)']
y_values = average_volume['Tumor Volume (mm3)']
(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,(20,36),fontsize=15,color="red")
plt.xlabel('Weight (g)')
plt.ylabel('Tumor Volume (mm3)')
plt.title('Linear Regression for Mouse weight and Avg Tumor Volume for Capomulin')
plt.show()<jupyter_output>The correlation between the mouse weight and average tumor volume is 0.84
{'fraction_non_alphanumeric': 0.10330002323960027, 'fraction_numerical': 0.009760632117127585, 'mean_word_length': 5.404017857142857, 'pattern_counts': {'":': 6, '<': 25, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '276995', 'license': 'ADSL', 'n_tokens_mistral': 2895, 'n_tokens_neox': 2663, 'n_words': 811, 'path': 'Pymaceuticals/pymaceuticals_starter.ipynb', 'repo_name': 'bareardon/matplotlib-challenge', 'size': '8606'}