From fdc33f3813119720ceeb2ad7a1273847caabd158 Mon Sep 17 00:00:00 2001 From: Visualize-ML <105787223+Visualize-ML@users.noreply.github.com> Date: Tue, 13 Sep 2022 06:16:36 -0400 Subject: [PATCH] Add files via upload --- .../Streamlit_Bk4_Ch3_01.py | 56 +++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 Book4_Ch03_Python_Codes/Streamlit_Bk4_Ch3_01.py diff --git a/Book4_Ch03_Python_Codes/Streamlit_Bk4_Ch3_01.py b/Book4_Ch03_Python_Codes/Streamlit_Bk4_Ch3_01.py new file mode 100644 index 0000000..586cfa0 --- /dev/null +++ b/Book4_Ch03_Python_Codes/Streamlit_Bk4_Ch3_01.py @@ -0,0 +1,56 @@ + +############### +# Authored by Weisheng Jiang +# Book 4 | From Basic Arithmetic to Machine Learning +# Published and copyrighted by Tsinghua University Press +# Beijing, China, 2022 +############### + +import streamlit as st +import plotly.graph_objects as go +import numpy as np + +x1 = np.linspace(-2.5, 2.5, num=101); +x2 = x1; +xx1, xx2 = np.meshgrid(x1,x2) + +with st.sidebar: + + st.write('Note: Lp norm, p >= 1') + p = st.slider('p', + min_value = -20.0, + max_value = 20., + step = 0.2) + +zz = ((np.abs((xx1))**p) + (np.abs((xx2))**p))**(1./p) + +fig_1 = go.Figure(data = + go.Contour(z = zz, + x = x1, + y = x2, + colorscale='RdYlBu_r')) + +fig_1.update_layout( + autosize=False, + width=500, + height=500, + margin=dict( + l=50, + r=50, + b=100, + t=100, + pad = 4)) + +fig_2 = go.Figure( + go.Surface( + x = x1, + y = x2, + z = zz, + colorscale='RdYlBu_r')) + + +with st.expander('2D contour'): + st.plotly_chart(fig_1) + +with st.expander('3D surface'): + st.plotly_chart(fig_2) \ No newline at end of file