<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
from sklearn import datasets

# 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_df = pd.merge(mouse_metadata, study_results, how='outer', on='Mouse ID')
# Display the data table for preview
combined_df.head()
# Checking the number of mice.
total_mouse = len(combined_df["Mouse ID"].unique())
total_mouse
# Getting the duplicate mice by ID number that shows up for Mouse ID and Timepoint. 
dup_mouse = combined_df.loc[combined_df.duplicated(subset=['Mouse ID', 'Timepoint',]),'Mouse ID'].unique()
dup_mouse
# Optional: Get all the data for the duplicate mouse ID. 
dup_mouse = combined_df.loc[combined_df['Mouse ID'] == "g989",:]
dup_mouse

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

data_clean_df = combined_df[combined_df["Mouse ID"].isin(dup_mouse)==False]
data_clean_df.head()
# Checking the number of mice in the clean DataFrame.
total_mouse_clean = len(data_clean_df["Mouse ID"].unique())
total_mouse_clean<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

stat_data_clean_df = data_clean_df.groupby (["Drug Regimen"])
stat_data_clean_df.count().head()

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


drug_regime_mean = round(stat_data_clean_df["Tumor Volume (mm3)"].mean(),2)
drug_regime_median= round(stat_data_clean_df["Tumor Volume (mm3)"].median(),2)
drug_regime_variance = round(stat_data_clean_df["Tumor Volume (mm3)"].var(),2)
drug_regime_stdr_dev = round(stat_data_clean_df["Tumor Volume (mm3)"].std(),2)
regimen_sem = round(data_clean_df["Tumor Volume (mm3)"].sem(),2)

# Assemble the resulting series into a single summary dataframe.

summary_df = pd.DataFrame({"Mean":drug_regime_mean,
                           "Median":drug_regime_median, 
                           "Variance": drug_regime_variance, 
                           "Standard Deviation": drug_regime_stdr_dev,
                           "SEM": regimen_sem})
summary_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
summary_II = round(data_clean_df.groupby('Drug Regimen').agg(['mean','median','var','std','sem'])["Tumor Volume (mm3)"],2)
summary_II.head(12)<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.

total_measurments = data_clean_df.groupby(["Drug Regimen"]).count()["Mouse ID"] 

plot_pandas = total_measurments.plot.bar(color='r')

plt.xlabel("Drug Regimen")
plt.ylabel("Number of Mice")
plt.title("Number of Measurment")

plt.savefig("../Images/Number of Measurments.png", bbox_inches = "tight")
plt.tight_layout()
plt.show()
# Generate a bar plot showing the total number of measurements taken on each drug regimen using pyplot.

count_mouse = data_clean_df.groupby(["Drug Regimen"]).count()["Mouse ID"] 

count_mouse

mouse_list =data_clean_df.groupby(["Drug Regimen"])["Mouse ID"].count().tolist()
mouse_list

x_axis = np.arange(len(count_mouse))
fig1, ax1 = plt.subplots()
plt.bar(x_axis, mouse_list, color='r', alpha=0.8, align='center')

tick_locations = [value for value in x_axis]

plt.xticks(tick_locations, ['Capomulin', 'Ceftamin', 'Infubinol', 'Ketapril', 'Naftisol', 'Placebo', 'Propriva', 'Ramicane', 'Stelasyn', 'Zoniferol'],  rotation='vertical')

plt.xlim(-0.75, len(x_axis)-0.25)

plt.ylim(0, max(mouse_list)+10)

plt.title("Number of Mouses per Treatment")
plt.xlabel("Drug Regimen")
plt.ylabel("Number of Mouses")

plt.savefig("../Images/Number of Mouses per Treatment.png", bbox_inches = "tight")
# Generate a pie plot showing the distribution of female versus male mice using pandas
groupby_gender = data_clean_df.groupby(["Mouse ID","Sex"])
groupby_gender
gender_df = pd.DataFrame(groupby_gender.size())

mouse_gender = pd.DataFrame(gender_df.groupby(["Sex"]).count())
mouse_gender.columns = ["Total Count"]

mouse_gender["Percentage of Sex"] = round((100*(mouse_gender["Total Count"]/mouse_gender["Total Count"].sum())),2)

mouse_gender
# Generate a pie plot showing the distribution of female versus male mice using pandas
colors = ["pink", "blue"]
explode = (0.1, 0)
mouse_gender.plot.pie(y='Total Count', colors = colors, startangle=140, explode = explode, shadow = True, autopct="%1.1f%%")

plt.title("Distribution of Female Vs Male")
plt.ylabel(" ")
plt.axis("equal")

plt.savefig("../Images/Distribution of Female Vs Male.png", bbox_inches = "tight")
plt.show()
# Generate a pie plot showing the distribution of female versus male mice using pyplot

labels = ["Female","Male"]
sizes = [49.799197,50.200803]
colors = ['pink', 'blue']
explode = (0.1, 0)

