%23%20%2F%2F%2F%20script%0A%23%20dependencies%20%3D%20%5B%0A%23%20%20%20%20%20%22marimo%22%2C%0A%23%20%20%20%20%20%22matplotlib%3D%3D3.10.8%22%2C%0A%23%20%20%20%20%20%22numpy%3D%3D2.4.1%22%2C%0A%23%20%20%20%20%20%22scikit-learn%3D%3D1.8.0%22%2C%0A%23%20%5D%0A%23%20requires-python%20%3D%20%22%3E%3D3.13%22%0A%23%20%2F%2F%2F%0A%0Aimport%20marimo%0A%0A__generated_with%20%3D%20%220.19.7%22%0Aapp%20%3D%20marimo.App(%0A%20%20%20%20width%3D%22medium%22%2C%0A%20%20%20%20layout_file%3D%22data%3Aapplication%2Fjson%3Bbase64%2CewogICJ0eXBlIjogInNsaWRlcyIsCiAgImRhdGEiOiB7fQp9%22%2C%0A)%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20import%20marimo%20as%20mo%0A%20%20%20%20import%20numpy%20as%20np%0A%20%20%20%20import%20matplotlib.pyplot%20as%20plt%0A%20%20%20%20from%20sklearn.linear_model%20import%20LinearRegression%0A%20%20%20%20from%20sklearn.preprocessing%20import%20PolynomialFeatures%0A%20%20%20%20from%20sklearn.pipeline%20import%20Pipeline%0A%20%20%20%20from%20sklearn.mixture%20import%20GaussianMixture%0A%20%20%20%20return%20LinearRegression%2C%20Pipeline%2C%20PolynomialFeatures%2C%20mo%2C%20np%2C%20plt%0A%0A%0A%40app.cell%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%20S%C3%A9ance%2002%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(mo)%3A%0A%20%20%20%20mo.outline()%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%20Generalized%20Linear%20Model%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(np%2C%20plt)%3A%0A%20%20%20%20x%20%3D%20np.random.uniform(low%3D0.0%2C%20high%3D1.0%2C%20size%3D(100%2C))%0A%20%20%20%20y%20%3D%20np.exp(-(x**2))%20*%20np.sin(10.0%20*%20x)%20%2B%200.1%20*%20np.random.randn(100)%0A%0A%20%20%20%20_fig%2C%20_rep%20%3D%20plt.subplots()%0A%20%20%20%20_rep.set_title(%22Data%22)%0A%20%20%20%20_rep.scatter(x%2C%20y)%0A%20%20%20%20return%20x%2C%20y%0A%0A%0A%40app.cell%0Adef%20_(LinearRegression%2C%20np%2C%20plt%2C%20x%2C%20y)%3A%0A%20%20%20%20X1%20%3D%20x.reshape(100%2C%201)%0A%0A%20%20%20%20lr%20%3D%20LinearRegression()%0A%0A%20%20%20%20lr.fit(X1%2C%20y)%0A%0A%20%20%20%20_fig%2C%20_rep%20%3D%20plt.subplots()%0A%0A%20%20%20%20xs%20%3D%20np.linspace(0%2C%201%2C%20100)%0A%20%20%20%20ys%20%3D%20lr.predict(xs.reshape(-1%2C%201))%0A%20%20%20%20_rep.set_title(%22Linear%20Regression%22)%0A%20%20%20%20_rep.plot(xs%2C%20ys%2C%20color%3D%22red%22%2C%20label%3D%22prediction%22)%0A%20%20%20%20_rep.scatter(x%2C%20y%2C%20label%3D%22data%22)%0A%20%20%20%20_rep.legend()%0A%20%20%20%20return%20X1%2C%20xs%0A%0A%0A%40app.cell%0Adef%20_(LinearRegression%2C%20Pipeline%2C%20PolynomialFeatures%2C%20X1%2C%20plt%2C%20x%2C%20xs%2C%20y)%3A%0A%20%20%20%20poly5%20%3D%20Pipeline(%0A%20%20%20%20%20%20%20%20steps%3D%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22puissances%22%2C%20PolynomialFeatures(degree%3D5))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22r%C3%A9gression%22%2C%20LinearRegression())%2C%0A%20%20%20%20%20%20%20%20%5D%0A%20%20%20%20)%0A%0A%20%20%20%20poly5.fit(X1%2C%20y)%0A%0A%20%20%20%20_fig%2C%20_rep%20%3D%20plt.subplots()%0A%0A%0A%20%20%20%20ys2%20%3D%20poly5.predict(xs.reshape(-1%2C%201))%0A%20%20%20%20_rep.set_title(%22Generalized%20Linear%20Regression%22)%0A%20%20%20%20_rep.plot(xs%2C%20ys2%2C%20color%3D%22red%22%2C%20label%3D%22prediction%22)%0A%20%20%20%20_rep.scatter(x%2C%20y%2C%20label%3D%22data%22)%0A%20%20%20%20_rep.legend()%0A%20%20%20%20return%20(poly5%2C)%0A%0A%0A%40app.cell%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%20Probl%C3%A8me%20inverse%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(np%2C%20plt)%3A%0A%20%20%20%20yi%20%3D%20np.random.uniform(low%3D0.0%2C%20high%3D1.0%2C%20size%3D(100%2C))%0A%20%20%20%20xi%20%3D%20np.sin(np.pi%20*%20yi)%20%2B%200.1%20*%20np.random.randn(100)%0A%0A%20%20%20%20_fig%2C%20_rep%20%3D%20plt.subplots()%0A%20%20%20%20_rep.set_title(%22data%22)%0A%20%20%20%20_rep.scatter(xi%2C%20yi)%0A%20%20%20%20return%20xi%2C%20yi%0A%0A%0A%40app.cell%0Adef%20_(LinearRegression%2C%20np%2C%20plt%2C%20xi%2C%20yi)%3A%0A%20%20%20%20X1i%20%3D%20xi.reshape(100%2C%201)%0A%0A%20%20%20%20lri%20%3D%20LinearRegression()%0A%0A%20%20%20%20lri.fit(X1i%2C%20yi)%0A%0A%20%20%20%20_fig%2C%20_rep%20%3D%20plt.subplots()%0A%0A%20%20%20%20xsi%20%3D%20np.linspace(0%2C%201%2C%20100)%0A%20%20%20%20ysi%20%3D%20lri.predict(xsi.reshape(-1%2C%201))%0A%20%20%20%20_rep.set_title(%22Linear%20Regression%22)%0A%20%20%20%20_rep.plot(xsi%2C%20ysi%2C%20color%3D%22red%22%2C%20label%3D%22prediction%22)%0A%20%20%20%20_rep.scatter(xi%2C%20yi%2C%20label%3D%22data%22)%0A%20%20%20%20_rep.legend()%0A%20%20%20%20return%20X1i%2C%20xsi%0A%0A%0A%40app.cell%0Adef%20_(%0A%20%20%20%20LinearRegression%2C%0A%20%20%20%20Pipeline%2C%0A%20%20%20%20PolynomialFeatures%2C%0A%20%20%20%20X1i%2C%0A%20%20%20%20plt%2C%0A%20%20%20%20poly5%2C%0A%20%20%20%20xi%2C%0A%20%20%20%20xsi%2C%0A%20%20%20%20yi%2C%0A)%3A%0A%20%20%20%20poly5i%20%3D%20Pipeline(%0A%20%20%20%20%20%20%20%20steps%3D%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22puissances%22%2C%20PolynomialFeatures(degree%3D5))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22r%C3%A9gression%22%2C%20LinearRegression())%2C%0A%20%20%20%20%20%20%20%20%5D%0A%20%20%20%20)%0A%0A%20%20%20%20poly5i.fit(X1i%2C%20yi)%0A%0A%20%20%20%20_fig%2C%20_rep%20%3D%20plt.subplots()%0A%0A%0A%20%20%20%20ys2i%20%3D%20poly5.predict(xsi.reshape(-1%2C%201))%0A%20%20%20%20_rep.set_title(%22Generalized%20Linear%20Regression%22)%0A%20%20%20%20_rep.plot(xsi%2C%20ys2i%2C%20color%3D%22red%22%2C%20label%3D%22prediction%22)%0A%20%20%20%20_rep.scatter(xi%2C%20yi%2C%20label%3D%22data%22)%0A%20%20%20%20_rep.legend()%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%20Gaussian%20Mixture%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(mo)%3A%0A%20%20%20%20p%20%3D%20mo.ui.slider(start%3D0.01%2C%20stop%3D0.99%2C%20step%3D0.05%2C%20value%3D0.5%2C%20label%3D%22p%22%2C%20show_value%3DTrue)%0A%20%20%20%20m1%20%3D%20mo.ui.slider(start%3D0.01%2C%20stop%3D0.99%2C%20step%3D0.05%2C%20value%3D0.1%2C%20label%3D%22m1%22%2C%20show_value%3DTrue)%0A%20%20%20%20m2%20%3D%20mo.ui.slider(start%3D0.01%2C%20stop%3D0.99%2C%20step%3D0.05%2C%20value%3D0.9%2C%20label%3D%22m2%22%2C%20show_value%3DTrue)%0A%20%20%20%20sigma%20%3D%20mo.ui.slider(%0A%20%20%20%20%20%20%20%20start%3D0.01%2C%20stop%3D1.0%2C%20step%3D0.01%2C%20value%3D0.1%2C%20label%3D%22sigma%22%2C%20show_value%3DTrue%0A%20%20%20%20)%0A%20%20%20%20return%20m1%2C%20m2%2C%20p%2C%20sigma%0A%0A%0A%40app.cell%0Adef%20_(m1%2C%20m2%2C%20mo%2C%20np%2C%20p%2C%20plt%2C%20sigma)%3A%0A%20%20%20%20_x%20%3D%20np.linspace(-0.5%2C%201.5%2C%20200)%0A%20%20%20%20_y%20%3D%20p.value%20*%20np.exp(-((_x%20-%20m1.value)%20**%202)%20%2F%20(2%20*%20sigma.value))%20%2B%20(1%20-%20p.value)%20*%20np.exp(%0A%20%20%20%20%20%20%20%20-((_x%20-%20m2.value)%20**%202)%20%2F%20(2%20*%20sigma.value)%0A%20%20%20%20)%0A%20%20%20%20_fig%2C%20_rep%20%3D%20plt.subplots()%0A%20%20%20%20_rep.set_xticks(%5B0%2C%20m1.value%2C%20m2.value%2C%201.0%5D)%0A%20%20%20%20_rep.set_xticklabels(%5B%220%22%2C%20%22%24m_1%24%22%2C%20%22%24m_2%24%22%2C%20%221%22%5D)%0A%20%20%20%20_rep.set_ylim(0.0%2C%203.0)%0A%20%20%20%20_rep.plot(_x%2C%20_y%20%2F%20np.sqrt(2%20*%20np.pi%20*%20sigma.value))%0A%20%20%20%20_rep.set_title(%0A%20%20%20%20%20%20%20%20r%22%24y%3Dp%20e%5E%7B-%5Cfrac%7B(x%20-%20m_1)%5E2%7D%7B2%20%5Csigma%7D%7D%20%2B%20(1-p)%20e%5E%7B-%5Cfrac%7B(x%20-%20m_2)%5E2%7D%7B2%20%5Csigma%7D%7D%24%22%0A%20%20%20%20)%0A%20%20%20%20mo.hstack(%5B_rep%2C%20mo.vstack(%5Bm1%2C%20m2%2C%20p%2C%20sigma%5D)%5D)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_()%3A%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
9c09e9d318f0a3bb800acd181718d676