<jupyter_start><jupyter_text>Observations and Insights<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
mouse_df = pd.merge(mouse_metadata, study_results, on='Mouse ID')

# Display the data table for preview
mouse_df.head()
# Checking the number of mice.
mouse_df["Mouse ID"].nunique()
# Getting the duplicate mice by ID number that shows up for Mouse ID and Timepoint. 
mouse_df[mouse_df.duplicated(subset=["Mouse ID", "Timepoint"], keep=False)]
# Optional: Get all the data for the duplicate mouse ID. 
duplicated = mouse_df[mouse_df["Mouse ID"] == "g989"]
# Create a clean DataFrame by dropping the duplicate mouse by its ID.
mouse_full = mouse_df.drop(duplicated.index)
# Checking the number of mice in the clean DataFrame.
mouse_full["Mouse ID"].nunique()
import seaborn as sns<jupyter_output><empty_output><jupyter_text>Summary Statistics<jupyter_code>mouse_full.head()
# 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.
drug_group = mouse_full.groupby(["Drug Regimen"])
drug_mean = drug_group.mean()["Tumor Volume (mm3)"]
drug_median = drug_group.median()["Tumor Volume (mm3)"]
drug_var = drug_group.var()["Tumor Volume (mm3)"]
drug_std = drug_group.std()["Tumor Volume (mm3)"]
drug_sem = drug_group.sem()["Tumor Volume (mm3)"]

drug_summary = pd.DataFrame({"Mean":drug_mean, "Median":drug_median, "Variance":drug_var, 
             "Standard Dev": drug_std, "SEM":drug_sem})
drug_summary
# 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
mouse_full.groupby(["Drug Regimen"]).agg({"Tumor Volume (mm3)":["mean", "median", "var", "std", "sem"]})<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.
drug_group.agg({"Mouse ID":"count"}).sort_values("Mouse ID", ascending=False).plot(kind='bar',
                                                                                  title="Number of Measurements by Drug Regimen")
plt.ylabel("Number of Measurements")
plt.xlabel("Drug Regimen");
# Generate a bar plot showing the total number of measurements taken on each drug regimen using pandas.
sns.countplot(data=mouse_full, x="Drug Regimen",
             order= mouse_full["Drug Regimen"].value_counts().index)

plt.title("Number of Measurements by Drug Regimen")
plt.ylabel("Number of Measurements")
plt.xticks(rotation=90)
plt.show()
# Generate a bar plot showing the total number of measurements taken on each drug regimen using pyplot.
drug_rbymouse = drug_group.count()["Mouse ID"]
drug_rbymouse = drug_rbymouse.reset_index()
drug_rbymouse = drug_rbymouse.sort_values("Mouse ID", ascending=False)
# Generate a bar plot showing the total number of measurements taken on each drug regimen using pyplot.
plt.bar(drug_rbymouse["Drug Regimen"], drug_rbymouse["Mouse ID"])

plt.xticks(rotation=90)
plt.title("Number of Measurements by Drug Regimen")
plt.ylabel("Number of Measurements")
plt.xlabel("Drug Regimen")
plt.show()
# Generate a pie plot showing the distribution of female versus male mice using pandas
mouse_full["Sex"].value_counts().plot(kind='pie', autopct="%1.1f%%", startangle=140)

plt.title("Female vs Male Distribution")

plt.show()
# Generate a pie plot showing the distribution of female versus male mice using pyplot
gender_group = mouse_full.groupby(["Sex"]).count()["Mouse ID"]
gender_group = gender_group.reset_index()
gender_group
plt.pie(gender_group["Mouse ID"], labels= gender_group["Sex"], autopct= "%1.1f%%",
       colors=["#FF7F0E", "#2077B4"])

plt.title("Female vs Male Distribution")

plt.show()<jupyter_output><empty_output><jupyter_text>Quartiles, Outliers and Boxplots<jupyter_code>mouse_full
# Calculate the final tumor volume of each mouse across four of the treatment regimens Sort DF:  
# Capomulin, Ramicane, Infubinol, and Ceftamin
drug_list = ["Capomulin", "Ramicane", "Infubinol", "Ceftamin"]
drugs_dataframe = mouse_full[mouse_full["Drug Regimen"].isin(drug_list)]
drugs_dataframe
# Start by getting the last (greatest) timepoint for each mouse
max_time = drugs_dataframe.groupby(["Mouse ID"]).max()["Timepoint"]
max_time = max_time.reset_index()
max_time
# Merge this group df with the original dataframe to get the tumor volume at the Mouse ID & last timepoint 
drugs_df = pd.merge(drugs_dataframe, max_time, on=["Mouse ID", "Timepoint"])
drugs_df
# Put treatments into a list for for loop (and later for plot labels)
drug_list = ["Capomulin", "Ramicane", "Infubinol", "Ceftamin"]

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

