<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

# 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
data = pd.merge(
    left=mouse_metadata,
    right=study_results,
    how='outer',
    on = "Mouse ID")
# Checking the number of mice in the DataFrame.
num_of_mice = len(data['Mouse ID'].unique())
# Getting the duplicate mice by ID number that shows up for Mouse ID and Timepoint. 
data[['Mouse ID','Timepoint']].value_counts().head(5)
# Optional: Get all the data for the duplicate mouse ID. 
indexed_data = data.set_index('Mouse ID')
indexed_data.loc['g989', :]
# Create a clean DataFrame by dropping the duplicate mouse by its ID.
clean_data = indexed_data.drop(index='g989')
clean_data = clean_data.reset_index()
clean_data
# Checking the number of mice in the clean DataFrame.
num_of_mice = len(clean_data['Mouse ID'].unique())
num_of_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
tumor_volume_mean = clean_data.groupby('Drug Regimen').mean()['Tumor Volume (mm3)']
tumor_volume_median = clean_data.groupby('Drug Regimen').median()['Tumor Volume (mm3)']
tumor_volunme_var = clean_data.groupby('Drug Regimen').var()['Tumor Volume (mm3)']
tumor_volume_std = clean_data.groupby('Drug Regimen').std()['Tumor Volume (mm3)']
tumor_volume_sem = clean_data.groupby('Drug Regimen').sem()['Tumor Volume (mm3)']

# This method is the most straightforward, creating multiple series and putting them all together at the end.
clean_data_stats_1 = pd.DataFrame({
    'Tumor Volume Mean': tumor_volume_mean,
    'Tumor Volume Median': tumor_volume_median,
    'Tumor Volume Variance': tumor_volunme_var,
    'Tumor Volume Std Dev': tumor_volume_std,
    'Tumor Volume SEM': tumor_volume_sem
})
clean_data_stats_1
# 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.
clean_data_stats_2 = clean_data.groupby('Drug Regimen').agg(['mean','median','var','std','sem'])['Tumor Volume (mm3)']
clean_data_stats_2<jupyter_output><empty_output><jupyter_text>Bar Plots<jupyter_code># Generate a bar plot showing the number of mice per time point for each treatment throughout the course of the study using pandas.
mice_count_per_drug = clean_data.groupby('Drug Regimen').count()['Timepoint']

mice_count_per_drug.plot(kind='bar')
plt.title('Number of Mice per Drug Regimen')
plt.ylabel('Number of Mice')
plt.show()
# Generate a bar plot showing the number of mice per time point for each treatment throughout the course of the study using pyplot.
drug_regimen = clean_data['Drug Regimen'].unique()
drug_regimen
mice_count_per_drug

plt.bar(
    x=drug_regimen,
    height=mice_count_per_drug
)
plt.xticks(rotation=90)
plt.title('Number of Mice per Drug Regimen')
plt.xlabel('Drug Regimen')
plt.ylabel('Number of Mice')
plt.show()<jupyter_output><empty_output><jupyter_text>Pie Plots<jupyter_code># Generate a pie plot showing the distribution of female versus male mice using pandas
mouse_gender_groupby = clean_data.groupby(['Mouse ID', 'Sex'])
mouse_gender_df = pd.DataFrame(mouse_gender_groupby.size()).groupby('Sex')

mouse_gender_count = pd.DataFrame(mouse_gender_df.count())
mouse_gender_count.columns = ['Count']
mouse_gender_count

mouse_gender_count.plot(kind='pie', y='Count', autopct='%1.1f%%')
plt.show()
# Generate a pie plot showing the distribution of female versus male mice using pyplot
gender = clean_data['Sex'].unique()
values = [125, 123]

