<jupyter_start><jupyter_text>Observations and Insights  1: There was an even sample of female and male gender mice used in the study.2: For the drug Capomulin, the mouse weight and tumor volume have a correlation value of 0.84, indicating a strong correlation between the two variables.    3: When randomly selecting a mouse treated by Capomulin, the mouse showed the tumor volume decreased as the time point increased.4: Through producing the quartile range and generating a Box and Whisker plot it is evident that there is one outlier for the drug Infubinol.<jupyter_code># Dependencies and setup
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
import scipy.stats as st
from scipy.stats import linregress

# Study data files: import csv
mouse_metadata_path = "Pymaceuticals_data_Mouse_metadata.csv"
study_results_path = "Pymaceuticals_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
combinedata = pd.merge(study_results, mouse_metadata, on="Mouse ID", how="outer")
# Display the data table for preview
combinedata.head()
# Checking the number of mice.
num_of_rows=combinedata.shape
print(f"Total number of mice: {num_of_rows}")
uniquemice=combinedata['Mouse ID'].nunique()
print(f"Total number of unique mice: {uniquemice}")
# Getting the duplicate mice by ID number that shows up for Mouse ID and Timepoint. 
id_count=combinedata.groupby("Mouse ID")[["Timepoint"]].count()

find_duplicate=id_count.loc[id_count["Timepoint"]>10]
find_duplicate
# Optional: Get all the data for the duplicate mouse ID. 
duplicate_mouse=combinedata.loc[combinedata["Mouse ID"]=="g989"]
duplicate_mouse.head()
# Create a clean DataFrame by dropping the duplicate mouse by its ID.
metadata_df=combinedata.drop(range(860, 872))
check=metadata_df.loc[metadata_df["Mouse ID"] == "g989"]
check
# Checking the number of mice in the clean DataFrame.
mice_id_check=metadata_df["Mouse ID"].unique()
len(mice_id_check)<jupyter_output><empty_output><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
#Creating multiple series and putting them all together at the end.
summarydata = metadata_df[["Drug Regimen", "Tumor Volume (mm3)"]]
groupbydata = summarydata.groupby(["Drug Regimen"])

summarydf = pd.DataFrame({
    "Mean": groupbydata["Tumor Volume (mm3)"].mean().map('{:.2f}'.format),
    "Median": groupbydata["Tumor Volume (mm3)"].median().map('{:.2f}'.format),
    "Variance": groupbydata["Tumor Volume (mm3)"].var().map('{:.2f}'.format),
    "Std Deviation": groupbydata["Tumor Volume (mm3)"].std().map('{:.2f}'.format),
    "SEM": groupbydata["Tumor Volume (mm3)"].sem().map('{:.2f}'.format)})
summarydf
# 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
summary_df=metadata_df.groupby("Drug Regimen")["Tumor Volume (mm3)"].agg({"mean","median","var","std","sem"})
summary_df=summary_df.round(2)
summary_df.columns = ['Standard Deviation','Variance','SEM','Median','Mean']
summary_df=summary_df[['Mean','Median','Variance','Standard Deviation','SEM']]
summary_df<jupyter_output><empty_output><jupyter_text>Bar and Pie Charts<jupyter_code>#BAR PLOT (pandas) showing the total number of mice for each treatment throughout the course of the study. 
groupbydata=metadata_df["Drug Regimen"].value_counts()
groupbydata.plot(kind="bar",facecolor="red")
plt.title("Count of Mice per Drug Regimen")
plt.xlabel("Drug Regimen")
plt.ylabel("Count of Mice")
plt.figure(figsize=(6, 3))
plt.show()
#BAR PLOT (pyplot) showing the total number of mice for each treatment throughout the course of the study. 
xAxis = np.arange(len(summarydf))
tickLocations = [value for value in xAxis]

plt.bar(xAxis, groupbydata, facecolor='red')
plt.xticks(tickLocations, list(summarydf.index), rotation="vertical") 
plt.title("Count of Mice per Drug Regimen")
plt.xlabel("Drug Regimen")
plt.ylabel("Count of Mice")
plt.figure(figsize=(6, 3))
plt.show()
#PIE CHART (pandas) showing distribution of female versus male mice
gendercount = combinedata["Sex"].value_counts()

