@@ -278,7 +278,7 @@ cancer["Class"].value_counts(normalize=True)
278
278
```
279
279
280
280
Next, let's draw a colored scatter plot to visualize the relationship between the
281
- perimeter and concavity variables. Recall that ` altair's ` default palette
281
+ perimeter and concavity variables. Recall that the default palette in ` altair `
282
282
is colorblind-friendly, so we can stick with that here.
283
283
284
284
``` {code-cell} ipython3
@@ -639,6 +639,32 @@ new_obs_Concavity = 3.5
639
639
)
640
640
```
641
641
642
+ ``` {code-cell} ipython3
643
+ :tags: [remove-cell]
644
+ # code needed to render the latex table with distance calculations
645
+ from IPython.display import Latex
646
+ five_neighbors = (
647
+ cancer
648
+ [["Perimeter", "Concavity", "Class"]]
649
+ .assign(dist_from_new = (
650
+ (cancer["Perimeter"] - new_obs_Perimeter) ** 2
651
+ + (cancer["Concavity"] - new_obs_Concavity) ** 2
652
+ )**(1/2))
653
+ .nsmallest(5, "dist_from_new")
654
+ ).reset_index()
655
+
656
+ for i in range(5):
657
+ glue(f"gn{i}_perim", "{:0.2f}".format(five_neighbors["Perimeter"][i]))
658
+ glue(f"gn{i}_concav", "{:0.2f}".format(five_neighbors["Concavity"][i]))
659
+ glue(f"gn{i}_class", five_neighbors["Class"][i])
660
+
661
+ # typeset perimeter,concavity with parentheses if negative for latex
662
+ nperim = f"{five_neighbors['Perimeter'][i]:.2f}" if five_neighbors['Perimeter'][i] > 0 else f"({five_neighbors['Perimeter'][i]:.2f})"
663
+ nconcav = f"{five_neighbors['Concavity'][i]:.2f}" if five_neighbors['Concavity'][i] > 0 else f"({five_neighbors['Concavity'][i]:.2f})"
664
+
665
+ glue(f"gdisteqn{i}", Latex(f"\sqrt{{(0-{nperim})^2+(3.5-{nconcav})^2}}={five_neighbors['dist_from_new'][i]:.2f}"))
666
+ ```
667
+
642
668
In {numref}` tab:05-multiknn-mathtable ` we show in mathematical detail how
643
669
we computed the ` dist_from_new ` variable (the
644
670
distance to the new observation) for each of the 5 nearest neighbors in the
@@ -648,11 +674,11 @@ training data.
648
674
:name: tab:05-multiknn-mathtable
649
675
| Perimeter | Concavity | Distance | Class |
650
676
|-----------|-----------|----------------------------------------|-------|
651
- | 0.24 | 2.65 | $\sqrt{(0-0.24)^2+(3.5-2.65)^2}=0.88$| Benign |
652
- | 0.75 | 2.87 | $\sqrt{(0-0.75)^2+(3.5-2.87)^2}=0.98$| Malignant |
653
- | 0.62 | 2.54 | $\sqrt{(0-0.62)^2+(3.5-2.54)^2}=1.14$| Malignant |
654
- | 0.42 | 2.31 | $\sqrt{(0-0.42)^2+(3.5-2.31)^2}=1.26$| Malignant |
655
- | -1.16 | 4.04 | $\sqrt{(0-(-1.16))^2+(3.5-4.04)^2}=1.28$| Benign |
677
+ | {glue:text}`gn0_perim` | {glue:text}`gn0_concav` | {glue:}`gdisteqn0` | {glue:text}`gn0_class` |
678
+ | {glue:text}`gn1_perim` | {glue:text}`gn1_concav` | {glue:}`gdisteqn1` | {glue:text}`gn1_class` |
679
+ | {glue:text}`gn2_perim` | {glue:text}`gn2_concav` | {glue:}`gdisteqn2` | {glue:text}`gn2_class` |
680
+ | {glue:text}`gn3_perim` | {glue:text}`gn3_concav` | {glue:}`gdisteqn3` | {glue:text}`gn3_class` |
681
+ | {glue:text}`gn4_perim` | {glue:text}`gn4_concav` | {glue:}`gdisteqn4` | {glue:text}`gn4_class` |
656
682
```
657
683
658
684
+++
0 commit comments