<jupyter_start><jupyter_text>Observations and Insights  1)Final Tumor Volumes were significantly smaller for mice treated with Capomulin and Ramicane versus mice treated with Infubinol and Ceftamin.2)Average Tumor Volume for Ketapril was highest across the study, indicating that this treatment may have been least effective.3)Of the 4 regimens, only Infubinol had an outlier for Final Tumor Volume.4)There is a strong correlation(.82) between Mouse Weight and Average Tumor Volume in the study for Capomulin.5)The sex of the mice was near even. But more analysis could be done to see if sex plays a factor in Final Tumor Volume.<jupyter_code># Dependencies and Setup
import matplotlib.pyplot as plt
import pandas as pd
import scipy.stats as st
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

combine_data=pd.merge(mouse_metadata, study_results, on='Mouse ID', how='left')

# Display the data table for preview
combine_data.head()
#len(combine_data['Mouse ID'])


# Checking the number of mice.

len(combine_data)
combine_data.sort_values(['Mouse ID','Timepoint'],ascending=True).head()

combine_data.info()

# Getting the duplicate mice by ID number that shows up for Mouse ID and Timepoint. 

duplicate_data=combine_data[combine_data.duplicated(['Mouse ID','Timepoint'])]
duplicate_data

# Optional: Get all the data for the duplicate mouse ID. 

duplicate_data['Mouse ID'].unique()
combine_data.loc[combine_data['Mouse ID']=='g989']
# Create a clean DataFrame by dropping the duplicate mouse by its ID.
combine_data_clean=combine_data.loc[combine_data['Mouse ID'] != 'g989']
combine_data_clean
# Checking the number of mice in the clean DataFrame.
len(combine_data_clean['Mouse ID'].unique())<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

# This method is the most straighforward, creating multiple series and putting them all together at the end.


groupByData = combine_data_clean.groupby(["Drug Regimen"])
groupByData
summaryDF = pd.DataFrame({
    "Mean": groupByData["Tumor Volume (mm3)"].mean().map('{:.2f}'.format),
    "Median": groupByData["Tumor Volume (mm3)"].median().map('{:.2f}'.format),
    "Variance": groupByData["Tumor Volume (mm3)"].var().map('{:.2f}'.format),
    "Standard Variance": groupByData["Tumor Volume (mm3)"].std().map('{:.2f}'.format),
    "SEM": groupByData["Tumor Volume (mm3)"].sem().map('{:.2f}'.format)
})

summaryDF.head()
# 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

groupby_regimen=combine_data.groupby('Drug Regimen')
aggregate = groupby_regimen.agg(['mean','median','var','std','sem'])["Tumor Volume (mm3)"]
#aggregate['mean'].map('{:.2f}'.format)
aggregate<jupyter_output><empty_output><jupyter_text>Bar and Pie Charts<jupyter_code># Generate a bar plot showing the total number of mice for each treatment throughout the course of the study using pandas. 
Total_mice_eachregimen1=pd.DataFrame(combine_data_clean.groupby('Drug Regimen').count()['Mouse ID'])
#Total_mice_eachregimen.reset_index(inplace=True)
#Total_mice_eachregimen.style.hide_index(inplace=True)
Total_mice_eachregimen1.plot(kind="bar", figsize=(5,3),rot=50)
plt.title("Total number of mice for each treatment regimen")
plt.xlabel("Drug Regimen")
plt.ylabel("Number of mice")
plt.savefig("../Images/pandas_bar.png")
plt.show()
plt.tight_layout()

# Generate a bar plot showing the total number of mice for each treatment throughout the course of the study using pyplot.

Total_mice_eachregimen=pd.DataFrame(combine_data.groupby('Drug Regimen').count()['Mouse ID'])
Total_mice_eachregimen.reset_index(inplace=True)
#x_axis=[]
#x_axis=Total_mice_eachregimen2['Drug Regimen
x_axis=np.arange(len(Total_mice_eachregimen['Drug Regimen']))
tickLocations = [value for value in x_axis]
y_axis=Total_mice_eachregimen['Mouse ID']
plt.bar(x_axis, y_axis, color="b", align="center")
plt.xticks(tickLocations, Total_mice_eachregimen['Drug Regimen'], rotation="vertical")

plt.title("Total number of mice for each treatment regimen")
plt.xlabel("Drug Regimen")
plt.ylabel("Number of mice")
plt.savefig("../Images/pyplot_bar.png")
plt.show()
plt.tight_layout()

miceCount = combine_data_clean["Sex"].value_counts()
miceCount
# Generate a pie plot showing the distribution of female versus male mice using pandas

plt.figure()
miceCount.plot(kind="pie",rot=50, autopct='%1.2f%%', startangle=140)
plt.tight_layout()
plt.axis("equal")
plt.title("Distribution of female versus male mice")
plt.tight_layout()
plt.show()
plt.savefig("../Images/pandas_pie.png")
# Generate a pie plot showing the distribution of female versus male mice using pyplot

plt.pie(miceCount.values, explode=(0.1,0), labels=miceCount.index.values, colors=["red","blue"],
        autopct="%1.2f%%", shadow=True, startangle=150)
plt.axis("equal")
plt.title("Distribution of female versus male mice")
plt.show()
plt.savefig("../Images/pyplot_pie.png")<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
regimen_data = combine_data_clean[(combine_data_clean["Drug Regimen"] == "Capomulin") | 
                          (combine_data_clean["Drug Regimen"] == "Ramicane") | 
                          (combine_data_clean["Drug Regimen"] == "Infubinol") | 
                          (combine_data_clean["Drug Regimen"] == "Ceftamin")]


