<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
from scipy.stats import linregress

# Study data files
mouse_metadata_path = "Resources/Mouse_metadata.csv"
study_results_path = "Resources/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
mousestudy_df = pd.merge(mouse_metadata, study_results, how='outer', on='Mouse ID')

# Display the data table for preview
mousestudy_df.head()

# Checking the number of mice.
number_mice = mousestudy_df["Mouse ID"].nunique()
number_mice
# Getting the duplicate mice by ID number that shows up for Mouse ID and Timepoint. 
duplicate = mousestudy_df[mousestudy_df.duplicated(subset=['Mouse ID', 'Timepoint'], keep=False)]
duplicate
# Optional: Get all the data for the duplicate mouse ID. 
duplicate_mouse = mousestudy_df.loc[mousestudy_df["Mouse ID"] == "g989"]
duplicate_mouse
# Create a clean DataFrame by dropping the duplicate mouse by its ID.
clean_df = mousestudy_df.drop(mousestudy_df[mousestudy_df["Mouse ID"] == "g989"].index)
clean_df.head()
# Checking the number of mice in the clean DataFrame.
number_mice = clean_df['Mouse ID'].nunique()
number_mice<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

# This method is the most straighforward, creating multiple series and putting them all together at the end.

mean = clean_df.groupby('Drug Regimen')['Tumor Volume (mm3)'].mean()
median = clean_df.groupby('Drug Regimen')['Tumor Volume (mm3)'].median()
variance = clean_df.groupby('Drug Regimen')['Tumor Volume (mm3)'].var()
std_deviation = clean_df.groupby('Drug Regimen')['Tumor Volume (mm3)'].std()

SEM = clean_df.groupby('Drug Regimen')['Tumor Volume (mm3)'].sem()


summary1 = pd.DataFrame(
        {"Mean": mean,
         "Median": median,
         "Variance": variance,
         "Standard Deviation": std_deviation,
         "SEM": SEM})

summary1

# 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

summary2 = clean_df.groupby('Drug Regimen').agg(
                            {'Tumor Volume (mm3)':['mean', 'median', 'var', 'std', 'sem']})
summary2<jupyter_output><empty_output><jupyter_text>Bar and Pie Charts<jupyter_code># Create a DataFrame with number of mouse for each treatment

drug_mouse = pd.DataFrame((clean_df.groupby('Drug Regimen')['Mouse ID'].count()).reset_index())
drug_mouse

# Generate a bar plot showing the total number of mice for each treatment throughout the course of the study using pandas. 

bar_pandas = drug_mouse.plot(kind='bar',legend=False, x="Drug Regimen", y="Mouse ID", 
                             title="The Total Number of Mouse for each Drug Regimen", 
                             ylim=(0, max(drug_mouse["Mouse ID"])+20), rot=45)

bar_pandas.set_ylabel("Number of Mouse")


# Generate a bar plot showing the total number of mice for each treatment throughout the course of the study using pyplot.

x_axis = drug_mouse["Drug Regimen"].unique()
plt.bar(x_axis, drug_mouse["Mouse ID"],align="center",width=0.5)

plt.title("The Total Number of Mouse for each Drug Regimen")
plt.xlabel("Drug Regimen")
plt.ylabel("Number of Mouse")

plt.ylim(0, max(drug_mouse["Mouse ID"])+20)
plt.xticks(rotation = 45)


plt.show()
# Create a new DataFrame for gender distribution
mouse_gender = pd.DataFrame((clean_df.groupby('Sex')['Mouse ID'].count()).reset_index())
mouse_gender
# Generate a pie plot showing the distribution of female versus male mice using pandas

pie_pandas = mouse_gender.plot(kind='pie', x="Sex", labels=mouse_gender["Sex"], y="Mouse ID", title="The Gender Distribution of Mouse", subplots=True, autopct='%.0f%%', startangle=50, shadow=False, legend=False, figsize=(8, 4.5))


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

plt.pie(mouse_gender["Mouse ID"], labels=mouse_gender["Sex"], autopct='%.0f%%', startangle=50, shadow=False)

plt.axis("equal")

plt.title("The Gender Distribution of Mouse")
plt.ylabel("Mouse ID")

plt.show()<jupyter_output><empty_output><jupyter_text>Bar and Pie Charts (Unique Mouse IDs)  Identify each Mouse ID as one unique Mouse (removing all the duplicates)<jupyter_code>unique_mouse = clean_df.drop_duplicates(['Mouse ID'])

