<jupyter_start><jupyter_text>Observations and Insights<jupyter_code># Dependencies and Setup
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
import random
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(study_results ,mouse_metadata, how="left", on="Mouse ID")

# Display the data table for preview
combined_data
# Checking the number of mice.
len(combined_data["Mouse ID"].unique())
# Getting the duplicate mice by ID number that shows up for Mouse ID and Timepoint. 
duplicate_mouse=combined_data.loc[combined_data.duplicated(subset=["Mouse ID","Timepoint"]),"Mouse ID"].unique()
# Optional: Get all the data for the duplicate mouse ID. 
print(duplicate_mouse)
# Create a clean DataFrame by dropping the duplicate mouse by its ID.
clean_combined_df = combined_data[combined_data["Mouse ID"].isin(duplicate_mouse) == False]
clean_combined_df
# Checking the number of mice in the clean DataFrame.
len(clean_combined_df["Mouse ID"].unique())<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: 
# mean, median, variance, standard deviation, and SEM of the tumor volume. 
tv_mean=clean_combined_df.groupby(["Drug Regimen"]).mean()["Tumor Volume (mm3)"]
tv_median=clean_combined_df.groupby(["Drug Regimen"]).median()["Tumor Volume (mm3)"]
tv_variance=clean_combined_df.groupby(["Drug Regimen"]).var()["Tumor Volume (mm3)"]
tv_std=clean_combined_df.groupby(["Drug Regimen"]).std()["Tumor Volume (mm3)"]
tv_sem=clean_combined_df.groupby(["Drug Regimen"]).sem()["Tumor Volume (mm3)"]

# Assemble the resulting series into a single summary dataframe.
tv_summary_stat = pd.DataFrame({"Tumor Volume Mean (mm3)": tv_mean,
                                "Tumor Volume Median (mm3)": tv_median,
                                "Tumor Volume Variance (mm3)": tv_variance,
                                "Tumor Volume STD (mm3)": tv_std,
                                "Tumor Volume SEM (mm3)": tv_sem})
# Formatting
tv_summary_stat = tv_summary_stat.round(2)
tv_summary_stat
# 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
tv_summary_stat_2 = (clean_combined_df.groupby("Drug Regimen")).agg(["mean","median","var","std","sem"])["Tumor Volume (mm3)"]

# Formating
tv_summary_stat_2 = tv_summary_stat_2.round(2)
tv_summary_stat_2 = tv_summary_stat_2.rename(columns={"mean": "Tumor Volume Mean (mm3)","median":"Tumor Volume Median (mm3)",
                                                     "var":"Tumor Volume Variance (mm3)","std":"Tumor Volume STD (mm3)","sem":"Tumor Volume SEM (mm3)"})
tv_summary_stat_2<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.

# Calculate the total number of measurements taken on each drug regimen
regimen_data_points = clean_combined_df.groupby(["Drug Regimen"]).count()["Mouse ID"]

# Bar plot using pandas
regimen_data_points.plot(kind="bar", facecolor='blue')

# Chart title
plt.title("Total Drug Count")
plt.xlabel("Drug Regimen")
plt.ylabel("Data Points")

plt.show()
# Generate a bar plot showing the total number of measurements taken on each drug regimen using pyplot.

# Create an array for the total number of measurements taken on each drug regimen
data_points_array = (clean_combined_df.groupby(["Drug Regimen"]).count()["Mouse ID"]).tolist()
data_points_array

# Bar plot using pyplot
x_axis = np.arange(len(regimen_data_points))
plt.bar(x_axis, data_points_array, color='b', alpha=0.8, align='center',  width= .5)

# Formating
tick_locations = [drug for drug in x_axis]
plt.xticks(tick_locations, ['Capomulin', 'Ceftamin', 'Infubinol', 'Ketapril', 'Naftisol', 'Placebo', 'Propriva', 'Ramicane', 'Stelasyn', 'Zoniferol'],  rotation='vertical')
plt.xlim(-0.75, len(x_axis)-0.25)
plt.ylim(0, max(data_points_array)+10)

plt.title("Total Drug Count")
plt.xlabel("Drug Regimen")
plt.ylabel("Data Points")

plt.show()
# Generate a pie plot showing the distribution of female versus male mice using pandas

# Calculate the distribution of female versus male mice
by_gender = clean_combined_df.groupby(["Mouse ID","Sex"])
by_gender_df = pd.DataFrame(by_gender.size())

# Create the dataframe with total count
mice_by_gender_df = pd.DataFrame(by_gender_df.groupby(["Sex"]).count())
mice_by_gender_df.columns = ["Total Count"]
# Calculate percentage
mice_by_gender_df["Gender Percentage"] = (100*(mice_by_gender_df["Total Count"]/mice_by_gender_df["Total Count"].sum())).round(2)

# Distribution of female versus male mice data frame
mice_by_gender_df
# Pie plot using pandas

plot = mice_by_gender_df.plot.pie(y='Total Count', colors=['red', 'blue'],explode = (0.1, 0), autopct="%1.1f%%", startangle=90)

# Formatting
plt.ylabel(" ")
plt.title("Male vs Female Mouse Population ")

plt.show()
# Generate a pie plot showing the distribution of female versus male mice using pyplot

# Create Labels for the sections of the pie
labels = mice_by_gender_df.index
distribution = mice_by_gender_df["Gender Percentage"]

# Pie plot using pyplot
plt.pie(distribution, labels=labels, colors = ['red', 'blue'],explode = (0.1, 0), autopct="%1.1f%%", startangle=90 )

