import%20marimo%0A%0A__generated_with%20%3D%20%220.24.0%22%0Aapp%20%3D%20marimo.App(width%3D%22medium%22)%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20import%20marimo%20as%20mo%0A%0A%20%20%20%20return%20(mo%2C)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%20Note%2001%3A%20Inner%20Products%3A%20Measuring%20Similarity%20in%20Vector%20Spaces%0A%0A%20%20%20%20%26larr%3B%20Previous%20Note%3A%20%5B00%20Systems%20of%20Linear%20Equations%5D(00_introduction.py)%20%7C%20Next%20Note%3A%20%5B02%20Norms%20and%20Metrics%5D(02_norm_and_metric.py)%20%26rarr%3B%0A%0A%20%20%20%20---%0A%0A%20%20%20%20%23%23%20%5Ba%5D%20Why%20do%20you%20need%20to%20know%20these%20concepts%3F%0A%0A%20%20%20%20Every%20time%20a%20machine%20learning%20model%20computes%20a%20**similarity%20score**%2C%20a%20**distance**%2C%20or%20a%20**projection**%2C%20it%20is%20evaluating%20an%20inner%20product.%20This%20single%20operation%20is%20the%20computational%20atom%20behind%3A%0A%0A%20%20%20%20-%20**Cosine%20similarity**%20in%20recommendation%20engines%20and%20search%20(how%20similar%20are%20two%20document%20embeddings%3F)%0A%20%20%20%20-%20**Attention%20scores**%20in%20Transformers%20(%24%5Cmathbf%7Bq%7D%5ET%20%5Cmathbf%7Bk%7D%24%3A%20how%20relevant%20is%20token%20%24j%24%20to%20token%20%24i%24%3F)%0A%20%20%20%20-%20**Kernel%20methods**%20in%20SVMs%20(computing%20decision%20boundaries%20in%20implicitly%20high-dimensional%20spaces%20without%20ever%20constructing%20those%20spaces)%0A%20%20%20%20-%20**Confusion%20matrices**%20as%20dot%20products%20(TP%2C%20FP%2C%20FN%2C%20TN%20computed%20via%20binary%20vector%20inner%20products)%0A%20%20%20%20-%20**Projection**%20of%20data%20onto%20principal%20components%20(PCA)%2C%20regression%20hyperplanes%2C%20and%20orthogonal%20bases%0A%0A%20%20%20%20Without%20inner%20products%2C%20there%20is%20no%20notion%20of%20angle%2C%20length%2C%20orthogonality%2C%20or%20similarity%2C%20and%20therefore%20no%20geometry%20to%20learn%20from.%0A%0A%20%20%20%20**Prerequisites**%3A%20Chapter%201%20(vectors%2C%20linear%20combinations%2C%20matrix-vector%20multiplication).%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20---%0A%0A%20%20%20%20%23%23%20%5Bb%5D%20Concept%20explanation%20with%20their%20role%20in%20ML%2FAI%2FStats%3F%0A%0A%20%20%20%20%23%23%23%20Definition%3A%20What%20Is%20an%20Inner%20Product%3F%0A%0A%20%20%20%20An%20**inner%20product**%20on%20a%20vector%20space%20%24V%24%20is%20a%20function%20%24%5Clangle%20%5Ccdot%2C%20%5Ccdot%20%5Crangle%20%3A%20V%20%5Ctimes%20V%20%5Cto%20F%24%20(where%20%24F%20%3D%20%5Cmathbb%7BR%7D%24%20or%20%24%5Cmathbb%7BC%7D%24)%20satisfying%3A%0A%0A%20%20%20%20%7C%20Property%20%7C%20Formula%20%7C%20Intuition%20%7C%0A%20%20%20%20%7C%20%3A---%20%7C%20%3A---%20%7C%20%3A---%20%7C%0A%20%20%20%20%7C%20**Linearity**%20%7C%20%24%5Clangle%20au%20%2B%20bv%2C%20w%20%5Crangle%20%3D%20a%5Clangle%20u%2C%20w%20%5Crangle%20%2B%20b%5Clangle%20v%2C%20w%20%5Crangle%24%20%7C%20Distributes%20over%20addition%20and%20scaling%20%7C%0A%20%20%20%20%7C%20**Conjugate%20symmetry**%20%7C%20%24%5Clangle%20u%2C%20v%20%5Crangle%20%3D%20%5Coverline%7B%5Clangle%20v%2C%20u%20%5Crangle%7D%24%20%7C%20Order%20matters%20only%20over%20%24%5Cmathbb%7BC%7D%24%20%7C%0A%20%20%20%20%7C%20**Positive%20definiteness**%20%7C%20%24%5Clangle%20v%2C%20v%20%5Crangle%20%5Cgeq%200%24%2C%20with%20equality%20iff%20%24v%20%3D%20%5Cmathbf%7B0%7D%24%20%7C%20Non-zero%20vectors%20have%20positive%20%22length%22%20%7C%0A%0A%20%20%20%20In%20%24%5Cmathbb%7BR%7D%5En%24%2C%20the%20standard%20inner%20product%20is%20the%20familiar%20**dot%20product**%3A%0A%0A%20%20%20%20%24%24%0A%20%20%20%20%5Clangle%20%5Cmathbf%7Bu%7D%2C%20%5Cmathbf%7Bv%7D%20%5Crangle%20%3D%20%5Cmathbf%7Bu%7D%20%5Ccdot%20%5Cmathbf%7Bv%7D%20%3D%20%5Csum_%7Bi%3D1%7D%5En%20u_i%20v_i%20%3D%20%5Cmathbf%7Bu%7D%5ET%20%5Cmathbf%7Bv%7D%0A%20%20%20%20%24%24%0A%0A%20%20%20%20%23%23%23%20What%20the%20Inner%20Product%20Unlocks%0A%0A%20%20%20%20From%20this%20single%20operation%2C%20we%20derive%20the%20entire%20geometric%20toolkit%3A%0A%0A%20%20%20%20%7C%20Derived%20Concept%20%7C%20Formula%20%7C%20Where%20It%20Appears%20%7C%0A%20%20%20%20%7C%20%3A---%20%7C%20%3A---%20%7C%20%3A---%20%7C%0A%20%20%20%20%7C%20**Length%20(Norm)**%20%7C%20%24%5C%7C%5Cmathbf%7Bv%7D%5C%7C%20%3D%20%5Csqrt%7B%5Clangle%20%5Cmathbf%7Bv%7D%2C%20%5Cmathbf%7Bv%7D%20%5Crangle%7D%24%20%7C%20Weight%20magnitudes%2C%20gradient%20norms%20%7C%0A%20%20%20%20%7C%20**Angle%20%2F%20Cosine%20Similarity**%20%7C%20%24%5Ccos%5Ctheta%20%3D%20%5Cfrac%7B%5Clangle%20%5Cmathbf%7Bu%7D%2C%20%5Cmathbf%7Bv%7D%20%5Crangle%7D%7B%5C%7C%5Cmathbf%7Bu%7D%5C%7C%20%5C%7C%5Cmathbf%7Bv%7D%5C%7C%7D%24%20%7C%20Semantic%20similarity%2C%20attention%20scores%20%7C%0A%20%20%20%20%7C%20**Orthogonality**%20%7C%20%24%5Clangle%20%5Cmathbf%7Bu%7D%2C%20%5Cmathbf%7Bv%7D%20%5Crangle%20%3D%200%24%20%7C%20PCA%20axes%2C%20decorrelated%20features%20%7C%0A%20%20%20%20%7C%20**Projection**%20%7C%20%24%5Ctext%7Bproj%7D_%7B%5Cmathbf%7Bu%7D%7D(%5Cmathbf%7Bv%7D)%20%3D%20%5Cfrac%7B%5Clangle%20%5Cmathbf%7Bv%7D%2C%20%5Cmathbf%7Bu%7D%20%5Crangle%7D%7B%5Clangle%20%5Cmathbf%7Bu%7D%2C%20%5Cmathbf%7Bu%7D%20%5Crangle%7D%20%5Cmathbf%7Bu%7D%24%20%7C%20Gram-Schmidt%2C%20least-squares%2C%20QR%20%7C%0A%0A%20%20%20%20---%0A%0A%20%20%20%20%23%23%23%20Role%20in%20Statistics%3A%20The%20Confusion%20Matrix%20as%20a%20Dot%20Product%0A%0A%20%20%20%20For%20binary%20classification%20with%20true%20labels%20%24%5Cmathbf%7By%7D%20%5Cin%20%5C%7B0%2C1%5C%7D%5En%24%20and%20predictions%20%24%5Chat%7B%5Cmathbf%7By%7D%7D%20%5Cin%20%5C%7B0%2C1%5C%7D%5En%24%2C%20every%20cell%20of%20the%20confusion%20matrix%20is%20an%20inner%20product%3A%0A%0A%20%20%20%20%24%24%0A%20%20%20%20%5Ctext%7BTP%7D%20%3D%20%5Cmathbf%7By%7D%20%5Ccdot%20%5Chat%7B%5Cmathbf%7By%7D%7D%2C%20%5Cquad%0A%20%20%20%20%5Ctext%7BFN%7D%20%3D%20%5Cmathbf%7By%7D%20%5Ccdot%20(%5Cmathbf%7B1%7D%20-%20%5Chat%7B%5Cmathbf%7By%7D%7D)%2C%20%5Cquad%0A%20%20%20%20%5Ctext%7BFP%7D%20%3D%20(%5Cmathbf%7B1%7D%20-%20%5Cmathbf%7By%7D)%20%5Ccdot%20%5Chat%7B%5Cmathbf%7By%7D%7D%2C%20%5Cquad%0A%20%20%20%20%5Ctext%7BTN%7D%20%3D%20(%5Cmathbf%7B1%7D%20-%20%5Cmathbf%7By%7D)%20%5Ccdot%20(%5Cmathbf%7B1%7D%20-%20%5Chat%7B%5Cmathbf%7By%7D%7D)%0A%20%20%20%20%24%24%0A%0A%20%20%20%20Once%20TP%20is%20known%2C%20the%20other%20three%20counts%20follow%20from%20the%20class%20totals%2C%20so%20only%20**one%20full%20dot%20product**%20is%20required.%0A%0A%20%20%20%20---%0A%0A%20%20%20%20%23%23%23%20Role%20in%20Machine%20Learning%3A%20The%20Kernel%20Trick%20%26%20SVMs%0A%0A%20%20%20%20In%20the%20dual%20form%20of%20the%20soft-margin%20SVM%2C%20the%20optimization%20depends%20entirely%20on%20pairwise%20inner%20products%3A%0A%0A%20%20%20%20%24%24%0A%20%20%20%20%5Cmax_%7B%5Calpha%7D%20%5Csum_%7Bi%3D1%7D%5En%20%5Calpha_i%20-%20%5Cfrac%7B1%7D%7B2%7D%20%5Csum_%7Bi%2Cj%7D%20%5Calpha_i%20%5Calpha_j%20y_i%20y_j%20%5Clangle%20%5Cmathbf%7Bx%7D_i%2C%20%5Cmathbf%7Bx%7D_j%20%5Crangle%0A%20%20%20%20%5Cquad%20%5Ctext%7Bs.t.%7D%20%5Cquad%200%20%5Cleq%20%5Calpha_i%20%5Cleq%20C%2C%20%5Cquad%20%5Csum_i%20%5Calpha_i%20y_i%20%3D%200%0A%20%20%20%20%24%24%0A%0A%20%20%20%20**The%20Kernel%20Trick**%3A%20Replace%20%24%5Clangle%20%5Cmathbf%7Bx%7D_i%2C%20%5Cmathbf%7Bx%7D_j%20%5Crangle%24%20with%20a%20kernel%20function%20%24K(%5Cmathbf%7Bx%7D_i%2C%20%5Cmathbf%7Bx%7D_j)%20%3D%20%5Clangle%20%5Cphi(%5Cmathbf%7Bx%7D_i)%2C%20%5Cphi(%5Cmathbf%7Bx%7D_j)%20%5Crangle%24%20that%20implicitly%20computes%20inner%20products%20in%20a%20higher-dimensional%20feature%20space%2C%20without%20ever%20constructing%20%24%5Cphi(%5Cmathbf%7Bx%7D)%24%20explicitly%3A%0A%0A%20%20%20%20%7C%20Kernel%20%7C%20%24K(%5Cmathbf%7Bx%7D%2C%20%5Cmathbf%7By%7D)%24%20%7C%20Effect%20%7C%0A%20%20%20%20%7C%20%3A---%20%7C%20%3A---%20%7C%20%3A---%20%7C%0A%20%20%20%20%7C%20Linear%20%7C%20%24%5Cmathbf%7Bx%7D%20%5Ccdot%20%5Cmathbf%7By%7D%24%20%7C%20No%20transformation%20%7C%0A%20%20%20%20%7C%20Polynomial%20(degree%20%24d%24)%20%7C%20%24(%5Cmathbf%7Bx%7D%20%5Ccdot%20%5Cmathbf%7By%7D%20%2B%201)%5Ed%24%20%7C%20Captures%20interactions%20up%20to%20order%20%24d%24%20%7C%0A%20%20%20%20%7C%20RBF%20(Gaussian)%20%7C%20%24%5Cexp(-%5Cgamma%20%5C%7C%5Cmathbf%7Bx%7D%20-%20%5Cmathbf%7By%7D%5C%7C%5E2)%24%20%7C%20Infinite-dimensional%20feature%20space%20%7C%0A%0A%20%20%20%20---%0A%0A%20%20%20%20%23%23%23%20Role%20in%20Deep%20Learning%3A%20Attention%20Scores%0A%0A%20%20%20%20The%20scaled%20dot-product%20attention%20in%20Transformers%20is%20fundamentally%20a%20**batch%20of%20inner%20products**%20between%20query%20and%20key%20vectors%3A%0A%0A%20%20%20%20%24%24%0A%20%20%20%20%5Ctext%7BAttention%7D(Q%2C%20K%2C%20V)%20%3D%20%5Ctext%7Bsoftmax%7D%5Cleft(%5Cfrac%7BQK%5ET%7D%7B%5Csqrt%7Bd_k%7D%7D%5Cright)V%0A%20%20%20%20%24%24%0A%0A%20%20%20%20Each%20element%20of%20%24QK%5ET%24%20is%20%24%5Clangle%20%5Cmathbf%7Bq%7D_i%2C%20%5Cmathbf%7Bk%7D_j%20%5Crangle%24%2C%20the%20inner%20product%20that%20measures%20how%20much%20token%20%24i%24%20should%20attend%20to%20token%20%24j%24.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20---%0A%0A%20%20%20%20%23%23%20%5Bc%5D%20Code%20Examples%0A%0A%20%20%20%20%23%23%23%20Example%201%3A%20Confusion%20Matrix%20via%20the%20Dot%20Product%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20import%20numpy%20as%20np%0A%0A%20%20%20%20y_true%20%3D%20np.array(%5B0%2C%201%2C%201%2C%201%2C%200%2C%200%2C%200%2C%201%2C%201%2C%200%5D%20*%2010000)%0A%20%20%20%20y_pred%20%3D%20np.array(%5B1%2C%201%2C%201%2C%200%2C%200%2C%201%2C%201%2C%200%2C%201%2C%201%5D%20*%2010000)%0A%0A%20%20%20%20def%20confusion_matrix_dot(t%2C%20p)%3A%0A%20%20%20%20%20%20%20%20%22%22%22Full%20dot-product%20approach%3A%204%20inner%20products.%22%22%22%0A%20%20%20%20%20%20%20%20TP%20%3D%20t%20%40%20p%0A%20%20%20%20%20%20%20%20FP%20%3D%20(1%20-%20t)%20%40%20p%0A%20%20%20%20%20%20%20%20FN%20%3D%20t%20%40%20(1%20-%20p)%0A%20%20%20%20%20%20%20%20TN%20%3D%20(1%20-%20t)%20%40%20(1%20-%20p)%0A%20%20%20%20%20%20%20%20return%20%7B%22TP%22%3A%20TP%2C%20%22FP%22%3A%20FP%2C%20%22FN%22%3A%20FN%2C%20%22TN%22%3A%20TN%7D%0A%0A%20%20%20%20def%20confusion_matrix_fast(t%2C%20p)%3A%0A%20%20%20%20%20%20%20%20%22%22%22Optimized%3A%201%20dot%20product%20%2B%20scalar%20arithmetic.%22%22%22%0A%20%20%20%20%20%20%20%20TP%20%3D%20t%20%40%20p%0A%20%20%20%20%20%20%20%20P%20%3D%20np.sum(t)%20%20%23%20actual%20positives%0A%20%20%20%20%20%20%20%20PP%20%3D%20np.sum(p)%20%20%23%20predicted%20positives%0A%20%20%20%20%20%20%20%20N%20%3D%20len(t)%20-%20P%20%20%23%20actual%20negatives%0A%20%20%20%20%20%20%20%20FP%20%3D%20PP%20-%20TP%0A%20%20%20%20%20%20%20%20FN%20%3D%20P%20-%20TP%0A%20%20%20%20%20%20%20%20TN%20%3D%20N%20-%20FP%0A%20%20%20%20%20%20%20%20return%20%7B%22TP%22%3A%20TP%2C%20%22FP%22%3A%20FP%2C%20%22FN%22%3A%20FN%2C%20%22TN%22%3A%20TN%7D%0A%0A%20%20%20%20cm_full%20%3D%20confusion_matrix_dot(y_true%2C%20y_pred)%0A%20%20%20%20cm_fast%20%3D%20confusion_matrix_fast(y_true%2C%20y_pred)%0A%0A%20%20%20%20assert%20cm_full%20%3D%3D%20cm_fast%2C%20%22Results%20must%20match%22%0A%20%20%20%20cm_full%0A%20%20%20%20return%20(np%2C)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%23%20Example%202%3A%20Kernel%20Trick%3A%20Polynomial%20Kernel%20vs%20Explicit%20Feature%20Mapping%0A%0A%20%20%20%20For%20%24%5Cmathbf%7Bx%7D_1%20%3D%20(1%2C%202)%24%20and%20%24%5Cmathbf%7Bx%7D_2%20%3D%20(3%2C%204)%24%20with%20degree-2%20polynomial%20kernel%20%24K(%5Cmathbf%7Bx%7D%2C%20%5Cmathbf%7By%7D)%20%3D%20(%5Cmathbf%7Bx%7D%20%5Ccdot%20%5Cmathbf%7By%7D%20%2B%201)%5E2%24%3A%0A%0A%20%20%20%20**Explicit%20mapping**%3A%20%24%5Cphi(%5Cmathbf%7Bx%7D)%20%3D%20(1%2C%20%5Csqrt%7B2%7Dx_1%2C%20%5Csqrt%7B2%7Dx_2%2C%20x_1%5E2%2C%20%5Csqrt%7B2%7D%5C%2Cx_1%20x_2%2C%20x_2%5E2)%24%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(np)%3A%0A%20%20%20%20%23%20Kernel%20trick%3A%20compute%20in%20input%20space%0A%20%20%20%20x1_kern%20%3D%20np.array(%5B1%2C%202%5D)%0A%20%20%20%20x2_kern%20%3D%20np.array(%5B3%2C%204%5D)%0A%20%20%20%20kernel_result%20%3D%20(x1_kern%20%40%20x2_kern%20%2B%201)%20**%202%0A%0A%20%20%20%20%23%20Explicit%20feature%20mapping%3A%20compute%20in%20transformed%20space%0A%20%20%20%20def%20phi(x)%3A%0A%20%20%20%20%20%20%20%20return%20np.array(%5B1.0%2C%20np.sqrt(2)%20*%20x%5B0%5D%2C%20np.sqrt(2)%20*%20x%5B1%5D%2C%20x%5B0%5D%20**%202%2C%20np.sqrt(2)%20*%20x%5B0%5D%20*%20x%5B1%5D%2C%20x%5B1%5D%20**%202%5D)%0A%0A%20%20%20%20explicit_result%20%3D%20phi(x1_kern)%20%40%20phi(x2_kern)%0A%0A%20%20%20%20assert%20np.isclose(kernel_result%2C%20explicit_result)%2C%20%22Kernel%20trick%20must%20equal%20explicit%20mapping%22%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%22kernel_result%22%3A%20kernel_result%2C%0A%20%20%20%20%20%20%20%20%22explicit_result%22%3A%20explicit_result%2C%0A%20%20%20%20%20%20%20%20%22match%22%3A%20np.isclose(kernel_result%2C%20explicit_result)%2C%0A%20%20%20%20%7D%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%23%20Example%203%3A%20SVM%20Decision%20Boundaries%20with%20Different%20Kernels%0A%0A%20%20%20%20The%20inner%20product%20(or%20its%20kernel%20generalization)%20determines%20the%20shape%20of%20the%20decision%20boundary%3A%0A%20%20%20%20-%20**Linear%20kernel**%3A%20straight%20line%0A%20%20%20%20-%20**Polynomial%20kernel**%3A%20curved%20boundary%0A%20%20%20%20-%20**RBF%20kernel**%3A%20highly%20flexible%2C%20locally%20adaptive%20boundary%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(np)%3A%0A%20%20%20%20import%20plotly.graph_objects%20as%20go%0A%20%20%20%20from%20plotly.subplots%20import%20make_subplots%0A%20%20%20%20from%20sklearn.datasets%20import%20make_moons%0A%20%20%20%20from%20sklearn.metrics%20import%20accuracy_score%0A%20%20%20%20from%20sklearn.model_selection%20import%20train_test_split%0A%20%20%20%20from%20sklearn.svm%20import%20SVC%0A%0A%20%20%20%20X%2C%20y%20%3D%20make_moons(n_samples%3D300%2C%20noise%3D0.2%2C%20random_state%3D47)%0A%20%20%20%20X_train%2C%20X_test%2C%20y_train%2C%20y_test%20%3D%20train_test_split(X%2C%20y%2C%20test_size%3D0.2%2C%20random_state%3D47)%0A%0A%20%20%20%20kernels%20%3D%20%5B%22linear%22%2C%20%22poly%22%2C%20%22rbf%22%5D%0A%20%20%20%20C_values%20%3D%20%5B0.1%2C%201.0%2C%2010.0%5D%0A%0A%20%20%20%20fig%20%3D%20make_subplots(%0A%20%20%20%20%20%20%20%20rows%3Dlen(C_values)%2C%0A%20%20%20%20%20%20%20%20cols%3Dlen(kernels)%2C%0A%20%20%20%20%20%20%20%20subplot_titles%3D%5Bf%22%7Bk.capitalize()%7D%20(C%3D%7Bc%7D)%22%20for%20c%20in%20C_values%20for%20k%20in%20kernels%5D%2C%0A%20%20%20%20%20%20%20%20vertical_spacing%3D0.08%2C%0A%20%20%20%20%20%20%20%20horizontal_spacing%3D0.06%2C%0A%20%20%20%20)%0A%0A%20%20%20%20h%20%3D%200.05%0A%20%20%20%20x_min%2C%20x_max%20%3D%20X%5B%3A%2C%200%5D.min()%20-%200.5%2C%20X%5B%3A%2C%200%5D.max()%20%2B%200.5%0A%20%20%20%20y_min%2C%20y_max%20%3D%20X%5B%3A%2C%201%5D.min()%20-%200.5%2C%20X%5B%3A%2C%201%5D.max()%20%2B%200.5%0A%20%20%20%20xx%20%3D%20np.arange(x_min%2C%20x_max%2C%20h)%0A%20%20%20%20yy%20%3D%20np.arange(y_min%2C%20y_max%2C%20h)%0A%20%20%20%20grid_x%2C%20grid_y%20%3D%20np.meshgrid(xx%2C%20yy)%0A%0A%20%20%20%20for%20i%2C%20C%20in%20enumerate(C_values)%3A%0A%20%20%20%20%20%20%20%20for%20j%2C%20kernel%20in%20enumerate(kernels)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20model%20%3D%20SVC(kernel%3Dkernel%2C%20degree%3D2%2C%20C%3DC)%0A%20%20%20%20%20%20%20%20%20%20%20%20model.fit(X_train%2C%20y_train)%0A%20%20%20%20%20%20%20%20%20%20%20%20acc%20%3D%20accuracy_score(y_test%2C%20model.predict(X_test))%0A%20%20%20%20%20%20%20%20%20%20%20%20Z%20%3D%20model.predict(np.c_%5Bgrid_x.ravel()%2C%20grid_y.ravel()%5D).reshape(grid_x.shape)%0A%0A%20%20%20%20%20%20%20%20%20%20%20%20fig.add_trace(%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20go.Contour(%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20x%3Dxx%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20y%3Dyy%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20z%3DZ%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20showscale%3DFalse%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20opacity%3D0.3%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20colorscale%3D%5B%5B0%2C%20%22%233498db%22%5D%2C%20%5B1%2C%20%22%23e74c3c%22%5D%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20hoverinfo%3D%22skip%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20row%3Di%20%2B%201%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20col%3Dj%20%2B%201%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20)%0A%0A%20%20%20%20%20%20%20%20%20%20%20%20show_legend%20%3D%20i%20%3D%3D%200%20and%20j%20%3D%3D%200%0A%20%20%20%20%20%20%20%20%20%20%20%20fig.add_trace(%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20go.Scatter(%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20x%3DX_test%5By_test%20%3D%3D%200%2C%200%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20y%3DX_test%5By_test%20%3D%3D%200%2C%201%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20mode%3D%22markers%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20marker%3Ddict(color%3D%22%232980b9%22%2C%20size%3D6%2C%20line%3Ddict(width%3D0.5%2C%20color%3D%22black%22))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20name%3D%22Class%200%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20showlegend%3Dshow_legend%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20row%3Di%20%2B%201%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20col%3Dj%20%2B%201%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20)%0A%20%20%20%20%20%20%20%20%20%20%20%20fig.add_trace(%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20go.Scatter(%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20x%3DX_test%5By_test%20%3D%3D%201%2C%200%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20y%3DX_test%5By_test%20%3D%3D%201%2C%201%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20mode%3D%22markers%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20marker%3Ddict(color%3D%22%23c0392b%22%2C%20size%3D6%2C%20line%3Ddict(width%3D0.5%2C%20color%3D%22black%22))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20name%3D%22Class%201%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20showlegend%3Dshow_legend%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20row%3Di%20%2B%201%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20col%3Dj%20%2B%201%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20)%0A%0A%20%20%20%20fig.update_layout(%0A%20%20%20%20%20%20%20%20title%3Ddict(text%3D%22SVM%20Decision%20Boundaries%20Across%20Kernels%20and%20Margins%22%2C%20font%3Ddict(size%3D14))%2C%0A%20%20%20%20%20%20%20%20template%3D%22plotly_white%22%2C%0A%20%20%20%20%20%20%20%20width%3D880%2C%0A%20%20%20%20%20%20%20%20height%3D750%2C%0A%20%20%20%20)%0A%0A%20%20%20%20fig%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20---%0A%0A%20%20%20%20%23%23%20%5Bd%5D%20Takeaway%0A%0A%20%20%20%20-%20The%20**inner%20product**%20%24%5Clangle%20%5Cmathbf%7Bu%7D%2C%20%5Cmathbf%7Bv%7D%20%5Crangle%20%3D%20%5Cmathbf%7Bu%7D%5ET%20%5Cmathbf%7Bv%7D%24%20is%20the%20single%20operation%20from%20which%20length%2C%20angle%2C%20orthogonality%2C%20and%20projection%20are%20derived.%0A%20%20%20%20-%20**In%20Statistics**%3A%20The%20confusion%20matrix%20for%20binary%20classification%20can%20be%20computed%20as%20dot%20products%20between%20binary%20label%20vectors%2C%20where%20one%20dot%20product%20plus%20scalar%20arithmetic%20suffices.%0A%20%20%20%20-%20**In%20Machine%20Learning**%3A%20SVMs%20depend%20entirely%20on%20pairwise%20inner%20products.%20The%20**kernel%20trick**%20replaces%20these%20with%20kernel%20functions%20to%20learn%20non-linear%20decision%20boundaries%20without%20explicit%20high-dimensional%20feature%20construction.%0A%20%20%20%20-%20**In%20Deep%20Learning**%3A%20Every%20attention%20score%20in%20a%20Transformer%20is%20an%20inner%20product%20%24%5Clangle%20%5Cmathbf%7Bq%7D_i%2C%20%5Cmathbf%7Bk%7D_j%20%5Crangle%24%2C%20making%20the%20inner%20product%20the%20computational%20heartbeat%20of%20modern%20language%20models.%0A%20%20%20%20-%20**Practical%20rule**%3A%20If%20two%20vectors%20point%20in%20similar%20directions%2C%20their%20inner%20product%20is%20large%20and%20positive.%20If%20orthogonal%2C%20it%20is%20zero.%20If%20opposing%2C%20it%20is%20negative.%20This%20geometric%20intuition%20drives%20similarity%20search%2C%20attention%2C%20and%20classification.%0A%0A%20%20%20%20---%0A%0A%20%20%20%20%26larr%3B%20Previous%20Note%3A%20%5B00%20Systems%20of%20Linear%20Equations%5D(00_introduction.py)%20%7C%20Next%20Note%3A%20%5B02%20Norms%20and%20Metrics%5D(02_norm_and_metric.py)%20%26rarr%3B%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0Aif%20__name__%20%3D%3D%20%22__main__%22%3A%0A%20%20%20%20app.run()%0A
b3e233b14a8a60a123667cfcb3cc3b0d