<jupyter_start><jupyter_text>Observations and Insights   1) The least number of taken measurements drug was Propriva. 2) There is a equal gender distribution in mice. 3) The correlation coefficient between mouse weight and average tumor volume is 0.84 .<jupyter_code># Dependencies and Setup
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
import scipy.stats as st
from scipy.stats import linregress
import sklearn.datasets as dta

# 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)
mouse_metadata
study_results
# Combine the data into a single dataset
df = mouse_metadata.merge(study_results, left_on = 'Mouse ID', right_on = 'Mouse ID', how = 'inner').drop_duplicates()

# Display the data table for preview
df
# Checking the number of mice = 249
mice_num = df['Mouse ID'].nunique()
print(f"the number of mice is {mice_num}.")
# Getting the duplicate mice by ID number that shows up for Mouse ID and Timepoint. 
dups_id_timepoint = df[['Mouse ID', 'Timepoint']]
dups_id_timepoint[dups_id_timepoint.duplicated()]
# Optional: Get all the data for the duplicate mouse ID. 
dups = df[df.duplicated(['Mouse ID','Timepoint'])]
dups
# Create a clean DataFrame by dropping the duplicate mouse by its ID.
cleaned_df = df[df['Mouse ID'] != 'g989']
cleaned_df
# Checking the number of mice in the clean DataFrame.
num_cleaned_mice = cleaned_df['Mouse ID'].nunique()

print(f"there are total {num_cleaned_mice} mice after cleaning the DataFrame")<jupyter_output>there are total 248 mice after cleaning the DataFrame<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
#mean
tumor_mean = round(cleaned_df['Tumor Volume (mm3)'].mean(),2)
print(f"mean of the tumar volume is {tumor_mean}")

#median
tumor_median = round(cleaned_df['Tumor Volume (mm3)'].median(),2)
print(f"median of the tumar volume is {tumor_median}")

#variance
tumor_variance = round(cleaned_df['Tumor Volume (mm3)'].var(),2)
print(f"median of the tumar volume is {tumor_variance}")

#standard deviation
tumor_std = round(cleaned_df['Tumor Volume (mm3)'].std(),2)
print(f"std of the tumar volume is {tumor_std}")

#SEM
tumor_sem = round(cleaned_df['Tumor Volume (mm3)'].sem(),2)
print(f"sem of the tumar volume is {tumor_sem}")

#Summary statistics table
summary_table = pd.DataFrame({'mean': [tumor_mean],
                'median':[tumor_median],
                'variance':[tumor_variance],
                'std':[tumor_std],
                'sem':[tumor_sem]})

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

drug_tumor_mean = cleaned_df.groupby('Drug Regimen')['Tumor Volume (mm3)'].mean()
drug_tumor_median = cleaned_df.groupby('Drug Regimen')['Tumor Volume (mm3)'].median()
drug_tumor_variance = cleaned_df.groupby('Drug Regimen')['Tumor Volume (mm3)'].var()
drug_tumor_std = cleaned_df.groupby('Drug Regimen')['Tumor Volume (mm3)'].std()
drug_tumor_sem = cleaned_df.groupby('Drug Regimen')['Tumor Volume (mm3)'].sem()

table1 = pd.DataFrame(drug_tumor_mean)
summary_table = table1.rename(columns={"Tumor Volume (mm3)": "Mean"})
summary_table["Median"] = drug_tumor_median
summary_table["Variance"] = drug_tumor_variance
summary_table["std"] = drug_tumor_std
summary_table["sem"] = drug_tumor_sem

summary_table
# 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_table = cleaned_df.groupby(df['Drug Regimen'])['Tumor Volume (mm3)'].agg(["mean",
                                                                                  "median",
                                                                                  "var",
                                                                                  "std",
                                                                                  "sem"])

