Add files via upload

This commit is contained in:
Visualize-ML
2022-09-30 16:43:30 -04:00
committed by GitHub
parent 3a8f8289f3
commit a2395b1b7a
9 changed files with 706 additions and 3 deletions

View File

@@ -0,0 +1,88 @@
###############
# 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 numpy as np
from plotly.subplots import make_subplots
import plotly.graph_objects as go
import plotly.express as px
def bmatrix(a):
"""Returns a LaTeX bmatrix
:a: numpy array
:returns: LaTeX bmatrix as a string
"""
if len(a.shape) > 2:
raise ValueError('bmatrix can at most display two dimensions')
lines = str(a).replace('[', '').replace(']', '').splitlines()
rv = [r'\begin{bmatrix}']
rv += [' ' + ' & '.join(l.split()) + r'\\' for l in lines]
rv += [r'\end{bmatrix}']
return '\n'.join(rv)
#%%
with st.sidebar:
num_a = st.slider('Number of rows, a:',
3,6,step = 1)
num_b = st.slider('Number of rows, b:',
3,6,step = 1)
a = np.random.uniform(0,1,num_a).reshape((-1,1))
a = np.round(a,1)
b = np.random.uniform(0,1,num_b).reshape((-1,1))
b = np.round(b,1)
show_number = False
with st.sidebar:
show_number = st.checkbox('Show values:')
tensor_a_b = a@b.T
#%% visualization
st.latex('a = ' + bmatrix(a))
st.latex('b = ' + bmatrix(b))
st.latex('a \otimes b = ab^{T}')
st.latex( bmatrix(a) + '@' + bmatrix(b.T) + ' = ' + bmatrix(tensor_a_b))
col1, col2, col3 = st.columns(3)
with col1:
fig_a = px.imshow(a, text_auto=show_number,
color_continuous_scale='viridis',
aspect = 'equal')
fig_a.update_layout(height=400, width=300)
fig_a.layout.coloraxis.showscale = False
st.plotly_chart(fig_a)
with col2:
fig_b = px.imshow(b, text_auto=show_number,
color_continuous_scale='viridis',
aspect = 'equal')
fig_b.update_layout(height=400, width=300)
fig_b.layout.coloraxis.showscale = False
st.plotly_chart(fig_b)
with col3:
fig_ab = px.imshow(tensor_a_b, text_auto=show_number,
color_continuous_scale='viridis',
aspect = 'equal')
fig_ab.update_layout(height=400, width=400)
fig_ab.layout.coloraxis.showscale = False
st.plotly_chart(fig_ab)

View File

