<jupyter_start><jupyter_text>Observations and Insights     The correlation between mouse weight and tumor volume was strong. which is to say that if you failed to control mouse weight as you were trying to determine efficacy of the drug then the results may be skewed.    Going by the final tumor volume one could conclude that Capomulin and Ramicane appeared to be the most effective.The data observed only came back with one outlier from the drug Infubinol.    The study had nearly an equal amount of Male and Female mice which could help to determine if sex correlated to the efficacy of certain drugs.<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
combined_df = pd.merge(mouse_metadata, study_results, on="Mouse ID")
# Display the data table for preview
combined_df.head()<jupyter_output><empty_output><jupyter_text>Summary Statistics<jupyter_code># Method 1:  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.


# Method 2(optional): 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. (https://pandas.pydata.org/pandas-docs/version/0.22.0/generated/pandas.core.groupby.DataFrameGroupBy.agg.html)
drug_group_summary = combined_df.groupby('Drug Regimen').agg(\
        tumor_mean=('Tumor Volume (mm3)', np.mean),\
        tumor_median=('Tumor Volume (mm3)', np.median),\
        tumor_var=('Tumor Volume (mm3)', np.var),\
        tumor_std=('Tumor Volume (mm3)', np.std),\
        tumor_sem=('Tumor Volume (mm3)', st.sem)\
).round(3)

drug_group_summary<jupyter_output><empty_output><jupyter_text>Bar and Pie Charts<jupyter_code># Use Pandas to generate a bar plot showing the total number of mice in each treatment regimen throughout the course of the study. 
drug_group = combined_df.groupby('Drug Regimen')

drug_group_count = pd.DataFrame(drug_group['Drug Regimen'].count())

count_chart = drug_group_count.plot(kind='bar', title="Count for each Drug Regimen", color="cyan")

count_chart.set_xlabel("Drug Regimen")
count_chart.set_ylabel("Count")
plt.tight_layout()
plt.savefig("figures/PandasBar.png")

plt.show()
# Use Pyplot to generate a bar plot showing the total number of mice treatment in each treatment regimen throughout the course of the study.
x_axis = np.arange(len(drug_group_count))
tick_locations = [value for value in x_axis]

plt.figure(figsize=(6,5))
plt.bar(x_axis, drug_group_count["Drug Regimen"], color='cyan', align="center", width = 0.52)
plt.xticks(tick_locations, list(drug_group_count.index), rotation="vertical")
plt.xlim(-0.7, len(x_axis)-0.3)
plt.ylim(0, max(drug_group_count["Drug Regimen"])*1.05)
plt.title("Count for each Drug Regimen")
plt.xlabel("Drug Regimen")
plt.ylabel("Count")

column_name = ["Drug Regimen"]
plt.legend(column_name,loc="best")
plt.savefig("figures/PyplotBar.png")

plt.show()
##  Note: this plot will be identical to the one that uses Pandas
# Use Pandas to generate a pie plot showing the distribution of female versus male mice
sex_group = combined_df.groupby('Sex')

sex_group_count = pd.DataFrame(sex_group['Sex'].count())

sex_group_count.plot(kind='pie', y='Sex', \
                     title="Distribution of female versus male mice",startangle=140,\
                     autopct='%1.1f%%',shadow=True, fontsize=16, colors =["cyan","goldenrod"],legend =False)
plt.axis("equal")
plt.savefig("figures/PandaPie.png")
plt.show()
## Note: this plot will be identical to the one that uses Pyplot
# Use Pyplot to generate a pie plot showing the distribution of female versus male mice
sexes = list(sex_group_count.index.values)

sex_counts = sex_group_count['Sex']

colors = ["cyan", "goldenrod"]

plt.pie(sex_counts, labels=sexes, colors=colors,
        autopct="%1.1f%%", shadow=True, startangle=140)
plt.rcParams['font.size'] = 16
plt.title("Distribution of female versus male mice")
plt.ylabel("Sex")
plt.axis("equal")
plt.savefig("figures/PyplotPie.png")
plt.show()
##  Note: this plot will be identical to the one that uses Pandas<jupyter_output><empty_output><jupyter_text>Quartiles, Outliers and Boxplots<jupyter_code># Calculate the final tumor volume of each mouse across four of the drug regimens:  
# Capomulin, Ramicane, Infubinol, and Ceftamin
drug_list = ['Capomulin', 'Ramicane', 'Infubinol', 'Ceftamin']
four_drugs = combined_df[combined_df['Drug Regimen'].isin(drug_list)]

four_tumor = four_drugs.groupby(['Drug Regimen','Mouse ID'])
four_last_tumor = four_tumor.agg(last_tumor=('Tumor Volume (mm3)',lambda x: x.iloc[-1])).round(3)

four_last_tumor_unstack = four_last_tumor.stack(level=0).unstack(level=0)

four_last_tumor
counter = 0
    
for drug in drug_list:
    quartiles = four_last_tumor_unstack[drug].quantile([.25,.5,.75]).round(2)
    lowerq = quartiles[0.25].round(2)
    upperq = quartiles[0.75].round(2)
    iqr = round(upperq-lowerq,2)
    lower_bound = round(lowerq - (1.5*iqr),2)
    upper_bound = round(upperq + (1.5*iqr),2)
    

    if counter == 0:
        print(f"--------------------------------------------------------")
    print(f"{drug} IQR data is:")
    print(f"The lower quartile of {drug} is: {lowerq}")
    print(f"The upper quartile of {drug} is: {upperq}")
    print(f"The interquartile range of {drug} is: {iqr}")
    print(f"The the median of {drug} is: {quartiles[0.5]} ")
    print(f"Values below {lower_bound} for {drug} could be outliers.")
    print(f"Values above {upper_bound} for {drug} could be outliers.")
    print(f"--------------------------------------------------------")
    counter += 1
    