plt.pie(
    x=values,
    labels=gender,
    autopct='%1.1f%%'
)
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 most promising treatment regimens. Calculate the IQR and quantitatively determine if there are any potential outliers. 
capomulin_df = clean_data.loc[clean_data['Drug Regimen'] == 'Capomulin', :]
ramicane_df = clean_data.loc[clean_data['Drug Regimen'] == 'Ramicane', :]
infubinol_df = clean_data.loc[clean_data['Drug Regimen'] == 'Infubinol', :]
ceftamin_df = clean_data.loc[clean_data['Drug Regimen'] == 'Ceftamin', :]

# Capomulin
capomulin_volume_df = capomulin_df['Tumor Volume (mm3)']
cap_quartiles = capomulin_volume_df.quantile(q=[.25,.5,.75])
cap_lowerq = cap_quartiles[.25]
cap_upperq = cap_quartiles[.75]
cap_iqr = cap_upperq - cap_lowerq
cap_lower_bound = cap_lowerq - (1.5 * cap_iqr)
cap_upper_bound = cap_upperq + (1.5 * cap_iqr)
print('Capomulin:')
print(f"Values below {cap_lower_bound} could be outliers.")
print(f"Values above {cap_upper_bound} could be outliers.")

cap_lower_outliers = len(capomulin_df.loc[capomulin_df['Tumor Volume (mm3)'] < cap_lower_bound])
cap_upper_outliers = len(capomulin_df.loc[capomulin_df['Tumor Volume (mm3)'] > cap_upper_bound])
print(f"{cap_lower_outliers} lower outlier(s) and {cap_upper_outliers} upper outlier(s) found.")
print('\n')


# Ramicane
ramicane_volume_df = ramicane_df['Tumor Volume (mm3)']
ram_quartiles = ramicane_volume_df.quantile(q=[.25,.5,.75])
ram_lowerq = ram_quartiles[.25]
ram_upperq = ram_quartiles[.75]
ram_iqr = ram_upperq - ram_lowerq
ram_lower_bound = ram_lowerq - (1.5 * ram_iqr)
ram_upper_bound = ram_upperq + (1.5 * ram_iqr)
print('Ramicane:')
print(f"Values below {ram_lower_bound} could be outliers.")
print(f"Values above {ram_upper_bound} could be outliers.")

ram_lower_outliers = len(ramicane_df.loc[ramicane_df['Tumor Volume (mm3)'] < ram_lower_bound])
ram_upper_outliers = len(ramicane_df.loc[ramicane_df['Tumor Volume (mm3)'] > ram_upper_bound])
print(f"{ram_lower_outliers} lower outlier(s) and {ram_upper_outliers} upper outlier(s) found.")
print('\n')


# Infubinol
infubinol_volume_df = infubinol_df['Tumor Volume (mm3)']
inf_quartiles = infubinol_volume_df.quantile(q=[.25,.5,.75])
inf_lowerq = inf_quartiles[.25]
inf_upperq = inf_quartiles[.75]
inf_iqr = inf_upperq - inf_lowerq
inf_lower_bound = inf_lowerq - (1.5 * inf_iqr)
inf_upper_bound = inf_upperq + (1.5 * inf_iqr)
print('Infubinol:')
print(f"Values below {inf_lower_bound} could be outliers.")
print(f"Values above {inf_upper_bound} could be outliers.")

inf_lower_outliers = len(infubinol_df.loc[infubinol_df['Tumor Volume (mm3)'] < inf_lower_bound])
inf_upper_outliers = len(infubinol_df.loc[infubinol_df['Tumor Volume (mm3)'] > inf_upper_bound])
print(f"{inf_lower_outliers} lower outlier(s) and {inf_upper_outliers} upper outlier(s) found.")
print('\n')


# Ceftamin
ceftamin_volume_df = ceftamin_df['Tumor Volume (mm3)']
cef_quartiles = ceftamin_volume_df.quantile(q=[.25,.5,.75])
cef_lowerq = cef_quartiles[.25]
cef_upperq = cef_quartiles[.75]
cef_iqr = cef_upperq - cef_lowerq
cef_lower_bound = cef_lowerq - (1.5 * inf_iqr)
cef_upper_bound = cef_upperq + (1.5 * inf_iqr)
print('Ceftamin:')
print(f"Values below {cef_lower_bound} could be outliers.")
print(f"Values above {cef_upper_bound} could be outliers.")