plt.pie(sizes, explode=explode,labels=labels, colors=colors, autopct="%1.1f%%", shadow=True, startangle=140,)
plt.title("Distribution of Female Vs Male")

plt.axis("equal")

plt.savefig("../Images/Distribution of Female Vs Male Pyplot", bbox_inches = "tight")
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
max_timepoint = data_clean_df.groupby(['Mouse ID'])['Timepoint'].max()
max_timepoint

# Merge this group df with the original dataframe to get the tumor volume at the last timepoint
maxtimepoint_df = pd.merge(max_timepoint , data_clean_df, on=(["Mouse ID","Timepoint"]))
maxtimepoint_df.head()

# Put treatments into a list for for loop (and later for plot labels)

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

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

tumor_volume = []
total_drug_regimen = len(drug_regimen)
quartile = [0,1,2,3]
lowerq = [0,1,2,3]
upperq = [0,1,2,3]
iqr = [0,1,2,3]
lower_bound = [0,1,2,3]
upper_bound = [0,1,2,3]

# Locate the rows which contain mice on each drug and get the tumor volumes
    
for drug in drug_regimen:
    drug_regimen_value = maxtimepoint_df.loc[maxtimepoint_df["Drug Regimen"] == drug]
    tumor = drug_regimen_value['Tumor Volume (mm3)']
    tumor_volume.append(tumor)

for i in range(total_drug_regimen):
    quartile[i] = tumor_volume[i].quantile([.25,.5,.75])
    lowerq[i] = quartile[i][0.25]
    upperq[i] = quartile[i][0.75]
    iqr[i] = upperq[i]-lowerq[i]
    
    print(f"{drug_regimen[i]} treatment lower quartile is: {lowerq[i]}")
    print(f"{drug_regimen[i]} treatment upper quartile is: {upperq[i]}")
    print(f"{drug_regimen[i]} treatment interquartile range is: {iqr[i]}")

    lower_bound[i] = lowerq[i] - (1.5*IQR[i])
    upper_bound[i] = upperq[i] + (1.5*IQR[i])
    
    print(f"{drug_regimen[i]} treatment Values below {lower_bound[i]} could be outliers.")
    print(f"{drug_regimen[i]} treatment Values above {upper_bound[i]} could be outliers.")
    print("")
# Generate a box plot of the final tumor volume of each mouse across four regimens of interest<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

line_plot = capomulin_df.loc[capomulin_df["Mouse ID"] == "b128"]
line_plot.head()
x_axis = line_plot["Timepoint"]
y_axis = line_plot["Tumor Volume (mm3)"]

fig1, ax1 = plt.subplots()
plt.title("Mouse b128 Treated With Capomulin")
plt.plot(x_axis, y_axis, linewidth=1, markersize=8,marker="o",color="purple")
plt.xlabel("Timepoint")
plt.ylabel("Tumor Volume (mm3)")


plt.savefig("../Images/Mouse b128 Treated With Capomulin", bbox_inches = "tight")
plt.show()
# Generate a scatter plot of average tumor volume vs. mouse weight for the Capomulin regimen

capomulin_average_volume =capomulin_df.groupby(['Mouse ID']).mean()

marker_size=15
plt.scatter(capomulin_average_volume['Weight (g)'],capumulin_average_volume["Tumor Volume (mm3)"],s=75, color="purple")
plt.title("Capomulin Tumor Volume vs. Mouse Weight")
plt.xlabel("Weight (g)")
plt.ylabel("Averag Tumor Volume (mm3)")


plt.savefig("../Images/Capomulin Tumor Volume vs. Mouse Weight.png", bbox_inches = "tight")
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

correlation=round(st.pearsonr(capomulin_average_volume["Weight (g)"],capomulin_average_volume["Tumor Volume (mm3)"])[0],2)
print(f"The correlation between mouse weight and average tumor volume is {correlation}")

x_values = capomulin_average_volume["Weight (g)"]
y_values = capomulin_average_volume["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=14,color="red")
plt.title("Linear Regression Equation")
plt.xlabel("Average Tumor Volume (mm3)")
plt.ylabel("Weight (g)")

print(f"The r-squared is: {rvalue**2}")
plt.savefig("../Images/Linear Regression.png", bbox_inches = "tight")
plt.show()<jupyter_output>The r-squared is: 0.7088568047708717
{'fraction_non_alphanumeric': 0.13122216724393865, 'fraction_numerical': 0.01553686293913904, 'mean_word_length': 5.9792817679558015, 'pattern_counts': {'":': 5, '<': 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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '183854', 'license': 'ADSL', 'n_tokens_mistral': 3681, 'n_tokens_neox': 3406, 'n_words': 895, 'path': 'Pymaceuticals/.ipynb_checkpoints/pymaceuticals_starter-checkpoint.ipynb', 'repo_name': 'JoseH-Git/matplotlib-challenge', 'size': '10105'}