<jupyter_start><jupyter_text>Observations and Insights   1. The study used approximately the same number of male and female mice in the study 2. Strong correlation between weight of mouse and tumor volume, so final tumor volume could be skewed by weight of mouse if not controlled 3. Of the 4 regimens review, Ramicane appear to be most effictive in decreasing tumor volume assuming beginning tumor volume was controlled<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 sem

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

# Display the data table for preview
merged_data.head()
# Checking the number of mice.
print(merged_data.count())
print(merged_data.nunique())
# Getting the duplicate mice by ID number that shows up for Mouse ID and Timepoint. 
duplicated_mice = merged_data[merged_data.duplicated(['Mouse ID', 'Timepoint'], keep=False)]
duplicated_mice.count()
# Create a clean DataFrame by dropping the duplicate mouse by its ID.
clean_data = merged_data.drop_duplicates(['Mouse ID', 'Timepoint'], keep='first')
clean_data
# Checking the number of mice in the clean DataFrame.
clean_data["Mouse ID"].count()
clean_data["Mouse ID"].nunique()<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
# Use groupby and summary statistical methods to calculate the following properties of each drug regimen: 
regimen_group = clean_data.groupby('Drug Regimen')

# mean, median, variance, standard deviation, and SEM of the tumor volume. 
mean_data = (regimen_group["Tumor Volume (mm3)"]).mean()
median_data = regimen_group["Tumor Volume (mm3)"].median()
var_data = regimen_group["Tumor Volume (mm3)"].var()
std_data = regimen_group["Tumor Volume (mm3)"].std()
sem_data = regimen_group["Tumor Volume (mm3)"].sem()

# Assemble the resulting series into a single summary dataframe.
summary_df = pd.DataFrame({
    "Mean Tumor" : mean_data,
    "Median Tumor" : median_data,
    "Variance": var_data,
    "STDev": std_data,
    "SEM": sem_data
})

summary_df
# Generate a summary statistics table of mean, median, variance, standard deviation, and SEM of the tumor volume for each regimen

# Using the aggregation method, produce the same summary statistics in a single line


total_sum = regimen_group.agg({"Tumor Volume (mm3)": ["mean","median", "var", "std","sem"]}).rename(columns={"mean":"Tumor Mean", "median":"Tumor Median","var":"Variance", "std":"STDev", "sem":"SEM"})

total_sum<jupyter_output><empty_output><jupyter_text>Bar and Pie Charts<jupyter_code># Generate a bar plot showing the total number of measurements taken on each drug regimen using pandas.
measurements = regimen_group["Drug Regimen"].count()
measurements.plot(kind = "bar", title = "Measurements by Regimen")
plt.ylabel("Number of Measurements")
plt.show()

# Generate a bar plot showing the total number of measurements taken on each drug regimen using pyplot.
x_axis = np.arange(len(measurements))
y_axis =[x for x in measurements.index]
ticks = [x for x in x_axis]
plt.bar(x_axis, regimen_group["Drug Regimen"].count())
plt.title("Measurements by Regimen")
plt.xlabel("Drug Regimen")
plt.ylabel("Number of Measurements")
plt.xticks(ticks, y_axis, rotation = "vertical")
plt.show()
# Generate a pie plot showing the distribution of female versus male mice using pandas
gender_group = clean_data.groupby("Sex")
gender_count = gender_group["Mouse ID"].nunique()
gender_count.plot(kind = "pie", title = "Gender of Mice", autopct = "%1.1f%%", startangle = 90)
plt.axis("equal")
plt.show()
# Generate a pie plot showing the distribution of female versus male mice using pyplot
pie_labels = [x for x in gender_count.index]
plt.pie(gender_count, labels = pie_labels, autopct = "%1.1f%%", startangle = 90 )
plt.title("Gender of Mice")
plt.axis("equal")
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
treatments = ["Capomulin", "Ramicane", "Infubinol", "Ceftamin"]
# Start by getting the last (greatest) timepoint for each mouse
last_tp = clean_data.groupby("Mouse ID")['Timepoint'].max()

