<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
from scipy.stats import iqr
from scipy.stats import linregress
import numpy as np
# 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
#https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.merge.html
df = pd.merge(study_results, mouse_metadata, how = 'outer', on = 'Mouse ID') 
# Display the data table for preview
df.head()

# Checking the number of mice.
n_mice = df['Mouse ID'].nunique()
n_mice
# Create a clean DataFrame by dropping the duplicate mouse by its ID.
#https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.drop_duplicates.html#pandas.DataFrame.drop_duplicates
df_clean = df.drop_duplicates(subset= ('Mouse ID', 'Timepoint'))
df_clean
# Checking the number of mice in the clean DataFrame.
n_mice = df_clean['Mouse ID'].nunique()
n_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
#https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.html

# Use groupby and summary statistical methods to calculate the following properties of each drug regimen: 
drug_stat_df = df_clean.groupby('Drug Regimen')
# mean, median, variance, standard deviation, and SEM of the tumor volume. 
mean = drug_stat_df['Tumor Volume (mm3)'].mean()
median = drug_stat_df['Tumor Volume (mm3)'].median()
variance = drug_stat_df ['Tumor Volume (mm3)'].var()
stdev = drug_stat_df['Tumor Volume (mm3)'].std()
sem = drug_stat_df['Tumor Volume (mm3)'].sem()

# Assemble the resulting series into a single summary dataframe.
tumor_summary_df = pd.DataFrame({'Mean': mean, 'Median': median, 'Variance': variance,
                                    'Standard Deviation': stdev, 'SEM': sem})

tumor_summary_df.head()<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.
#https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.plot.bar.html

bar_plot = df_clean['Drug Regimen'].value_counts().plot.bar(width=0.7, zorder=3, color=('blue'))
# Generate a bar plot showing the total number of measurements taken on each drug regimen using pyplot.


# Generate a pie plot showing the distribution of female versus male mice using pandas
#https://stackoverflow.com/questions/38337918/plot-pie-chart-and-table-of-pandas-dataframe
m_f = df_clean['Sex'].value_counts()
plot = m_f.plot.pie(figsize=(5, 5),  autopct="%1.1f%%")

# Generate a pie plot showing the distribution of female versus male mice using pyplot<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
gtimept = pd.DataFrame(df_clean.groupby('Mouse ID')['Timepoint'].max())

# Merge this group df with the original dataframe to get the tumor volume at the last timepoint
mergedclean = pd.merge(gtimept, df_clean, on=['Mouse ID', 'Timepoint'], how='left')
mergedclean
# Put treatments into a list for for loop (and later for plot labels)
drug_regimen = ['Capomulin']

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

# Calculate the IQR and quantitatively determine if there are any potential outliers. 
#https://www.kite.com/python/answers/how-to-convert-a-column-in-a-pandas-dataframe-to-a-numpy-array-in-python
#https://stackoverflow.com/questions/53337391/how-do-you-calculate-interquartile-range-iqr-correctly-using-python
#https://stackoverflow.com/questions/23228244/how-do-you-find-the-iqr-in-numpy
drug_df = mergedclean.loc[mergedclean['Drug Regimen'] == 'Capomulin']
drug_df
t_arr = drug_df.loc[:,'Tumor Volume (mm3)']
numbers = t_arr.values
upperq, lowerq = np.percentile(numbers, [75 ,25])
quart = iqr(numbers, rng=(25,75))
print(f'IQR for {drug_regimen}: {quart}')
    # Determine outliers using upper and lower bounds
lower_bound = lowerq - (1.5*quart)
upper_bound = upperq + (1.5*quart)
print(f'Lower Bound for {drug_regimen}: {lower_bound}')
print(f'Upper Bound for {drug_regimen}: {upper_bound}') 
outliers_count = ([(drug_df.loc[:,'Tumor Volume (mm3)'] >= upper_bound) | 
                 (drug_df.loc[:,'Tumor Volume (mm3)'] <= lower_bound)]).count()
print(f'Number of {drug} outliers: {outliers_count}\n')
# Generate a box plot of the final tumor volume of each mouse across four regimens of interest
data = (20.70, 7.78, 51.83)
 
fig = plt.figure(figsize =(10, 7))
 
# Creating plot
plt.boxplot(data)
plt.xticks([1], ['Capomulin'])
 
# show plot
plt.show()<jupyter_output><empty_output><jupyter_text>Line and Scatter Plots<jupyter_code>mouse = df_clean.loc[df_clean['Mouse ID'] == 'b128']
mouse

# Generate a line plot of tumor volume vs. time point for a mouse treated with Capomulin
plt.plot(mouse['Timepoint'], mouse['Tumor Volume (mm3)'])
plt.title("Mouse b128 Tumor Volume Over Time w/ Capomulin")
plt.xlabel("Timepoint")
plt.ylabel("Tumor Volume (mm3)")
# Generate a scatter plot of average tumor volume vs. mouse weight for the Capomulin regimen
cap_drug = df_clean.loc[df_clean['Drug Regimen'] == 'Capomulin']
cap_drug

ave_cap =  cap_drug.groupby('Mouse ID').mean()
ave_cap
x_values = ave_cap['Weight (g)']
y_values = ave_cap['Tumor Volume (mm3)']

plt.scatter(x_values, y_values, marker="o", facecolors="blue",
           edgecolors ="black")

plt.title('Mouse Weight vs. Average Tumor Volume for Capomulin')
plt.xlabel('Mouse Weight (g)')
plt.ylabel('Tumor Volume (mm3)')

plt.show()<jupyter_output><empty_output><jupyter_text>Correlation and Regression<jupyter_code>#https://docs.scipy.org/doc/scipy/reference/generated/scipy.stats.linregress.html
x_values = ave_cap['Weight (g)']
y_values = ave_cap['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, marker="o", facecolors="blue",
           edgecolors ="black")
plt.plot(x_values,regress_values,"r-")
plt.annotate(line_eq,(20,38),fontsize=15,color="red")
plt.title('Mouse Weight vs. Average Tumor Volume for Capomulin')
plt.xlabel('Mouse Weight (g)')
plt.ylabel('Tumor Volume (mm3)')
plt.show()
# Observations:

# 1) There is a positive correlation between a mouse's weight in grams and the tumor volume (mm3).

# 2) Mouse b128, treated with the Capomulin drug regimen, 
#showed a significant decrease in tumor volume between 
#datapoint 6 (timepoint 30 ) to datapoint 5 (timepoint 35).<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12466954222902463, 'fraction_numerical': 0.013635731181299569, 'mean_word_length': 5.588450962419798, 'pattern_counts': {'":': 0, '<': 26, '<?xml version=': 0, '>': 26, 'https://': 9, 'lorem ipsum': 0, 'www.': 1, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '153331', 'license': 'ADSL', 'n_tokens_mistral': 2601, 'n_tokens_neox': 2405, 'n_words': 663, 'path': 'Pymaceuticals/pymaceuticals_starter.ipynb', 'repo_name': 'SyedAli2021/Matplotlib_challange', 'size': '7187'}