<jupyter_start><jupyter_text>Observations and Insights  1)Overall based on the bar graphs and pie charts, there seems to be a balanced test group based on sex and number that are being tested.  I think this shows the study was done well and used controlled variables to get accurate or unbiased data. 2)Capomulin seemed to be the best drug, and in every mouse that got this drug regimen seemed to do better.  The Tumor size seemed to shrink.3)The R value of the last scatter plot was '0.8419363424694717' which means the weight and tumor volume are closely related.  The heavier the mouse is, the bigger the tumor size will be.<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
combined_data = pd.merge(mouse_metadata, study_results, on = "Mouse ID", how = 'outer')
# Display the data table for preview
no_dupe_data = combined_data.drop_duplicates(keep = "last")

# Checking the number of mice.
no_dupe_data["Mouse ID"].value_counts()
#mean calculation
meanVals = pd.DataFrame(no_dupe_data.groupby("Drug Regimen")["Tumor Volume (mm3)"].mean())
meanVals.columns = {"Mean of Tumor Vol (mm3)"}
meanVals
#median calculation
medianVals = pd.DataFrame(no_dupe_data.groupby("Drug Regimen")["Tumor Volume (mm3)"].median())
medianVals.columns = {"Median of Tumor Vol (mm3)"}
medianVals
#variance calculation
varVals = pd.DataFrame(no_dupe_data.groupby("Drug Regimen")["Tumor Volume (mm3)"].var())
varVals.columns = {"Variance of Tumor Vol (mm3)"}
varVals
#std calculation
stdVals = pd.DataFrame(no_dupe_data.groupby("Drug Regimen")["Tumor Volume (mm3)"].std())
stdVals.columns = {"STD of Tumor Vol (mm3)"}
stdVals
#SEM calculation
semVals = pd.DataFrame(no_dupe_data.groupby("Drug Regimen")["Tumor Volume (mm3)"].sem())
semVals.columns = {"SEM of Tumor Vol (mm3)"}
semVals
#making the summary table
sum_table = meanVals

sum_table["Median"] = round(medianVals, 2)
sum_table["Variance"] = round(varVals, 2)
sum_table["STD"] = round(stdVals, 2)
sum_table["SEM"] = round(semVals, 2)

sum_table<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. 
# Assemble the resulting series into a single summary dataframe.

sum_table
#I WAS NOT SURE WHICH WAS CORRECT.  
#EITHER DUPLICATE MICE ID OR WITH UNIQUE VALUES (Mouse ID) SO I DID BOTH.

counts = no_dupe_data["Drug Regimen"].value_counts()
counts

barchart = counts.plot(kind='bar', legend = None)

plt.title("Number of Mice on Regimens")
barchart.set_xlabel("Drug Regimen")
barchart.set_ylabel("Number of Mice")

plt.show()<jupyter_output><empty_output><jupyter_text>Bar and Pie Charts<jupyter_code># Generate a bar plot showing the total number of unique mice tested on each drug regimen using pandas.
uniqueVals = pd.DataFrame(no_dupe_data.drop_duplicates(subset=["Mouse ID"]))
totalMice = pd.DataFrame(uniqueVals.groupby("Drug Regimen")["Mouse ID"].count())
totalMice

x_axis = np.arange(len(totalMice))

plt.bar(x_axis, totalMice["Mouse ID"], color ="r", alpha=0.5, align="center")

tick_loc = [value for value in x_axis]
plt.xticks(tick_loc, ["Capomulin", "Ceftamin", "Infubinol", "Ketapril", "Naftisol", "Placebo", "Propriva", "Ramicane", " Stelasyn", "Zoniferol"], rotation = 90)

plt.title("Number of Mice on Regimens")
plt.xlabel("Regimen")
plt.ylabel("Number of Mice")
plt.show()
totalMice
barchart = totalMice.plot(kind='bar', legend = None)

plt.title("Number of Mice on Regimens")
barchart.set_xlabel("Drug Regimen")
barchart.set_ylabel("Number of Mice")

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

gender_df = no_dupe_data[["Mouse ID", "Sex"]]

no_dupe_gender_df = pd.DataFrame(gender_df.drop_duplicates(subset = ["Mouse ID"]))

no_dupe_gender_df
male = (no_dupe_gender_df.loc[:, "Sex"] == "Male").mean()
female = (no_dupe_gender_df.loc[:, "Sex"] == "Female").mean()
percentages = [male, female]
sexes = ["Male", "Female"]
colors = ["blue", "red"]