summary_table<jupyter_output><empty_output><jupyter_text>Bar and Pie Charts<jupyter_code>plt.style.use('ggplot')
cleaned_df<jupyter_output><empty_output><jupyter_text>Bar chart<jupyter_code># Generate a bar plot showing the total number of measurements taken on each drug regimen using pandas.
df_bar = pd.DataFrame(cleaned_df['Drug Regimen'].value_counts())
df_bar.plot(figsize = (10, 5), 
                        kind = 'bar',
                        title = 'The total number of measurements taken on each drug regimen', 
                        xlabel = 'Drug Regimen', 
                        ylabel = 'Total Measurements');
# Generate a bar plot showing the total number of measurements taken on each drug regimen using pyplot.
df_bar = pd.DataFrame(cleaned_df['Drug Regimen'].value_counts()).reset_index()
df_bar_x = list(df_bar['index'])
df_bar_y = list(df_bar['Drug Regimen'])

fig1, ax1 = plt.subplots(figsize=(14,6))
ax1.bar(df_bar_x, df_bar_y);

ax1.set(xlabel = "Drug Regimen",
      ylabel = "Total Measurements",
      title = "The total number of measurements taken on each drug regimen");<jupyter_output><empty_output><jupyter_text>Pie Chart<jupyter_code># Dataset
pie_chart = cleaned_df[['Mouse ID','Sex']].drop_duplicates().groupby('Sex').count().rename(columns = {'Mouse ID' : 'mice gender distribution'})
pie_chart
# Generate a pie plot showing the distribution of female versus male mice using pandas
pie_chart.plot.pie(figsize=(5, 5),
            y='mice gender distribution',
            autopct="%1.1f%%",
            startangle=140)
# Generate a pie plot showing the distribution of female versus male mice using pyplot
#Dataset
pie_chart = pie_chart.reset_index()
labels = list(pie_chart['Sex'])
values = list(pie_chart['mice gender distribution'])
explode = (0.1, 0)

#Pie Chart
fig2, ax2 = plt.subplots(figsize = (5,5));
ax2.pie(values,
        explode=explode,
        labels = labels,
        autopct='%1.1f%%',
        startangle=140,
        shadow=True);<jupyter_output><empty_output><jupyter_text>Quartiles, Outliers and Boxplots<jupyter_code>cleaned_df.head()
# Calculate the final tumor volume of each mouse across four of the treatment regimens:  
# Capomulin, Ramicane, Infubinol, and Ceftamin
treatment = cleaned_df[cleaned_df['Drug Regimen'].isin(['Capomulin', 'Ramicane', 'Infubinol', 'Ceftamin'])]

# Start by getting the last (greatest) timepoint for each mouse
timepoint = pd.DataFrame(treatment.groupby(['Drug Regimen','Mouse ID'])['Timepoint'].max()).reset_index()

# Merge this group df with the original dataframe to get the tumor volume at the last timepoint
merge = timepoint.merge(cleaned_df, left_on = ['Drug Regimen','Mouse ID','Timepoint'], right_on = ['Drug Regimen','Mouse ID','Timepoint'])
merge
# Put treatments into a list for for loop (and later for plot labels)
treatments = list(merge['Drug Regimen'].unique())
treatments
# Create empty list to fill with tumor vol data (for plotting)
tumor = []

# 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 treatment in treatments:    
    tumor_vol = merge.loc[merge['Drug Regimen'] == treatment]['Tumor Volume (mm3)']
    # add subset
    tumor.append(tumor_vol)
        
    # Determine outliers using upper and lower bounds
    quartiles = tumor_vol.quantile([.25,.5,.75])
    lowerq = quartiles[0.25]
    upperq = quartiles[0.75]
    iqr = upperq-lowerq

    print(f"The lower quartile of tumor_vol is: {lowerq}")
    print(f"The upper quartile of tumor_vol is: {upperq}")
    print(f"The interquartile range of tumor_vol is: {iqr}")
    print(f"The the median of tumor_vol 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.")
    print(" ")
plt.style.available
plt.style.use('seaborn-pastel')
# Generate a box plot of the final tumor volume of each mouse across four regimens of interest
labels= ['Capomulin', 'Ceftamin', 'Infubinol', 'Ramicane']

