<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
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
data_combined= pd.merge(mouse_metadata,study_results, on="Mouse ID", how="outer")
data_combined
# Checking the number of mice in the DataFrame.
number_mouse_df= data_combined["Mouse ID"].nunique()
number_mouse_df
# Getting the duplicate mice by ID number that shows up for Mouse ID and Timepoint. 
un_data=mouse_metadata ["Mouse ID"].value_counts()
unq_data= study_results["Mouse ID"].value_counts()
# Optional: Get all the data for the duplicate mouse ID. 
duplicate_data_mouse= mouse_metadata.loc[mouse_metadata["Mouse ID"]=="g989",:]
duplicate_data_result= study_results.loc[study_results["Mouse ID"]=="g989",:]
# Create a clean DataFrame by dropping the duplicate mouse by its ID.
duplicate_data_mouse= mouse_metadata.loc[mouse_metadata["Mouse ID"]!="g989",:]
duplicate_data_result= study_results.loc[study_results["Mouse ID"]!="g989",:]
# Checking the number of mice in the clean DataFrame.
clean_combined_data= pd.merge(duplicate_data_mouse,duplicate_data_result, on="Mouse ID", how="outer")
clean_combined_data.dropna(how="any")
number_mouse_clean_data= clean_combined_data["Mouse ID"].count()
number_mouse_clean_data<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_data= clean_combined_data["Tumor Volume (mm3)"]
average_tumor_volume= np.mean(tumor_volume_data)
mode_tumor_volume= st.mode(tumor_volume_data)
median_tumor_volume=np.median(tumor_volume_data)
variance_tumor_volume= np.var(tumor_volume_data, ddof=0)
STD_tumor_volume= np.std(tumor_volume_data, ddof=0)
SEM_tumor_volume=st.sem(tumor_volume_data, ddof=0)
average_tumor_volume
# This method is the most straightforward, creating multiple series and putting them all together at the end.
print(f"the mean of the tumor volume for each regimen: Mean= {average_tumor_volume}")
print(f"The mode of the tumor volume for each regimen: Mode= {mode_tumor_volume}")
print(f"The median of the tumor volume for each regimen: Median= {median_tumor_volume}")
print(f"The variance of the tumor volume for each regimen: Variance= {variance_tumor_volume}")
print(f"the variance of the tumor volume for each regimen: STD= {STD_tumor_volume}")
print(f"The standard Error of the Mean of the tumor volume for each regimen is: SEM= {SEM_tumor_volume}")

Summary_PD=pd.DataFrame({"mean": round(average_tumor_volume,2),
          " standard deviation":round(STD_tumor_volume,2),
          "variance":round(variance_tumor_volume,2),
          "mode" : mode_tumor_volume,
          " median":round(median_tumor_volume,2),
          " standard error of the mean" :round(SEM_tumor_volume,2)})
Summary_PD
# Generate a summary statistics table of mean, median, variance, standard deviation, and SEM of the tumor volume for each regimen
mean= clean_combined_data["Tumor Volume (mm3)"].mean()
mode=clean_combined_data["Tumor Volume (mm3)"].mode()
median=clean_combined_data["Tumor Volume (mm3)"].median()
var=clean_combined_data["Tumor Volume (mm3)"].var()
std=clean_combined_data["Tumor Volume (mm3)"].std()
sem=clean_combined_data["Tumor Volume (mm3)"].sem()
# This method produces everything in a single groupby function.
Summary_PD=pd.DataFrame({"mean": round(mean,2),
          " standard deviation":round(std,2),
          "variance":round(var,2),              
          "mode" : mode,
          " median":round(median,2),
          " standard error of the mean" :round(sem,2)})
Summary_PD<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.
Group_table_IT=clean_combined_data.groupby(["Drug Regimen"] )
Timepoint_df= Group_table_IT["Mouse ID"].count()
Timepoint_df

Timepoint_df.plot(kind="bar",color="r",figsize=(8,3) )
plt.xlabel("Treatment")
plt.ylabel("number of mice")
plt.title("number of mice per time point for each treatment")
Graph_data= Group_table_IT["Mouse ID"].count()