@@ -0,0 +1,156 @@
###############
# Authored by Weisheng Jiang
# Book 4 | From Basic Arithmetic to Machine Learning
# Published and copyrighted by Tsinghua University Press
# Beijing, China, 2022
###############
import plotly.graph_objects as go
import streamlit as st
import numpy as np
import plotly.express as px
import pandas as pd
import sympy
from scipy.spatial import distance
#%% define a function for distances
def fcn_Minkowski(xx, yy, mu, p = 2, Chebychev = False):
if Chebychev:
zz = np.maximum(np.abs(xx - mu[0]),np.abs(yy - mu[1]))
else:
zz = ((np.abs((xx - mu[0]))**p) + (np.abs((yy - mu[1]))**p))**(1./p)
return zz
def fcn_mahal(xx, yy, mu, Sigma, standardized = False):
if standardized:
D = np.diag(np.diag(Sigma))
Sigma_inv = np.linalg.inv(D)
else:
Sigma_inv = np.linalg.inv(Sigma)
xy_ = np.stack((xx.flatten(), yy.flatten())).T
zz = np.diag(np.sqrt(np.dot(np.dot((xy_-mu),Sigma_inv),(xy_-mu).T)))
zz = np.reshape(zz,xx.shape)
return zz
#%%
df = px.data.iris()
with st.sidebar:
dist_type = st.radio('Choose a type of distance: ',
options = ['Euclidean',
'City block',
'Minkowski',
'Chebychev',
'Mahalanobis',
'Standardized Euclidean'])
if dist_type == 'Minkowski':
with st.sidebar:
p = st.slider('Specify a p value:',1.0, 20.0, step = 0.5)
#%% compute distance
X = df[['sepal_length', 'petal_length']]
mu = X.mean().to_numpy()
Sigma = X.cov().to_numpy()
# st.write(mu)
# st.write(Sigma)
x_array = np.linspace(0,10,101)
y_array = np.linspace(0,10,101)
xx,yy = np.meshgrid(x_array,y_array)
if dist_type == 'Minkowski':
zz = fcn_Minkowski(xx, yy, mu, p)
elif dist_type == 'Euclidean':
zz = fcn_Minkowski(xx, yy, mu, 2)
elif dist_type == 'Chebychev':
zz = fcn_Minkowski(xx, yy, mu, Chebychev = True)
elif dist_type == 'Mahalanobis':
zz = fcn_mahal(xx, yy, mu, Sigma)
elif dist_type == 'City block':
zz = fcn_Minkowski(xx, yy, mu, 1)
elif dist_type == 'Standardized Euclidean':
zz = fcn_mahal(xx, yy, mu, Sigma, True)
# st.write(zz)
#%% Visualization
st.title(dist_type + ' distance')
# Scatter plot
fig_2 = px.scatter(df, x='sepal_length', y='petal_length')
# plot distance contour
fig_2.add_trace(go.Contour(
x = x_array,
y = y_array,
z = zz,
contours_coloring='lines',
showscale=False)
)
# st.write(X.mean().to_frame().T)
# plot centroid
# fig_2.add_traces(
# px.scatter(X.mean().to_frame().T,
# x='sepal_length',
# y='petal_length').update_traces(
# marker_size=20,
# marker_color="yellow").data)
fig_2.add_traces(
px.scatter(X.mean().to_frame().T,
x='sepal_length',
y='petal_length').update_traces(
marker_size=20,
marker_color="red",
marker_symbol= 'x').data)
fig_2.update_layout(yaxis_range=[0,10])
fig_2.update_layout(xaxis_range=[0,10])
fig_2.add_hline(y=mu[1])
fig_2.add_vline(x=mu[0])
fig_2.update_yaxes(
scaleratio = 1,
)
fig_2.update_layout(width=600, height=600)
st.plotly_chart(fig_2)

View File

