<jupyter_start><jupyter_text>Observations and Insights  1) The lowest tumor volume for Mouse "b742", on the Capomulin treatment, was on Day 20 (even though treatment continued for another 25 days).2) Propriva had the least number of measurements of all treatments tested.3) Of the four regimens of interest, Ramicane had the lowest average final tumor volume.<jupyter_code># Dependencies and Setup
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import scipy.stats as st
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_data = pd.merge(mouse_metadata, study_results, how='outer', on="Mouse ID")

# Display the data table for preview
combined_data.head()
# Checking the number of mice.
mice_count = combined_data["Mouse ID"].nunique()
mice_count
# Getting the duplicate mice by ID number that shows up for Mouse ID and Timepoint. 
dup_ID = combined_data.loc[combined_data.duplicated(subset=['Mouse ID', 'Timepoint']),'Mouse ID'].unique()
dup_ID
# Optional: Get all the data for the duplicate mouse ID. 
dup_ID_data = combined_data.loc[combined_data["Mouse ID"] == "g989", :]
dup_ID_data
# Create a clean DataFrame by dropping the duplicate mouse by its ID.
clean_df = combined_data[combined_data['Mouse ID'].isin(dup_ID)==False]
# Checking the number of mice in the clean DataFrame.
clean_count = clean_df["Mouse ID"].nunique()
clean_count<jupyter_output><empty_output><jupyter_text>Summary Statistics<jupyter_code># Use groupby and summary statistical methods to calculate the following properties of each drug regimen: 
# mean, median, variance, standard deviation, and SEM of the tumor volume.
mean = clean_df['Tumor Volume (mm3)'].groupby(clean_df['Drug Regimen']).mean()
median = clean_df['Tumor Volume (mm3)'].groupby(clean_df['Drug Regimen']).median()
var = clean_df['Tumor Volume (mm3)'].groupby(clean_df['Drug Regimen']).var()
std = clean_df['Tumor Volume (mm3)'].groupby(clean_df['Drug Regimen']).std()
sem = clean_df['Tumor Volume (mm3)'].groupby(clean_df['Drug Regimen']).sem()

# Assemble the resulting series into a single summary dataframe.
summary_df = pd.DataFrame({"Mean Tumor Vol.": mean, 
                           "Median Tumor Vol.": median, 
                           "Tumor Vol. Variance": var, 
                           "Tumor Vol. Std. Dev.": std, 
                           "Tumor Vol. Std. Err.": sem})

# Generate a summary statistics table of mean, median, variance, standard deviation, and SEM of the tumor volume for each regimen
summary_df
# Using the aggregation method, produce the same summary statistics in a single line
summary_aggregate = clean_df.groupby(['Drug Regimen'])[['Tumor Volume (mm3)']].agg(
    ['mean', 'median', 'var', 'std', 'sem'])
summary_aggregate<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.
bar_plot = clean_df['Drug Regimen'].value_counts().plot.bar(width=0.6)
bar_plot.set_xlabel("Drug Regimen")
bar_plot.set_ylabel("Number of Measurements")
bar_plot.set_title("Number of Measurements for Each Drug Regimen")
# Generate a bar plot showing the total number of measurements taken on each drug regimen using pyplot.
counts = clean_df['Drug Regimen'].value_counts()
x_axis = np.arange(len(counts))
plt.bar(x_axis, counts, width = 0.6)

tick_locations = [value for value in x_axis]
plt.xticks(tick_locations, counts.index.values)
plt.xticks(rotation=90)

