<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
main_df = study_results.merge(mouse_metadata, on='Mouse ID')
# Display the data table for preview
main_df.head()
# Checking the number of mice.
main_df['Mouse ID'].value_counts().count()
# Getting the duplicate mice by ID number that shows up for Mouse ID and Timepoint. 

main_df[main_df.duplicated(['Mouse ID','Timepoint'])]
# Optional: Get all the data for the duplicate mouse ID. 

main_df[main_df['Mouse ID'] == 'g989']
# Create a clean DataFrame by dropping the duplicate mouse by its ID.
clean_df = main_df[main_df['Mouse ID'] != 'g989']
# Checking the number of mice in the clean DataFrame.
clean_df['Mouse ID'].value_counts().count()<jupyter_output><empty_output><jupyter_text>Summary Statistics<jupyter_code>clean_df.head()
# Generate a summary statistics table of mean, median, variance, standard deviation, and SEM of the tumor volume for each regimen
# This method is the most straighforward, creating multiple series and putting them all together at the end.

grp = clean_df.groupby('Drug Regimen')['Tumor Volume (mm3)']
pd.DataFrame({'mean':grp.mean(), 'median':grp.median(),'var':grp.var(),'std':grp.std(),'sem':grp.sem()})
# 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

grp.agg(['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 mice for each treatment throughout the course of the study using pandas. 
clean_df['Drug Regimen'].value_counts().plot(kind='bar',rot=45, title='Mice per Treatment')
plt.xlabel("Drug Regimen")
plt.ylabel('total Mice')
plt.show()
# Generate a bar plot showing the total number of mice for each treatment throughout the course of the study using pyplot.
regimen_counts = clean_df['Drug Regimen'].value_counts()
plt.bar(regimen_counts.index, regimen_counts)
plt.xlabel("Drug Regimen")
plt.ylabel('total Mice')
plt.title('Mice per Treatment')
plt.xticks(rotation=45)
plt.show()
# Generate a pie plot showing the distribution of female versus male mice using pandas
clean_df.Sex.value_counts().plot(kind='pie',autopct='%1.1f%%')
plt.title('Female Vs Male')
plt.show()
# Generate a pie plot showing the distribution of female versus male mice using pyplot
sex_counts = clean_df.Sex.value_counts()
plt.pie(sex_counts, autopct='%1.1f%%', labels=sex_counts.index)
plt.title('Female Vs Male')
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
max_df = clean_df.groupby('Mouse ID').max().reset_index()

# Merge this group df with the original dataframe to get the tumor volume at the last timepoint
merged_df = max_df[['Mouse ID','Timepoint']].merge(clean_df,on=['Mouse ID','Timepoint'],how='left')
merged_df.head()
# Put treatments into a list for for loop (and later for plot labels)
drugs = ['Capomulin', 'Ramicane', 'Infubinol', 'Ceftamin']

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

# Calculate the IQR and quantitatively determine if there are any potential outliers. 
for drug in drugs:
    tumor_data = merged_df[merged_df['Drug Regimen'] == drug]['Tumor Volume (mm3)']
    
    iqr = tumor_data.quantile(.75) - tumor_data.quantile(.25)
    lower_bound = tumor_data.quantile(.25) - (1.5*iqr)
    upper_bound = tumor_data.quantile(.75) + (1.5*iqr)


# tumor_list.append(drug)
#     tumor_list.append({drug:{'data':tumor_data,'outliers':tumor_data[(tumor_data<lower_bound)|(tumor_data>upper_bound)]}})
    tumor_list.append(tumor_data)
    
# Locate the rows which contain mice on each drug and get the tumor volumes
    
# tumor_list
    # add subset 
    
    
    # Determine outliers using upper and lower bounds
    print(f'{drug} potential outliers: {tumor_data[(tumor_data<lower_bound)|(tumor_data>upper_bound)]}')
# Generate a box plot of the final tumor volume of each mouse across four regimens of interest
plt.boxplot(tumor_list,labels = drugs, flierprops ={'markerfacecolor':'red','markersize':12})
plt.title('Tumor Volume Regimen')
plt.xlabel("Regimen")
plt.ylabel("Tumor Volume")
plt.show()<jupyter_output><empty_output><jupyter_text>Line and Scatter Plots<jupyter_code># Generate a line plot of time point versus tumor volume for a mouse treated with Capomulin
capo = clean_df[clean_df['Drug Regimen'] == 'Capomulin']
mouse_data = capo[capo['Mouse ID']=='b128']
plt.plot(mouse_data['Timepoint'], mouse_data['Tumor Volume (mm3)'])
plt.xlabel('Timepoint (days)')
plt.ylabel('Tumor Volume (mm3)')
plt.title('Capomulin treatment of mouse b128')
plt.show()
# Generate a scatter plot of mouse weight versus average tumor volume for the Capomulin regimen
cap_avg = capo.groupby('Mouse ID').mean()
plt.scatter(cap_avg['Weight (g)'], cap_avg['Tumor Volume (mm3)'])
plt.xlabel('Weight (g)')
plt.ylabel('Tumor Volume (mm3)')
plt.title('Capomulin treatment Weight vs Tumor')
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
corr = round(st.pearsonr(cap_avg['Weight (g)'], cap_avg['Tumor Volume (mm3)'])[0],2)
print(f'The correlation between mouse weight and the average tumor volume is {corr}.')
model = st.linregress(cap_avg['Weight (g)'], cap_avg['Tumor Volume (mm3)'])
y = cap_avg['Weight (g)']*model[0] + model[1]
plt.scatter(cap_avg['Weight (g)'],cap_avg['Tumor Volume (mm3)'])
plt.plot(cap_avg['Weight (g)'], y, color='red')
plt.title("Weight and Average Tumor Volume")
plt.xlabel('Weight (g)')
plt.ylabel('Average Tumor Volume (mm3)')
plt.show()<jupyter_output>The correlation between mouse weight and the average tumor volume is 0.84.
{'fraction_non_alphanumeric': 0.12252113758647194, 'fraction_numerical': 0.007840122982321292, 'mean_word_length': 6.28555431131019, 'pattern_counts': {'":': 0, '<': 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': '71821', 'license': 'ADSL', 'n_tokens_mistral': 2255, 'n_tokens_neox': 2067, 'n_words': 641, 'path': 'pymaceuticals_starter.ipynb', 'repo_name': 'mava630/Matplotlib-challenge', 'size': '6505'}