# Merge this group df with the original dataframe to get the tumor volume at the last timepoint
time_merge = pd.merge(clean_data, last_tp, on ="Mouse ID", how = "outer")
tumor_vol = time_merge[time_merge["Timepoint_x"] == time_merge["Timepoint_y"]]
regimen_vol = tumor_vol.loc[tumor_vol["Drug Regimen"].isin(treatments)]


# Put treatments into a list for for loop (and later for plot labels)
# Create empty list to fill with tumor vol data (for plotting)
treatments = ["Capomulin", "Ramicane", "Infubinol", "Ceftamin"]
tumor_volume = []

for drug in treatments:
    vol = regimen_vol.loc[regimen_vol["Drug Regimen"] == drug, ["Tumor Volume (mm3)"]]
    tumor_volume.append(vol["Tumor Volume (mm3)"])

# Calculate the IQR and quantitatively determine if there are any potential outliers. 
    quartiles = vol["Tumor Volume (mm3)"].quantile([0.25,0.5,0.75])
    lowerq = quartiles[0.25]
    upperq = quartiles[0.75]
    iqr = upperq - lowerq

    
    
    # Determine outliers using upper and lower bounds
    lower_bound = lowerq - (1.5* iqr)
    upper_bound = upperq + (1.5 * iqr)

    outliers = vol.loc[(vol["Tumor Volume (mm3)"] < lower_bound) | (vol["Tumor Volume (mm3)"] > upper_bound)]

# Generate a box plot of the final tumor volume of each mouse across four regimens of interest
plt.boxplot(tumor_volume, labels = treatments, flierprops =dict(markerfacecolor = "r", marker = 'D'))
plt.title("Final measured Tumor volume by Drug Regimen")
plt.xlabel("Drug Regimen")
plt.ylabel('Tumor Vol (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
cap_df = clean_data[clean_data["Drug Regimen"] == "Capomulin"]
mouse_df = cap_df[cap_df["Mouse ID"] == "s185"]
mouse_df.head()
mouse_df.plot(x = "Timepoint", y = "Tumor Volume (mm3)", title = "Tumor Size vs Time")
plt.ylabel("Tumor Volume (mm3)")
plt.show()

# Generate a scatter plot of average tumor volume vs. mouse weight for the Capomulin regimen
weight_df = cap_df[["Mouse ID","Weight (g)","Tumor Volume (mm3)"]]
avg_tum = pd.DataFrame(weight_df.groupby(["Mouse ID", "Weight (g)"])["Tumor Volume (mm3)"].mean()).reset_index('Weight (g)')
avg = avg_tum.rename(columns = {"Tumor Volume (mm3)": "Avg Tumor Volume"})
avg.plot(x = "Weight (g)", y = "Avg Tumor Volume", kind = "scatter", title = "Avg Tumor Volume vs Weight")
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 = st.pearsonr(avg["Weight (g)"], avg["Avg Tumor Volume"])
print(f"The correlation coefficient is {round(correlation[0],2)}")

x_val = avg["Weight (g)"]
y_val = avg["Avg Tumor Volume"]
(slope, intercept, rvalue, pvalue, stderr) = st.linregress(x_val, y_val)
regress = slope * x_val + intercept
eq = f"y = {round(slope,2)}x + {round(intercept,2)}"
avg.plot(x = "Weight (g)", y = "Avg Tumor Volume", kind = "scatter", title = "Avg Tumor Volume vs Weight")
plt.plot(x_val, regress, "r-")
plt.annotate(eq, (17,41), color ="r")
plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12110552763819095, 'fraction_numerical': 0.00728643216080402, 'mean_word_length': 5.804273504273504, 'pattern_counts': {'":': 10, '<': 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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '446077', 'license': 'ADSL', 'n_tokens_mistral': 2713, 'n_tokens_neox': 2499, 'n_words': 842, 'path': 'Pymaceuticals/pymaceuticals_starter.ipynb', 'repo_name': 'perkins228/matplotlib', 'size': '7960'}