<jupyter_start><jupyter_text>Observations and Insights  %matplotlib inline<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
combined_mouse_df = pd.merge(mouse_metadata ,study_results, how="left", on="Mouse ID")
# Display the data table for preview
combined_mouse_df.head()
# Checking the number of mice.
uniquemice = len(combined_mouse_df["Mouse ID"].unique())
print(f"The number of mice in this study is {uniquemice}")
# Getting the duplicate mice by ID number that shows up for Mouse ID and Timepoint.
# Create a clean DataFrame by dropping the duplicate mouse by its ID.
clean_df = combined_mouse_df.drop_duplicates(subset=["Mouse ID","Timepoint"], keep = "last")
len(clean_df)
# Checking the number of mice in the clean DataFrame.
micemice = len(clean_df["Mouse ID"].unique())
print(f"CHECK : The number of mice in this study is {micemice}")<jupyter_output>CHECK : The number of mice in this study is 249<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
# This method is the most straighforward, creating multiple series and putting them all together at the end.
rmean = clean_df.groupby("Drug Regimen").mean()["Tumor Volume (mm3)"]
rmedian = clean_df.groupby("Drug Regimen").median()["Tumor Volume (mm3)"]
rvar = clean_df.groupby("Drug Regimen").var()["Tumor Volume (mm3)"]
rstd = clean_df.groupby("Drug Regimen").std()["Tumor Volume (mm3)"]
rsem = clean_df.groupby("Drug Regimen").sem()["Tumor Volume (mm3)"]
print("Summary Statistics for tumor volume in each region")
SummaryStat_DF = pd.DataFrame({"Mean" : rmean,
                                  "Median" : rmedian,
                                  "Variance" : rvar,
                                  "Standard Deviation" : rstd,
                                "Standard Errors of the Mean" : rsem})
SummaryStat_DF


# 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
print("Summary Statistics for tumor volume in each region")
summary = clean_df.groupby("Drug Regimen").agg(["mean","median","var","std","sem"])["Tumor Volume (mm3)"]
summary<jupyter_output>Summary Statistics for tumor volume in each region<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. 
# Set x axis and tick locations
bardatamice = combined_mouse_df.groupby("Drug Regimen").count()["Mouse ID"]
bardatamice.plot.bar(title = "Number of Mice for each treatment",color = "magenta", alpha = 0.5)
plt.ylabel("Number of mice")
# Generate a bar plot showing the total number of mice for each treatment throughout the course of the study using pyplot.
x_axis = np.arange(len(bardatamice))
plt.bar(x_axis, bardatamice, color='purple', alpha=0.5)
plt.xticks(x_axis, combined_mouse_df["Drug Regimen"], rotation="vertical")
plt.title("Number of Mice for each treatment")
plt.xlabel("Drug Regimen")
plt.ylabel("Number of mice")
# Generate a pie plot showing the distribution of female versus male mice using pandas
Gender_df = combined_mouse_df.groupby(["Sex"])
GenderCount = Gender_df["Mouse ID"].nunique()
GenderCount.plot.pie(autopct="%1.1f%%",colors = ["magenta", "skyblue"], title ="Females VS Males in the Mouse Experimentation for Tumors")
plt.legend()
# Generate a pie plot showing the distribution of female versus male mice using pyplot
labels = ["Male","Female"]
sizes = [GenderCount[0],GenderCount[1]]

# The colors of each section of the pie chart
colors = ["purple", "lightcoral"]

# Tells matplotlib to seperate the "Humans" section from the others
explode = (0.2, 0)
plt.pie(sizes, explode=explode, colors=colors,
        autopct="%1.1f%%", shadow=True, startangle=140)
plt.axis("equal")
plt.title("Females VS Males in the Mouse Experimentation for Tumors")
plt.legend(labels=labels)
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
# Merge this group df with the original dataframe to get the tumor volume at the last timepoint
# Put treatments into a list for for loop (and later for plot labels)
# Create empty list to fill with tumor vol data (for plotting)

Capomulin_df = combined_mouse_df.loc[combined_mouse_df["Drug Regimen"] == "Capomulin", :]
Ramicane_df = combined_mouse_df.loc[combined_mouse_df["Drug Regimen"] == "Ramicane", :]
Infubinol_df = combined_mouse_df.loc[combined_mouse_df["Drug Regimen"] == "Infubinol", :]
Ceftamin_df = combined_mouse_df.loc[combined_mouse_df["Drug Regimen"] == "Ceftamin", :]