@@ -103,11 +103,11 @@ st.latex(r'''
fig.add_trace(go.Scatter(x=Txvyv[0], y=Txvyv[1],
mode="lines", line_width=lw,
line_color = 'blue'), 1, 2)
line_color = 'red'), 1, 2)
fig.add_trace(go.Scatter(x=Txhyh[0], y=Txhyh[1],
mode="lines", line_width=lw,
line_color = 'red'), 1, 2)
line_color = 'blue'), 1, 2)
fig.update_xaxes(range=[-4, 4])
fig.update_yaxes(range=[-4, 4])
fig.update_layout(width=800, height=500, showlegend=False, template="none",

View File

@@ -0,0 +1,117 @@
###############
# Authored by Weisheng Jiang
# Book 4 | From Basic Arithmetic to Machine Learning
# Published and copyrighted by Tsinghua University Press
# Beijing, China, 2022
###############
import plotly.graph_objects as go
import numpy as np
from plotly.subplots import make_subplots
import streamlit as st
def bmatrix(a):
"""Returns a LaTeX bmatrix
:a: numpy array
:returns: LaTeX bmatrix as a string
"""
if len(a.shape) > 2:
raise ValueError('bmatrix can at most display two dimensions')
lines = str(a).replace('[', '').replace(']', '').splitlines()
rv = [r'\begin{bmatrix}']
rv += [' ' + ' & '.join(l.split()) + r'\\' for l in lines]
rv += [r'\end{bmatrix}']
return '\n'.join(rv)
n = m = 20
fig = make_subplots(rows=1, cols=2, horizontal_spacing=0.035)
xv = []
yv = []
for k in range(-n, n+1):
xv.extend([k, k, np.nan])
yv.extend([-m, m, np.nan])
lw= 1 #line_width
fig.add_trace(go.Scatter(x=xv, y=yv, mode="lines", line_width=lw,
line_color = 'red'), 1, 1)
#set up the lists of horizontal line x and y-end coordinates
xh=[]
yh=[]
for k in range(-m, m+1):
xh.extend([-m, m, np.nan])
yh.extend([k, k, np.nan])
fig.add_trace(go.Scatter(x=xh, y=yh, mode="lines", line_width=lw,
line_color = 'blue'), 1, 1)
with st.sidebar:
st.latex(r'''
R = \begin{bmatrix}
\cos(\theta) & -\sin(\theta)\\
\sin(\theta) & \cos(\theta)
\end{bmatrix}''')
theta = st.slider('Degree',-180, 180, step = 5, value = 0)
theta = theta/180*np.pi
R = np.array([[np.cos(theta), -np.sin(theta)],
[np.sin(theta), np.cos(theta)]], dtype=float)
#get only the coordinates from -3 to 3
# X = np.array(xv[6:-6])
# Y = np.array(yv[6:-6])
X = np.array(xv)
Y = np.array(yv)
# transform by T the vector of coordinates [x, y]^T where the vector runs over the columns of np.stack((X, Y))
Txvyv = R@np.stack((X, Y)) #transform by T the vertical lines
# X = np.array(xh[6:-6])
# Y = np.array(yh[6:-6])
X = np.array(xh)
Y = np.array(yh)
Txhyh = R@np.stack((X, Y))# #transform by T the horizontal lines
st.latex(r'R = ' + bmatrix(R))
r1 = R[:,0].reshape((-1, 1))
r2 = R[:,1].reshape((-1, 1))
st.latex(r'''
r_1 = R e_1 = ''' + bmatrix(R) +
'e_1 = ' + bmatrix(r1)
)
st.latex(r'''
r_2 = R e_2 = ''' + bmatrix(R) +
'e_2 = ' + bmatrix(r2)
)
st.latex(r'\begin{vmatrix} R \end{vmatrix} = ' + str(np.linalg.det(R)))
fig.add_trace(go.Scatter(x=Txvyv[0], y=Txvyv[1],
mode="lines", line_width=lw,
line_color = 'red'), 1, 2)
fig.add_trace(go.Scatter(x=Txhyh[0], y=Txhyh[1],
mode="lines", line_width=lw,
line_color = 'blue'), 1, 2)
fig.update_xaxes(range=[-4, 4])
fig.update_yaxes(range=[-4, 4])
fig.update_layout(width=800, height=500, showlegend=False, template="none",
plot_bgcolor="white", yaxis2_showgrid=False, xaxis2_showgrid=False)
st.plotly_chart(fig)

View File

@@ -39,7 +39,6 @@ def bmatrix(a):
return '\n'.join(rv)
# A copy from Seaborn
iris = load_iris()
X = iris.data
y = iris.target

View File

@@ -0,0 +1,54 @@
###############
# Authored by Weisheng Jiang
# Book 4 | From Basic Arithmetic to Machine Learning
# Published and copyrighted by Tsinghua University Press
# Beijing, China, 2022
###############
import numpy as np
import streamlit as st
import time
# transition matrix
A = np.matrix([[0.7, 0.2],
[0.3, 0.8]])
with st.sidebar:
pi_0_chicken = st.slider('Ratio of chicken:',
0.0, 1.0, step = 0.1)
pi_0_rabbit = 1 - pi_0_chicken
st.write('Ratio of rabbit: ' + str(round(pi_0_rabbit,1)))
num_iterations = st.slider('Number of nights:',
20,100,step = 5)
progress_bar = st.sidebar.progress(0)
status_text = st.sidebar.empty()
last_rows = np.array([[pi_0_chicken, pi_0_rabbit]])
# st.write(last_rows) # row vector
chart = st.line_chart(last_rows)
for i in range(1, num_iterations):
last_status = last_rows[-1,:]
# st.write(last_status)
new_rows = last_status@A.T
percent = (i + 1)*100/num_iterations
status_text.text("%i%% Complete" % percent)
chart.add_rows(new_rows)
progress_bar.progress(i)
last_rows = new_rows
time.sleep(0.1)
progress_bar.empty()

View File

@@ -0,0 +1,87 @@
###############
# 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.express as px
import numpy as np
import pandas as pd
from sklearn.datasets import load_iris
#%%
# A copy from Seaborn
iris = load_iris()
X = iris.data
y = iris.target
feature_names = ['Sepal length, x1','Sepal width, x2',
'Petal length, x3','Petal width, x4']
# Convert X array to dataframe
X_df = pd.DataFrame(X, columns=feature_names)
with st.sidebar:
st.latex('X = USV^T')
st.latex('X = \sum_{j=1}^{D} s_j u_j v_j^T')
st.latex('X \simeq \sum_{j=1}^{p} s_j u_j v_j^T')
#%% Original data, X
X = X_df.to_numpy();
U, S, V_T = np.linalg.svd(X, full_matrices=False)
S = np.diag(S)
V = V_T.T
with st.sidebar:
p = st.slider('Choose p, number of component to approximate X:',
1,4,step = 1)
#%% Approximate X
X_apprx = U[:,0:p]@S[0:p,0:p]@V[:,0:p].T
X_apprx_df = pd.DataFrame(X_apprx, columns = feature_names)
Error_df = X_df - X_apprx_df
#%%
col1, col2, col3 = st.columns(3)
with col1:
st.latex('X')
fig_1 = px.imshow(X_df,
color_continuous_scale='RdYlBu_r',
range_color = [0,8])
fig_1.layout.height = 500
fig_1.layout.width = 300
fig_1.update_layout(coloraxis_showscale=False)
st.plotly_chart(fig_1)
with col2:
st.latex('\hat{X}')
fig_2 = px.imshow(X_apprx_df,
color_continuous_scale='RdYlBu_r',
range_color = [0,8])
fig_2.layout.height = 500
fig_2.layout.width = 300
fig_2.update_layout(coloraxis_showscale=False)
st.plotly_chart(fig_2)
with col3:
st.latex('X - \hat{X}')
fig_3 = px.imshow(Error_df,
color_continuous_scale='RdYlBu_r',
range_color = [0,8])
fig_3.layout.height = 500
fig_3.layout.width = 300
fig_3.update_layout(coloraxis_showscale=False)
st.plotly_chart(fig_3)

View File

@@ -0,0 +1,85 @@
###############
# Authored by Weisheng Jiang
# Book 4 | From Basic Arithmetic to Machine Learning
# Published and copyrighted by Tsinghua University Press
# Beijing, China, 2022
###############
import sympy
import numpy as np
from sympy.functions import exp
import streamlit as st
import plotly.figure_factory as ff
import plotly.graph_objects as go
#define symbolic vars, function
x1,x2 = sympy.symbols('x1 x2')
f_x = x1*exp(-(x1**2 + x2**2))
st.latex('f(x_1, x_2) = ' + sympy.latex(f_x))
#take the gradient symbolically
grad_f = [sympy.diff(f_x,var) for var in (x1,x2)]
st.latex(r'\nabla f = ' + sympy.latex(grad_f) + '^T')
f_x_fcn = sympy.lambdify([x1,x2],f_x)
#turn into a bivariate lambda for numpy
grad_fcn = sympy.lambdify([x1,x2],grad_f)
x1_array = np.linspace(-2,2,100)
x2_array = np.linspace(-2,2,100)
xx1, xx2 = np.meshgrid(x1_array,x2_array)
# coarse mesh
xx1_, xx2_ = np.meshgrid(np.linspace(-2,2,20),np.linspace(-2,2,20))
V = grad_fcn(xx1_,xx2_)
ff_x = f_x_fcn(xx1,xx2)
#%% visualizations
fig_surface = go.Figure(go.Surface(
x = x1_array,
y = x2_array,
z = ff_x,
showscale=False))
fig_surface.update_layout(
autosize=False,
width =800,
height=800)
st.plotly_chart(fig_surface)
f = ff.create_quiver(xx1_, xx2_, V[0], V[1])
trace1 = f.data[0]
trace2 = go.Contour(
x = x1_array,
y = x2_array,
z = ff_x,
showscale=False)
data=[trace1,trace2]
fig = go.FigureWidget(data)
fig.update_layout(
autosize=False,
width =800,
height=800)
fig.add_hline(y=0, line_color = 'black')
fig.add_vline(x=0, line_color = 'black')
fig.update_xaxes(range=[-2, 2])
fig.update_yaxes(range=[-2, 2])
fig.update_coloraxes(showscale=False)
st.plotly_chart(fig)

View File

@@ -0,0 +1,117 @@
###############
# Authored by Weisheng Jiang
# Book 4 | From Basic Arithmetic to Machine Learning
# Published and copyrighted by Tsinghua University Press
# Beijing, China, 2022
###############
import numpy as np
from sympy import lambdify, diff, exp, latex, simplify, symbols
import numpy as np
import plotly.figure_factory as ff
import plotly.graph_objects as go
import streamlit as st
x1,x2 = symbols('x1 x2')
num = 301; # number of mesh grids
x1_array = np.linspace(-3,3,num)
x2_array = np.linspace(-3,3,num)
xx1,xx2 = np.meshgrid(x1_array,x2_array)
# f_xy = x*exp(- x**2 - y**2);
f_x = 3*(1-x1)**2*exp(-(x1**2) - (x2+1)**2)\
- 10*(x1/5 - x1**3 - x2**5)*exp(-x1**2-x2**2)\
- 1/3*exp(-(x1+1)**2 - x2**2)
f_x_fcn = lambdify([x1,x2],f_x)
f_zz = f_x_fcn(xx1,xx2)
st.latex('f(x_1, x_2) = ' + latex(f_x))
#%% gradient
#take the gradient symbolically
grad_f = [diff(f_x,var) for var in (x1,x2)]
#turn into a bivariate lambda for numpy
grad_fcn = lambdify([x1,x2],grad_f)
x1__ = np.linspace(-3,3,40)
x2__ = np.linspace(-3,3,40)
# coarse mesh
xx1_, xx2_ = np.meshgrid(x1__,x2__)
V = grad_fcn(xx1_,xx2_)
#%%
#%% visualizations
fig_surface = go.Figure(go.Surface(
x = x1_array,
y = x2_array,
z = f_zz,
showscale=False,
colorscale = 'RdYlBu_r'))
fig_surface.update_layout(
autosize=False,
width =800,
height=600)
st.plotly_chart(fig_surface)
#%% gradient vector plot
f = ff.create_quiver(xx1_, xx2_,
V[0], V[1],
arrow_scale=.1,
scale = 0.03)
f_stream = ff.create_streamline(x1__,x2__,
V[0], V[1],
arrow_scale=.1)
trace1 = f.data[0]
trace3 = f_stream.data[0]
trace2 = go.Contour(
x = x1_array,
y = x2_array,
z = f_zz,
showscale=False,
colorscale = 'RdYlBu_r')
data=[trace1,trace2]
fig = go.FigureWidget(data)
fig.update_layout(
autosize=False,
width =800,
height=800)
fig.add_hline(y=0, line_color = 'black')
fig.add_vline(x=0, line_color = 'black')
fig.update_xaxes(range=[-2, 2])
fig.update_yaxes(range=[-2, 2])
fig.update_coloraxes(showscale=False)
st.plotly_chart(fig)
#%% streamlit plot
data2=[trace3,trace2]
fig2 = go.FigureWidget(data2)
fig2.update_layout(
autosize=False,
width =800,
height=800)
fig2.add_hline(y=0, line_color = 'black')
fig2.add_vline(x=0, line_color = 'black')
fig2.update_xaxes(range=[-2, 2])
fig2.update_yaxes(range=[-2, 2])
fig2.update_coloraxes(showscale=False)
st.plotly_chart(fig2)