<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
output = pd.merge(mouse_metadata,study_results,
                 on='Mouse ID',
                 how='outer')
# Display the data table for preview
output
# Checking the number of mice.
number_mice = output['Mouse ID'].count()
mice_number = pd.DataFrame({"Number of Mice": [number_mice]}, index=[0])
mice_number


# Getting the duplicate mice by ID number that shows up for Mouse ID and Timepoint.
# Optional: Get all the data for the duplicate mouse ID. 
#output[df.duplicated()]

dup_rows = output[output.duplicated(['Mouse ID', 'Timepoint'])]
dup_rows
# Optional: Get all the data for the duplicate mouse ID. 
all_duplicate_mouse = output[output.duplicated(['Mouse ID'])]
all_duplicate_mouse
# Create a clean DataFrame by dropping the duplicate mouse by its ID. 
#apply drop duplicates to get clean data, use drop duplicates for a clean data
dropping_duplicates = output.drop_duplicates('Mouse ID')
dropping_duplicates
# Checking the number of mice in the clean DataFrame.
clean_data = dropping_duplicates['Mouse ID'].count()
clean_data = pd.DataFrame({"Number of Mice": [clean_data]}, index=[0])
clean_data<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. 
summary_reg = pd.DataFrame(output.groupby("Drug Regimen").count())
summary_reg ["Mean"] = pd.DataFrame(output.groupby("Drug Regimen")["Tumor Volume (mm3)"].mean())
summary_reg ["Median"] = pd.DataFrame(output.groupby("Drug Regimen")["Tumor Volume (mm3)"].median())
summary_reg ["Variance"] = pd.DataFrame(output.groupby("Drug Regimen")["Tumor Volume (mm3)"].var())
summary_reg ["Standard Deviation"] = pd.DataFrame(output.groupby("Drug Regimen")["Tumor Volume (mm3)"].std())
summary_reg ["SEM"] = pd.DataFrame(output.groupby("Drug Regimen")["Tumor Volume (mm3)"].sem())


# Assemble the resulting series into a single summary dataframe.
summary_reg = summary_reg[["Mean","Median","Variance","Standard Deviation","SEM"]]
summary_reg.head()
# 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

#summary_reg = summary_reg[["Mean","Median","Variance","Standard Deviation","SEM"]]
regg_regi = output.groupby('Drug Regimen')['Tumor Volume (mm3)'].agg(['mean','median','var','std','sem'])
regg_regi<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.
total_measurements = output.groupby("Drug Regimen").agg({"Timepoint":'count'})

# Generate a bar plot showing the total number of measurements taken on each drug regimen using pyplot.
total_measurements.plot(kind="bar", figsize=(10,3))

plt.title("Number of Measurements by Drug Regimen")
plt.show()
plt.tight_layout()

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

gender_distribution = output.groupby(["Mouse ID","Sex"])

#group them by size
gender_distribution = pd.DataFrame(gender_distribution.size())

#begin breaking down the dataframe/rename by female and male counts
gender_summary = pd.DataFrame(gender_distribution.groupby(["Sex"]).count())
gender_summary.columns = ["Total Count"]

#create the percentage by dividing for the pie plot
gender_summary["Distribution of Mice by Gender"] = (100*(gender_summary["Total Count"]/gender_summary["Total Count"].sum()))

#plot the pie chart
explode = (0.1,0)
colors = ['pink','brown']
plot = gender_summary.plot.pie(y='Total Count',
                                 figsize=(6,6),
                                 colors=colors, 
                                 startangle=140, 
                                 explode = explode,
                                 shadow = True,
                                 autopct="%1.1f%%")

# Generate a pie plot showing the distribution of female versus male mice using pyplot
sex_distribution = (output.groupby(["Sex"])["Mouse ID"].count())

labels = ["Female","Males"]
colors = ["Pink","brown"]
explode = (0.1,0)

plt.pie(sex_distribution, 
        explode=explode, 
        labels=labels,
        colors=colors, 
        autopct="%1.1f%%", 
        shadow=True, 
        startangle=160)

plt.axis("equal")<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

treatment_regimens = output[output["Drug Regimen"].isin(["Capomulin", "Ramicane", "Infubinol", "Ceftamin"])]


# Start by getting the last (greatest) timepoint for each mouse
treatment_regimens = treatment_regimens.sort_values(["Timepoint"],ascending = True)

# Merge this group df with the original dataframe to get the tumor volume at the last timepoint
treatment_summary = treatment_regimens[["Drug Regimen", "Mouse ID", "Timepoint", "Tumor Volume (mm3)"]]
treatment_summary
# 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
    
# Generate a box plot of the final tumor volume of each mouse across four regimens of interest<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
line_cap = output.loc[(output["Drug Regimen"] == "Capomulin"),:]
line_cap = line_cap.reset_index()

#select only one mouse
one_mouse = line_cap .loc[line_cap["Mouse ID"] == "s185"]

time_point = one_mouse["Timepoint"]
tumor_line = one_mouse["Tumor Volume (mm3)"]

tumor_line = plt.plot(time_point,tumor_line)

plt.xlabel('Timepoint')
plt.ylabel('Tumor Volume')
plt.title('Tumor Volume of mice on Capomulin')

# Generate a scatter plot of average tumor volume vs. mouse weight for the Capomulin regimen
# Pull values for x and y values
mice_weight = line_cap.groupby(line_cap["Mouse ID"])["Weight (g)"].mean()
tumor_vol = line_cap.groupby(line_cap["Mouse ID"])["Tumor Volume (mm3)"].mean()

#plot the values 
plt.scatter(mice_weight, tumor_vol)
plt.xlabel("Weight of Mouse")
plt.ylabel("Tumor Volume")
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
mice_weight = line_cap.groupby(line_cap["Mouse ID"])["Weight (g)"].mean()
tumor_vol = line_cap.groupby(line_cap["Mouse ID"])["Tumor Volume (mm3)"].mean()

slope, int, r, p, std_err = st.linregress(mice_weight,tumor_vol)

fit = slope * mice_weight + int

#plot the linear regression model
plt.scatter(mice_weight,tumor_vol)
plt.xlabel("Weight of Mouse")
plt.ylabel("Tumor Volume")
plt.plot(mice_weight,fit,"--")
plt.xticks(mice_weight, rotation=90)
plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11217183770883055, 'fraction_numerical': 0.005150106770506218, 'mean_word_length': 5.013595166163142, 'pattern_counts': {'":': 3, '<': 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': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '615533', 'license': 'ADSL', 'n_tokens_mistral': 2543, 'n_tokens_neox': 2363, 'n_words': 751, 'path': 'Pymaceuticals/.ipynb_checkpoints/pymaceuticals_starter-checkpoint.ipynb', 'repo_name': 'LOT60/Matplotlib_Chllenge', 'size': '7961'}