<jupyter_start><jupyter_text>Observations and Insights 1) There is a correlation of 70% between tumor volume and weight.  Heavier mice tend to develop larger tumors.  Therefore, heavier mice may need a larger dose of medication.2) By looking at the line plot, mouse b-128 saw its tumor size decrease drastically over time.3) By looking at the box plot, Capomulin and Ramicane had lower medians than the other two drugs.  On average, mice that were treated with Capomulin and Ramicane developed smaller tumors than mice that were treated with Infubinol and Ceftamin.<jupyter_code># Dependencies and Setup
import matplotlib.pyplot as plt
import pandas as pd
import scipy.stats as st
from scipy.stats import linregress
import numpy as numpy

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

data_df = data_df.reset_index(drop=True)

# Display the data table for preview
data_df
# Checking the number of mice.

mice = len(data_df['Mouse ID'].unique())
print(mice)
# Getting the duplicate mice by ID number that shows up for Mouse ID and Timepoint. 

dup_mice = data_df[data_df.duplicated(['Mouse ID','Timepoint'])]

print((dup_mice['Mouse ID']).to_string(index= False))

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

dup_mice
# Create a clean DataFrame by dropping the duplicate mouse by its ID.


data_df.drop_duplicates(['Mouse ID', 'Timepoint'], inplace= True)

clean_data_df = pd.DataFrame(data_df, columns= ['Mouse ID','Drug Regimen','Sex','Age_months',
                                                'Weight (g)','Timepoint','Tumor Volume (mm3)',
                                               'Metastatic Sites'])
clean_data_df
# Checking the number of mice in the clean DataFrame.
total_mice = len(clean_data_df['Mouse ID'].unique())
print(total_mice)<jupyter_output>249<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.

#create grouby object that groups by drug regimen
clean_data_drug = pd.DataFrame
data_drug_grouped = clean_data_df.groupby(['Drug Regimen'])

#mean
mean = data_drug_grouped['Tumor Volume (mm3)'].mean()

#median
median = data_drug_grouped['Tumor Volume (mm3)'].median()

#variance
variance = data_drug_grouped['Tumor Volume (mm3)'].var()

#standard deviation
std = data_drug_grouped['Tumor Volume (mm3)'].std()

#SEM
sem = data_drug_grouped['Tumor Volume (mm3)'].sem()

#create summary dataframe
sum_stats_df = pd.DataFrame({'Mean': mean,
                            'Median': median,
                            'Variance': variance,
                            'Standard Deviation': std,
                            'SEM': sem})
sum_stats_df
# 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

sum_stats2 = clean_data_df.groupby(['Drug Regimen']).agg(['mean','median','var','std','sem'])['Tumor Volume (mm3)']

print(sum_stats2)<jupyter_output>mean     median        var       std       sem
Drug Regimen                                                     
Capomulin     40.675741  41.557809  24.947764  4.994774  0.329346
Ceftamin      52.591172  51.776157  39.290177  6.268188  0.469821
Infubinol     52.884795  51.820584  43.128684  6.567243  0.492236
Ketapril      55.235638  53.698743  68.553577  8.279709  0.603860
Naftisol      54.331565  52.509285  66.173479  8.134708  0.596466
Placebo       54.033581  52.288934  61.168083  7.821003  0.581331
Propriva      52.393463  50.909965  43.138803  6.568014  0.525862
Ramicane      40.216745  40.673236  23.486704  4.846308  0.320955
Stelasyn      54.233149  52.431737  59.450562  7.710419  0.573111
Zoniferol     53.236507  51.818479  48.533355  6.966589  0.516398<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.


bar1_df = pd.DataFrame(data_drug_grouped.count())

bar_chart1 = bar1_df.plot(kind='bar',color='blue')

plt.legend().remove()
plt.title('Number of measurements taken for each drug')
plt.xlabel('Name of Drug')
plt.ylabel('Number of Measurements')
plt.show()
# Generate a bar plot showing the total number of measurements taken on each drug regimen using pyplot.

x_values = bar1_df.index
y_values = bar1_df['Mouse ID']

plt.bar(x_values, y_values, color='b')
plt.title('Number of measurements taken for each drug')
plt.xlabel('Name of Drug')
plt.ylabel('Number of Measurements')

plt.xticks(x_values, x_values, rotation=90)
plt.show()
# Generate a pie plot showing the distribution of female versus male mice using pandas

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

gender_summary_df = pd.DataFrame({'Sex': gender})