#Formatting
plt.title("Male vs Female Mouse Population")

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
max_tp_df = pd.DataFrame(clean_combined_df.groupby('Mouse ID')['Timepoint'].max().sort_values()).reset_index().rename(columns={'Timepoint': 'Max Timepoint'})

# Merge this group df with the original dataframe to get the tumor volume at the last timepoint
merged_df = pd.merge(clean_combined_df, max_tp_df, on='Mouse ID')

# For treatment regimens Capomulin, Ramicane, Infubinol, and Ceftamin
best_regimens_df = merged_df[merged_df["Drug Regimen"].isin(["Capomulin", "Ramicane", "Infubinol", "Ceftamin"])]
best_regimens_df
# Put treatments into a list for for loop (and later for plot labels)
treatments = ['Capomulin', 'Ramicane', 'Infubinol', 'Ceftamin']

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

# Calculate the IQR and quantitatively determine if there are any potential outliers. 
for treatment in treatments:
    temp_df = best_regimens_df.loc[best_regimens_df['Drug Regimen'] == treatment]
    # Create subset 
    final_tum_volume_df = temp_df.loc[temp_df['Timepoint'] == temp_df['Max Timepoint']]
    # Final volume values for each regimen
    values = final_tum_volume_df['Tumor Volume (mm3)']
    tum_vol_values.append(values)
    # Determine outliers using upper and lower bounds
    quartiles = values.quantile([.25,.5,.75])
    lowerq = quartiles[0.25]
    upperq = quartiles[0.75]
    iqr = (upperq-lowerq).round(2)
    print(f'IQR for {treatment}: {iqr}')
    # Find upper and lower bounds to help identify outliers for each regimen
    lower_bound = (lowerq - (1.5*iqr)).round(2)
    upper_bound = (upperq + (1.5*iqr)).round(2)
    print(f'Lower Bound for {treatment}: {lower_bound}')
    print(f'Upper Bound for {treatment}: {upper_bound}')
    # Quantitatively check for outliers
    outliers_count = (values.loc[(final_tum_volume_df['Tumor Volume (mm3)'] >= upper_bound) | 
                                 (final_tum_volume_df['Tumor Volume (mm3)'] <= lower_bound)]).count()
    print(f'Number of {treatment} outliers: {outliers_count}')
# Generate a box plot of the final tumor volume of each mouse across four regimens of interest
plt.boxplot(tum_vol_values, flierprops=dict(marker='o', markerfacecolor='b', markersize=10))

# Add title and labels to boxplot
plt.title('Final Tumor Volume by Regimens')
plt.ylabel('Final Tumor Volume (mm3)')
plt.xticks([1,2,3,4],treatments)

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
capomulin_df = clean_combined_df.loc[clean_combined_df['Drug Regimen'] == 'Capomulin']

random_mouse = random.choice(list(capomulin_df["Mouse ID"]))
print(f'For Capomulin treated Mouse ID: {random_mouse}')
mouse= clean_combined_df.loc[clean_combined_df['Mouse ID'] == random_mouse]


plt.plot(mouse["Timepoint"], mouse["Tumor Volume (mm3)"])
plt.xlabel("Timepoint")
plt.ylabel("Tumor Volume (mm3)")
plt.title("Tumor Volume (mm3) vs. Timepoint")

plt.show()
# BONUS: Generate a line plot of tumor volume vs. time point for "ALL" mouse treated with Capomulin
capomulin_df = clean_combined_df.loc[clean_combined_df['Drug Regimen'] == 'Capomulin']
mouses = capomulin_df['Mouse ID']
for mouse in mouses:
    df= capomulin_df.loc[capomulin_df['Mouse ID'] == mouse]


    plt.plot(df["Timepoint"], df["Tumor Volume (mm3)"])
    plt.xlabel("Timepoint")
    plt.ylabel("Tumor Volume (mm3)")
    plt.title("Tumor Volume (mm3) vs. Timepoint")
  
# Generate a scatter plot of average tumor volume vs. mouse weight for the Capomulin regimen
capomulin_weight= clean_combined_df.loc[clean_combined_df['Drug Regimen'] == 'Capomulin']
capomulin_avg=capomulin_weight.groupby(["Mouse ID"]).mean()
x_values = capomulin_avg["Weight (g)"]
y_values = capomulin_avg["Tumor Volume (mm3)"]

plt.scatter(x_values,y_values )
plt.xlabel("Weight")
plt.ylabel("Tumor Volume (mm3)")
plt.title("Avg Tumor Volume (mm3) vs. Weight for Capomulin")

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

(slope, intercept, rvalue, pvalue, stderr) = linregress(x_values, y_values)
regress_values = x_values * slope + intercept
line_eq = "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,(20,36),fontsize=15,color="red")
plt.xlabel("Weight")
plt.ylabel("Tumor Volume (mm3)")
plt.title("Avg Tumor Volume (mm3) vs. Weight for Capomulin")

# UPDATE: Calculate the correlation coefficient between mouse weight and average tumor volume
correlation = st.pearsonr(x_values, y_values)

plt.show()
print(f"The r-squared is: {rvalue**2}")

# UDTADE: Print correlation coefficient between mouse weight and average tumor volume
print(f"""The correlation between mouse weight and average tumor volume on the Capomulin regimen is {round(correlation[0],2)}.""")<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11973451327433628, 'fraction_numerical': 0.009203539823008849, 'mean_word_length': 5.663325471698113, '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': 3, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '369363', 'license': 'ADSL', 'n_tokens_mistral': 3906, 'n_tokens_neox': 3644, 'n_words': 1030, 'path': 'Pymaceuticals/pymaceuticals.ipynb', 'repo_name': 'jessicapardo/Matplotlib-challenge', 'size': '11300'}