list_data= list(Graph_data)
x_axis= np.arange(len(list_data))
list_data
# Generate a bar plot showing the number of mice per time point for each treatment throughout the course of the study using pyplot.
data_tick=[value for value in x_axis]
plt.figure(figsize=(10,4))
plt.bar(x_axis,list_data, color="b", align="center")
plt.xticks(data_tick, ["Capomulin","Ceftamin","Infubinol","Ketapril","Naftisol","Placebo","Propriva","Ramicane","Stelasyn","Zoniferol"])<jupyter_output><empty_output><jupyter_text>Pie Plots<jupyter_code># Generate a pie plot showing the distribution of female versus male mice using pandas
Group_table_gender=clean_combined_data["Sex"].value_counts()
num_gender= list(Group_table_gender)
labels=("Male","Female")
colors=("red","lightskyblue")
Group_table_gender.plot(kind="pie",colors=colors, autopct="%.2f%%", fontsize=14,startangle= 140)
# Generate a pie plot showing the distribution of female versus male mice using pyplot
explode=(.1,0)
plt.pie(num_gender, colors=colors, labels=labels,explode=explode, autopct="%.2f%%",startangle= 55)
plt.title("pie plot distribution of gender")<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. 
sort_data_ID= clean_combined_data.sort_values(["Drug Regimen","Timepoint","Tumor Volume (mm3)"], ascending=True)
final_value= clean_combined_data.loc[clean_combined_data["Timepoint"]==45]
final_value
# Generate a box plot of the final tumor volume of each mouse across four regimens of interest
ramicane_regimen= final_value.loc[final_value["Drug Regimen"]=="Ramicane"]["Tumor Volume (mm3)"]
ramicane_regimen
fig, ax1= plt.subplots()
ax1.set_title("final tumor volume box plot" )
ax1.set_ylabel("Ramicane final volume")
ax1.boxplot(ramicane_regimen)
plt.show()
list(ramicane_regimen)
#  quartile calculations
quartiles = ramicane_regimen.quantile([.25,.5,.75])
lowerq = quartiles[0.25]
upperq = quartiles[0.75]
iqr = upperq-lowerq

print(f"The lower quartile of temperatures is: {lowerq}")
print(f"The upper quartile of temperatures is: {upperq}")
print(f"The interquartile range of temperatures is: {iqr}")
print(f"The the median of temperatures is: {quartiles[0.5]} ")

lower_bound = lowerq - (1.5*iqr)
upper_bound = upperq + (1.5*iqr)
print(f"Values below {lower_bound} could be outliers.")
print(f"Values above {upper_bound} could be outliers.")
Capomulin_regimen= final_value.loc[final_value["Drug Regimen"]=="Capomulin"]["Tumor Volume (mm3)"]
Capomulin_regimen
fig, ax1= plt.subplots()
ax1.set_title("final tumor volume box plot" )
ax1.set_ylabel("Ramicane final volume")
ax1.boxplot(Capomulin_regimen)
plt.show()
Ceftamin_regimen= final_value.loc[final_value["Drug Regimen"]=="Ceftamin"]["Tumor Volume (mm3)"]
Ceftamin_regimen
fig, ax1= plt.subplots()
ax1.set_title("final tumor volume box plot" )
ax1.set_ylabel("Ramicane final volume")
ax1.boxplot(Ceftamin_regimen)
plt.show()
quartiles = Ceftamin_regimen.quantile([.25,.5,.75])
lowerq = quartiles[0.25]
upperq = quartiles[0.75]
iqr = upperq-lowerq

print(f"The lower quartile of temperatures is: {lowerq}")
print(f"The upper quartile of temperatures is: {upperq}")
print(f"The interquartile range of temperatures is: {iqr}")
print(f"The the median of temperatures is: {quartiles[0.5]} ")

