<jupyter_start><jupyter_text>Import Libraries<jupyter_code>import numpy as np
import pandas as pd
import sklearn
from sklearn.datasets import load_breast_cancer
import matplotlib.pyplot as plt

plt.style.use('dark_background')
pd.set_option('display.max_columns', 32)<jupyter_output><empty_output><jupyter_text>Load Dataset<jupyter_code>breast_cancer = load_breast_cancer()

print('Features name:', breast_cancer.feature_names)
print('Target:', breast_cancer.target_names)
print('Dataset size:', breast_cancer.data.shape)
breast_cancer_df = pd.DataFrame(breast_cancer.data, columns=breast_cancer.feature_names)
breast_cancer_df['Target'] = breast_cancer.target
breast_cancer_df['Target'] = ['Malignant' if type==0 else 'Benign' for type in breast_cancer_df['Target']]
breast_cancer_df.head()
breast_cancer_df.tail()<jupyter_output><empty_output><jupyter_text>Data Visualization<jupyter_code>with plt.style.context('dark_background'):
    color = {'Malignant':'tab:red', 'Benign':'tab:green'}
    plt.figure(figsize=(10,5))
    for tumor_type in breast_cancer_df['Target'].unique():
        plt.scatter(breast_cancer_df[breast_cancer_df['Target']==tumor_type]['mean radius'],
                   breast_cancer_df[breast_cancer_df['Target']==tumor_type]['mean texture'],
                   c=color[tumor_type],
                   s=200,
                   alpha=0.6,
                   label=tumor_type,
                   )
        
    plt.xlabel('Mean Radius')
    plt.ylabel('Mean Texture')
    plt.title('Mean Radius vs Mean Texture Scatter Plot')
    plt.legend(title='Tumor Type', loc='best')
with plt.style.context('dark_background'):
    avg_radius_per_tumor_type = breast_cancer_df.groupby(by='Target').mean()[['mean radius']]
    
    plt.figure(figsize=(5,5))
    plt.bar(avg_radius_per_tumor_type.index,
            avg_radius_per_tumor_type['mean radius'],
            color='tab:blue',
            width=0.6
           )
    plt.ylabel('Mean Radius')
    plt.title('Average mean radius per tumor type')
with plt.style.context('dark_background'):
    plt.figure(figsize=(6,4))
    plt.hist(breast_cancer_df['mean radius'], color='tab:orange')
    plt.ylabel('mean radius')
    plt.title('Mean Radius Distribution')
import klib

klib.dist_plot(breast_cancer_df['mean radius'], font_kws={"color": "#ffffff","alpha": 1})
plt.show()
import matplotlib.gridspec as gridspec

def create_figure(plot1_f1, plot1_f2, plot2_f, plot3_f):
    with plt.style.context('dark_background'):
        fig = plt.figure(constrained_layout=True, figsize=(10,8))
        specs = gridspec.GridSpec(ncols=2, nrows=2, figure=fig)
        
        ax1 = fig.add_subplot(specs[0, :])
        ax2 = fig.add_subplot(specs[1, 0])
        ax3 = fig.add_subplot(specs[1, 1])
        
        color = {'Malignant':'tab:red', 'Benign':'tab:green'}
        for tumor_type in breast_cancer_df['Target'].unique():
            ax1.scatter(breast_cancer_df[breast_cancer_df['Target']==tumor_type][plot1_f1],
                        breast_cancer_df[breast_cancer_df['Target']==tumor_type][plot1_f2],
                        c=color[tumor_type],
                        s=200,
                        alpha=0.6,
                        label=tumor_type,
                       )
            
        ax1.set_xlabel(plot1_f1)
        ax1.set_ylabel(plot1_f2)
        ax1.set_title('{} vs {} Scatter Plot'.format(plot1_f1, plot1_f2))
        ax1.legend(title='Tumor Type', loc='best')
        
        avg_radius_per_tumor_type = breast_cancer_df.groupby(by='Target').mean()[[plot2_f]]
        ax2.bar(avg_radius_per_tumor_type.index,
                avg_radius_per_tumor_type[plot2_f],
                color='tab:blue',
                width=0.6,
               )
        ax2.set_ylabel(plot2_f)
        ax2.set_title('Average {} per tumor type'.format(plot2_f))
        
        ax3.hist(breast_cancer_df[plot3_f], color='tab:orange')
        ax3.set_ylabel(plot3_f)
        ax3.set_title('{} distribution'.format(plot3_f))
        
        plt.close(fig)
        return fig
create_figure('mean radius', 'mean texture', 'mean radius', 'mean radius')<jupyter_output><empty_output><jupyter_text>Creating the dashboard<jupyter_code>import panel as pn
pn.extension()

kw = dict(plot1_f1=breast_cancer.feature_names,
          plot1_f2=breast_cancer.feature_names,
          plot2_f=breast_cancer.feature_names,
          plot3_f=breast_cancer.feature_names,
         )

dash1 = pn.interact(create_figure, **kw)
dash1
dash2 = pn.Column(
                    pn.Row(dash1[0][0], dash1[0][1], align='center'),
                    pn.Row(dash1[0][2], dash1[0][3], align='center'),
                    dash1[1]
                 )
dash2<jupyter_output><empty_output><jupyter_text>More Interactive Dashboard with Dropdown<jupyter_code>import panel.widgets as pnw

f1 = pnw.Select(options=list(breast_cancer.feature_names), name='Scatter Plot Feature 1')
f2 = pnw.Select(options=list(breast_cancer.feature_names), name='Scatter Plot Feature 2')
f3 = pnw.Select(options=list(breast_cancer.feature_names), name='Bar Chart Feature')
f4 = pnw.Select(options=list(breast_cancer.feature_names), name='Histogram Feature')
@pn.depends(f1.param.value, f2.param.value, f3.param.value, f4.param.value)
def create_dash(f1,f2,f3,f4):
    return create_figure(f1,f2,f3,f4)
heading = '<h3>Breast Cancer Data Analysis Dashboard</h3>\nPlease feel free to try various combinations of parameters'
text = pnw.StaticText(value=heading, align='center')

dash3 = pn.Column(
                    text,
                    pn.Row(f1, f2, align='center'),
                    pn.Row(f3, f4, align='center'),
                    pn.Row(create_dash)
                 )
dash3<jupyter_output><empty_output><jupyter_text>Save the dashboard<jupyter_code>dash3.servable()
dash3.save('dash.html')<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.13100735419873438, 'fraction_numerical': 0.021549512570549, 'mean_word_length': 3.357675111773472, 'pattern_counts': {'":': 2, '<': 27, '<?xml version=': 0, '>': 27, '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': '653070', 'license': 'MIT', 'n_tokens_mistral': 2118, 'n_tokens_neox': 1974, 'n_words': 287, 'path': '25 - Breast Cancer Data Visualization/Data_Visualization.ipynb', 'repo_name': 'algonacci/Projects', 'size': '5847'}