pie_chart1 = gender_summary_df.plot.pie(y='Sex',autopct='%1.1f%%', startangle=30, figsize=(5,5))
plt.title('Mouse Gender')
plt.legend().remove()
plt.show()
# Generate a pie plot showing the distribution of female versus male mice using pyplot

mf_label = ['Female', 'Male']

plt.pie(gender, labels= mf_label,
        autopct="%1.1f%%", startangle=30)
# Create axes which are equal so we have a perfect circle
plt.axis("equal")
plt.title('Mouse Gender')
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
#Capomulin

# only include capomulin data
drug1_df = clean_data_df.loc[clean_data_df["Drug Regimen"] == "Capomulin",:]

# group by mouse ID and get final times
drug1_final_time = drug1_df.groupby('Mouse ID').max()['Timepoint']

# create dataframe
drug1_vol = pd.DataFrame(drug1_final_time)

# merge dataframe with the cleaned dataframe
capomulin_data = pd.merge(drug1_vol, clean_data_df, on=("Mouse ID","Timepoint"),how="left")

capomulin_data.head()


# Select Tumor Volume.  (This is the data that will be plotted and used for IQR)
cap_tumor_vol = capomulin_data["Tumor Volume (mm3)"]

# Capomulin IQR
quartiles = cap_tumor_vol.quantile([.25,.5,.75])
lowerq = quartiles[0.25]
upperq = quartiles[0.75]
iqr = upperq-lowerq

print(f"The lower quartile of tumor volume is: {lowerq}")
print(f"The upper quartile of tumor volume is: {upperq}")
print(f"The interquartile range of tumor volume is: {iqr}")
print(f"The the median of tumor volume is: {quartiles[0.5]} ")

lower_bound = lowerq - (1.5*iqr)
upper_bound = upperq + (1.5*iqr)
print(f"Values below {lower_bound} could be outliers.")
print(f"Values above {upper_bound} could be outliers.")
# Ramicane

# only include ramicane data
drug2_df = clean_data_df.loc[clean_data_df["Drug Regimen"] == "Ramicane",:]

# group by mouse ID and get final times
drug2_final_time = drug2_df.groupby('Mouse ID').max()['Timepoint']

# create dataframe
drug2_vol = pd.DataFrame(drug2_final_time)

# merge dataframe with the cleaned dataframe
ramicane_data = pd.merge(drug2_vol, clean_data_df, on=("Mouse ID","Timepoint"),how="left")

ramicane_data.head()
# Select Tumor Volume.  (This is the data that will be plotted and used for IQR)
ram_tumor_vol = ramicane_data["Tumor Volume (mm3)"]

# Ramicane IQR
quartiles = ram_tumor_vol.quantile([.25,.5,.75])
lowerq = quartiles[0.25]
upperq = quartiles[0.75]
iqr = upperq-lowerq

print(f"The lower quartile of tumor volume is: {lowerq}")
print(f"The upper quartile of tumor volume is: {upperq}")
print(f"The interquartile range of tumor volume is: {iqr}")
print(f"The the median of tumor volume is: {quartiles[0.5]} ")

lower_bound = lowerq - (1.5*iqr)
upper_bound = upperq + (1.5*iqr)
print(f"Values below {lower_bound} could be outliers.")
print(f"Values above {upper_bound} could be outliers.")
# Infubinol

# only include infubinol data
drug3_df = clean_data_df.loc[clean_data_df["Drug Regimen"] == "Infubinol",:]

# group by mouse ID and get final times
drug3_final_time = drug3_df.groupby('Mouse ID').max()['Timepoint']

# create dataframe
drug3_vol = pd.DataFrame(drug3_final_time)

# merge dataframe with the cleaned dataframe
infubinol_data = pd.merge(drug3_vol, clean_data_df, on=("Mouse ID","Timepoint"),how="left")

infubinol_data.head()
# Select Tumor Volume.  (This is the data that will be plotted and used for IQR)
inf_tumor_vol = infubinol_data["Tumor Volume (mm3)"]

# Ramicane IQR
quartiles = inf_tumor_vol.quantile([.25,.5,.75])
lowerq = quartiles[0.25]
upperq = quartiles[0.75]
iqr = upperq-lowerq

print(f"The lower quartile of tumor volume is: {lowerq}")
print(f"The upper quartile of tumor volume is: {upperq}")
print(f"The interquartile range of tumor volume is: {iqr}")
print(f"The the median of tumor volume is: {quartiles[0.5]} ")