unique_mouse.head()
# Create a DataFrame with number of mouse for each treatment
unique_mousedrug = pd.DataFrame((unique_mouse.groupby('Drug Regimen')['Mouse ID'].count()).reset_index())
unique_mousedrug
# Generate a bar plot showing the total number of mice for each treatment throughout the course of the study using pandas. 

bar_pandas2 = unique_mousedrug.plot(kind='bar',legend=False, x="Drug Regimen", y="Mouse ID", 
                             title="The Total Number of Mouse for each Drug Regimen", 
                             ylim=(0, max(unique_mousedrug["Mouse ID"])+5), rot=45)

bar_pandas2.set_ylabel("Number of Mouse")

# Generate a bar plot showing the total number of mice for each treatment throughout the course of the study using pyplot.

x_axis = unique_mousedrug["Drug Regimen"].unique()
plt.bar(x_axis, unique_mousedrug["Mouse ID"],align="center",width=0.5)

plt.title("The Total Number of Mouse for each Drug Regimen")
plt.xlabel("Drug Regimen")
plt.ylabel("Number of Mouse")

plt.ylim(0, max(unique_mousedrug["Mouse ID"])+5)
plt.xticks(rotation = 45)


plt.show()
# Create a new DataFrame for gender distribution
unique_gender = pd.DataFrame((unique_mouse.groupby('Sex')['Mouse ID'].count()).reset_index())
unique_gender
# Generate a pie plot showing the distribution of female versus male mice using pandas

pie_pandas2 = unique_gender.plot(kind='pie', x="Sex", labels=unique_gender["Sex"], y="Mouse ID", title="The Gender Distribution of Mouse", subplots=True,autopct='%.0f%%', startangle=50, shadow=False, legend=False, figsize=(8, 4.5))

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

plt.pie(unique_gender["Mouse ID"], labels=unique_gender["Sex"], autopct='%.0f%%', startangle=50, shadow=False)

plt.axis("equal")

plt.title("The Gender Distribution of Mouse")
plt.ylabel("Mouse ID")

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
last_timepoint = clean_df.groupby('Mouse ID')['Timepoint'].max()

# Merge this group df with the original dataframe to get the tumor volume at the last timepoint
merge_df = pd.merge(clean_df, last_timepoint, how='right', on=('Mouse ID','Timepoint'))

# Calculate the IQR and quantitatively for Capomulin

IQR_capomulin = merge_df.loc[(merge_df["Drug Regimen"] == "Capomulin")]
tumor_capomulin = IQR_capomulin["Tumor Volume (mm3)"]

quartiles = tumor_capomulin.quantile([.25,.5,.75])
lowerq = quartiles[0.25]
upperq = quartiles[0.75]
iqr = upperq-lowerq

lower_bound = lowerq - (1.5*iqr)
upper_bound = upperq + (1.5*iqr)
print(f"For Capomulin, values below {lower_bound} or above {upper_bound} could be outliers.")

outliers = IQR_capomulin.loc[(tumor_capomulin < lower_bound) | (tumor_capomulin > upper_bound)]
print(f"For Capomulin, there are {len(outliers)} outliers.")
# Calculate the IQR and quantitatively for Ramicane

IQR_ramicane = merge_df.loc[(merge_df["Drug Regimen"] == "Ramicane")]
tumor_ramicane = IQR_ramicane["Tumor Volume (mm3)"]

quartiles = tumor_ramicane.quantile([.25,.5,.75])
lowerq = quartiles[0.25]
upperq = quartiles[0.75]
iqr = upperq-lowerq

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

outliers = IQR_ramicane.loc[(tumor_ramicane < lower_bound) | (tumor_ramicane > upper_bound)]
print(f"For Ramicane, there are {len(outliers)} outliers.")
# Calculate the IQR and quantitatively for Infubinol

IQR_infubinol = merge_df.loc[(merge_df["Drug Regimen"] == "Infubinol")]
tumor_infubinol = IQR_infubinol["Tumor Volume (mm3)"]

quartiles = tumor_infubinol.quantile([.25,.5,.75])
lowerq = quartiles[0.25]
upperq = quartiles[0.75]
iqr = upperq-lowerq

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

outliers = IQR_infubinol.loc[(tumor_infubinol < lower_bound) | (tumor_infubinol > upper_bound)]
print(f"For Infubinol, there are {len(outliers)} outliers.")
# Calculate the IQR and quantitatively for Infubinol

IQR_ceftamin = merge_df.loc[(merge_df["Drug Regimen"] == "Ceftamin")]
tumor_ceftamin = IQR_ceftamin["Tumor Volume (mm3)"]

