<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
from scipy.stats import linregress
# 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
merge = pd.merge(mouse_metadata,study_results, how = "left", on = "Mouse ID")
# Display the data table for preview
merge
# Checking the number of mice.
mice_number = len(merge["Mouse ID"].unique())
mice_number
# Getting the duplicate mice by ID number that shows up for Mouse ID and Timepoint. 
duplicate_df = merge.loc[merge.duplicated(subset=["Mouse ID","Timepoint"]),"Mouse ID"].unique()
duplicate_df
# Optional: Get all the data for the duplicate mouse ID. 
duplicate_mouse = clean_df.loc[clean_df["Mouse ID"]== "g989"]
duplicate_mouse
# Create a clean DataFrame by dropping the duplicate mouse by its ID.

clean_df = merge.loc[merge["Mouse ID"]!= "g989"]
clean_df
# Checking the number of mice in the clean DataFrame.
number_mice = len(clean_df["Mouse ID"].unique())
number_mice<jupyter_output><empty_output><jupyter_text>Summary Statistics<jupyter_code>average = clean_df.groupby(["Drug Regimen"]).mean()["Tumor Volume (mm3)"]
average
median = clean_df.groupby(["Drug Regimen"]).median()["Tumor Volume (mm3)"]
median
standard_dev = clean_df.groupby(["Drug Regimen"]).std()["Tumor Volume (mm3)"]
standard_dev
sem = clean_df.groupby(["Drug Regimen"]).sem()["Tumor Volume (mm3)"]
sem
# 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.
Drug_analysis = pd.DataFrame({"Average": average, "Median": median, "Standard Dev.": standard_dev, "SEM": sem})
Drug_analysis

# Generate a summary statistics table of mean, median, variance, standard deviation, and SEM of the tumor volume for each regimen
summary = clean_df.groupby(["Drug Regimen"]).agg({"Tumor Volume (mm3)" : ["mean", "median","std","sem"]})
# Using the aggregation method, produce the same summary statistics in a single line
summary<jupyter_output><empty_output><jupyter_text>Bar and Pie Charts<jupyter_code>drug_group = clean_df.groupby('Drug Regimen')

# Count how many times each maker appears in our group
drug_makers = drug_group['Drug Regimen'].count()

drug_makers
# # Generate a bar plot showing the total number of measurements taken on each drug regimen using pandas.
drug_bar = pd.DataFrame({"Drug Makers":drug_makers })
drug_bar.plot(kind="bar")
x_axis = np.arange(len(drug_makers))
x_axis
# Generate a bar plot showing the total number of measurements taken on each drug regimen using pyplot.
plt.bar(x_axis, drug_makers, color="b", align="center")
male_female = clean_df.groupby('Sex')

# Count how many times each maker appears in our group
mouse_sex = male_female['Mouse ID'].unique()

mouse_sex
count = clean_df.Sex.value_counts()
count.plot(kind="pie",autopct="%1.1f%%")
plt.show()

male_df = clean_df.loc[clean_df["Sex"]== "Male"]
male = male_df["Mouse ID"].unique()
len(male)
female_df = clean_df.loc[clean_df["Sex"]== "Female"]
female = female_df["Mouse ID"].unique()
len(female)
sizes = [len(male),len(female) ]
colors = ["yellow","green"]
explode = (0.1, 0)
# Generate a pie plot showing the distribution of female versus male mice using pandas
# plt.pie(sizes, explode=explode, colors=colors,
#         autopct="%1.1f%%", shadow=True, startangle=140)
# Generate a pie plot showing the distribution of female versus male mice using pyplot
plt.pie(count, colors=colors,autopct="%1.1f%%")<jupyter_output><empty_output><jupyter_text>Quartiles, Outliers and Boxplots<jupyter_code># drugs = ["Capomulin", "Ramicane", "Infubinol", "Ceftamin"]
# 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


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

tumor = clean_df.groupby(["Mouse ID"])["Timepoint"].max()
tumor = tumor.reset_index()
final_df = tumor.merge(clean_df, on=["Mouse ID", "Timepoint"], how = "left")
final_df
specific_tumor_volume = final_df[final_df["Drug Regimen"].isin(["Capomulin", "Ramicane", "Infubinol", "Ceftamin"])]
specific_tumor_volume.head()
# Put treatments into a list for for loop (and later for plot labels)


# Create empty list to fill with tumor vol data (for plotting)


# 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
    
    
    # add subset 
    
    
    # Determine outliers using upper and lower bounds
    
tumor_volume = []
for x in drugs:
    final_tumor_volume = specific_tumor_volume.loc[specific_tumor_volume["Drug Regimen"]== x,"Tumor Volume (mm3)"]
    tumor_volume.append(final_tumor_volume)
    quartiles =  final_tumor_volume.quantile([0.25,0.5,0.75])
    lower_quartiles = quartiles[0.25]
    upper_quartiles = quartiles[0.75]
    iqr = upper_quartiles-lower_quartiles
print(f"The lower quartile of Tumor Volume (mm3) is: {lower_quartiles}")
print(f"The upper quartile of Tumor Volume (mm3) is: {upper_quartiles}")
print(f"The interquartile range of Tumor Volume (mm3) is: {iqr}")
print(f"The the median of Tumor Volume (mm3) is: {quartiles[0.5]} ")

lower_bound = lower_quartiles- (1.5*iqr)
upper_bound = upper_quartiles + (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
fig1, ax1 = plt.subplots()
ax1.set_title('final tumor volume')
ax1.set_ylabel('tumor volume')
ax1.boxplot(final_tumor_volume)
plt.show()<jupyter_output><empty_output><jupyter_text>Line and Scatter Plots<jupyter_code>line_df = clean_df.loc[clean_df["Drug Regimen"]== "Capomulin"].loc[clean_df["Mouse ID"]=="s185"]
line_df
scatter_df = clean_df.loc[clean_df["Drug Regimen"]== "Capomulin"]
scatter_df
scatter_average = scatter_df.groupby(["Mouse ID"]).mean()
scatter_average
# Generate a line plot of tumor volume vs. time point for a mouse treated with Capomulin
line_df.plot(kind="line", x="Timepoint", y="Tumor Volume (mm3)", grid=True, figsize=(8,8),
              title="tumor volume vs. mouse weight")
plt.show()
# Generate a scatter plot of average tumor volume vs. mouse weight for the Capomulin regimen
scatter_average.plot(kind="scatter", x="Weight (g)", y="Tumor Volume (mm3)", grid=True, figsize=(8,8),
              title="tumor volume vs. mouse weight")
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

# Linear Regression Model
x_values = scatter_average['Weight (g)']
y_values =scatter_average['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,(6,10),fontsize=15,color="red")
plt.xlabel('Weight (g)')
plt.ylabel('Tumor Volume (mm3)')
plt.show()
line_eq
# Correlation Model
plt.scatter(scatter_average.iloc[:,1],scatter_average.iloc[:,3])
plt.xlabel('Weight (g)')
plt.ylabel('Tumor Volume (mm3)')
plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12023898431665422, 'fraction_numerical': 0.009210853871048046, 'mean_word_length': 6.035901926444834, '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': '410258', 'license': 'ADSL', 'n_tokens_mistral': 2795, 'n_tokens_neox': 2586, 'n_words': 787, 'path': 'pymaceuticals_starter.ipynb', 'repo_name': 'gonzal29/Matplot-Challenge', 'size': '8034'}