plt.xlabel("Drug Regimen")
plt.ylabel("Number of Measurements")
plt.title('Number of Measurements for Each Drug Regimen')
plt.show()
# Generate a pie plot showing the distribution of female versus male mice using pandas
data = clean_df['Sex'].value_counts()
pie_plot = data.plot.pie(autopct="%1.1f%%", startangle=90, title='Male vs. Female Mice')
pie_plot.set_ylabel('')
# Generate a pie plot showing the distribution of female versus male mice using pyplot
plt.pie(data, labels=data.index.values, autopct="%1.1f%%", startangle=90)
plt.title('Male vs. Female 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
last_tp = clean_df.groupby("Mouse ID")["Timepoint"].max()

# Merge this group df with the original dataframe to get the tumor volume at the last timepoint
merge_df = pd.merge(last_tp, clean_df, on=["Mouse ID", "Timepoint"], how="left").rename(columns = {"Timepoint": "Last Timepoint"})
merge_df
# Put treatments into a list for for loop (and later for plot labels)
drugs = ["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. 
# Locate the rows which contain mice on each drug and get the tumor volumes
for drug in drugs:
    drug_df = merge_df.loc[merge_df["Drug Regimen"] == drug]
    
    # add subset 
    final_df = drug_df.loc[drug_df["Last Timepoint"] == drug_df["Last Timepoint"]]
    
    # Determine outliers using upper and lower bounds    
    values = final_df["Tumor Volume (mm3)"]
    tumor_vol_data.append(values)

    quartiles = values.quantile([.25, .5, .75])
    lowerq = quartiles[0.25]
    upperq = quartiles[0.75]
    iqr = upperq-lowerq
    print(f'IQR for {drug}: {iqr}')
    
    lower_bound = lowerq - (1.5*iqr)
    upper_bound = upperq + (1.5*iqr)
    print(f'Lower Bound for {drug}: {lower_bound}')
    print(f'Upper Bound for {drug}: {upper_bound}')

    outliers_count = (values.loc[(final_df["Tumor Volume (mm3)"] >= upper_bound) | (final_df["Tumor Volume (mm3)"] <= lower_bound)]).count()
    print(f'Number of {drug} outliers: {outliers_count}')
# Generate a box plot of the final tumor volume of each mouse across four regimens of interest
flierprops = dict(marker="o", markerfacecolor= 'r', markersize=8, markeredgecolor= 'black')

plt.boxplot(tumor_vol_data, flierprops=flierprops)

plt.title('Final Tumor Volume by Treatment')
plt.ylabel('Final Tumor Volume (mm3)')
plt.xticks([1, 2, 3, 4], ["Capomulin", "Ramicane", "Infubinol", "Ceftamin"])

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
mouse = clean_df.loc[clean_df['Mouse ID'] == 'b742']

plt.plot(mouse['Timepoint'], mouse['Tumor Volume (mm3)'], marker = 'o')
plt.xlabel("Time (days)")
plt.ylabel("Tumor Volume (mm3)")
plt.title("Capomulin Treatment for Mouse b742")
plt.show()
# Generate a scatter plot of average tumor volume vs. mouse weight for the Capomulin regimen
capomulin_df = clean_df.loc[clean_df['Drug Regimen'] == 'Capomulin']

avg_vol_df = pd.DataFrame(capomulin_df.groupby('Mouse ID')['Tumor Volume (mm3)'].mean().sort_values()).reset_index().rename(columns={'Tumor Volume (mm3)': 'avg_tumor_vol'})

vol_df = pd.merge(capomulin_df, avg_vol_df, on='Mouse ID')
final_vol_df = vol_df[['Weight (g)', 'avg_tumor_vol']].drop_duplicates()
final_vol_df

x = final_vol_df['Weight (g)']
y = final_vol_df['avg_tumor_vol']

plt.scatter(x, y)
plt.xlabel("Weight (g)")
plt.ylabel("Average Tumor Volume (mm3)")
plt.title('Average Tumor Volume by 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(x,y)

print(f"The correlation between mouse weight and average tumor volume for the Capomulin regimen is {round(correlation[0],2)}.")

(slope, intercept, rvalue, pvalue, stderr) = linregress(x, y)
regress_values = x * slope + intercept
line_eq = "y = " + str(round(slope,2)) + "x + " + str(round(intercept,2))

plt.scatter(x,y)
plt.plot(x,regress_values,"r-")
plt.annotate(line_eq,(20,37),fontsize=15,color="red")
plt.xlabel("Weight (g)")
plt.ylabel("Average Tumor Volume (mm3)")
plt.title('Average Tumor Volume by Weight')
plt.show()<jupyter_output>The correlation between mouse weight and average tumor volume for the Capomulin regimen is 0.84.
{'fraction_non_alphanumeric': 0.12047491560935863, 'fraction_numerical': 0.009544872541031311, 'mean_word_length': 5.407158836689038, 'pattern_counts': {'":': 6, '<': 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': 5, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '292696', 'license': 'ADSL', 'n_tokens_mistral': 2980, 'n_tokens_neox': 2757, 'n_words': 859, 'path': 'Pymaceuticals/pymaceuticals.ipynb', 'repo_name': 'bananerehe/matplotlib-challenge', 'size': '8591'}