<jupyter_start><jupyter_text>Observations and Insights  1. There was a fairly even split of male and female mice2. There was only one outlier in final readings across the four studies: Capomulin, Ramicane, Infubinol, Ceftamin3. There is a fairly strong correlation between the mouse weight and tumor size  Start of Analysis<jupyter_code># Dependencies and Setup
import matplotlib.pyplot as plt
import pandas as pd
import scipy.stats as st
import numpy as np
from scipy.stats import linregress

# Loading the 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_all = pd.merge(mouse_metadata, study_results, how='outer', on="Mouse ID")

# Display the data table for preview
mouse_all
# Checking the number of mice.
count=mouse_all["Mouse ID"].nunique()
print(f'The total unique mouse count is {count}.')
# Getting the duplicate mice by ID number that shows up for Mouse ID and Timepoint. 

duplicate_ID = mouse_all[mouse_all.duplicated(['Mouse ID','Timepoint'])]
ID = duplicate_ID['Mouse ID'].values[0]
print(f'The duplicated Mouse ID is {ID}')
# Optional: Get all the data for the duplicate mouse ID. 

duplicate_ID = mouse_all[mouse_all.duplicated(['Mouse ID','Timepoint'])]
duplicate_ID
# Create a clean DataFrame by dropping the duplicate mouse by its ID.

cleandf=mouse_all.drop(mouse_all[mouse_all['Mouse ID']==ID].index)
# Checking the number of mice in the clean DataFrame.
new_count=cleandf['Mouse ID'].nunique()
print(f'The total unique mouse count is now {new_count}.')
# printing the clean dataframe for reference

cleandf.head(20)<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. 

mean=cleandf.groupby(by=["Drug Regimen"]).mean()
mean=mean["Tumor Volume (mm3)"]
mean

median=cleandf.groupby(by=["Drug Regimen"]).median()
median=median["Tumor Volume (mm3)"]
median

variance=cleandf.groupby(by=["Drug Regimen"]).var()
variance=variance["Tumor Volume (mm3)"]
variance

stdev=cleandf.groupby(by=["Drug Regimen"]).std()
stdev=stdev["Tumor Volume (mm3)"]
stdev

stderrm=cleandf.groupby(by=["Drug Regimen"]).sem()
stderrm=stderrm["Tumor Volume (mm3)"]
stderrm

# merge the tables together to form a summary table
sum_table=pd.merge(mean, median, on='Drug Regimen')
sum_table=sum_table.rename(columns={"Tumor Volume (mm3)_x":"Mean","Tumor Volume (mm3)_y":"Median"})

sum_table=pd.merge(sum_table, variance, on='Drug Regimen')
sum_table=sum_table.rename(columns={"Tumor Volume (mm3)":"Variance"})

sum_table=pd.merge(sum_table, stdev, on='Drug Regimen')
sum_table=sum_table.rename(columns={"Tumor Volume (mm3)":"Std Deviation"})

sum_table=pd.merge(sum_table, stderrm, on='Drug Regimen')
sum_table=sum_table.rename(columns={"Tumor Volume (mm3)":"SEM"})

# Print the results
sum_table
# 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

sum2=cleandf.groupby('Drug Regimen').agg({'Tumor Volume (mm3)': ['mean','median','var','std','sem']})

sum2<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.

#Get the counts in to a dataframe
counts=cleandf["Drug Regimen"].value_counts()

# Plot and format from that dataframe

counts.plot(kind="bar", figsize=(10,5), width=.75)
plt.title("Mouse Study Drug Regimens by Counts")
plt.xlabel("Drug Regimen")
plt.ylabel("Count of Measurements taken")

plt.show()
# Generate a bar plot showing the total number of measurements taken on each drug regimen using pyplot.

# Get the dataframe and reset the index so the title can be used for labeling
df = pd.DataFrame(data=counts)
df.reset_index(inplace = True)

#setting the x axis, labeling it, and then plotting

x_axis=df.iloc[:,0]
plt.bar(x_axis, df.iloc[:,1], color='b', alpha=1, align="center")

tick_locations = [value for value in x_axis]
plt.xticks(tick_locations, x_axis, rotation="vertical")

# labeling
plt.title("Mouse Study Drug Regimens by Counts")
plt.xlabel("Drug Regimen")
plt.ylabel("Count of Measurements taken")

plt.show()
# Generate a pie plot showing the distribution of female versus male mice using pandas

# Created a new dataframe or the demographics
demo=cleandf.groupby(['Mouse ID','Sex']).min()

demo.reset_index(inplace = True)

#Getting the counts and producing a single column result for easy plotting
demo=demo.groupby('Sex').count()
demo=demo['Mouse ID']

# Plotting the pie chart, formatting, and labeling
demodf=pd.DataFrame(demo)
colors =['red','blue']
demodf.plot(kind='pie',y='Mouse ID',autopct='%1.1f%%', colors=colors,startangle=90, legend=False)

plt.title("Study Male/Female Ratio")
plt.ylabel('')
plt.show()
# Generate a pie plot showing the distribution of female versus male mice using pyplot

demo=cleandf.groupby(['Mouse ID','Sex']).min()

demo.reset_index(inplace = True)

demo=demo.groupby('Sex').count()
demo=demo['Mouse ID']

demodf=pd.DataFrame(demo)
demodf=demodf.reset_index()

sizes=demodf.iloc[:,1]
labels=demodf.iloc[:,0]
colors =['red','blue']
plt.pie(sizes, labels=labels, colors=colors,
        autopct="%1.1f%%", shadow=False, startangle=90)

