File size: 7,751 Bytes
ff9549f
 
689e49e
ff9549f
 
 
b8863d9
 
 
 
 
 
ff9549f
 
 
 
 
 
 
 
 
 
b8863d9
ff9549f
 
 
b8863d9
ff9549f
 
 
 
b8863d9
 
ff9549f
b8863d9
 
 
 
ff9549f
 
 
 
 
 
 
 
 
 
 
 
 
b8863d9
ff9549f
 
 
 
 
 
b8863d9
ff9549f
b8863d9
ff9549f
 
 
 
 
 
 
b8863d9
ff9549f
b8863d9
ff9549f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b8863d9
ff9549f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b8863d9
ff9549f
 
 
 
 
 
 
 
 
 
 
 
 
 
b8863d9
 
ff9549f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b8863d9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2a796f1
b8863d9
 
 
 
 
2a796f1
b8863d9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2a796f1
b8863d9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2a796f1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b8863d9
 
 
 
 
 
ff9549f
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
import marimo

__generated_with = "0.11.17"
app = marimo.App(width="medium")


@app.cell
def _(mo):
    mo.md(r"""# Analyzing Colorectal Cancer Dataset""")
    return


@app.cell
def _():
    import marimo as mo
    import polars as pl
    return mo, pl


@app.cell
def _(pl):
    dataset = pl.read_csv('./dataset/colorectal_cancer_dataset.csv')
    # dataset.select("Tumor_Size_mm").describe()
    return (dataset,)


@app.cell(hide_code=True)
def _(dataset, pl):
    from sklearn.preprocessing import OneHotEncoder, OrdinalEncoder

    ord_encoder = OrdinalEncoder()
    ord_encoded = ord_encoder.fit_transform(dataset.select('Early_Detection', 'Cancer_Stage', 'Survival_5_years'))
    encoded_features = ord_encoder.get_feature_names_out(['Early_Detection', 'Cancer_Stage', 'Survival_5_years'])
    encoded_schema = {name: pl.Int8 for name in encoded_features}
    # print(encoded_schema)
    dataset_encoded_parts = pl.DataFrame(ord_encoded, encoded_schema)
    dataset_encoded = dataset.with_columns(dataset_encoded_parts)
    # dataset_encoded
    return (
        OneHotEncoder,
        OrdinalEncoder,
        dataset_encoded,
        dataset_encoded_parts,
        encoded_features,
        encoded_schema,
        ord_encoded,
        ord_encoder,
    )


@app.cell
def _(dataset_encoded, mo):
    from sklearn.linear_model import LogisticRegression
    from sklearn.naive_bayes import BernoulliNB
    from sklearn.tree import DecisionTreeClassifier
    from sklearn.model_selection import train_test_split
    from sklearn.metrics import accuracy_score, precision_score, classification_report, confusion_matrix

    X = dataset_encoded.select(['Tumor_Size_mm', 'Early_Detection', 'Cancer_Stage'])
    y = dataset_encoded.select(['Survival_5_years'])
    X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=101)
    logreg = LogisticRegression()
    y_pred_logreg = logreg.fit(X_train, y_train).predict(X_test)
    bnb = BernoulliNB()
    y_pred_bnb = bnb.fit(X_train, y_train).predict(X_test)
    dectree = DecisionTreeClassifier()
    y_pred_dectree = dectree.fit(X_train, y_train).predict(X_test)


    mo.md(f"""
    ## Logistic Regression

        Accuracy score: {accuracy_score(y_test, y_pred_logreg)}

        Precision score: {precision_score(y_test, y_pred_logreg)}

        Confusion matrix:
    ```
            {confusion_matrix(y_test, y_pred_logreg)}
    ```

        Classification report:
    ```
            {classification_report(y_test, y_pred_logreg)}
    ```

    ## Bernoulli Naive Bayes

        Accuracy score: {accuracy_score(y_test, y_pred_bnb)}

        Precision score: {precision_score(y_test, y_pred_bnb)}

        Confusion matrix:
    ```
            {confusion_matrix(y_test, y_pred_bnb)}
    ```

        Classification report:
    ```
            {classification_report(y_test, y_pred_bnb)}
    ```

    ## Decision Tree Classifier

        Accuracy score: {accuracy_score(y_test, y_pred_dectree)}

        Precision score: {precision_score(y_test, y_pred_dectree)}

        Confusion matrix:
    ```
            {confusion_matrix(y_test, y_pred_dectree)}
    ```

        Classification report:
    ```
            {classification_report(y_test, y_pred_dectree)}
    ```

    {mo.callout("Classifiers don't work well with this dataset, let's try something else.", kind='info')}
    """)
    return (
        BernoulliNB,
        DecisionTreeClassifier,
        LogisticRegression,
        X,
        X_test,
        X_train,
        accuracy_score,
        bnb,
        classification_report,
        confusion_matrix,
        dectree,
        logreg,
        precision_score,
        train_test_split,
        y,
        y_pred_bnb,
        y_pred_dectree,
        y_pred_logreg,
        y_test,
        y_train,
    )