plt.pie(percentages, labels = sexes, colors = colors, autopct='%.2f%%')
plt.show()
no_dupe_data.groupby(["Sex"]).count().plot(kind='pie', y ="Mouse ID", autopct='%.2f%%')

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

test = no_dupe_data[["Mouse ID"]]

test["Mouse ID"] = test.drop_duplicates(keep = 'last')

test["Timepoint"] = no_dupe_data["Timepoint"]

final_time_df = test[test["Mouse ID"].notna()]

# Start by getting the last (greatest) timepoint for each mouse
final_time_df
# Merge this group df with the original dataframe to get the tumor volume at the last timepoint
merged_df = pd.merge(final_time_df, no_dupe_data, how="left", left_index=True, right_index=True)

merged_df
# Put treatments into a list for for loop (and later for plot labels)
drug_list = ['Capomulin','Ramicane','Infubinol','Ceftamin']
#capomulin df
capomulin_only =  merged_df.loc[merged_df["Drug Regimen"] == "Capomulin"]

capomulinList = capomulin_only["Tumor Volume (mm3)"].tolist()
capomulin_only
#ramicane df
ramicane_only =  merged_df.loc[merged_df["Drug Regimen"] == "Ramicane"]
ramicaneList = ramicane_only["Tumor Volume (mm3)"].tolist()
ramicane_only
#infubinol df
infubinol_only =  merged_df.loc[merged_df["Drug Regimen"] == "Infubinol"]
infubinolList = infubinol_only["Tumor Volume (mm3)"].tolist()
infubinol_only
#ceftamin df
ceftamin_only =  merged_df.loc[merged_df["Drug Regimen"] == "Ceftamin"]
ceftaminList = ceftamin_only["Tumor Volume (mm3)"].tolist()
ceftamin_only
# Calculate the IQR and quantitatively determine if there are any potential outliers. 

# Capomulin   
capomulin_vols = capomulin_only["Tumor Volume (mm3)"]

quartiles = capomulin_vols.quantile([.25, .5, .75])

capomulin_lowerq = quartiles[0.25]
capomulin_upperq = quartiles[0.75]
capomulin_iqr = capomulin_upperq - capomulin_lowerq

print(f"Cap Lower Q: {capomulin_lowerq}")
print(f"Cap Upper Q: {capomulin_upperq}")

capomulin_lower_bound = capomulin_lowerq - (1.5*capomulin_iqr)
capomulin_upper_bound = capomulin_upperq + (1.5*capomulin_iqr)

print(f"Cap lower bound: {capomulin_lower_bound}")
print(f"Cap Upper bound: {capomulin_upper_bound}")
#ramicane
ramicane_vols = ramicane_only["Tumor Volume (mm3)"]

quartiles = ramicane_vols.quantile([.25, .5, .75])

ramicane_lowerq = quartiles[0.25]
ramicane_upperq = quartiles[0.75]
ramicane_iqr = ramicane_upperq - ramicane_lowerq

print(f"Ramicane Lower Q: {ramicane_lowerq}")
print(f"Ramicane Upper Q: {ramicane_upperq}")

ramicane_lower_bound = ramicane_lowerq - (1.5*ramicane_iqr)
ramicane_upper_bound = ramicane_upperq + (1.5*ramicane_iqr)

print(f"Ramicane lower bound: {ramicane_lower_bound}")
print(f"Ramicane Upper bound: {ramicane_upper_bound}")
#infubinol
infubinol_vols = infubinol_only["Tumor Volume (mm3)"]

quartiles = infubinol_vols.quantile([.25, .5, .75])

infubinol_lowerq = quartiles[0.25]
infubinol_upperq = quartiles[0.75]
infubinol_iqr = infubinol_upperq - infubinol_lowerq

print(f"Infubinol Lower Q: {infubinol_lowerq}")
print(f"Infubinol Upper Q: {infubinol_upperq}")

infubinol_lower_bound = infubinol_lowerq - (1.5*infubinol_iqr)
infubinol_upper_bound = infubinol_upperq + (1.5*infubinol_iqr)

print(f"Infubinol lower bound: {infubinol_lower_bound}")
print(f"Infubinol Upper bound: {infubinol_upper_bound}")
#ceftamin
ceftamin_vols = ceftamin_only["Tumor Volume (mm3)"]

quartiles = ceftamin_vols.quantile([.25, .5, .75])

ceftamin_lowerq = quartiles[0.25]
ceftamin_upperq = quartiles[0.75]
ceftamin_iqr = ceftamin_upperq - ceftamin_lowerq