plt.title("Study Sex Ratio")

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

treatment_list=["Capomulin", "Ramicane", "Infubinol", "Ceftamin"]

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

df=cleandf.groupby(['Mouse ID']).max()
max_time=df['Timepoint']
max_time=pd.DataFrame(max_time)

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

max_volume=pd.merge(max_time, cleandf, on=['Timepoint',"Mouse ID"], how='inner')
max_volume[max_volume['Drug Regimen'].isin(treatment_list)]  
# Put treatments into a list for for loop (and later for plot labels)

    # done above treatment_list=["Capomulin", "Ramicane", "Infubinol", "Ceftamin"]

vol_list=[]

# Get the volume for Capomulin, then put only the tumor volume into the dataset, repeat for each drug in the list
# 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        
    # Determine outliers using upper and lower bounds

for drug in treatment_list:
    vol_list_1=max_volume.loc[max_volume['Drug Regimen'] == drug, "Tumor Volume (mm3)"]
    vol_list.append([vol_list_1]) #writing the results to the volume list
    pointlist=vol_list_1.quantile([.25,.5,.75]) #Calculate the quantiles
    
    #Pull out the upper and lower value for outlier calc
    lowerq_cap = pointlist[0.25]
    upperq_cap = pointlist[0.75]
    
    #Find the outliers
    outliers=vol_list_1.loc[(vol_list_1 <lowerq_cap) | (vol_list_1>upperq_cap)]
    
    #printing the results with limits to check for accuracy
    print(f"{drug} : Outliers list:  {outliers} , Lower cap:{lowerq_cap} , Upper cap: {upperq_cap}")
    
 
# Generate a box plot of the final tumor volume of each mouse across four regimens of interest

# I had already done this approach before figuring out the for loop and it took significantly less time to develop to meet the need

# created a list of each drugs values
Capomulin=max_volume[max_volume['Drug Regimen']=='Capomulin']
Capomulin_list=Capomulin['Tumor Volume (mm3)']

Ramicane=max_volume[max_volume['Drug Regimen']=='Ramicane']
Ramicane_list=Ramicane['Tumor Volume (mm3)']

Infubinol=max_volume[max_volume['Drug Regimen']=='Infubinol']
Infubinol_list=Infubinol['Tumor Volume (mm3)']

Ceftamin=max_volume[max_volume['Drug Regimen']=='Ceftamin']
Ceftamin_list=Ceftamin['Tumor Volume (mm3)']

# put the list into a master list, I likely could use the vol_list above but again this was done before that for loop for developed

data = [Capomulin_list, Ramicane_list, Infubinol_list, Ceftamin_list]

# creating the diamond to signifier the truest outlier
red_diamond = dict(markerfacecolor='r', marker='D')

# plotted the box plot using a google search approach that seemed efficient
fig, boxs = plt.subplots()
boxs.set_title('Four Drugs Final Values')
boxs.boxplot(data, flierprops=red_diamond)

# renaming the axis'
plt.xticks([1, 2, 3,4], ['Capomulin', 'Ramicane', 'Infubinol','Ceftamin'])
plt.xlabel("Drug Regimen")
plt.ylabel("Final Reading Values")

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

# Mouse_ID = input("Enter a Mouse ID ")
Mouse_ID='y793'

Choosen_mouse_data=cleandf[cleandf['Mouse ID']==Mouse_ID]

Choosen_mouse_data.plot('Timepoint', 'Tumor Volume (mm3)', legend=False)
plt.title(f"Mouse ID: {Mouse_ID}, Results Over Trial")
plt.xlabel("Time (Days)")
plt.ylabel("Tumor Volume (mm3)")

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

Capolumlin_data=cleandf[cleandf['Drug Regimen']=='Capomulin']

mean_wght=cleandf.groupby(['Weight (g)','Drug Regimen']).mean()
mean_wght.reset_index(inplace = True)
                              
mean_wght.plot.scatter('Weight (g)', 'Tumor Volume (mm3)', legend=False)
plt.title("Capomulin Therapy: Weight to Avg Tumor Size")
plt.xlabel("Weight (g)")
plt.ylabel("Avg 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

x_values = mean_wght['Weight (g)']
y_values = mean_wght['Tumor Volume (mm3)']

(slope, intercept, rvalue, pvalue, stderr) = linregress(x_values, y_values)

regress_values = x_values * slope + intercept

line_eq = "y = " + str(round(slope,2)) + "x + " + str(round(intercept,2))
plt.scatter(x_values,y_values)
plt.plot(x_values,regress_values,"r-")
plt.annotate(line_eq,(0,50),fontsize=15,color="red")

plt.title("Capomulin Therapy: Weight to Avg Tumor Size")
plt.xlabel("Weight (g)")
plt.ylabel("Avg Tumor Volume (mm3)")
print(f"The r-squared is: {rvalue**2}")
print(f'The linear model equation is: {line_eq}')
plt.show()<jupyter_output>The r-squared is: 0.7009252699907923
The linear model equation is: y = 1.36x + 15.58
{'fraction_non_alphanumeric': 0.11860549790642635, 'fraction_numerical': 0.009466593846714, 'mean_word_length': 6.148340923877684, 'pattern_counts': {'":': 5, '<': 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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '546382', 'license': 'ADSL', 'n_tokens_mistral': 3851, 'n_tokens_neox': 3609, 'n_words': 1098, 'path': 'Pymaceuticals/Main.ipynb', 'repo_name': 'delery7/Matplotlib', 'size': '10986'}