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
2ea1d75
b1ae1b2
2ea1d75
b1ae1b2
 
 
 
 
 
 
 
2ea1d75
 
 
 
b1ae1b2
 
2ea1d75
b1ae1b2
 
 
 
 
 
2ea1d75
 
b1ae1b2
 
 
 
 
 
 
 
 
 
2ea1d75
 
b1ae1b2
 
 
 
 
2ea1d75
 
b1ae1b2
 
 
 
2ea1d75
 
b1ae1b2
 
 
 
 
 
 
 
2ea1d75
 
b1ae1b2
 
 
 
 
2ea1d75
b1ae1b2
2ea1d75
b1ae1b2
2ea1d75
b1ae1b2
 
2ea1d75
 
 
 
b1ae1b2
 
 
2ea1d75
 
 
 
b1ae1b2
 
2ea1d75
 
 
b1ae1b2
 
 
2ea1d75
 
 
b1ae1b2
 
2ea1d75
b1ae1b2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2ea1d75
 
 
 
b1ae1b2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2ea1d75
 
 
 
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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
import marimo

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


@app.cell
def _():
    import pandas as pd
    import numpy as np
    import plotly.express as px
    import plotly.graph_objects as go
    import altair as alt
    from sklearn.datasets import fetch_california_housing
    from sklearn.model_selection import train_test_split
    from sklearn.linear_model import LinearRegression
    from sklearn.metrics import mean_squared_error, mean_absolute_error, r2_score
    from sklearn.preprocessing import StandardScaler
    import joblib
    import torch
    import torch.nn as nn
    import torch.optim as optim
    from torch.utils.data import DataLoader, TensorDataset
    import warnings
    return (
        DataLoader,
        LinearRegression,
        StandardScaler,
        TensorDataset,
        alt,
        fetch_california_housing,
        joblib,
        mean_absolute_error,
        mean_squared_error,
        nn,
        np,
        optim,
        pd,
        px,
        r2_score,
        torch,
        train_test_split,
    )


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


@app.cell
def _(mo):
    mo.md(r"""# eXplainable AI (XAI)""")
    return


@app.cell
def _(mo):
    mo.md(
        """
    ## What is XAI?
    XAI refers to a set of techniques and methods that make the decision-making process of AI models transparent, understandable, and interpretable to humans.
    """
    )
    return


@app.cell
def _(mo):
    mo.md(r"""When the complexity of a model increases, you get better accuracy, but then explaining how your model makes its decisions becomes harder. The feild of XAI tries to find the best trade-off between explainability and accuracy.""")
    return


@app.cell
def _(mo):
    mo.image("/Users/negarvahid/Desktop/intorduction_to_xai/increasing_complexity.png")
    return


@app.cell
def _(mo):
    mo.md(r"""## What is Interpretibility?""")
    return


@app.cell
def _(mo):
    mo.md(r"""You call a model *interpretable* when you can understand the internal mechanisms of a model.""")
    return


@app.cell
def _(mo):
    mo.md(r"""Let’s say you’re predicting someone’s risk of heart disease. If you use a linear regression model, you’ll get a clean equation like:""")
    return


@app.cell
def _(mo):
    mo.md(
        r"""
    $$
    \text{Risk} = 0.3 \cdot \text{Age} + 0.5 \cdot \text{Cholesterol} + 0.7 \cdot \text{SmokingStatus}
    $$
    """
    )
    return


@app.cell
def _(mo):
    mo.md(r"""This model is interpretable. You can look at the numbers and instantly understand how each feature contributes to the prediction. You don’t need any extra tools, as **it’s transparent by design**.""")
    return


@app.cell
def _(mo):
    mo.md(r"""## What is Explainbility?""")
    return


@app.cell
def _(mo):
    mo.md(r"""Now imagine you use a deep neural network for the same task. The network might give highly accurate predictions, but its internal workings are opaque due to multiple hidden layers and non-linear transformations. You can't just "look" at the model and understand how it made a decision.""")
    return


