File size: 10,083 Bytes
5e089eb
ebcdddb
 
346590b
 
5e089eb
ebcdddb
5e089eb
 
 
ebcdddb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5e089eb
ebcdddb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
346590b
 
ebcdddb
346590b
 
 
 
 
 
 
 
 
ebcdddb
346590b
 
 
ebcdddb
346590b
 
 
 
 
 
 
 
 
 
 
 
 
ebcdddb
346590b
 
 
 
 
 
 
 
 
 
 
 
 
ebcdddb
346590b
ebcdddb
 
346590b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ebcdddb
346590b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ebcdddb
346590b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ebcdddb
346590b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ebcdddb
346590b
ebcdddb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
346590b
ebcdddb
346590b
ebcdddb
 
346590b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5e089eb
 
ebcdddb
 
 
 
 
 
 
 
 
5e089eb
 
ebcdddb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
65bf92e
 
 
 
ebcdddb
 
 
346590b
ebcdddb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5e089eb
 
ebcdddb
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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
import gradio as gr
import pandas as pd
import numpy as np
import plotly.graph_objects as go
from plotly.subplots import make_subplots

import vlai_template

# ────────────────────────────  Functions  ─────────────────────────    

def simple_pca(X, n_components=2):
    """Simple PCA implementation"""
    # Center the data
    X_centered = X - np.mean(X, axis=0)
    
    # Compute covariance matrix
    cov_matrix = np.cov(X_centered.T)
    
    # Compute eigenvalues and eigenvectors
    eigenvalues, eigenvectors = np.linalg.eigh(cov_matrix)
    
    # Sort by eigenvalues (descending)
    idx = np.argsort(eigenvalues)[::-1]
    eigenvalues = eigenvalues[idx]
    eigenvectors = eigenvectors[:, idx]
    
    # Select top n_components
    components = eigenvectors[:, :n_components]
    
    # Transform data
    X_pca = X_centered @ components
    
    # Calculate explained variance ratio
    explained_variance_ratio = eigenvalues[:n_components] / np.sum(eigenvalues)
    
    return X_pca, components, explained_variance_ratio

def simple_kmeans(X, k, max_iters=100, random_state=42):
    """Simple KMeans implementation"""
    np.random.seed(random_state)
    
    # Initialize centroids randomly
    n_samples, n_features = X.shape
    centroids = X[np.random.choice(n_samples, k, replace=False)]
    
    for _ in range(max_iters):
        # Assign points to closest centroid
        distances = np.sqrt(((X - centroids[:, np.newaxis])**2).sum(axis=2))
        labels = np.argmin(distances, axis=0)
        
        # Update centroids
        new_centroids = np.array([X[labels == i].mean(axis=0) for i in range(k)])
        
        # Check for convergence
        if np.allclose(centroids, new_centroids):
            break
            
        centroids = new_centroids
    
    # Calculate inertia
    inertia = sum([np.sum((X[labels == i] - centroids[i])**2) for i in range(k)])
    
    return labels, centroids, inertia

def standardize_data(X):
    """Standardize data to have mean=0 and std=1"""
    return (X - np.mean(X, axis=0)) / np.std(X, axis=0)

def create_plotly_visualization(X_pca, wine_types, labels, centroids_pca, k, explained_var):
    """Create a plotly visualization with two subplots"""
    
    # Create subplots
    fig = make_subplots(
        rows=1, cols=2,
        subplot_titles=(
            "Original Data (Red vs White Wine)",
            f"After KMeans Clustering (K={k})"
        ),
        horizontal_spacing=0.1
    )
    
    # Plot 1: Original Wine Types
    red_mask = np.array(wine_types) == "red"
    white_mask = np.array(wine_types) == "white"
    
    # Add red wine points
    if np.any(red_mask):
        fig.add_trace(
            go.Scatter(
                x=X_pca[red_mask, 0],
                y=X_pca[red_mask, 1],
                mode='markers',
                marker=dict(color='#d62728', size=4, opacity=0.6),
                name='Red Wine',
                showlegend=True
            ),
            row=1, col=1
        )
    
    # Add white wine points
    if np.any(white_mask):
        fig.add_trace(
            go.Scatter(
                x=X_pca[white_mask, 0],
                y=X_pca[white_mask, 1],
                mode='markers',
                marker=dict(color='#1f77b4', size=4, opacity=0.6),
                name='White Wine',
                showlegend=True
            ),
            row=1, col=1
        )
    
    # Plot 2: KMeans Clusters
    cluster_colors = ["#ff7f0e", "#2ca02c", "#d62728", "#9467bd", "#8c564b", "#e377c2", "#7f7f7f", "#bcbd22"]
    
    # Add cluster points
    for i in range(k):
        cluster_mask = labels == i
        if np.any(cluster_mask):
            fig.add_trace(
                go.Scatter(
                    x=X_pca[cluster_mask, 0],
                    y=X_pca[cluster_mask, 1],
                    mode='markers',
                    marker=dict(color=cluster_colors[i % len(cluster_colors)], size=4, opacity=0.6),
                    name=f'Cluster {i}',
                    showlegend=True
                ),
                row=1, col=2
            )
    
    # Add centroids
    fig.add_trace(
        go.Scatter(
            x=centroids_pca[:, 0],
            y=centroids_pca[:, 1],
            mode='markers+text',
            marker=dict(color='black', size=12, line=dict(color='white', width=2)),
            text=[str(i) for i in range(k)],
            textfont=dict(color='white', size=10),
            textposition="middle center",
            name='Centroids',
            showlegend=True
        ),
        row=1, col=2
    )
    
    # Update layout
    fig.update_layout(
        title="Wine Quality Dataset - KMeans Clustering with PCA",
        title_x=0.5,
        height=600,
        plot_bgcolor='white',
        paper_bgcolor='white',
        font=dict(size=12),
        showlegend=True,
        legend=dict(
            orientation="h",
            yanchor="bottom",
            y=-0.2,
            xanchor="center",
            x=0.5
        )
    )
    
    # Update x and y axes labels and styling
    fig.update_xaxes(
        title_text=f"PC1 ({explained_var[0]:.1%} variance)",
        showgrid=True,
        gridcolor='lightgray',
        gridwidth=1,
        zeroline=True,
        zerolinecolor='lightgray',
        row=1, col=1
    )
    fig.update_xaxes(
        title_text=f"PC1 ({explained_var[0]:.1%} variance)",
        showgrid=True,
        gridcolor='lightgray',
        gridwidth=1,
        zeroline=True,
        zerolinecolor='lightgray',
        row=1, col=2
    )
    fig.update_yaxes(
        title_text=f"PC2 ({explained_var[1]:.1%} variance)",
        showgrid=True,
        gridcolor='lightgray',
        gridwidth=1,
        zeroline=True,
        zerolinecolor='lightgray',
        row=1, col=1
    )
    fig.update_yaxes(
        title_text=f"PC2 ({explained_var[1]:.1%} variance)",
        showgrid=True,
        gridcolor='lightgray',
        gridwidth=1,
        zeroline=True,
        zerolinecolor='lightgray',
        row=1, col=2
    )
    
    return fig

