<jupyter_start><jupyter_text>Observations and Insights Capomulin seems to be more effective based on the box plots from the final tumor volumeThe pie chart showed a close distribution between male and female miceCapomulin had the highest rate of survival averaging 90%<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
combined_data_df = pd.merge(mouse_metadata, study_results, how='outer', on='Mouse ID')
combined_data_df.head()

# Display the data table for preview
# Checking the number of mice.
mouse_no =combined_data_df["Mouse ID"].count()
mouse_no
# Getting the duplicate mice by ID number that shows up for Mouse ID and Timepoint. 
duplicate_mice = combined_data_df.groupby("Mouse ID")["Timepoint"].nunique()
duplicate_mice

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

all_duplicate_mice = combined_data_df[combined_data_df.duplicated(["Mouse ID",])]
all_duplicate_mice
# Create a clean DataFrame by dropping the duplicate mouse by its ID.
clean_data_df =combined_data_df.drop_duplicates("Mouse ID")
clean_data_df
# Checking the number of mice in the clean DataFrame.
mouse_count = clean_data_df["Mouse ID"].count()
mouse_count<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 = combined_data_df.groupby("Drug Regimen")["Tumor Volume (mm3)"].mean()
median=combined_data_df.groupby("Drug Regimen")["Tumor Volume (mm3)"].median()
variance = combined_data_df.groupby("Drug Regimen")["Tumor Volume (mm3)"].var()
standard_dev =combined_data_df.groupby("Drug Regimen")["Tumor Volume (mm3)"].std()
SEM = combined_data_df.groupby("Drug Regimen")["Tumor Volume (mm3)"].sem()
sum_stat_df = pd.DataFrame({"Mean": mean, "Median": median, "Variance": variance, "Standard Deviation": standard_dev, "SEM": SEM})
sum_stat_df<jupyter_output><empty_output><jupyter_text>Bar and Pie Charts<jupyter_code># Generate a bar plot showing the total number of timepoints for all mice tested for each drug regimen using Pandas.

drugs_data = pd.DataFrame(pd.value_counts(combined_data_df["Drug Regimen"]))


drugs_data.plot(kind = "bar")
plt.title("Drug Data Regimen")
plt.ylabel("Number of Mice Per Time Point")
plt.xlabel("Drug Regimen")
plt.show()
plt.tight_layout()
# Generate a bar plot showing the total number of timepoints for all mice tested for each drug regimen using pyplot.

x_axis = combined_data_df["Drug Regimen"].value_counts().index
y_axis = combined_data_df["Drug Regimen"].value_counts()
plt.bar(x_axis,y_axis, width = .6)
plt.xticks(x_axis, rotation = 90)
plt.xlim(-0.75, len(x_axis)-0.25)
plt.ylim(0, max(y_axis)+0.4)
plt.ylabel("Number of Mice Per Time Point")
plt.xlabel("Drug Regimen")
plt.title("Drug Data Regimen")
plt.show()
# Generate a pie plot showing the distribution of female versus male mice using Pandas
gender_df = pd.DataFrame(combined_data_df.groupby(["Sex"]).count()).reset_index()
gender_df.head()



gender_mice = gender_df.plot(kind="pie", y="Mouse ID", autopct='%1.1f%%', labels = gender_df["Sex"])
gender_mice.set_ylabel("Sex")
plt.title("Male versus Female Mice Percentage")
plt.tight_layout()
plt.axis("equal")
plt.show()
# Generate a pie plot showing the distribution of female versus male mice using pyplot

sizes = [325, 324]
plt.pie(
    sizes, 
    labels=["Male","Female"],          
    colors=["Blue","Orange"],
    autopct='%1.1f%%',  
    startangle=140
)

# Tells matplotlib that we want a pie chart with equal axes
plt.axis("equal")

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:  

combined_data_df.head()
# Capomulin, Ramicane, Infubinol, and Ceftamin

# Start by getting the last (greatest) timepoint for each mouse


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

drug_list = ['Capomulin', 'Ramicane', 'Infubinol', 'Ceftamin']

four_drugs = combined_data_df[combined_data_df['Drug Regimen'].isin(drug_list)]

