<jupyter_start><jupyter_text>Observations* As seen from the pie plots, both the male and female mice population were evenly distributed for the study as males constitued 51% of the clinical data while females constitued 49%. * Based on the box plots, Capomulin and Ramicane are far more effective in reducing the final tumor volume compared to Infubinol and Ceftamin.* From the linear regression analysis, we can see a strong positive correlation of 0.84 between mouse weight and average tumor volume. This indicates that as a mouse's weight increases, the average tumor volume also increases.<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
combined_df = pd.merge(study_results, mouse_metadata, how='outer', on="Mouse ID")

# Display the data table for preview
combined_df
# Checking the number of mice.
no_of_mice = combined_df['Mouse ID'].nunique()
no_of_mice
# Getting the duplicate mice by ID number that shows up for Mouse ID and Timepoint. 
duplicate_mice = combined_df.loc[combined_df.duplicated(subset=['Mouse ID', 'Timepoint']), 'Mouse ID'].unique()
duplicate_mice
# Optional: Get all the data for the duplicate mouse ID. 
duplicate_mouse_data = combined_df.loc[combined_df.duplicated(subset=['Mouse ID', 'Timepoint'])]
duplicate_mouse_data
# Create a clean DataFrame by dropping the duplicate mouse by its ID.
clean_df = combined_df[combined_df['Mouse ID'].isin(duplicate_mice) == False]
clean_df
# Checking the number of mice in the clean DataFrame.
no_of_mice_clean = clean_df['Mouse ID'].nunique()
no_of_mice_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

# 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.
grouped_drug = clean_df.groupby('Drug Regimen')
tumor_mean = grouped_drug['Tumor Volume (mm3)'].mean()
tumor_median = grouped_drug['Tumor Volume (mm3)'].median()
tumor_variance = grouped_drug['Tumor Volume (mm3)'].var()
tumor_std = grouped_drug['Tumor Volume (mm3)'].std()
tumor_sem = grouped_drug['Tumor Volume (mm3)'].sem()

summary_statistics = pd.DataFrame({
    'Mean Tumor Volume': tumor_mean,
    'Median Tumor Volume': tumor_median,
    'Tumor Volume Variance': tumor_variance,
    'Tumor Volume Std. Dev': tumor_std,
    'Tumor Volume Std. Err.': tumor_sem
})

summary_statistics
# 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_statistics_aggregation = grouped_drug.agg(['mean', 'median', 'var', 'std', 'sem'])['Tumor Volume (mm3)']
summary_statistics_aggregation<jupyter_output><empty_output><jupyter_text>Bar and Pie Charts<jupyter_code># Generate a bar plot showing the total number of timepoints for all mice tested for each drug regimen using Pandas.
no_of_measurements = grouped_drug['Mouse ID'].count()

pandas_bar = no_of_measurements.sort_values(ascending=False).plot.bar(figsize=(10,5), fontsize=10)

plt.title('Total Number of Mice Treated per Drug Regimen', fontsize=12)
plt.xlabel('Drug Regiment', fontsize=12)
plt.ylabel('Total Number of Measurements', fontsize=12)

plt.show()
# Generate a bar plot showing the total number of timepoints for all mice tested for each drug regimen using pyplot.
x_values = clean_df['Drug Regimen'].unique()
y_values = clean_df['Drug Regimen'].value_counts()

fig1, ax1 = plt.subplots(figsize=(10,5))
plt.bar(x_values, y_values)

plt.title('Total Number of Mice Treated per Drug Regimen', fontsize=12)
plt.xlabel('Drug Regiment', fontsize=12)
plt.ylabel('Total Number of Measurements', fontsize=12)
plt.xticks(rotation=90)

plt.xlim(-0.75, len(x_values)-0.25)
plt.ylim(0, max(y_values)+10)

plt.show()
# Generate a pie plot showing the distribution of female versus male mice using Pandas
gender_distribution = clean_df['Sex'].value_counts()

pandas_pie = gender_distribution.plot.pie(figsize=(15,10), autopct='%1.1f%%')

plt.title('Male vs. Female Mice Population', fontsize=15)
plt.ylabel('Sex', fontsize=15)

plt.show()
# Generate a pie plot showing the distribution of female versus male mice using pyplot
labels = ['Male', 'Female']
pie_distribution = clean_df['Sex'].value_counts()/clean_df['Sex'].count()

fig1, ax1 = plt.subplots(figsize=(15,10))
plt.pie(pie_distribution, labels=labels, autopct="%1.1f%%")

plt.title('Male vs. Female Mice Population', fontsize=15)
plt.ylabel('Sex', fontsize=15)

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
capomulin_final = clean_df.loc[clean_df['Drug Regimen'] == 'Capomulin', :]
ramicane_final = clean_df.loc[clean_df['Drug Regimen'] == 'Ramicane', :]
infubinol_final = clean_df.loc[clean_df['Drug Regimen'] == 'Infubinol', :]
ceftamin_final = clean_df.loc[clean_df['Drug Regimen'] == 'Ceftamin', :]