def run_kmeans_analysis(n_clusters, random_state):
    """Main function to run the KMeans analysis"""
    try:
        # Load data
        df = pd.read_csv('data/winequality-merged.csv')
        
        # Prepare features (exclude wine_type)
        numeric_cols = df.select_dtypes(include=[np.number]).columns.tolist()
        X = df[numeric_cols].values
        
        # Standardize data
        X_scaled = standardize_data(X)
        
        # Apply PCA
        X_pca, components, explained_var = simple_pca(X_scaled, n_components=2)
        
        # Apply KMeans
        labels, centroids, inertia = simple_kmeans(X_scaled, n_clusters, random_state=random_state)
        
        # Transform centroids to PCA space
        centroids_centered = centroids - np.mean(X_scaled, axis=0)
        centroids_pca = centroids_centered @ components
        
        # Create plot
        wine_types = df['wine_type'].tolist() if 'wine_type' in df.columns else ['unknown'] * len(df)
        plot_fig = create_plotly_visualization(X_pca, wine_types, labels, centroids_pca, n_clusters, explained_var)
        
        return plot_fig
        
    except Exception as e:
        # Return an empty plotly figure with error message
        fig = go.Figure()
        fig.add_annotation(
            text=f"Error: {str(e)}",
            xref="paper", yref="paper",
            x=0.5, y=0.5,
            showarrow=False,
            font=dict(color="red", size=16)
        )
        fig.update_layout(
            plot_bgcolor='white',
            paper_bgcolor='white',
            xaxis=dict(visible=False),
            yaxis=dict(visible=False)
        )
        return fig

# ────────────────────────────  Main  ─────────────────────────
with gr.Blocks(theme='gstaff/sketch', css=vlai_template.custom_css, title="Wine Quality KMeans Demo") as demo:
    vlai_template.create_header()

    gr.Markdown("""
    ## 🍷 Wine Quality Dataset - KMeans Clustering with PCA
    
    This demo applies **KMeans clustering** to the merged wine quality dataset using **Principal Component Analysis (PCA)** 
    for dimensionality reduction. We visualize how the data looks before and after clustering.
    """)

    with gr.Row(equal_height=True, variant="panel"):
        with gr.Column(scale=1):
            n_clusters = gr.Slider(
                minimum=2, maximum=6, step=1, value=3,
                label="Number of Clusters (K)", 
                info="Choose how many clusters KMeans should find"
            )
            random_state = gr.Slider(
                minimum=1, maximum=100, step=1, value=42,
                label="Random Seed", 
                info="For reproducible results"
            )
            
            run_btn = gr.Button("πŸ” Run KMeans Analysis", variant="primary", size="lg")
            
            gr.Markdown("""
            ### πŸ’‘ How it works:
            1. **Load Data**: Wine quality features from merged dataset.
            2. **Standardize**: Scale all features to same range.  
            3. **PCA**: Reduce to 2 dimensions for visualization.
            4. **KMeans**: Group wines into K clusters.
            5. **Visualize**: Compare original vs. clustered data.
            """)

        with gr.Column(scale=7):
            output_plot = gr.Plot(label="πŸ“ˆ PCA Visualization & KMeans Results")

    run_btn.click(
        run_kmeans_analysis,
        inputs=[n_clusters, random_state],
        outputs=[output_plot],
    )
    
    # Auto-run on page load
    demo.load(
        run_kmeans_analysis,
        inputs=[gr.Number(3, visible=False), gr.Number(42, visible=False)],
        outputs=[output_plot]
    )

    vlai_template.create_footer()

if __name__ == "__main__":
    demo.launch(allowed_paths=["static/aivn_logo.png", "static/vlai_logo.png", "static", "data"])