<jupyter_start><jupyter_text>Observations and Insights 1. The tumor volume for mice treated with the Capomulin regimen varied directly with mouse weight. 2. The Capomulin regimen and Ramicane regimen trials had data for the greatest number of mice. 3. The mice participating in the Capomulin regimen and Ramicane regimen trials had a smaller average final tumor volume than mice participaing in the Infubinol regimen and the Ceftamin regimen trials.<jupyter_code># Dependencies and Setup
import matplotlib.pyplot as plt
import pandas as pd
import scipy.stats as st
import numpy as np
# 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
pymaceuticals_data_complete = pd.merge(mouse_metadata, study_results, how="left", on=["Mouse ID", "Mouse ID"])
# Display the data table for preview
pymaceuticals_data_complete
# Checking the number of mice.
number_mice = pymaceuticals_data_complete.groupby("Mouse ID")
len(number_mice)
# Getting the duplicate mice by ID number that shows up for Mouse ID and Timepoint. 
duplicate_df = pymaceuticals_data_complete.loc[pymaceuticals_data_complete.duplicated(subset = 
                                                                ['Mouse ID', 'Timepoint']),"Mouse ID"].unique()


# Optional: Get all the data for the duplicate mouse ID. 
duplicate_df

# Create a clean DataFrame by dropping the duplicate mouse by its ID.

clean_df = pymaceuticals_data_complete[pymaceuticals_data_complete["Mouse ID"].isin(duplicate_df)==False]
clean_df
# Checking the number of mice in the clean DataFrame.
len(clean_df.groupby("Mouse ID"))<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
mean_data = clean_df.groupby("Drug Regimen").mean()["Tumor Volume (mm3)"]
med_data = clean_df.groupby("Drug Regimen").median()["Tumor Volume (mm3)"]
var_data = clean_df.groupby("Drug Regimen").var()["Tumor Volume (mm3)"]
std_data = clean_df.groupby("Drug Regimen").std()["Tumor Volume (mm3)"]
sem_data = clean_df.groupby("Drug Regimen").sem()["Tumor Volume (mm3)"]
# This method is the most straighforward, creating multiple series and putting them all together at the end.

summary_df = pd.DataFrame({"Mean": mean_data, "Median": med_data, "Variance": var_data, "Standard Deviation": std_data,
                          "SEM": sem_data})
summary_df
summary_df2 = clean_df.groupby("Drug Regimen").agg({"Tumor Volume (mm3)": ["mean", "median", "var", "std", "sem"]})
summary_df2
# 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<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. 

# Generate a bar plot showing the total number of mice for each treatment throughout the course of the study using pandas. 
total_mice = clean_df["Mouse ID"].value_counts()
regimen_data = clean_df["Drug Regimen"].value_counts()


regimen_data.plot(kind="bar", facecolor="red")
plt.title("Number Mice per Drug Regimen")
plt.ylabel("Total Number Mice")
plt.xlabel("Drug Regimen")
plt.show()
plt.tight_layout()
# Generate a bar plot showing the total number of mice for each treatment throughout the course of the study using pyplot.

plt.bar(regimen_data.index.values,regimen_data.values)
plt.title("Number Mice per Drug Regimen")
plt.ylabel("Total Number Mice")
plt.xlabel("Drug Regimen")
plt.show
plt.xticks(rotation=90)

# Generate a pie plot showing the distribution of female versus male mice using pandas
gender_data.plot(kind="pie", autopct='%1.1f%%')
plt.title("Distribution by Sex")
plt.show()

# Generate a pie plot showing the distribution of female versus male mice using pyplot

