Files
Book4_Power-of-Matrix/Book4_Ch03_Python_Codes/Bk4_Ch3_01.py
2022-07-15 06:07:14 -04:00

53 lines
1.4 KiB
Python

###############
# Authored by Weisheng Jiang
# Book 4 | From Basic Arithmetic to Machine Learning
# Published and copyrighted by Tsinghua University Press
# Beijing, China, 2022
###############
# Bk4_Ch3_01.py
import matplotlib.pyplot as plt
import numpy as np
p_values = [0.05, 0.2, 0.5, 1, 1.5, 2, 4, 8, np.inf]
x1 = np.linspace(-2.5, 2.5, num=101);
x2 = x1;
xx1, xx2 = np.meshgrid(x1,x2)
fig, axes = plt.subplots(ncols=3,nrows=3,
figsize=(12, 12))
for p, ax in zip(p_values, axes.flat):
if np.isinf(p):
zz = np.maximum(np.abs(xx1),np.abs(xx2))
else:
zz = ((np.abs((xx1))**p) + (np.abs((xx2))**p))**(1./p)
# plot contour of Lp
ax.contourf(xx1, xx2, zz, 20, cmap='RdYlBu_r')
# plot contour of Lp = 1
ax.contour (xx1, xx2, zz, [1], colors='k', linewidths = 2)
# decorations
ax.axhline(y=0, color='k', linewidth = 0.25)
ax.axvline(x=0, color='k', linewidth = 0.25)
ax.set_xlim(-2.5, 2.5)
ax.set_ylim(-2.5, 2.5)
ax.spines['top'].set_visible(False)
ax.spines['right'].set_visible(False)
ax.spines['bottom'].set_visible(False)
ax.spines['left'].set_visible(False)
ax.set_xlabel('$x_1$')
ax.set_ylabel('$x_2$')
ax.set_title('p = ' + str(p))
ax.set_aspect('equal', adjustable='box')
plt.show()