quartiles = tumor_ceftamin.quantile([.25,.5,.75])
lowerq = quartiles[0.25]
upperq = quartiles[0.75]
iqr = upperq-lowerq

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

outliers = IQR_ceftamin.loc[(tumor_ceftamin < lower_bound) | (tumor_ceftamin > upper_bound)]
print(f"For Ceftamin, there are {len(outliers)} outliers.")
# Generate a box plot of the final tumor volume of Capomulin

boxplot = plt.boxplot((tumor_capomulin, tumor_ramicane, tumor_infubinol, tumor_ceftamin), notch=0, sym='gD')

plt.title("Box Plots of the Final Tumor Volume for Different Drug Regimens")
plt.ylabel("Tumor Volume (mm3)")
plt.xlabel("Drug Regimens")
plt.xticks([1,2,3,4],["Capomulin", "Ramicane", "Infubinol", "Ceftamin"])

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

# Generate a new DataFrame by filter the clean DataFrame with Capomulin and a random selected Mouse ID
line_df = clean_df.loc[(clean_df["Drug Regimen"] == "Capomulin") & (clean_df["Mouse ID"] == "s185")]
line_df
x_axis = line_df["Timepoint"]

plt.plot(x_axis, line_df["Tumor Volume (mm3)"])

plt.title("Tumor Volume at Different Timepoints for Mouse s185")
plt.xlabel("Timepoint")
plt.ylabel("Tumor Volume (mm3)")

plt.xlim(0,45)
plt.ylim(20,50)
plt.grid()

plt.show()

# Generate a scatter plot of mouse weight versus average tumor volume for the Capomulin regimen

# Filter to capomulin
filter_df = clean_df.loc[clean_df["Drug Regimen"] == "Capomulin"]

# Calculate the average tumor volume and create a DataFrame for it
tumor_sum = filter_df.groupby("Mouse ID")["Tumor Volume (mm3)"].sum()
tumor_average = tumor_sum / (filter_df["Mouse ID"].value_counts())

tumor_average_df = pd.DataFrame(tumor_average).reset_index()

tumor_average_df = tumor_average_df.rename(columns={"index": "Mouse ID", 0: "Average Tumor Volume"})

tumor_average_df.head()

# Merge the two DataFrames to create a clean DataFrame for the scatter plot
scatter_df = pd.merge(filter_df, tumor_average_df, how='left',on=('Mouse ID'))

# Clean the scatter DataFrame to remove all the duplicates for plotting
scatter_clean = scatter_df.drop_duplicates(subset=['Mouse ID', 'Weight (g)'])

scatter_clean.head()
# Scatter Plot
x_axis = scatter_clean["Weight (g)"]

plt.scatter(x_axis, scatter_clean["Average Tumor Volume"], marker="o", facecolors="b", edgecolors="black", s=x_axis, alpha=0.75)

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

plt.xlim(14, 26)
plt.ylim(34, 48)
plt.grid()

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
# Calculate the correlation coefficient
mouse_weight = scatter_clean["Weight (g)"]
average_tumor_volume = scatter_clean["Average Tumor Volume"]

correlation = round(st.pearsonr(mouse_weight,average_tumor_volume)[0],2)
print(f"The correlation coefficient between mouse weight and average tumor volume is {correlation}.")

# Calculate the linear regression model
(slope, intercept, rvalue, pvalue, stderr) = linregress(mouse_weight, average_tumor_volume)
regress_values = mouse_weight * slope + intercept
line_eq = "y = " + str(round(slope,2)) + "x + " + str(round(intercept,2))

print(line_eq)

# Add the linear regression equation and line to plot

plt.scatter(mouse_weight, average_tumor_volume, marker="o", facecolors="b", edgecolors="black", s=x_axis, alpha=0.75)

plt.plot(mouse_weight,regress_values,"r-")
plt.annotate(line_eq,(20,36),fontsize=15,color="red")
plt.title("Capomulin's Mouse Weight vs. Average Tumor Volume")
plt.xlabel('Mouse Weight (g)')
plt.ylabel("Average Tumor Volume (mm3)")

plt.xlim(14, 26)
plt.ylim(34, 48)
plt.grid()

plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.124298072545151, 'fraction_numerical': 0.013659128851115495, 'mean_word_length': 5.950949367088608, 'pattern_counts': {'":': 6, '<': 33, '<?xml version=': 0, '>': 33, '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': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '364128', 'license': 'ADSL', 'n_tokens_mistral': 4729, 'n_tokens_neox': 4324, 'n_words': 1224, 'path': 'pymaceuticals_starter.ipynb', 'repo_name': 'Rain-Yang18/matplotlib-challenge', 'size': '13178'}