lower_bound = lowerq - (1.5*iqr)
upper_bound = upperq + (1.5*iqr)
print(f"Values below {lower_bound} could be outliers.")
print(f"Values above {upper_bound} could be outliers.")
Infubinol_regimen= final_value.loc[final_value["Drug Regimen"]=="Infubinol"]["Tumor Volume (mm3)"]
Infubinol_regimen
fig, ax1= plt.subplots()
ax1.set_title("final tumor volume box plot" )
ax1.set_ylabel("Ramicane final volume")
ax1.boxplot(Infubinol_regimen)
plt.show()
quartiles = Ceftamin_regimen.quantile([.25,.5,.75])
lowerq = quartiles[0.25]
upperq = quartiles[0.75]
iqr = upperq-lowerq

print(f"The lower quartile of temperatures is: {lowerq}")
print(f"The upper quartile of temperatures is: {upperq}")
print(f"The interquartile range of temperatures is: {iqr}")
print(f"The the median of temperatures is: {quartiles[0.5]} ")

lower_bound = lowerq - (1.5*iqr)
upper_bound = upperq + (1.5*iqr)
print(f"Values below {lower_bound} could be outliers.")
print(f"Values above {upper_bound} could be outliers.")<jupyter_output>The lower quartile of temperatures is: 61.43389223
The upper quartile of temperatures is: 67.52748237
The interquartile range of temperatures is: 6.093590140000003
The the median of temperatures is: 64.29983003 
Values below 52.29350701999999 could be outliers.
Values above 76.66786758 could be outliers.<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_select=  clean_combined_data.loc[clean_combined_data["Drug Regimen"]=="Capomulin"]
Capomulin_select
Capomulin_volume=Capomulin_select[["Timepoint","Tumor Volume (mm3)"]]
Capomulin_volume
Capomulin_volume.plot(kind="line",color="g",figsize=(10,5))
select_Capomulin=Capomulin_select.loc[Capomulin_select["Mouse ID"]=="s185"]
select_Capomulin
reduced_Capomulin_volume=select_Capomulin[["Timepoint","Tumor Volume (mm3)"]]
reduced_Capomulin_volume
reduced_Capomulin_volume["Tumor Volume (mm3)"].plot(kind="line",color="y",figsize=(10,5))
plt.title("line plot of time point versus tumor volume ")
plt.ylabel("Tumor Volume")
plt.xlabel("time point")
plt.legend()
#extracting Weight (g) and finding the average tumor volume
Capomulin_select=  clean_combined_data.loc[clean_combined_data["Drug Regimen"]=="Capomulin"].groupby(["Mouse ID","Weight (g)"])
average_volume=Capomulin_select["Tumor Volume (mm3)"].mean().reset_index()
Average_data_V= pd.DataFrame(average_volume)
average_graph= Average_data_V.rename(columns={"Tumor Volume (mm3)":"Average Volume (mm3)"})
average_graph
# Generate a scatter plot of mouse weight versus average tumor volume for the Capomulin regimen
average_graph.plot(kind="scatter", x="Weight (g)", y="Average Volume (mm3)", grid=True, figsize=(8,8), title="scatter plot of mouse weight versus average tumor volume")
plt.show()<jupyter_output><empty_output><jupyter_text>Correlation and Regression<jupyter_code># Calculate the correlation coefficient and linear regression model 

weight =average_graph.iloc[:,1]
average_volume =average_graph.iloc[:,2]
correlation = st.pearsonr(weight,average_volume)
print(f"The correlation between both factors is {round(correlation[0],2)}")

# for mouse weight and average tumor volume for the Capomulin regimen
x_values =average_grath['Weight (g)']
y_values = average_grath['Average 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)
plt.plot(x_values,regress_values,"r-")
plt.annotate(line_eq,(6,10),fontsize=15,color="red")
plt.xlabel('Weight (g)')
plt.ylabel('Average Volume (mm3)')
plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12057946314443971, 'fraction_numerical': 0.019173412867490414, 'mean_word_length': 7.560175054704596, 'pattern_counts': {'":': 9, '<': 28, '<?xml version=': 0, '>': 28, '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': '365096', 'license': 'ADSL', 'n_tokens_mistral': 4200, 'n_tokens_neox': 3878, 'n_words': 997, 'path': 'Pymaceuticals/pymaceuticals_starter.ipynb', 'repo_name': 'danykoud/Matplotlib_Challenge', 'size': '11735'}