four_drugs.head()
four_last_tumor = four_drugs.groupby(['Drug Regimen','Mouse ID']).agg(\
        
Last_tumor_size=('Tumor Volume (mm3)',lambda x: x.iloc[-1])).round(3)
four_last_tumor
four_last_tumor_unstack = four_last_tumor.stack(level=0).unstack(level=0)
four_last_tumor_unstack.head()
# Put treatments into a list for for loop (and later for plot labels)


# 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
count = 0
for drug in drug_list:
    quartiles = four_last_tumor_unstack[drug].quantile([.25,.5,.75]).round(2)
    lowerq = quartiles[0.25].round(2)
    upperq = quartiles[0.75].round(2)
    iqr = round(upperq-lowerq,2)
    lower_bound = round(lowerq - (1.5*iqr),2)
    upper_bound = round(upperq + (1.5*iqr),2)
    
    if count == 0:
        print(f"--------------------------------------------------------")
    print(f"{drug} IQR data is:")
    print(f"The lower quartile of {drug} is: {lowerq}")
    print(f"The upper quartile of {drug} is: {upperq}")
    print(f"The interquartile range of {drug} is: {iqr}")
    print(f"The the median of {drug} is: {quartiles[0.5]} ")
    print(f"Values below {lower_bound} for {drug} could be outliers.")
    print(f"Values above {upper_bound} for {drug} could be outliers.")
    print(f"--------------------------------------------------------")
    count += 1
    
# Generate a box plot of the final tumor volume of each mouse across four regimens of interest
boxplot_list = []
for drug in drug_list:
    boxplot_list.append(list(four_last_tumor_unstack[drug].dropna()))
fig1, ax = plt.subplots(figsize=(7,5))
ax.set_title('Final Tumor Volume from Drug Regimen')
ax.set_xlabel('Drug Regimen')
ax.set_ylabel('Final Tumor Volume (mm3)')
ax.boxplot(boxplot_list,notch=0,sym='gD')
plt.xticks([1,2,3,4],drug_list)
plt.show()<jupyter_output><empty_output><jupyter_text>Line and Scatter Plots<jupyter_code>capomulin_data = combined_data_df.loc[combined_data_df['Drug Regimen'] == 'Capomulin']
capomulin_data.head()
# Generate a line plot of tumor volume vs. time point for a mouse treated with Capomulin
x_axis =combined_data_df.loc[combined_data_df["Mouse ID"]== "s185"]
x_axis = x_axis["Timepoint"]

y_axis =combined_data_df.loc[combined_data_df["Mouse ID"]== "s185"]
y_axis = y_axis["Tumor Volume (mm3)"]

plt.plot(x_axis, y_axis)
plt.title("Tumor Volume with Capomulin for Mouse s185")
plt.xlabel("Timepoint (days)")
plt.ylabel("Tumor Volume (mm3)")
# Generate a scatter plot of average tumor volume vs. mouse weight for the Capomulin regimen
mouse_weight = capomulin_data["Weight (g)"]
tumor_volume = capomulin_data["Tumor Volume (mm3)"]
plt.scatter(mouse_weight, tumor_volume)
plt.title("Average Tumor Volume Versus Mouse Weight forthe Capomulin Regimen")
plt.xlabel("Weight (g)")
plt.ylabel("Tumor Volume (mm3)")
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(mouse_weight, tumor_volume)
print(f"The correlation between both weight and tumor volume is {round(correlation[0],2)}")

(slope, intercept, rvalue, pvalue, stderr) = st.linregress(mouse_weight, tumor_volume)
regress_values = slope * mouse_weight + intercept
plt.scatter(mouse_weight, tumor_volume)
plt.plot(mouse_weight, regress_values, "--", color="r")
plt.title("Average Mouse Weight versus Average Tumor Volume for Capomulin Treatment")
plt.xlabel("Weight (g)")
plt.ylabel("Tumor Volume (mm3)")
plt.show()<jupyter_output>The correlation between both weight and tumor volume is 0.53
{'fraction_non_alphanumeric': 0.1239736324736903, 'fraction_numerical': 0.010523881114837515, 'mean_word_length': 5.294032023289665, 'pattern_counts': {'":': 5, '<': 24, '<?xml version=': 0, '>': 24, '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': '289322', 'license': 'ADSL', 'n_tokens_mistral': 3034, 'n_tokens_neox': 2800, 'n_words': 830, 'path': 'Pymaceuticals.ipynb', 'repo_name': 'Tinuola-1/Matplotlib-Challenge', 'size': '8647'}