# Start by getting the last (greatest) timepoint for each mouse
capomulin_last = capomulin_final.groupby('Mouse ID').max()['Timepoint']
ramicane_last = ramicane_final.groupby('Mouse ID').max()['Timepoint']
infubinol_last = infubinol_final.groupby('Mouse ID').max()['Timepoint']
ceftamin_last = ceftamin_final.groupby('Mouse ID').max()['Timepoint']


# Merge this group df with the original dataframe to get the tumor volume at the last timepoint
capomulin_merge = pd.merge(capomulin_last, clean_df, on=(['Mouse ID', 'Timepoint']), how='left')
ramicane_merge = pd.merge(ramicane_last, clean_df, on=(['Mouse ID', 'Timepoint']), how='left')
infubinol_merge = pd.merge(infubinol_last, clean_df, on=(['Mouse ID', 'Timepoint']), how='left')
ceftamin_merge = pd.merge(ceftamin_last, clean_df, on=(['Mouse ID', 'Timepoint']), how='left')
# Put treatments into a list for for loop (and later for plot labels)
list_of_tumor_volumes = [capomulin_merge, ramicane_merge, infubinol_merge, ceftamin_merge]

# Create empty list to fill with tumor vol data (for plotting)
tumor_vol_data =[]

# Calculate the IQR and quantitatively determine if there are any potential outliers. 
for x in list_of_tumor_volumes:
    
    # Locate the rows which contain mice on each drug and get the tumor volumes
    quartiles = x['Tumor Volume (mm3)'].quantile([.25, .5, .75])
    tumor_vol_data.append(x['Tumor Volume (mm3)'])
    
    lowerq = quartiles[0.25]
    upperq = quartiles[0.75]
    iqr = upperq - lowerq
    
    # Determine outliers using upper and lower bounds
    lower_bound = lowerq - (1.5*iqr)
    upper_bound = upperq + (1.5*iqr)
    
    print(f"{x['Drug Regimen'].unique()[0]}'s potential outliers: {lower_bound} {upper_bound}")
# Generate a box plot of the final tumor volume of each mouse across four regimens of interest
drug_regimen= ['Capomulin', 'Ramicane', 'Infubinol','Ceftamin']

fig1, ax1 = plt.subplots(figsize=(8,8))
plt.boxplot(tumor_vol_data, labels=drug_regimen, flierprops={'marker': 'o', 'markersize': 10, 'markerfacecolor': 'red'})

ax1.set_title('Final Tumor Volume of Each Mouse Across Four Regimens', fontsize=12)
ax1.set_xlabel('Drug Regimen', fontsize=12)
ax1.set_ylabel('Final Tumor Volume (mm3)', fontsize=12)

plt.ylim(10,80)

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
mouse_l509 = capomulin_final.loc[capomulin_final['Mouse ID'] == 'l509', :]

x_axis = mouse_l509['Timepoint']
y_axis = mouse_l509['Tumor Volume (mm3)']

fig1, ax1 = plt.subplots(figsize=(8,6))
plt.plot(x_axis, y_axis)

plt.title('Capomulin Treatment of l509', fontsize=12)
plt.xlabel('Timepoint (days)', fontsize=12)
plt.ylabel('Tumor Volume (mm3)', fontsize=12)

plt.show()
# Generate a scatter plot of average tumor volume vs. mouse weight for the Capomulin regimen
capomulin_tumor_mean = capomulin_final.groupby(['Mouse ID']).mean()

fig1, ax1 = plt.subplots(figsize=(8,5))
plt.scatter(capomulin_tumor_mean['Weight (g)'], capomulin_tumor_mean['Tumor Volume (mm3)'])

plt.title('Average Tumor Volume vs. Mouse Weight', fontsize=12)
plt.xlabel('Weight (g)', fontsize=12)
plt.ylabel('Average Tumor Volume (mm3)', fontsize=12)

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_tumor_mean['Weight (g)'], capomulin_tumor_mean['Tumor Volume (mm3)'])[0], 2)
print(f"The correlation between mouse weight and average tumor volume is {correlation}")

x_values = capomulin_tumor_mean['Weight (g)']
y_values = capomulin_tumor_mean['Tumor Volume (mm3)']

(slope, intercept, rvalue, pvalue, stderr) = st.linregress(x_values, y_values)
regress_values = x_values * slope + intercept

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

fig1, ax1 = plt.subplots(figsize=(8,5))
plt.scatter(x_values, y_values)
plt.plot(x_values, regress_values,"r-")

plt.title('Regression Plot of Average Tumor Volume vs. Mouse Weight', fontsize=12)
plt.xlabel('Weight (g)', fontsize=12)
plt.ylabel('Average Tumor Volume (mm3)', fontsize=12)
plt.annotate(line_eq, (20,45), fontsize=12, color="red")

plt.show()

print(f"The r-squared is: {rvalue**2}")<jupyter_output>The correlation between mouse weight and average tumor volume is 0.84
{'fraction_non_alphanumeric': 0.11849796888932924, 'fraction_numerical': 0.017041513920538987, 'mean_word_length': 6.346433770014556, '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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '454100', 'license': 'ADSL', 'n_tokens_mistral': 3615, 'n_tokens_neox': 3297, 'n_words': 976, 'path': 'pymaceuticals_starter.ipynb', 'repo_name': 'gjumaan/matplotlib-challenge', 'size': '10093'}