gender_data = clean_df["Sex"].value_counts()
plt.pie(gender_data.values, labels=gender_data.index.values, autopct='%1.1f%%')
plt.title("Distribution by 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
last_timepoint = clean_df.groupby("Mouse ID").max()["Timepoint"]
merge_df=pd.merge(last_timepoint, clean_df, how="left", on=["Mouse ID", "Timepoint"])

# Merge this group df with the original dataframe to get the tumor volume at the last timepoint


merge_df
# Put treatments into a list for for loop (and later for plot labels)
drug_list = ["Capomulin", "Ramicane", "Infubinol", "Ceftamin"]
volume_list = []
for x in drug_list:
    drug_volume = merge_df.loc[(merge_df["Drug Regimen"] == x)]["Tumor Volume (mm3)"]
    volume_list.append(drug_volume)

    quartiles = drug_volume.quantile([.25,.5,.75])
    lowerq = quartiles[0.25]
    upperq = quartiles[0.75]
    iqr = upperq-lowerq
    lower_bound = lowerq - (1.5*iqr)
    upper_bound = upperq + (1.5*iqr)
    outlier = drug_volume.loc[(drug_volume < lower_bound) | (drug_volume > upper_bound)]
    print(f"{x} Outlier is: {outlier}")
# Create empty list to fill with tumor vol data (for plotting)


# 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
    
    
    # add subset 
    
    
    # Determine outliers using upper and lower bounds
    
# Generate a box plot of the final tumor volume of each mouse across four regimens of interest
plt.boxplot(volume_list, labels=drug_list)
plt.title("Final Tumor Volume verus Drug Regimen")
plt.ylabel("Final Tumor Volume (mm3)")
plt.xlabel("Drug Regimen")
plt.show()<jupyter_output><empty_output><jupyter_text>Line and Scatter Plots<jupyter_code>capomulin_df.groupby("Mouse ID").head()

# Generate a line plot of time point versus tumor volume for a mouse treated with Capomulin
time = np.arange(0,50,5)

time_point = [0, 5, 10, 15, 20]
tumor_volume = [45, 43.878496, 37.614948, 38.177232, 36.866876]
s_185, = plt.plot(time_point, tumor_volume, color="red", label="Mouse ID s185")
plt.title("Capomulin Tumor Volume for Mouse ID s185")
plt.xlabel("Time Point")
plt.ylabel("Tumor Volume (mm3)")
plt.legend(handles=[s_185], loc="best")
plt.grid()
plt.show()
capo_df = capomulin_df.groupby("Mouse ID").mean()
capo_df
# Generate a scatter plot of mouse weight versus average tumor volume for the Capomulin regimen


plt.scatter(capo_df["Weight (g)"],capo_df["Tumor Volume (mm3)"])
plt.title("Capomulin Tumor Volume versus Mouse Weight")

plt.grid()

plt.ylabel("Average Tumor Volume (mm3)")
plt.xlabel("Mouse Weight (g)")

plt.show()<jupyter_output><empty_output><jupyter_text>Correlation and Regression<jupyter_code># Calculate the correlation coefficient and linear regression model 

plt.scatter(capo_df["Weight (g)"],capo_df["Tumor Volume (mm3)"])

plt.xticks(capo_df["Weight (g)"], rotation=90)
plt.ylabel("Average Tumor Volume (mm3)")
plt.xlabel("Mouse Weight (g)")
plt.title("Capomulin Tumor Volume versus Mouse Weight")

plt.grid()
capo_slope, capo_int, capo_r, capo_p, capo_std_err = st.linregress(capo_df["Weight (g)"],capo_df["Tumor Volume (mm3)"])
capo_fit = capo_slope * capo_df["Weight (g)"] + capo_int
plt.plot(capo_df["Weight (g)"],capo_fit,"--")
plt.show()
print(f"The linear regression equation is: Average Tumor Volume (mm3) = {capo_slope} mm3/g * Mouse Weight (g) + {capo_int} mm3")
print(f"The correlation coefficient is: {capo_r}")<jupyter_output>The linear regression equation is: Average Tumor Volume (mm3) = 0.9544396890241046 mm3/g * Mouse Weight (g) + 21.552160532685015 mm3
The correlation coefficient is: 0.8419363424694719
{'fraction_non_alphanumeric': 0.107958050586058, 'fraction_numerical': 0.019370758790869833, 'mean_word_length': 5.4692737430167595, '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': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '542337', 'license': 'ADSL', 'n_tokens_mistral': 2828, 'n_tokens_neox': 2564, 'n_words': 826, 'path': 'Pymaceuticals/pymaceuticals_starter.ipynb', 'repo_name': 'shellymnixon/Matplotlib-challenge', 'size': '8105'}