fig3, ax3 = plt.subplots(figsize=(9,5));
ax3.set_title('Tumor Volume by Regimens');
ax3.set_ylabel('Tumor Volume (mm3)');
ax3.boxplot(tumor,
            patch_artist=True,
            labels=labels);

plt.show();<jupyter_output><empty_output><jupyter_text>Line and Scatter Plots<jupyter_code>cleaned_df<jupyter_output><empty_output><jupyter_text>line chart<jupyter_code>plt.style.use('ggplot')
# Generate a line plot of tumor volume vs. time point for a mouse treated with Capomulin
#dataset
Capomulin = cleaned_df[cleaned_df['Drug Regimen'] == 'Capomulin'][['Mouse ID','Tumor Volume (mm3)','Timepoint']]

mouse = Capomulin[Capomulin['Mouse ID'] == 's185']
mouse

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

#lince chart
fig4, ax4 = plt.subplots();
ax4.plot(x_axis,y_axis, color='red', marker='o');
ax4.set(title = "A mouse tumor volume vs. time point",
        xlabel= "Timepoint",
        ylabel = "Tumor Volume (mm3)",
        ylim = (min(y_axis) - 3, max(y_axis) + 3)
       );<jupyter_output><empty_output><jupyter_text>Scatter Plot<jupyter_code># Generate a scatter plot of average tumor volume vs. mouse weight for the Capomulin regimen
# Dataset
avg_capo = cleaned_df.loc[cleaned_df['Drug Regimen'] == 'Capomulin'].groupby(['Mouse ID']).agg('mean').reset_index().rename(columns = {'Tumor Volume (mm3)': 'avg_tumor_vol'})

#axis
x_axis = list(avg_capo['Weight (g)'])
y_axis = list(avg_capo['avg_tumor_vol'])

# scatter plot
fig5, ax5 = plt.subplots(figsize=(10,7));
ax5.scatter(x_axis, y_axis, color='blue', marker="o");
ax5.set(title = "Capomulin average tumor volume vs. mouse weight",
        xlabel = "Weight (g)",
        ylabel = "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
#Dataset
avg_capo

#axis
x_axis = avg_capo['Weight (g)']
y_axis = avg_capo['avg_tumor_vol']

#correlation coefficient
print(f"The correlation coefficient between mouse weight and average tumor volume is {round(st.pearsonr(x_axis,y_axis)[0],2)}")<jupyter_output>The correlation coefficient between mouse weight and average tumor volume is 0.84<jupyter_text>linear regression model<jupyter_code>#linear regression model
(slope, intercept, rvalue, pvalue, stderr) = linregress(x_axis, y_axis)
regress_values = x_axis * slope + intercept
line_eq = "y = " + str(round(slope,2)) + "x + " + str(round(intercept,2))

# scatter plot
fig5, ax5 = plt.subplots(figsize = (10,7));
ax5.scatter(x_axis, y_axis, color='blue', marker="*");
ax5.plot(x_axis,regress_values,"r-");
ax5.annotate(line_eq,(22,40),fontsize=15,color="red");
ax5.set(title = "Capomulin average tumor volume vs. mouse weight",
        xlabel = "Weight (g)",
        ylabel = "Tumor Volume (mm3)");
print(f"The r-squared is: {rvalue**2}")

plt.show();<jupyter_output>The r-squared is: 0.7088568047708717
{'fraction_non_alphanumeric': 0.12003131251630861, 'fraction_numerical': 0.013220840219187614, 'mean_word_length': 4.415920866698069, 'pattern_counts': {'":': 1, '<': 42, '<?xml version=': 0, '>': 42, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 12, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '11', 'dir': 'jupyter-structured-clean-dedup', 'id': '358475', 'license': 'ADSL', 'n_tokens_mistral': 3928, 'n_tokens_neox': 3660, 'n_words': 1043, 'path': 'Pymaceuticals/pymaceuticals_starter.ipynb', 'repo_name': 'hej6853/Matplotlib_Challenge_The-Power-of-Plots', 'size': '11497'}