<jupyter_start><jupyter_text>Observations and Insights  1. In the data used, 248 unique mice was analyzed with different treatment options with a 51% to 49% male to female ratio.2. Mouse weight and tumor volume were closely related, showing that weight is important to analyzing drug effectiveness.3. There was one outlier data point with the drug Infubinol.<jupyter_code># Dependencies and Setup
import matplotlib.pyplot as plt
import pandas as pd
import scipy.stats as st

# 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
dataComplete = pd.merge(study_results, mouse_metadata, how = "left", on =["Mouse ID"])


# Display the data table for preview
dataComplete.head()
# Checking the number of mice.
numMice = len(dataComplete["Mouse ID"].unique())
numMice
# Getting the duplicate mice by ID number that shows up for Mouse ID and Timepoint. 
sameMiceID = dataComplete.loc[dataComplete.duplicated(subset = ["Mouse ID", "Timepoint"]), 
                                                                "Mouse ID"].unique()
sameMiceID
# Optional: Get all the data for the duplicate mouse ID. 


# Create a clean DataFrame by dropping the duplicate mouse by its ID.
dataClean = dataComplete[dataComplete["Mouse ID"].isin(sameMiceID) == False]
dataClean.head()
# Checking the number of mice in the clean DataFrame.
numNoDupMice = len(dataClean["Mouse ID"].unique())
numNoDupMice<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

# This method is the most straighforward, creating multiple series and putting them all together at the end.
mean = dataClean.groupby("Drug Regimen").mean()["Tumor Volume (mm3)"]
median = dataClean.groupby("Drug Regimen").median()["Tumor Volume (mm3)"]
var = dataClean.groupby("Drug Regimen").var()["Tumor Volume (mm3)"]
std = dataClean.groupby("Drug Regimen").std()["Tumor Volume (mm3)"]
sem = dataClean.groupby("Drug Regimen").sem()["Tumor Volume (mm3)"]
dataTable = pd.DataFrame({"Mean Tumor Volume" : mean, 
                              "Median Tumor Volume" : median,
                              "Tumor Volume Variance" : var,
                              "Tumor Volume Std. Dev." : std,
                              "Tumor Volume Std. Err." : sem})
dataTable

# 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
dataTable = dataClean.groupby("Drug Regimen").agg({"Tumor Volume (mm3)":
                                                   ["mean","median","var","std","sem"]})
dataTable<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. 

num = dataClean["Drug Regimen"].value_counts()
num.plot(kind = "bar")
plt.xlabel("Drug Regimen")
plt.xticks(rotation = 90)
plt.ylabel("Number of Data Points")
plt.show()
# Generate a bar plot showing the total number of mice for each treatment throughout the course of the study using pyplot.

num = dataClean["Drug Regimen"].value_counts()
plt.bar(num.index.values, num.values)
plt.xlabel("Drug Regimen")
plt.xticks(rotation = 90)
plt.ylabel("Number of Data Points")
plt.show()
# Generate a pie plot showing the distribution of female versus male mice using pandas

num = dataClean.Sex.value_counts()
num.plot(kind = "pie", autopct = '%2.2f%%')
plt.show()
# Generate a pie plot showing the distribution of female versus male mice using pyplot

num = dataClean.Sex.value_counts()
plt.pie(num.values, labels = num.index.values, autopct = '%2.2f%%')
plt.ylabel("Sex")
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

maxPoint = dataClean.groupby(["Mouse ID"])["Timepoint"].max()
maxPoint = maxPoint.reset_index()

# Merge this group df with the original dataframe to get the tumor volume at the last timepoint
mergeDataComplete = maxPoint.merge(dataClean, on = ["Mouse ID", "Timepoint"], how = "left")
# Put treatments into a list for for loop (and later for plot labels)
treatment = ["Capomulin", "Ramicane", "Infubinol", "Ceftamin"]

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

# Calculate the IQR and quantitatively determine if there are any potential outliers. 
for drugs in treatment:
    
    # Locate the rows which contain mice on each drug and get the tumor volumes
    tumor = mergeDataComplete.loc[mergeDataComplete["Drug Regimen"] == drugs, "Tumor Volume (mm3)"]
    
    # add subset 
    tumorList.append(tumor)
    
    # Determine outliers using upper and lower bounds
    quart = tumor.quantile([.25, .5, .75])
    lowerQuart = quart[.25]
    upperQuart = quart[.75]
    midQuart = upperQuart - lowerQuart
    lowerBound = lowerQuart - (1.5 * midQuart)
    upperBound = upperQuart + (1.5 * midQuart)
    outliers = tumor.loc[(tumor < lowerBound) | (tumor > upperBound)]
    print(f"{drugs}'s potential outliers: {outliers}")
# Generate a box plot of the final tumor volume of each mouse across four regimens of interest
boxPlot = dict(markerfacecolor = "blue", markersize = 12)
plt.boxplot(tumorList, labels = treatment, flierprops = boxPlot)
plt.ylabel("Final Tumor Volume (mm3)")
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
capomulData = dataClean.loc[dataClean["Drug Regimen"] == "Capomulin"]
mouseData = capomulData.loc[capomulData["Mouse ID"] == "x401"]
plt.plot(mouseData["Timepoint"], mouseData["Tumor Volume (mm3)"])
plt.xlabel("Timepoint (days)")
plt.ylabel("Tumor Volume (mm3)")
plt.title("Mouse x401 Capomulin Data")
plt.show()
# Generate a scatter plot of mouse weight versus average tumor volume for the Capomulin regimen
capomulData = dataClean.loc[dataClean["Drug Regimen"] == "Capomulin"]
capomulMean = capomulData.groupby(["Mouse ID"]).mean()
plt.scatter(capomulMean["Weight (g)"], capomulMean["Tumor Volume (mm3)"])
plt.xlabel("Weight (g)")
plt.ylabel("Average 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

correlation = round(st.pearsonr(capomulMean["Weight (g)"], capomulMean["Tumor Volume (mm3)"])[0],2)

print(f"The correlation between mouse weight and the average tumor volume is {correlation}")

model = st.linregress(capomulMean["Weight (g)"], capomulMean["Tumor Volume (mm3)"])

y_values = capomulMean["Weight (g)"] * model[0] + model[1]
plt.scatter(capomulMean["Weight (g)"], capomulMean["Tumor Volume (mm3)"])
plt.plot(capomulMean["Weight (g)"], y_values, color = "blue")
plt.xlabel("Weight (g)")
plt.ylabel("Average Tumor Volume (mm3)")
plt.show()<jupyter_output>The correlation between mouse weight and the average tumor volume is 0.84
{'fraction_non_alphanumeric': 0.11052492745977315, 'fraction_numerical': 0.0081772619361646, 'mean_word_length': 4.684407796101949, 'pattern_counts': {'":': 1, '<': 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': '666789', 'license': 'MIT', 'n_tokens_mistral': 2456, 'n_tokens_neox': 2259, 'n_words': 773, 'path': 'pymaceuticals_starter.ipynb', 'repo_name': 'Mark-Liang-2000/Matplotlib-Challenge', 'size': '7582'}