# Start by getting the last (greatest) timepoint for each mouse
tumor_volume_df = regimen_data.groupby(regimen_data['Mouse ID']).agg({'Timepoint':['max']})
tumor_volume_df.columns = ['Timepoint']
tumor_volume_df

# Merge this group df with the original dataframe to get the tumor volume at the last timepoint
tumor_volume_df = tumor_volume_df.reset_index()
tumor_vol_final_df = pd.merge(tumor_volume_df, combine_data_clean, how="left", on=["Mouse ID", "Timepoint"])
tumor_vol_final_df

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

treatments = ['Capomulin', 'Ramicane', 'Infubinol', 'Ceftamin']


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


# Calculate the IQR and quantitatively determine if there are any potential outliers. 
    
    # Locate the rows which contain mice on each drug and get the tumor volumes
for drug in treatments:
    tumor_vol_by_drug = tumor_vol_final_df['Tumor Volume (mm3)'].loc[tumor_vol_final_df['Drug Regimen'] == drug]   
    
    # add subset 
    
    
    # Determine outliers using upper and lower bounds
    quartiles = tumor_vol_by_drug.quantile([.25,.5,.75])
    lowerq = quartiles[0.25]
    upperq = quartiles[0.75]
    iqr = upperq-lowerq
    print(f'For {drug}, Interquartile Range (IQR) is {iqr}')
    lower_bound = lowerq - (1.5*iqr)
    upper_bound = upperq + (1.5*iqr)
    print(f'For {drug}, values below {lower_bound} could be outliers')
    print(f'For {drug}, values above {upper_bound} could be outliers\n')
    tumor_volumes.append(tumor_vol_by_drug)    
    
# Generate a box plot of the final tumor volume of each mouse across four regimens of interest
fig, ax = plt.subplots()
ax.set_title('Final Tumor Volume per Regimen')
ax.set_xticklabels(treatments)
ax.set_ylabel('Tumor Volume (mm3)')
ax.boxplot(tumor_volumes, flierprops=dict(markerfacecolor='g', marker='D'))
plt.show()
plt.savefig("../Images/boxplot.png")<jupyter_output><empty_output><jupyter_text>Line and Scatter Plots<jupyter_code>combine_data_clean.loc[combine_data_clean['Drug Regimen']=='Capomulin'].head(20)
# Generate a line plot of time point versus tumor volume for a mouse treated with Capomulin
s185_mouse = combine_data_clean[['Timepoint', 'Tumor Volume (mm3)']].loc[(combine_data_clean['Drug Regimen'] == 'Capomulin') & (combine_data_clean['Mouse ID']=='s185')]
s185_mouse

plt.plot(s185_mouse['Timepoint'], s185_mouse['Tumor Volume (mm3)'], marker='o')
plt.title("Capomulin Regimen - Mouse (k403)")
plt.ylabel("Tumor Volume (mm3)")
plt.xlabel("Timepoint")
plt.show()
plt.savefig("../Images/lineplot.png")
# Generate a scatter plot of mouse weight versus average tumor volume for the Capomulin regimen

capomulin_avg_tumor_wgt = combine_data_clean.loc[combine_data_clean['Drug Regimen'] == 'Capomulin'].groupby(combine_data_clean['Timepoint']).agg({'Tumor Volume (mm3)':['mean'], 'Weight (g)':['mean']})
capomulin_avg_tumor_wgt.columns = ['Average Tumor Volume {mm3}', 'Average Mouse Weight (g)']
capomulin_avg_tumor_wgt.columns
weight=capomulin_avg_tumor_wgt['Average Mouse Weight (g)']
volume=capomulin_avg_tumor_wgt['Average Tumor Volume {mm3}']
plt.scatter(weight,volume)
plt.title('Avg Tumor Volume vs. Avg Mouse Weight')
plt.xlabel('Mouse Weight')
plt.ylabel('Tumor Volume')
plt.ylim(35,46)
plt.show()
plt.savefig("../Images/scatterplot.png")<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(volume,weight)
print(f'The correlation between Average Tumor Volume and Mouse Weight is {round(correlation[0],2)}')

# linear regression
(slope, intercept, rvalue, pvalue, stderr) = st.linregress(weight,volume)
line_eq = "y = " + str(round(slope,2)) + "x + " + str(round(intercept,2))
print(f'The linear regression equation is {line_eq}')

# plot line with scatter
volume = capomulin_avg_tumor_wgt['Average Tumor Volume {mm3}']
weight = capomulin_avg_tumor_wgt['Average Mouse Weight (g)']
plt.scatter(weight,volume)
plt.title('Avg Tumor Volume vs. Avg Mouse Weight')
plt.xlabel('Mouse Weight')
plt.ylabel('Tumor Volume')
plt.ylim(35,46)

# calculate regression values
reg_values = weight * slope + intercept
plt.plot(weight, reg_values, "r-")
plt.annotate(line_eq,(19.95,38),fontsize=15,color="red")
plt.show()
plt.savefig("../Images/regression.png")<jupyter_output>The correlation between Average Tumor Volume and Mouse Weight is 0.82
The linear regression equation is y = 20.29x + -364.52
{'fraction_non_alphanumeric': 0.1230535510824155, 'fraction_numerical': 0.011773642233194076, 'mean_word_length': 6.102494942683749, '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': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '105787', 'license': 'ADSL', 'n_tokens_mistral': 3718, 'n_tokens_neox': 3397, 'n_words': 931, 'path': 'Pymaceuticals/pymaceuticals.ipynb', 'repo_name': 'fasahathsyeda/Matplotlib-challenge', 'size': '10532'}