<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 stats
import numpy as np


# 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
mouse_study_df=pd.merge(mouse_metadata,study_results, on="Mouse ID")

# Display the data table for preview
mouse_study_df.head()

# Checking the number of mice.
mice_unique_df=mouse_study_df.nunique()["Mouse ID"]
mice_unique_df

# Getting the duplicate mice by ID number that shows up for Mouse ID and Timepoint. 
duplicate_mouse=mouse_study_df.loc[mouse_study_df.duplicated(subset=["Mouse ID","Timepoint"])]

duplicate_mouse


# Optional: Get all the data for the duplicate mouse ID. 
duplicate_mouse

# Create a clean DataFrame by dropping the duplicate mouse by its ID.
clean_mouse=mouse_study_df.set_index("Mouse ID")
clean_mouse_df=clean_mouse.drop(index = "g989")
cleaner_mouse_df=clean_mouse_df.reset_index()
cleaner_mouse_df

# Checking the number of mice in the clean DataFrame.
cleaner_mouse_unique_df=cleaner_mouse_df.nunique()["Mouse ID"]
cleaner_mouse_unique_df<jupyter_output><empty_output><jupyter_text>Summary Statistics<jupyter_code>drug_mouse=mouse_study_df["Drug Regimen"].value_counts()
drug_mouse
# 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.

tumor_mean=mouse_study_df.groupby("Drug Regimen").mean()["Tumor Volume (mm3)"]
tumor_median=mouse_study_df.groupby("Drug Regimen").median()["Tumor Volume (mm3)"]
tumor_var=mouse_study_df.groupby("Drug Regimen").var()["Tumor Volume (mm3)"]
tumor_sd=mouse_study_df.groupby("Drug Regimen").std()["Tumor Volume (mm3)"]
tumor_sem=mouse_study_df.groupby("Drug Regimen").sem()["Tumor Volume (mm3)"]

summary_regimen_df=pd.DataFrame({"Mean":tumor_mean, "Median":tumor_median, "Variance":tumor_var,
                                 "Standard Deviation":tumor_sd, "SEM":tumor_sem})
summary_regimen_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
regimen_group=mouse_study_df.groupby(["Drug Regimen"])
summary2_regimen=regimen_group.agg({"Tumor Volume (mm3)":["mean","median","var","std","sem"]})
summary2_regimen<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. 
fig_1=plt.figure()
drug=cleaner_mouse_df["Drug Regimen"]
drug_mouse.plot.bar(drug_mouse, color="green")
plt.xlabel('Drug Regimen')
plt.ylabel('Number of Mice')
plt.show()

# Generate a bar plot showing the total number of mice for each treatment 
# throughout the course of the study using pyplot.
fig_2=plt.figure()
drug=cleaner_mouse_df["Drug Regimen"]
plt.bar(drug_mouse.index.values,drug_mouse.values)
plt.xticks(rotation=90)
plt.xlabel('Drug Regimen')
plt.ylabel('Number of Mice')
plt.show()



# Generate a pie plot showing the distribution of female versus male mice using pandas

sex_mouse=mouse_study_df["Sex"].value_counts()
sex_mouse
sex_mouse.plot.pie(y="Sex",autopct='%1.1f%%')
sex_mouse
# Generate a pie plot showing the distribution of female versus male mice using pyplot
sex_mouse=mouse_study_df["Sex"].value_counts()
sex_mouse
plt.pie(sex_mouse.values,labels=sex_mouse.index.values,autopct='%1.1f%%')
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
treatment_list=["Capomulin", "Ramicane", "Infubinol", "Ceftamin"]


# Start by getting the last (greatest) timepoint for each mouse
max_tumor=cleaner_mouse_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
mergedata=max_tumor.merge(cleaner_mouse_df, on=["Mouse ID", "Timepoint"],how="left")
# Put treatments into a list for for loop (and later for plot labels)
treatment_list=["Capomulin", "Ramicane", "Infubinol", "Ceftamin"]

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

# Calculate the IQR and quantitatively determine if there are any potential outliers. 
for drug in treatment_list:
    
    # Locate the rows which contain mice on each drug and get the tumor volumes    
    tumor=mergedata.loc[mergedata["Drug Regimen"]== drug,"Tumor Volume (mm3)"]
    
    # add subset
    tumor_vol_data.append(tumor)

    # Determine outliers using upper and lower bounds    
    quartiles = tumor.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=tumor.loc[(tumor<lower_bound)|(tumor>upper_bound)]
    print(f"{drug}'s Potential outliers: {outliers}")
        
# Generate a box plot of the final tumor volume of each mouse across four regimens of interest

fig1,ax1=plt.subplots()
ax1.boxplot(tumor_vol_data,labels=treatment_list)
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=mouse_study_df.loc[mouse_study_df["Drug Regimen"] == "Capomulin",:]
justa_mouse=capomulin.loc[capomulin["Mouse ID"] == "m601",:]
x_axis= justa_mouse["Timepoint"]
y_axis = justa_mouse["Tumor Volume (mm3)"]

plt.title("Capomulin mouse m601")
plt.plot(x_axis, y_axis, color="purple")
plt.xlabel("Timepoint")
plt.ylabel("Tumor Volume (mm3)")

plt.show()

# Generate a scatter plot of mouse weight versus average tumor volume for the Capomulin regimen

capomulin=mouse_study_df.loc[mouse_study_df["Drug Regimen"] == "Capomulin",:]
capomulinavg=capomulin.groupby(["Mouse ID"]).mean()


plt.scatter(capomulinavg["Weight (g)"],capomulinavg["Tumor Volume (mm3)"])
plt.xlabel("Mouse Weight (g)")
plt.ylabel("Tumor Volume (mm3)")


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
capomulin=mouse_study_df.loc[mouse_study_df["Drug Regimen"] == "Capomulin",:]
capomulinavg=capomulin.groupby(["Mouse ID"]).mean()


plt.scatter(capomulinavg["Weight (g)"],capomulinavg["Tumor Volume (mm3)"])
plt.xlabel("Mouse Weight (g)")
plt.ylabel("Tumor Volume (mm3)")


(slope, intercept, rvalue, pvalue, stderr)= stats.linregress(capomulinavg["Weight (g)"],capomulinavg["Tumor Volume (mm3)"])
regress_values= capomulinavg["Weight (g)"] * slope + intercept
line_eq="y = " + str(round(slope,2)) + "x +" +str(round(intercept,2))

plt.plot(capomulinavg["Weight (g)"],regress_values,"r-")





plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11579089924160346, 'fraction_numerical': 0.0071776814734561215, 'mean_word_length': 6.551124744376278, 'pattern_counts': {'":': 6, '<': 26, '<?xml version=': 0, '>': 26, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '665367', 'license': 'ADSL', 'n_tokens_mistral': 2676, 'n_tokens_neox': 2441, 'n_words': 633, 'path': 'pymaceuticals_starter.ipynb', 'repo_name': 'kborig89/matplotlib-challenge', 'size': '7384'}