cef_lower_outliers = len(ceftamin_df.loc[ceftamin_df['Tumor Volume (mm3)'] < cef_lower_bound])
cef_upper_outliers = len(ceftamin_df.loc[ceftamin_df['Tumor Volume (mm3)'] > cef_upper_bound])
print(f"{cef_lower_outliers} lower outlier(s) and {cef_upper_outliers} upper outlier(s) found.")
print('\n')

# Generate a box plot of the final tumor volume of each mouse across four regimens of interest
fig1, ax1 = plt.subplots()
ax1.boxplot([capomulin_volume_df, ramicane_volume_df, infubinol_volume_df, ceftamin_volume_df])
ax1.set_title('Final Tumor Volume Across Drug Regimens')
ax1.set_xlabel('Drug Regimen')
ax1.set_ylabel('Tumor Volume (mm3)')
ax1.set_xticklabels(["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
capomulin_df.head()
cap_mouse_ID = 's185'
cap_mouse_ID_df = capomulin_df.loc[capomulin_df['Mouse ID'] == cap_mouse_ID, :]
cap_mouse_ID_timepoint = cap_mouse_ID_df['Timepoint']
cap_mouse_ID_tumor_volume = cap_mouse_ID_df['Tumor Volume (mm3)']

plt.plot(
    cap_mouse_ID_timepoint,
    cap_mouse_ID_tumor_volume
)
plt.title('Tumor Volume of S185 with Capomulin Regimen')
plt.xlabel('Timepoint')
plt.ylabel('Tumor Volume (mm3)')
plt.show()
# Generate a scatter plot of mouse weight versus average tumor volume for the Capomulin regimen
cap_weight_vs_tumor_volume = capomulin_df.groupby('Mouse ID').mean()
cap_weight = cap_weight_vs_tumor_volume['Weight (g)']
cap_tumor_volume = cap_weight_vs_tumor_volume['Tumor Volume (mm3)']

plt.scatter(
    x=cap_weight,
    y=cap_tumor_volume
)
plt.title('Mouse Weight vs. Avg Tumor Volume with Capomulin Regimen')
plt.xlabel('Mouse Weight (g)')
plt.ylabel('Avg Tumor Volume (mm3)')
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_weight,cap_tumor_volume)
print(f"The correlation between mouse weight (g) and average tumor volume (mm3) is {round(correlation[0],2)}" + '\n')

(slope, intercept, rvalue, pvalue, stderr) = st.linregress(cap_weight, cap_tumor_volume)
regress_values = cap_weight * slope + intercept
line_eq = "y = " + str(round(slope,2)) + "x + " + str(round(intercept,2))

plt.scatter(cap_weight,cap_tumor_volume)
plt.plot(cap_weight,regress_values,"y-")
plt.annotate(line_eq,(20,40),fontsize=15,color="blue")
plt.xlabel('Mouse Weight (g)')
plt.ylabel('Avg Tumor Volume (mm3)')
plt.show()<jupyter_output>The correlation between mouse weight (g) and average tumor volume (mm3) is 0.84
{'fraction_non_alphanumeric': 0.11960263597914822, 'fraction_numerical': 0.012491393724795909, 'mean_word_length': 6.968652037617555, 'pattern_counts': {'":': 0, '<': 32, '<?xml version=': 0, '>': 32, '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': '550361', 'license': 'Apache-2.0', 'n_tokens_mistral': 3885, 'n_tokens_neox': 3610, 'n_words': 853, 'path': 'main.ipynb', 'repo_name': 'kevogil/Pymaceuticals', 'size': '10167'}