sorted_Ca_df = Capomulin_df.sort_values("Timepoint").drop_duplicates(subset=["Mouse ID"], keep = "last")
sorted_R_df = Ramicane_df .sort_values("Timepoint").drop_duplicates(subset=["Mouse ID"], keep = "last")
sorted_I_df = Infubinol_df.sort_values("Timepoint").drop_duplicates(subset=["Mouse ID"], keep = "last")
sorted_Ce_df = Ceftamin_df.sort_values("Timepoint").drop_duplicates(subset=["Mouse ID"], keep = "last")


# Calculate the IQR and quantitatively determine if there are any potential outliers. 
drugs = [sorted_Ca_df, sorted_R_df, sorted_I_df,sorted_Ce_df]
drugnames = ["Capomulin", "Ramicane", "Infubinol", "Ceftamin"]
lowerqs = []
upperqs= []
lowerbound = []
upperbound =[]
maxes = []
mins = []

for drug in drugs:
    quartiles = drug["Tumor Volume (mm3)"].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)
    maxd = drug["Tumor Volume (mm3)"].max()
    mind = drug["Tumor Volume (mm3)"].min()
    lowerqs.append(lowerq)
    upperqs.append(upperq)
    lowerbound.append(lower_bound)
    upperbound.append(upper_bound)
    maxes.append(maxd)
    mins.append(mind)
  
newdf = pd.DataFrame({
    'Drug Regimen': drugnames,
    'Lower Bound': lowerbound,
    'Upper Bound': upperbound,
    "Min":mins,
    "Max": maxes})  
print(newdf) 
# Determine outliers using upper and lower bounds
print("There are outliers potentially for Infubinol with some values below the Lowerbound, no outliers for the rest")

# Generate a box plot of the final tumor volume of each mouse across four regimens of interest
fig, axs = plt.subplots(2,2)
fig.suptitle("Final Tumor Volumes for each mouse by Drug Regimen", fontsize=16, fontweight="bold")

#ax1.set_xlim(min(year), max(year))
axs[0,0].boxplot(sorted_Ca_df["Tumor Volume (mm3)"])
axs[1,0].boxplot(sorted_R_df["Tumor Volume (mm3)"])
axs[1,1].boxplot(sorted_I_df["Tumor Volume (mm3)"])
axs[0,1].boxplot(sorted_Ce_df["Tumor Volume (mm3)"])
axs[0,0].set_title("Capomulin")
axs[1,0].set_xlabel("Ramicane")
axs[1,1].set_xlabel("Infubinol")
axs[0,1].set_title("Ceftamin")

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
specialmouse_df = Capomulin_df.loc[Capomulin_df["Mouse ID"] == "s185", :]
specialmouse_df.plot.line(x="Timepoint",y="Tumor Volume (mm3)", color ="orchid", marker = "o", title = "Mouse:s185 - Timepoint VS Average Tumor Volume for the Capomulin regimen")
# Generate a scatter plot of mouse weight versus average tumor volume for the Capomulin regimen
avg_df = Capomulin_df.groupby("Mouse ID").mean()
mouseweight = avg_df["Weight (g)"]
tumorvol = avg_df["Tumor Volume (mm3)"]
plt.scatter(mouseweight,tumorvol,color = "mediumvioletred", marker = "o")
plt.xlabel("Mouse Weight")
plt.ylabel("Average Tumor Volume")
plt.title("Mouse Weight VS Average Tumor Volume for the Capomulin Regimen")<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
print(f"The correlation coefficient between mouse weight and average tumor volume is {round(st.pearsonr(mouseweight,tumorvol)[0],2)}")
x_values = mouseweight
y_values1 = tumorvol
(slope, intercept, rvalue, pvalue, stderr) = linregress(x_values, y_values1)
regress_values = x_values * slope + intercept
line_eq = "y = " + str(round(slope,2)) + "x + " + str(round(intercept,2))
plt.scatter(mouseweight,tumorvol,color = "hotpink")
plt.plot(mouseweight,regress_values,"m--")
plt.annotate(line_eq,(20,40),fontsize=15,color="purple")
plt.xlabel("Mouse Weight")
plt.ylabel("Average Tumor Volume")
plt.title("Mouse Weight VS Average Tumor Volume for the Capomulin Regimen")
print(f"The regression equation is {line_eq}")<jupyter_output>The correlation coefficient between mouse weight and average tumor volume is 0.84
The regression equation is y = 0.95x + 21.55
{'fraction_non_alphanumeric': 0.11719663937041369, 'fraction_numerical': 0.010422205679038605, 'mean_word_length': 5.6506364922206505, 'pattern_counts': {'":': 2, '<': 22, '<?xml version=': 0, '>': 22, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 3, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '644831', 'license': 'ADSL', 'n_tokens_mistral': 3235, 'n_tokens_neox': 3020, 'n_words': 909, 'path': 'Pymaceuticals/pymaceuticals_starter.ipynb', 'repo_name': 'tanvi-12/Matplotlib-Challenge', 'size': '9403'}