@app.cell
def _(OrdinalEncoder, dataset, mo, pl):
    def _():
        from sklearn.cluster import KMeans, SpectralClustering, DBSCAN
        from sklearn.svm import SVC
        from sklearn.metrics import adjusted_rand_score, normalized_mutual_info_score, homogeneity_score, completeness_score, v_measure_score, silhouette_score, davies_bouldin_score, calinski_harabasz_score
        import altair as alt

        genmut_encoder = OrdinalEncoder()
        genmut_encoded = genmut_encoder.fit_transform(dataset.select('Genetic_Mutation'))
        genmut_features = genmut_encoder.get_feature_names_out(['Genetic_Mutation'])
        encoded_schema = {name: pl.Int8 for name in genmut_features}
        dataset_encoded_parts = pl.DataFrame(genmut_encoded, encoded_schema)
        dataset_encoded = dataset.with_columns(dataset_encoded_parts)
        # Use samples since dataset is way too big to run locally
        dataset_encoded = dataset_encoded.sample(3000, seed=11)

        X = dataset_encoded.select(['Tumor_Size_mm', 'Genetic_Mutation'])
        y = dataset_encoded.select(['Cancer_Stage']).to_series()

        kmeans = KMeans(n_clusters=3, random_state=11)
        spec = SpectralClustering(n_clusters=3, random_state=11)

        labels_kmeans = kmeans.fit_predict(X)
        labels_spec = spec.fit_predict(X)

        # df_kmeans_parts = pl.DataFrame(labels_kmeans, schema=pl.String)
        df_kmeans = X.with_columns(pl.lit(labels_kmeans, dtype=pl.String).alias('kmeans_cluster'))
        df_spec = X.with_columns(pl.lit(labels_spec, dtype=pl.String).alias('spectral_cluster'))

        return mo.vstack([
            mo.md(f"""
            ## K-Means Clustering

            ### External Metrics (Based on Cancer Stage Labels)

            Adjusted Rand Index (ARI): {adjusted_rand_score(y, labels_kmeans)}

            Normalized Mutual Information (NMI): {normalized_mutual_info_score(y, labels_kmeans)}

            Homogeneity: {homogeneity_score(y, labels_kmeans)}

            Completeness: {completeness_score(y, labels_kmeans)}

            V-measure: {v_measure_score(y, labels_kmeans)}

            ### Internal Metrics

            Silhouette Score: {silhouette_score(X, labels_kmeans)}

            Davies-Bouldin Index: {davies_bouldin_score(X, labels_kmeans)}

            Calinski-Harabasz Index: {calinski_harabasz_score(X, labels_kmeans)}


            ## Spectral Clustering

            ### External Metrics (Based on Cancer Stage Labels)

            Adjusted Rand Index (ARI): {adjusted_rand_score(y, labels_spec)}

            Normalized Mutual Information (NMI): {normalized_mutual_info_score(y, labels_spec)}

            Homogeneity: {homogeneity_score(y, labels_spec)}

            Completeness: {completeness_score(y, labels_spec)}

            V-measure: {v_measure_score(y, labels_spec)}

            ### Internal Metrics

            Silhouette Score: {silhouette_score(X, labels_spec)}

            Davies-Bouldin Index: {davies_bouldin_score(X, labels_spec)}

            Calinski-Harabasz Index: {calinski_harabasz_score(X, labels_spec)}

            {mo.callout("Unsupervised clustering techniques do perform reasonably well, but does not correlate to other labels.", 'info')}
        """),

            mo.hstack([
                alt.Chart(df_kmeans, autosize='pad').mark_rect().encode(
                    alt.X('Genetic_Mutation:N'),
                    y='Tumor_Size_mm',
                    color='kmeans_cluster'
                ).properties(
                    width=325
                ).interactive(),

                alt.Chart(df_spec, autosize='pad').mark_rect().encode(
                    alt.X('Genetic_Mutation:N'),
                    y='Tumor_Size_mm',
                    color='spectral_cluster'
                ).properties(
                    width=325
                ).interactive(),
            ])
        ])


    _()
    return


if __name__ == "__main__":
    app.run()