@app.cell
def _(mo):
    mo.image("/Users/negarvahid/Desktop/intorduction_to_xai/Screenshot 2025-07-12 at 16.05.55.png")
    return


@app.cell
def _(mo):
    mo.md(
        r"""
    To make sense of this black-box model, you might use a tool like SHAP (SHapley Additive exPlanations) to show that for a particular patient, high cholesterol and smoking status increased their predicted risk by specific amounts. This is **explainability**
    You're using a tool to externally explain the behavior of a complex, non-interpretable model.
    """
    )
    return


@app.cell
def _(mo):
    mo.md(r"""#The California Housing Prices Dataset""")
    return


@app.cell
def _(mo):
    mo.md(
        r"""
    The California Housing Prices Dataset a tabular dataset that contains information collected from the 1990 California census, aimed at predicting the median house value (in USD) for various districts across California, based on features like:

    - Median income of residents
    - Average number of rooms per household
    - Average number of bedrooms
    - Population and household size
    - Latitude and longitude
    - Housing median age
    - 
    Total instances: 20,640 rows (each one represents a housing block group)

    Number of features: 8 input features + 1 target value (median house price)
    """
    )
    return


@app.cell
def _(mo):
    mo.md(
        r"""
    ### Load the dataset
    Run the following cell to load the dataset:
    """
    )
    return


@app.cell
def _():
    return


@app.cell
def _(fetch_california_housing, pd):
    def load_data():
        california = fetch_california_housing()

        data = pd.DataFrame(california.data, columns=california.feature_names)
        data['target'] = california.target

        return data

    data = load_data()
    return data, load_data


@app.cell
def _(data):
    data
    return


@app.cell
def _(mo):
    mo.md(
        r"""
    ### Visualize the Dataset:
    It would be great to visualize the dataset!
    Run the cell below to get an interactive plot of the dataset:
    """
    )
    return