lower_bound = lowerq - (1.5*iqr)
upper_bound = upperq + (1.5*iqr)
print(f"Values below {lower_bound} could be outliers.")
print(f"Values above {upper_bound} could be outliers.")
# Ceftamin

# only include ceftamin data
drug4_df = clean_data_df.loc[clean_data_df["Drug Regimen"] == "Ceftamin",:]

# group by mouse ID and get final times
drug4_final_time = drug4_df.groupby('Mouse ID').max()['Timepoint']

# create dataframe
drug4_vol = pd.DataFrame(drug4_final_time)

# merge dataframe with the cleaned dataframe
ceftamin_data = pd.merge(drug4_vol, clean_data_df, on=("Mouse ID","Timepoint"),how="left")

ceftamin_data.head()
# Select Tumor Volume.  (This is the data that will be plotted and used for IQR)
cef_tumor_vol = ceftamin_data["Tumor Volume (mm3)"]

# Ramicane IQR
quartiles = cef_tumor_vol.quantile([.25,.5,.75])
lowerq = quartiles[0.25]
upperq = quartiles[0.75]
iqr = upperq-lowerq

print(f"The lower quartile of tumor volume is: {lowerq}")
print(f"The upper quartile of tumor volume is: {upperq}")
print(f"The interquartile range of tumor volume is: {iqr}")
print(f"The the median of tumor volume is: {quartiles[0.5]} ")

lower_bound = lowerq - (1.5*iqr)
upper_bound = upperq + (1.5*iqr)
print(f"Values below {lower_bound} could be outliers.")
print(f"Values above {upper_bound} could be outliers.")
# Generate a box plot of the final tumor volume of each mouse across four regimens of interest

final_tumor_vol = [cap_tumor_vol, ram_tumor_vol, inf_tumor_vol, cef_tumor_vol]
label2 = ['Capomulin', 'Ramicane', 'Infubinol', 'Ceftamin']


fig1,ax1 = plt.subplots(figsize = (12, 10))
ax1.set_title('Final Tumor Size',fontsize =20)
ax1.set_ylabel('Final Tumor Volume (mm3)',fontsize = 12)
ax1.set_xlabel('Drug Regimen',fontsize = 12)
ax1.boxplot(final_tumor_vol, labels=label2, widths=0.4, patch_artist=True, vert=True)
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

b128_data = clean_data_df.loc[clean_data_df["Mouse ID"] == "b128",:]
b128_data
# get timepiont
b128_time = b128_data['Timepoint'].values

# get volume
b128_vol = b128_data['Tumor Volume (mm3)'].values

# line plot
b128_scatter, = plt.plot(b128_time, b128_vol, color = 'blue')

# label chart
plt.title('Tumor Volume over Time (mouse b128)')
plt.xlabel('Timepoints')
plt.ylabel('Tumor Volume (mm3)')
plt.show()
# Generate a scatter plot of average tumor volume vs. mouse weight for the Capomulin regimen

# average tumor volume
avg_vol = drug1_df.groupby(['Mouse ID']).mean()['Tumor Volume (mm3)']

# mouse weight
cap_wt = drug1_df.groupby(['Mouse ID']).mean()['Weight (g)']

# scatter plot
plt.scatter(cap_wt,avg_vol, marker='o', facecolors='blue', edgecolors='blue')

# label plot
plt.title('Mouse Weight vs. Average Tumor Volume (Capomulin)')
plt.ylabel('Average Tumor Volume (mm3)')
plt.xlabel('Weight (g)')
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

# Add the linear regression equation and line to plot
y_values = avg_vol
x_values = cap_wt
(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(cap_wt,avg_vol, marker='o', facecolors='blue', edgecolors='blue')
plt.plot(x_values,regress_values,"r-")
plt.annotate(line_eq,(20,38),fontsize=15,color="red")

plt.title('Mouse Weight vs. Average Tumor Volume (Capomulin)')
plt.ylabel('Average Tumor Volume (mm3)')
plt.xlabel('Weight (g)')

plt.show()

print(f'The correlation coeffecient is: {rvalue**2}')<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11500797084946482, 'fraction_numerical': 0.0443331055947772, 'mean_word_length': 4.990904956798545, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 23, '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': '579091', 'license': 'ADSL', 'n_tokens_mistral': 5000, 'n_tokens_neox': 4450, 'n_words': 1308, 'path': 'pymaceuticals_starter.ipynb', 'repo_name': 'moormeierz/Matplotlib', 'size': '13173'}