# Calculate the IQR and quantitatively determine if there are any potential outliers. 
for drug in drug_list:

    # Locate the rows which contain mice on each drug and get the tumor volumes
    drug_data = drugs_df.loc[drugs_df["Drug Regimen"] == drug, "Tumor Volume (mm3)"]
    tumor_vol.append(drug_data)
    
    # add subset 
    quartiles = drug_data.quantile([0.25, 0.5, 0.75])
    lower_q = quartiles[0.25]
    upper_q = quartiles[0.75]
    
    iqr = upper_q - lower_q
    
    # Determine outliers using upper and lower bounds
    lower_bound = lower_q - (1.5*iqr)
    upper_bound = upper_q + (1.5*iqr)
    
    outliers = drug_data[(drug_data > upper_bound) | (drug_data < lower_bound)]
    
    print(outliers)    
# Generate a box plot of the final tumor volume of each mouse across four regimens of interest
flierprops = dict(marker="o", markerfacecolor='blue', markersize=12,
                  linestyle='none')

fig, ax1 = plt.subplots()

ax1.boxplot(tumor_vol, labels= drug_list, flierprops=flierprops)
ax1.set_title("Final Tumor Volume of Treatment Regimens")
ax1.set_xlabel("Drug Regimen")
ax1.set_ylabel("Tumor Volume (mm3)")
plt.show()
fig, ax1 = plt.subplots()

sns.boxplot(y=tumor_vol, x=drug_list)

ax1.set_title("Final Tumor Volume of Treatment Regimens")
ax1.set_xlabel("Drug Regimen")
ax1.set_ylabel("Tumor Volume (mm3)")

plt.show()<jupyter_output><empty_output><jupyter_text>Line and Scatter Plots<jupyter_code>capo_df = drugs_dataframe[drugs_dataframe["Drug Regimen"] == "Capomulin"]
m_s2185 = capo_df[capo_df["Mouse ID"] == "s185"]
mouse_info = m_s2185.groupby(["Timepoint"]).mean()["Tumor Volume (mm3)"]
mouse_info = mouse_info.reset_index()
mouse_info
# Generate a line plot of tumor volume vs. time point for a mouse treated with Capomulin
plt.plot(mouse_info["Timepoint"], mouse_info["Tumor Volume (mm3)"], color='red')
plt.title('Capomulin Results: Mouse s185')
plt.xlabel('Timepoint')
plt.ylabel('Tumor Volume (mm3)')

plt.show()
# Generate a scatter plot of average tumor volume vs. mouse weight for the Capomulin regimen
w_capo_group = capo_df.groupby(["Weight (g)"]).mean()["Tumor Volume (mm3)"]
w_capo_df = w_capo_group.reset_index()
w_capo_df
x_values = w_capo_df["Weight (g)"]
y_values = w_capo_df["Tumor Volume (mm3)"]
plt.scatter(x_values, y_values)
plt.xlabel("Weight (g)")
plt.title("Weight and Average Tumor Volume Relationship")
plt.ylabel("Tumor Volume (mm3)")

plt.show()
sns.set_style("whitegrid")
sns.scatterplot(x=x_values, y=y_values, )
plt.title("Weight and Average Tumor Volume Relationship");<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
from scipy.stats import linregress
slope, intercept, rvalue, pvalue, std_err = linregress(x_values, y_values)

regress_values = slope * x_values + intercept
line_eq = f"y = {round(slope)}x + {round(intercept,2)}"
plt.scatter(x_values, y_values)

plt.plot(x_values, regress_values, color='red', ls ='--')
plt.annotate(line_eq,(20, 37),fontsize=14)

plt.xlabel("Weight (g)")
plt.title("Weight and Average Tumor Volume Relationship")
plt.ylabel("Tumor Volume (mm3)")

plt.show()
print(f"The correlation between both factors is: {round(rvalue,2)}")
print(f"The r2 of the model is: {(rvalue**2)}")
plt.scatter(x_values, y_values)
plt.xlabel("Weight (g)")
plt.title("Weight and Average Tumor Volume Relationship")
plt.ylabel("Tumor Volume (mm3)")

plt.show()<jupyter_output><empty_output>
StarcoderdataJupyterStructuredCleanDedup