@app.cell(hide_code=True)
def _(alt, data, mo):
    selection = alt.selection_point(fields=['index'], name='select')
    data_sample = data.sample(n=200, random_state=42)
    location_chart = mo.ui.altair_chart(
        alt.Chart(data_sample.reset_index()).mark_circle(size=60).encode(
            x=alt.X('Longitude:Q', title='Longitude', scale=alt.Scale(domain=[-124.5, -114.0])),
            y=alt.Y('Latitude:Q', title='Latitude', scale=alt.Scale(domain=[32.0, 42.5])),
            color=alt.Color('target:Q', 
                           scale=alt.Scale(scheme='viridis', domain=[0, 5]), 
                           title='Price ($100k)',
                           legend=alt.Legend(orient='top', titleFontSize=14, labelFontSize=12)),
            size=alt.Size('AveBedrms:Q', 
                         scale=alt.Scale(range=[100, 400], domain=[0, 2]), 
                         title='Avg Bedrooms',
                         legend=alt.Legend(orient='top', titleFontSize=14, labelFontSize=12)),
            tooltip=[
                alt.Tooltip('Longitude:Q', title='Longitude', format='.3f'),
                alt.Tooltip('Latitude:Q', title='Latitude', format='.3f'),
                alt.Tooltip('target:Q', title='Price ($100k)', format='.2f'),
                alt.Tooltip('AveBedrms:Q', title='Avg Bedrooms', format='.2f'),
                alt.Tooltip('MedInc:Q', title='Median Income', format='.2f'),
                alt.Tooltip('HouseAge:Q', title='House Age', format='.0f'),
                alt.Tooltip('AveRooms:Q', title='Avg Rooms', format='.2f'),
                alt.Tooltip('Population:Q', title='Population', format='.0f'),
                alt.Tooltip('AveOccup:Q', title='Avg Occupancy', format='.2f')
            ],
            opacity=alt.condition(selection, alt.value(1), alt.value(0.7))
        ).add_params(selection).properties(
            title='California Housing: 500 Sample Points - Location, Price & Bedrooms (Click to Select)',
            width=900,
            height=700
        ).interactive()
    )

    price_bedrooms_chart = mo.ui.altair_chart(
        alt.Chart(data_sample.reset_index()).mark_circle(size=60).encode(
            x=alt.X('AveBedrms:Q', title='Average Bedrooms', scale=alt.Scale(domain=[0, 2])),
            y=alt.Y('target:Q', title='Price ($100k)', scale=alt.Scale(domain=[0, 5])),
            color=alt.Color('MedInc:Q', 
                           scale=alt.Scale(scheme='plasma', domain=[0, 15]), 
                           title='Median Income',
                           legend=alt.Legend(orient='top', titleFontSize=14, labelFontSize=12)),
            size=alt.Size('Population:Q', 
                         scale=alt.Scale(range=[100, 400], domain=[0, 3000]), 
                         title='Population',
                         legend=alt.Legend(orient='top', titleFontSize=14, labelFontSize=12)),
            tooltip=[
                alt.Tooltip('AveBedrms:Q', title='Avg Bedrooms', format='.2f'),
                alt.Tooltip('target:Q', title='Price ($100k)', format='.2f'),
                alt.Tooltip('MedInc:Q', title='Median Income', format='.2f'),
                alt.Tooltip('Population:Q', title='Population', format='.0f'),
                alt.Tooltip('Longitude:Q', title='Longitude', format='.3f'),
                alt.Tooltip('Latitude:Q', title='Latitude', format='.3f'),
                alt.Tooltip('HouseAge:Q', title='House Age', format='.0f'),
                alt.Tooltip('AveRooms:Q', title='Avg Rooms', format='.2f'),
                alt.Tooltip('AveOccup:Q', title='Avg Occupancy', format='.2f')
            ],
            opacity=alt.condition(selection, alt.value(1), alt.value(0.7))
        ).add_params(selection).properties(
            title='Price vs Bedrooms: 500 Sample Points - Income & Population (Click to Select)',
            width=900,
            height=600
        ).interactive()
    )

    mo.md(f"""
    # California Housing Dataset - 500 Sample Points Analysis

    **Dataset Shape:** {data.shape[0]} total rows × {data.shape[1]} columns
    **Sample Size:** 500 random points (for better visualization)

    **Key Features:**
    - **Location**: Longitude & Latitude
    - **Price**: Median house value (target) - $0-500k
    - **Bedrooms**: Average number of bedrooms (AveBedrms) - 0-2
    - **Income**: Median income (MedInc) - $0-15k
    - **Population**: Block group population - 0-3000
    - **Age**: Median house age (HouseAge)
    - **Rooms**: Average rooms per household (AveRooms)
    - **Occupancy**: Average household members (AveOccup)

    **How to Use:** Click on any point to select it and see its data highlighted. Selected points will appear more opaque.

    ---
    """)

    mo.md("## 1. Interactive Map: Location, Price & Bedrooms")

    mo.md("**500 sample points** across California. **Color = Price** (viridis scale), **Size = Number of Bedrooms**. **Click on any point to select it** - selected points become more opaque. Hover for detailed information.")

    location_chart

    mo.md("## 2. Price vs Bedrooms Analysis")

    mo.md("**500 sample points** showing price vs bedrooms relationship. **Color = Income** (plasma scale), **Size = Population**. **Click on any point to select it** - the same selection works across both charts.")

    price_bedrooms_chart 
    return