plt.figure()
explode=(0.03,0.03)
colors=["Orange","Green"]
gendercount.plot(kind="pie", explode=explode, autopct='%1.1f%%', startangle=90, colors=colors)
plt.tight_layout()
plt.axis("equal")
plt.title("Distribution of Female versus Male mice")
plt.show()
#PIE CHART (pyplot) showing distribution of female versus male mice
plt.figure()
explode = (0.03,0.03)
colors = ["orange","green"]
plt.pie(gendercount.values, explode=explode, labels=gendercount.index.values, colors=colors,autopct="%1.1f%%", startangle=90)
plt.axis("equal")
plt.title("Distribution of Female versus 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
# Merge this group df with the original dataframe to get the tumor volume at the last timepoint

four_drug_list = ['Capomulin', 'Ramicane', 'Infubinol', 'Ceftamin']
four_drugs = combinedata[combinedata['Drug Regimen'].isin(four_drug_list)]
four_drugs.head()
#agg the dataframes, 
four_last_tumor = four_drugs.groupby(['Drug Regimen','Mouse ID']).agg(\
last_tumor_size=('Tumor Volume (mm3)',lambda x: x.iloc[-1])).round(3)
four_last_tumor_unstack = four_last_tumor.stack(level=0).unstack(level=0)
counter = 0

# Do quartile calculations for each drug
for drug in four_drug_list:
    quartiles = four_last_tumor_unstack[drug].quantile([.25,.5,.75]).round(2)
    lowerq = quartiles[0.25].round(2)
    upperq = quartiles[0.75].round(2)
    iqr = round(upperq-lowerq,2)
    lower_bound = round(lowerq - (1.5*iqr),2)
    upper_bound = round(upperq + (1.5*iqr),2)
    
    # Check if it is the first time through the for loop, if so then print dividing line at top only
    if counter == 0:
        print(f"--------------------------------------------------------")
    print(f"{drug} IQR data:")
    print(f"Lower quartile is: {lowerq}")
    print(f"Upper quartile is: {upperq}")
    print(f"Interquartile range is: {iqr}")
    print(f"Values below {lower_bound} for {drug} could be outliers.")
    print(f"Values above {upper_bound} for {drug} could be outliers.")
    print(f"--------------------------------------------------------")
    counter += 1
# Generate a box plot of the final tumor volume of each mouse across four regimens of interest
boxplot_list=[]
for drug in four_drug_list:
    boxplot_list.append(list(four_last_tumor_unstack[drug].dropna()))

fig1, ax = plt.subplots(figsize=(9,7))
ax.set_title('Final measured Tumor volume by Drug Regimen')
ax.set_ylabel('Tumor Volume (mm3)')
ax.boxplot(boxplot_list,notch=0,sym='gD')
plt.xticks([1,2,3,4],four_drug_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
lineplot = combinedata[(combinedata["Drug Regimen"] == "Capomulin") & (combinedata["Mouse ID"] == "s185")]
lineplot = lineplot.groupby(["Drug Regimen", "Timepoint"])
lineplotdf = lineplot["Tumor Volume (mm3)"].mean().to_frame()
lineplotindexdf = lineplotdf.unstack(0)
lineplotdf = lineplotindexdf["Tumor Volume (mm3)"]

plt.figure()
tumortotal, = plt.plot(xAxis, lineplotdf["Capomulin"], marker= "x", color="green", label="Total Tumor Volume" )
plt.legend(handles=[tumortotal], loc="best")
plt.title("Time Point Vs Tumor Volume for Mouse ID 's185'")
plt.xlabel("Time Point")
plt.ylabel("Average Tumor Volume (mm3)")
plt.grid(True)
plt.show()
# Generate a scatter plot of mouse weight versus average tumor volume for the Capomulin regimen
scatterplot = combinedata[(combinedata["Drug Regimen"] == "Capomulin")]
scatterplot = scatterplot[["Mouse ID", "Weight (g)", "Tumor Volume (mm3)"]]
groupbydata = scatterplot.groupby(["Mouse ID","Weight (g)"])
scatterplotdf = groupbydata["Tumor Volume (mm3)"].mean().to_frame()
scatterplotdf = scatterplotdf.reset_index()

plt.figure()
scatterplotdf.plot(x="Weight (g)", y="Tumor Volume (mm3)", kind = "scatter", color="green", marker= "x")
plt.title("Mouse Weight Vs Average Tumor Volume")
plt.xlabel("Mouse Weight (g)")
plt.ylabel("Average Tumor Volume (mm3)")
plt.grid(True)
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
# Print out the r-squared value along with the plot.
correlation = st.pearsonr(scatterplotdf['Weight (g)'],scatterplotdf["Tumor Volume (mm3)"])

x_values = scatterplotdf['Weight (g)']
y_values = scatterplotdf["Tumor Volume (mm3)"]
(slope, intercept, rvalue, pvalue, stderr) = linregress(x_values, y_values)
regress_values = x_values * slope + intercept
line_eq = f'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,(17,37),fontsize=15,color="black")
plt.title("Mouse weight vs. Avg. Tumor Volume for Drug Capomulin")
plt.xlabel("Mouse weight (g)")
plt.ylabel("Tumor Volume (mm3)")

#print/show
print(f"The correlation between Mouse Weight and Tumor Volume for the drug Capomulin is {round(correlation[0],2)}")
print(f"Regression equation: {line_eq}")
plt.show()<jupyter_output>The correlation between Mouse Weight and Tumor Volume for the drug Capomulin is 0.84
Regression equation: y = 0.95x + 21.55
{'fraction_non_alphanumeric': 0.14165312753858653, 'fraction_numerical': 0.01350528025995126, 'mean_word_length': 6.029978586723769, 'pattern_counts': {'":': 5, '<': 24, '<?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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '160744', 'license': 'ADSL', 'n_tokens_mistral': 3423, 'n_tokens_neox': 3183, 'n_words': 916, 'path': 'PymaceuticalsNotebook.ipynb', 'repo_name': 'LaurenPrins/matplotlib-challenge', 'size': '9848'}