# Create empty list to fill with tumor vol data (for plotting)
boxplot_list = []

for drug in drug_list:
    boxplot_list.append(list(four_last_tumor_unstack[drug].dropna()))

fig1, ax = plt.subplots(figsize=(9,7))
ax.set_title('Final measured Tumor volume by Drug Regimen')
ax.set_xlabel('Drug Regimen')
ax.set_ylabel('Tumor Vol (mm3)')
ax.boxplot(boxplot_list,notch=0,sym='gD')
plt.xticks([1,2,3,4],drug_list)

# Save the figure
plt.savefig("figures/BoxPlt.png")
plt.show()<jupyter_output><empty_output><jupyter_text>Line and Scatter Plots<jupyter_code># Generate a line plot of tumor volume vs. timepoint for a mouse treated with Capomulin
capomulin_data = combined_df.loc[combined_df['Drug Regimen'] == 'Capomulin']

# View the dataframe
capomulin_data.head()
# Generate a scatter plot of average tumor volume vs. mouse weight for the Capomulin regimen. 
# Note: this means mouse weight goes on the x-axis, with average tumor volume on the y-axis. 
cap_time_tumor = capomulin_data.groupby(['Timepoint']).agg(\
        Tumor_Vol_Mean=('Tumor Volume (mm3)', np.mean),\
        Tumor_Vol_SEM=('Tumor Volume (mm3)', st.sem)\
).round(3)

cap_time_tumor.head(10)

time_series = list(cap_time_tumor.index.values)

plt.errorbar(
    time_series,
    cap_time_tumor['Tumor_Vol_Mean'],
    yerr = cap_time_tumor['Tumor_Vol_SEM'], 
    label = "Time Series of Tumor Volume for Capomulin",
    fmt = "bs--",
    linewidth = 3 
    )

plt.title("Time Series of Tumor Volume for Capomulin")
plt.xlabel("Time (days)")
plt.ylabel("Tumor Volume (mm3)")


plt.xlim(min(time_series)-max(time_series)*0.05, max(time_series)*1.05)
plt.ylim(min(cap_time_tumor['Tumor_Vol_Mean'])*0.95, max(cap_time_tumor['Tumor_Vol_Mean'])*1.05)
plt.rcParams["figure.figsize"] = [8,7]

# Save the figure
plt.savefig("figures/LineChart.png")
plt.show()
cap_mouse_id = capomulin_data.groupby(['Mouse ID']).agg(\
        # Get the mean of the 'Tumor Volume (mm3)' column\
        Mouse_weight=('Weight (g)', np.mean),\
        # Get the standard error of measurement (SEM) of the 'Tumor Volume (mm3)' column\
        Tumor_vol_mean=('Tumor Volume (mm3)', np.mean)\
).round(3)
cap_mouse_id.head(10)
plt.scatter(
    cap_mouse_id['Mouse_weight'],
    cap_mouse_id['Tumor_vol_mean'],
    marker='o',
    facecolors='red',
    edgecolors='black',
    s=cap_mouse_id['Tumor_vol_mean'],
    alpha=.75)

# Create a title, x label, and y label for our chart
plt.title("Mouse weight vs. Avg. Tumor Volume")
plt.xlabel("Mouse weight (g)")
plt.ylabel("Tumor Volume (mm3)")
# Save the figure
plt.savefig("figures/ScatWtTumorVol.png")

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 = st.pearsonr(cap_mouse_id['Mouse_weight'],cap_mouse_id['Tumor_vol_mean'])
print(f"The correlation between both factors is {round(correlation[0],2)}")
x_values = cap_mouse_id['Mouse_weight']
y_values = cap_mouse_id['Tumor_vol_mean']
(slope, intercept, rvalue, pvalue, stderr) = linregress(x_values, y_values)
regress_values = x_values * slope + intercept

line_eq = f'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,(17,37),fontsize=15,color="black")
plt.title("Mouse weight vs. Avg. Tumor Volume")
plt.xlabel("Mouse weight (g)")
plt.ylabel("Tumor Volume (mm3)")
print(f"The r-squared is: {rvalue}")
print(f"The equation of the regression line is: {line_eq}")

# Save the figure
plt.savefig("figures/ScatRegress.png")
plt.show()<jupyter_output>The r-squared is: 0.8419461020261081
The equation of the regression line is: y = 0.95x + 21.55
{'fraction_non_alphanumeric': 0.1350020104543627, 'fraction_numerical': 0.014475271411338963, 'mean_word_length': 4.936157517899761, 'pattern_counts': {'":': 0, '<': 24, '<?xml version=': 0, '>': 24, 'https://': 1, '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': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '609550', 'license': 'ADSL', 'n_tokens_mistral': 3613, 'n_tokens_neox': 3312, 'n_words': 907, 'path': 'Pymaceuticals/pymaceuticals_starter.ipynb', 'repo_name': 'mneustadts/Matplotlib-challenge', 'size': '9948'}