@app.cell(hide_code=True)
def _(data, mo, px):
    def create_map_plot():
        fig = px.scatter(
            data, 
            x='Longitude', 
            y='Latitude', 
            color='target',
            color_continuous_scale='viridis',
            title='California Housing Prices by Location',
            labels={'target': 'Price ($100k)', 'Longitude': 'Longitude', 'Latitude': 'Latitude'},
            hover_data=['MedInc', 'HouseAge', 'AveRooms', 'AveBedrms', 'Population', 'AveOccup']
        )

        fig.update_layout(
            width=800,
            height=600,
            title_x=0.5
        )

        return fig

    map_plot = create_map_plot()
    interactive_map = mo.ui.plotly(map_plot)
    return (interactive_map,)


@app.cell
def _(interactive_map):
    interactive_map
    return


@app.cell(hide_code=True)
def _(
    DataLoader,
    LinearRegression,
    StandardScaler,
    TensorDataset,
    fetch_california_housing,
    joblib,
    mean_absolute_error,
    mean_squared_error,
    nn,
    np,
    optim,
    pd,
    r2_score,
    torch,
    train_test_split,
):
    class NeuralNetwork(nn.Module):
        def __init__(self, input_size=8, hidden_size=64, output_size=1):
            super(NeuralNetwork, self).__init__()
            self.layer1 = nn.Linear(input_size, hidden_size)
            self.layer2 = nn.Linear(hidden_size, hidden_size // 2)
            self.layer3 = nn.Linear(hidden_size // 2, hidden_size // 4)
            self.layer4 = nn.Linear(hidden_size // 4, output_size)
            self.relu = nn.ReLU()
            self.dropout = nn.Dropout(0.2)

        def forward(self, x):
            x = self.relu(self.layer1(x))
            x = self.dropout(x)
            x = self.relu(self.layer2(x))
            x = self.dropout(x)
            x = self.relu(self.layer3(x))
            x = self.layer4(x)
            return x

    class PyTorchModel:
        def __init__(self, input_size=8, hidden_size=64, epochs=100, lr=0.001):
            self.model = NeuralNetwork(input_size, hidden_size)
            self.epochs = epochs
            self.lr = lr
            self.criterion = nn.MSELoss()
            self.optimizer = optim.Adam(self.model.parameters(), lr=lr)
            self.is_trained = False

        def fit(self, X, y):
            # Convert to PyTorch tensors
            X_tensor = torch.FloatTensor(X)
            y_tensor = torch.FloatTensor(y.values if hasattr(y, 'values') else y).reshape(-1, 1)

            # Create data loader
            dataset = TensorDataset(X_tensor, y_tensor)
            dataloader = DataLoader(dataset, batch_size=32, shuffle=True)

            # Training loop
            self.model.train()
            for epoch in range(self.epochs):
                total_loss = 0
                for batch_X, batch_y in dataloader:
                    self.optimizer.zero_grad()
                    outputs = self.model(batch_X)
                    loss = self.criterion(outputs, batch_y)
                    loss.backward()
                    self.optimizer.step()
                    total_loss += loss.item()

                if (epoch + 1) % 20 == 0:
                    print(f"  Epoch [{epoch+1}/{self.epochs}], Loss: {total_loss/len(dataloader):.4f}")

            self.is_trained = True

        def predict(self, X):
            if not self.is_trained:
                raise ValueError("Model not trained yet!")

            self.model.eval()
            with torch.no_grad():
                X_tensor = torch.FloatTensor(X)
                predictions = self.model(X_tensor)
                return predictions.numpy().flatten()

    class CaliforniaHousingKaggleStyle:
        def __init__(self):
            self.scaler = StandardScaler()
            self.feature_names = None
            self.X_train = None
            self.X_test = None
            self.y_train = None
            self.y_test = None

        def load_data(self):
            """Load California Housing dataset"""
            print("Loading California Housing dataset...")
            california = fetch_california_housing()

            # Create DataFrame
            self.data = pd.DataFrame(california.data, columns=california.feature_names)
            self.data['target'] = california.target

            # Store feature names
            self.feature_names = california.feature_names

            print(f"Dataset shape: {self.data.shape}")
            print(f"Features: {list(self.feature_names)}")
            print(f"Target: Median house value (in $100,000s)")

            return self.data

        def prepare_data(self, test_size=0.2, random_state=42):
            """Prepare data for training"""
            print("\n=== Data Preparation ===")

            # Separate features and target
            X = self.data.drop('target', axis=1)
            y = self.data['target']

            # Split the data
            self.X_train, self.X_test, self.y_train, self.y_test = train_test_split(
                X, y, test_size=test_size, random_state=random_state
            )

            # Scale the features
            self.X_train_scaled = self.scaler.fit_transform(self.X_train)
            self.X_test_scaled = self.scaler.transform(self.X_test)

            print(f"Training set shape: {self.X_train.shape}")
            print(f"Test set shape: {self.X_test.shape}")

            return self.X_train_scaled, self.X_test_scaled, self.y_train, self.y_test

        def train_models(self):
            """Train Linear Regression and Neural Network"""
            print("\n=== Training Models ===")

            # Train Linear Regression
            print("Training Linear Regression...")
            self.linear_model = LinearRegression()
            self.linear_model.fit(self.X_train_scaled, self.y_train)
            print("  ✓ Linear Regression trained successfully")

            # Train Neural Network
            print("Training Neural Network...")
            self.nn_model = PyTorchModel(epochs=100, lr=0.001)
            self.nn_model.fit(self.X_train_scaled, self.y_train)
            print("  ✓ Neural Network trained successfully")

        def evaluate_models(self):
            """Evaluate both models"""
            print("\n=== Model Evaluation ===")

            # Linear Regression evaluation
            y_train_pred_lr = self.linear_model.predict(self.X_train_scaled)
            y_test_pred_lr = self.linear_model.predict(self.X_test_scaled)

            lr_train_rmse = np.sqrt(mean_squared_error(self.y_train, y_train_pred_lr))
            lr_test_rmse = np.sqrt(mean_squared_error(self.y_test, y_test_pred_lr))
            lr_train_mae = mean_absolute_error(self.y_train, y_train_pred_lr)
            lr_test_mae = mean_absolute_error(self.y_test, y_test_pred_lr)
            lr_train_r2 = r2_score(self.y_train, y_train_pred_lr)
            lr_test_r2 = r2_score(self.y_test, y_test_pred_lr)

            print("Linear Regression:")
            print(f"  Test RMSE: {lr_test_rmse:.4f}")
            print(f"  Test MAE: {lr_test_mae:.4f}")
            print(f"  Test R²: {lr_test_r2:.4f}")

            # Neural Network evaluation
            y_train_pred_nn = self.nn_model.predict(self.X_train_scaled)
            y_test_pred_nn = self.nn_model.predict(self.X_test_scaled)

            nn_train_rmse = np.sqrt(mean_squared_error(self.y_train, y_train_pred_nn))
            nn_test_rmse = np.sqrt(mean_squared_error(self.y_test, y_test_pred_nn))
            nn_train_mae = mean_absolute_error(self.y_train, y_train_pred_nn)
            nn_test_mae = mean_absolute_error(self.y_test, y_test_pred_nn)
            nn_train_r2 = r2_score(self.y_train, y_train_pred_nn)
            nn_test_r2 = r2_score(self.y_test, y_test_pred_nn)

            print("\nNeural Network:")
            print(f"  Test RMSE: {nn_test_rmse:.4f}")
            print(f"  Test MAE: {nn_test_mae:.4f}")
            print(f"  Test R²: {nn_test_r2:.4f}")

            return {
                'linear_regression': {
                    'train_rmse': lr_train_rmse,
                    'test_rmse': lr_test_rmse,
                    'train_mae': lr_train_mae,
                    'test_mae': lr_test_mae,
                    'train_r2': lr_train_r2,
                    'test_r2': lr_test_r2
                },
                'neural_network': {
                    'train_rmse': nn_train_rmse,
                    'test_rmse': nn_test_rmse,
                    'train_mae': nn_train_mae,
                    'test_mae': nn_test_mae,
                    'train_r2': nn_train_r2,
                    'test_r2': nn_test_r2
                }
            }

        def save_models(self):
            """Save both models and their parameters"""
            print("\n=== Saving Models ===")

            # Save Linear Regression
            linear_model_data = {
                'model': self.linear_model,
                'scaler': self.scaler,
                'feature_names': self.feature_names,
                'parameters': {
                    'coef_': self.linear_model.coef_.tolist(),
                    'intercept_': float(self.linear_model.intercept_)
                }
            }
            joblib.dump(linear_model_data, 'linear_regression_model.pkl')
            print("Linear Regression model saved to 'linear_regression_model.pkl'")

            # Save Neural Network
            nn_model_data = {
                'model': self.nn_model,
                'scaler': self.scaler,
                'feature_names': self.feature_names,
                'parameters': {
                    'state_dict': self.nn_model.model.state_dict(),
                    'input_size': 8,
                    'hidden_size': 64,
                    'epochs': 100,
                    'lr': 0.001
                }
            }
            joblib.dump(nn_model_data, 'neural_network_model.pkl')
            print("Neural Network model saved to 'neural_network_model.pkl'")

        def predict_with_linear_regression(self, sample_data):
            """Make prediction with Linear Regression"""
            if isinstance(sample_data, dict):
                sample_df = pd.DataFrame([sample_data])
            else:
                sample_df = sample_data

            # Ensure all features are present
            for feature in self.feature_names:
                if feature not in sample_df.columns:
                    raise ValueError(f"Missing feature: {feature}")

            # Reorder columns to match training data
            sample_df = sample_df[self.feature_names]

            # Scale the data
            sample_scaled = self.scaler.transform(sample_df)

            # Make prediction
            prediction = self.linear_model.predict(sample_scaled)[0]
            return prediction

        def predict_with_neural_network(self, sample_data):
            """Make prediction with Neural Network"""
            if isinstance(sample_data, dict):
                sample_df = pd.DataFrame([sample_data])
            else:
                sample_df = sample_data

            # Ensure all features are present
            for feature in self.feature_names:
                if feature not in sample_df.columns:
                    raise ValueError(f"Missing feature: {feature}")

            # Reorder columns to match training data
            sample_df = sample_df[self.feature_names]

            # Scale the data
            sample_scaled = self.scaler.transform(sample_df)

            # Make prediction
            prediction = self.nn_model.predict(sample_scaled)[0]
            return prediction

    def main():
        """Main function"""
        print("California Housing Price Prediction - Linear Regression & Neural Network")
        print("=" * 70)

        # Initialize
        predictor = CaliforniaHousingKaggleStyle()

        # Load data
        data = predictor.load_data()

        # Prepare data
        predictor.prepare_data()

        # Train models
        predictor.train_models()

        # Evaluate models
        results = predictor.evaluate_models()

        # Save models
        predictor.save_models()

        # Example predictions
        print("\n=== Example Predictions ===")
        sample_data = {
            'MedInc': 8.3252,
            'HouseAge': 41.0,
            'AveRooms': 6.984127,
            'AveBedrms': 1.023810,
            'Population': 322.0,
            'AveOccup': 2.555556,
            'Latitude': 37.88,
            'Longitude': -122.23
        }

        lr_prediction = predictor.predict_with_linear_regression(sample_data)
        nn_prediction = predictor.predict_with_neural_network(sample_data)

        print(f"Sample data: {sample_data}")
        print(f"Linear Regression prediction: ${lr_prediction * 100000:.2f}")
        print(f"Neural Network prediction: ${nn_prediction * 100000:.2f}")

    main() 
    return


@app.cell
def _(mo):
    mo.md(r"""You already have two models prepared for you. One is a linear regression, the other is a neural network. Run the followung cell to load them and make a sample prediction:""")
    return


@app.cell(hide_code=True)
def _(joblib, pd):
    def load_models():
        linear_model_data = joblib.load('linear_regression_model.pkl')
        nn_model_data = joblib.load('neural_network_model.pkl')

        return linear_model_data, nn_model_data

    def make_prediction(linear_model_data, nn_model_data, sample_data):
        if isinstance(sample_data, dict):
            sample_df = pd.DataFrame([sample_data])
        else:
            sample_df = sample_data

        feature_names = linear_model_data['feature_names']

        for feature in feature_names:
            if feature not in sample_df.columns:
                raise ValueError(f"Missing feature: {feature}")

        sample_df = sample_df[feature_names]

        scaler = linear_model_data['scaler']
        sample_scaled = scaler.transform(sample_df)

        linear_prediction = linear_model_data['model'].predict(sample_scaled)[0]

        nn_model = nn_model_data['model']
        nn_prediction = nn_model.predict(sample_scaled)[0]

        return linear_prediction, nn_prediction

    def predict_with_models():
        print("Loading saved models...")
        linear_model_data, nn_model_data = load_models()
        print("Models loaded successfully!")

        sample_data = {
            'MedInc': 8.3252,
            'HouseAge': 41.0,
            'AveRooms': 6.984127,
            'AveBedrms': 1.023810,
            'Population': 322.0,
            'AveOccup': 2.555556,
            'Latitude': 37.88,
            'Longitude': -122.23
        }

        print(f"\nSample data: {sample_data}")

        linear_pred, nn_pred = make_prediction(linear_model_data, nn_model_data, sample_data)

        print(f"Linear Regression prediction: ${linear_pred * 100000:.2f}")
        print(f"Neural Network prediction: ${nn_pred * 100000:.2f}")

    predict_with_models()
    return


@app.cell
def _(mo):
    mo.md(r"""# Interpret a Linear Regressor""")
    return


@app.cell
def _(mo):
    mo.md(r"""## Load Model's Coefficients and Intercept""")
    return


@app.cell
def _(mo):
    mo.md(r"""As you saw earlier, linear regression models are naturally interpretable. You can directly inspect the coefficients and intercept to understand how the model makes predictions. Go ahead and load the trained model so you can extract and examine those values:""")
    return


@app.cell
def _(joblib):
    model_data = joblib.load('linear_regression_model.pkl')
    model = model_data['model']
    feature_names = model_data['feature_names']
        # Get coefficients and intercept
    coefficients = model.coef_
    intercept = model.intercept_
    print("Linear Regression Model Interpretation")
    print("=" * 50)
    print(f"Intercept: {intercept:.4f}")
    print("\nFeature Coefficients:")
    print("-" * 30)
    print(coefficients)
    return coefficients, feature_names


@app.cell
def _(mo):
    mo.md(rf"""#### Question: In a linear regression model, what does the intercept represent?""")
    return


@app.cell
def _(mo):
    mo.md(
        r"""
    The intercept in a linear regression model represents the predicted output when all input features are zero.

    $$
    y = w_1 x_1 + w_2 x_2 + \cdots + w_n x_n + b
    $$

    where:

    $$
    w_1, w_2, \dots, w_n \text{ are the coefficients}
    $$

    $$
    x_1, x_2, \dots, x_n \text{ are the features}
    $$

    $$
    b \text{ is the intercept}
    $$

    So:

    $$
    \text{Intercept } b = y \quad \text{when all } x_i = 0
    $$

    Think of the intercept as the model’s baseline prediction — the value of \( y \) when there’s no influence from any feature.

    It anchors the regression line or hyperplane in the output space.
    """
    )
    return


@app.cell
def _(mo):
    mo.md(
        r"""
    ## Examine the most Influential Features
    To get a better sense of what the coefficients represent and which features they belong to, you can create a DataFrame that pairs each coefficient with its corresponding feature name. This helps you see which inputs have the strongest impact on the model’s predictions.
    """
    )
    return


@app.cell
def _(coefficients, feature_names, np, pd):
    def interpret_model():
        coef_df = pd.DataFrame({
            'Feature': feature_names,
            'Coefficient': coefficients,
            'Abs_Coefficient': np.abs(coefficients)
        }).sort_values('Abs_Coefficient', ascending=False)
        return coef_df
    return (interpret_model,)


@app.cell
def _(interpret_model):
    coef_df = interpret_model()
    coef_df
    return (coef_df,)


@app.cell
def _(mo):
    mo.md(r"""According to your model's coefs and intercept, the three most important features are:""")
    return


@app.cell
def _(coef_df):
    top_features = coef_df.head(3)
    print("\nTop 3 Most Important Features:")
    for _, row in top_features.iterrows():
        effect = "increases" if row['Coefficient'] > 0 else "decreases"
        print(f"• {row['Feature']}: {effect} price by {abs(row['Coefficient']):.4f} units")
    return


@app.cell
def _(mo):
    mo.md(
        r"""
    The coefficients and their signs tell you a lot about the model.

    **Latitude has the strongest negative effect**: moving north (higher latitude) decreases price.

    **Longitude is next**: moving east (higher longitude) also decreases price.

    **MedInc (median income) strongly increases housing price**: richer areas tend to have more expensive homes.
    """
    )
    return


@app.cell
def _(mo):
    mo.md(r"""🤔 Question: What effect does `AveRooms` have on your model?""")
    return


@app.cell
def _(mo):
    mo.md(r"""# Explain a Neural Network""")
    return


@app.cell
def _(mo):
    mo.md(r"""## Understand SHAP""")
    return


@app.cell
def _(mo):
    mo.md(
        r"""
    SHAP stands for SHapley Additive exPlanations.

    Shapley values come from coalitional game theory. They were introduced by Lloyd Shapley in 1953 as a principled way to distribute total gains fairly among players who form coalitions. The core idea is this:

    > Each player’s payout should reflect their average contribution across all possible orders in which players could join the team.

    In the context of machine learning:

    - Each feature is treated as a player.

    - The model's output is the total “reward” to be distributed.

    - SHAP assigns each feature a value that reflects how much it contributed to the prediction, across all possible subsets of features.

    It’s a way to explain a model’s output that’s consistent, additive, and fair.
    """
    )
    return


@app.cell
def _(mo):
    mo.md(r"""## Use SHAP""")
    return


@app.cell
def _(joblib, load_data, np, pd):
    import shap 

    def load_nn_model():
        model_data = joblib.load('neural_network_model.pkl')
        return model_data['model'], model_data['scaler'], model_data['feature_names']

    def perform_nn_shap_analysis():
        model, scaler, feature_names = load_nn_model()
        data = load_data()

        # Use only the features that were used during training (exclude target)
        X = data[feature_names]
        X_scaled = scaler.transform(X)

        # Use a subset for SHAP analysis 
        X_sample = X_scaled[:1000]

        # Create a wrapper function for the neural network
        def nn_predict(X):
            return model.predict(X)

        # Use KernelExplainer for neural networks
        explainer = shap.KernelExplainer(nn_predict, X_sample[:100])
        shap_values = explainer.shap_values(X_sample[:100])

        print("Neural Network SHAP Analysis Results:")
        print("=" * 50)

        # Calculate feature importance
        feature_importance = np.abs(shap_values).mean(0)
        importance_df = pd.DataFrame({
            'Feature': feature_names,
            'SHAP_Importance': feature_importance
        }).sort_values('SHAP_Importance', ascending=False)

        print("\nFeature Importance (SHAP):")
        print(importance_df)

        return shap_values, importance_df

    perform_nn_shap_analysis() 
    return


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