print(f"Ceftamin Lower Q: {ceftamin_lowerq}")
print(f"Ceftamin Upper Q: {ceftamin_upperq}")

ceftamin_lower_bound = ceftamin_lowerq - (1.5*ceftamin_iqr)
ceftamin_upper_bound = ceftamin_upperq + (1.5*ceftamin_iqr)

print(f"Ceftamin lower bound: {ceftamin_lower_bound}")
print(f"Ceftamin Upper bound: {ceftamin_upper_bound}")
#capmulin
capmulin_outliers = capomulin_only.loc[capomulin_only["Tumor Volume (mm3)"] > capomulin_upper_bound]
capmulin_outliers = capomulin_only.loc[capomulin_only["Tumor Volume (mm3)"] < capomulin_lower_bound]

capmulin_outliers
infubinol_outliers = infubinol_only.loc[infubinol_only["Tumor Volume (mm3)"] > infubinol_upper_bound]
infubinol_outliers = infubinol_only.loc[infubinol_only["Tumor Volume (mm3)"] < infubinol_lower_bound]

infubinol_outliers
#ceftamin
ceftamin_outliers = ceftamin_only.loc[ceftamin_only["Tumor Volume (mm3)"] > ceftamin_upper_bound]
ceftamin_outliers = ceftamin_only.loc[ceftamin_only["Tumor Volume (mm3)"] < ceftamin_lower_bound]

ceftamin_outliers
# Generate a box plot of the final tumor volume of each mouse across four regimens of interest
red = dict(markerfacecolor='r', marker='D')
data = [capomulinList, ramicaneList, infubinolList, ceftaminList]

fig1, ax1 = plt.subplots()
ax1.set_title("Top 4 Drugs")
ax1.boxplot(data, showfliers=True, flierprops = red)

ax1.set_xticklabels(drug_list)

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
mouseID = "s185"

lineData = no_dupe_data.loc[no_dupe_data["Mouse ID"] == mouseID]

Timepoints = [0, 5, 10, 15, 20, 25, 30, 35, 40, 45]

tumVols = lineData["Tumor Volume (mm3)"]
    
lineChart, = plt.plot(Timepoints, tumVols, marker= "o", color = "r")

plt.title("MouseID: s185 Change in Tumor")
plt.xlabel("Timepoint")
plt.ylabel("Tumor Volume (mm3)")

plt.xticks(np.arange(0, max(tumVols) + 1, 5.0))

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

scatter_data = no_dupe_data.loc[no_dupe_data["Drug Regimen"] == "Capomulin"]

mean_volumes = scatter_data.groupby("Mouse ID")["Tumor Volume (mm3)"].mean()

mean_weight =  scatter_data.groupby("Mouse ID")["Weight (g)"].mean()

scatter_plot = plt.scatter(mean_volumes, mean_weight, marker="o", facecolors="r",edgecolors="black")

#setting limits
plt.xlim(min(mean_volumes - .5), max(mean_volumes + 0.5))
plt.ylim(min(mean_weight - .5), max(mean_weight + 0.5))

#labels
plt.xlabel("Tumor Volume (mm3)")
plt.ylabel("Weight (g)")
plt.title("Capomulin - Average Tumor Volume vs. Mouse Weight")

plt.show()
#Calculate the correlation coefficient and linear regression model
#between mouse weight and average tumor volume for the Capomulin 
#treatment. Plot the linear regression model on top of the previous 
#scatter plot.
corr = st.pearsonr(mean_volumes, mean_weight)[0]

corr
(slope, intercept, rvalue, pvalue, stderr) = st.linregress(mean_volumes, mean_weight)

regress_values = mean_volumes * slope + intercept

line_eq = "y = " + str(round(slope, 2)) + "x + " + str(round(intercept, 2))

line_eq

plt.plot(mean_volumes, regress_values, color = "black")
plt.xlabel("Tumor Volume (mm3)")
plt.ylabel("Weight (g)")
plt.title("Capomulin - Average Tumor Volume vs. Mouse Weight")

plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12302133516861666, 'fraction_numerical': 0.013936682725395734, 'mean_word_length': 6.688492063492063, 'pattern_counts': {'":': 0, '<': 24, '<?xml version=': 0, '>': 24, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '257161', 'license': 'ADSL', 'n_tokens_mistral': 4518, 'n_tokens_neox': 4241, 'n_words': 1051, 'path': 'Pymaceuticals/pymaceuticals_starter.ipynb', 'repo_name': 'RileyEspersen1/matplotlib-challenge', 'size': '11624'}