<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 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
merged = study_results.merge(mouse_metadata, how="left", on="Mouse ID")

# Display the data table for preview
merged.head()
# Checking the number of mice.
len(merged["Mouse ID"].unique())
# Getting the duplicate mice by ID number that shows up for Mouse ID and Timepoint. 
duplicate_mice = merged.loc[merged.duplicated(subset=["Mouse ID", "Timepoint"])]
# Optional: Get all the data for the duplicate mouse ID. 
duplicate_mice = duplicate_mice["Mouse ID"]
print(duplicate_mice)
# Create a clean DataFrame by dropping the duplicate mouse by its ID.
cleaned_data = merged[merged["Mouse ID"].isin(duplicate_mice) ==False]
cleaned_data
# Checking the number of mice in the clean DataFrame.
len(cleaned_data["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. 
# Assemble the resulting series into a single summary dataframe.
mean_volume = []
meadian_volume = []
var_volume = []
std_volume = []
sem_volume = []

mean_volume = cleaned_data.groupby(["Drug Regimen"])["Tumor Volume (mm3)"].mean()
# print(mean_volume)

median_volume = cleaned_data.groupby(["Drug Regimen"])["Tumor Volume (mm3)"].median()
# print(median_volume)

var_volume = cleaned_data.groupby(["Drug Regimen"])["Tumor Volume (mm3)"].var()
# print(var_volume)

std_volume = cleaned_data.groupby(["Drug Regimen"])["Tumor Volume (mm3)"].std()
# print(std_volume)

sem_volume = cleaned_data.groupby(["Drug Regimen"])["Tumor Volume (mm3)"].sem()
# print(sem_volume)
# Generate a summary statistics table of mean, median, variance, standard deviation, and SEM of the tumor volume for each regimen
summ_stats1 = pd.DataFrame({"Mean Tumor Volume": mean_volume,
                          "Median Tumor Volume": median_volume,
                          "Variance of Tumor Volume": var_volume,
                          "Standard Deviation of Tumor Volume": std_volume,
                          "Standard Error of the Tumor Volume": sem_volume
                          })
summ_stats1
# Using the aggregation method, produce the same summary statistics in a single line
summ_stats2 = cleaned_data.groupby("Drug Regimen").aggregate({"Tumor Volume (mm3)":["mean", "median", "var", "std", "sem"]}) 
summ_stats2<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.
regimen_count = cleaned_data["Drug Regimen"].value_counts()
barchart = regimen_count.plot.bar(width=0.5)

barchart.set_xlabel("Drug Regimen Name")
barchart.set_ylabel("Total Number of Measurements")
barchart.set_title("Total Number of Measurements for Each Drug Regimen")
# Generate a bar plot showing the total number of measurements taken on each drug regimen using pyplot.
regimen_count = cleaned_data["Drug Regimen"].value_counts()

x_axis = np.arange(len(regimen_count))
plt.bar(x_axis, regimen_count, width=0.5)

plt.xlabel("Drug Regimen Name")
plt.ylabel("Total Number of Measurements")
plt.title("Total Number of Measurements for Each Drug Regimen")

plt.show()
# Generate a pie plot showing the distribution of female versus male mice using pandas
sex_data = cleaned_data["Sex"].value_counts()
pieplot = sex_data.plot.pie(autopct="%1.1f%%", startangle=40, title="Distribution by Sex")
pieplot.set_ylabel('')
# Generate a pie plot showing the distribution of female versus male mice using pyplot
sex_data = cleaned_data["Sex"].value_counts()

plt.pie(sex_data, autopct="%1.1f%%", startangle=40, labels=sex_data.index.values)
plt.title("Distribution by Sex")<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

# Put treatments into a list for for loop (and later for plot labels)
four_druglist = ["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. 
for drug in four_druglist:
    # Locate the rows which contain mice on each drug and get the tumor volumes
    something = cleaned_data.loc[cleaned_data["Drug Regimen"]==drug,"Tumor Volume (mm3)"]
    # add subset 
    tumor_vol_data.append(something)
    quartiles = something.quantile([.25,.5,.75])
    lowerq = quartiles[.25]
    upperq = quartiles[.75]
    iqr = upperq-lowerq
    # Determine outliers using upper and lower bounds
    lower_bound = lowerq - (1.5*iqr)
    upper_bound = upperq + (1.5*iqr)
    outliers = something.loc[(something < lower_bound) | (something > upper_bound)]
    outliers
    print(f"The Drug Regimen is {drug} and the lower quartiles is {lowerq}, upper quartile is {upperq}, iqr is {iqr}, lower bound is {lower_bound}, upper bound is {upper_bound}, and outliers are {outliers}")
    
# Generate a box plot of the final tumor volume of each mouse across four regimens of interest
plt.boxplot(tumor_vol_data, labels=four_druglist)
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
mousecap = cleaned_data.loc[cleaned_data["Mouse ID"] == "b128"]

# Plot a line chart with the time values on the x-axis and the tumor volume values on the y-axis
# marker = 'o' adds circles on each data point for visibility
plt.plot(mousecap["Timepoint"], mousecap["Tumor Volume (mm3)"], marker = "^")

# Add labels and title to plot
plt.xlabel("Time (days)")
plt.ylabel("Tumor Volume (mm3)")
plt.title("Capomulin Treatment of Mouse b128")

# Display plot
plt.show()
# Generate a scatter plot of average tumor volume vs. mouse weight for the Capomulin regimen

capomulin= cleaned_data.loc[cleaned_data["Drug Regimen"] == "Capomulin"]


avg_tumorvol = pd.DataFrame(capomulin.groupby("Mouse ID")["Tumor Volume (mm3)"].mean().sort_values()).reset_index().rename(columns={"Tumor Volume (mm3)": "average_tumor_vol"})

avg_tumorvol = pd.merge(capomulin, avg_tumorvol, on="Mouse ID")
final_avg_tumorvol = avg_tumorvol[["Weight (g)", "average_tumor_vol"]].drop_duplicates()
final_avg_tumorvol

x = final_avg_tumorvol["Weight (g)"]
y = final_avg_tumorvol["average_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<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.10586036577226005, 'fraction_numerical': 0.006274195701508477, 'mean_word_length': 5.425385934819897, 'pattern_counts': {'":': 7, '<': 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': '304767', 'license': 'ADSL', 'n_tokens_mistral': 2483, 'n_tokens_neox': 2292, 'n_words': 737, 'path': 'Pymaceuticals/pymaceuticals_starter-WORKCOPY.ipynb', 'repo_name': 'annelaframboise/Pymaceuticals', 'size': '7491'}