Visualize-ML / Book3_Elements-of-Mathematics

Book_3_《数学要素》 | 鸢尾花书:从加减乘除到机器学习;上架;欢迎继续纠错,纠错多的同学还会有赠书!
6.35k stars 1.1k forks source link

Book3_Ch11_代数函数_PAGE12_代码包含内容不全 #113

Closed lang07123 closed 5 months ago

lang07123 commented 1 year ago

图片 代码中只包含了 图13左, 并没有包含其它,

修改如下: (抱歉懒得合并代码了, 本地一堆修改)


###############
# Authored by Weisheng Jiang
# Book 3  |  From Basic Arithmetic to Machine Learning
# Published and copyrighted by Tsinghua University Press
# Beijing, China, 2022
###############

# Bk3_Ch11_02

import numpy as np
import matplotlib.pyplot as plt

def drawing(coefficient, variable, result, y_range, lable, step=0.5):
    fig, ax = plt.subplots()
    colors = plt.cm.jet(np.linspace(0, 1, result.shape[1]))

    for i in range(1, result.shape[1] + 1):
        plt.plot(
            variable, 
            result[:, i-1],
            color = colors[i-1],
            label = F'${lable} = {coefficient[i-1]:.0f}$'
        )

    plt.xlabel('x')
    plt.ylabel('f(x)      ', rotation=0)
    plt.legend()
    plt.xticks(np.arange(variable.min(), variable.max() + step, step=step))
    plt.yticks(np.arange(y_range[0], y_range[1] + step, step=step))
    plt.axis('scaled')

    ax.set_xlim(variable.min(), variable.max())
    ax.set_ylim(y_range[0], y_range[1])
    ax.spines[['top', 'right', 'bottom', 'left']].set_visible(False)
    ax.grid(linestyle='--', linewidth=0.25, color=[0.5, 0.5, 0.5])

    plt.show()

# y = ax**2 
# a = [-6, -5, -4, -3, -2, -1]
# -2 <= x <= 2
a_array = np.linspace(-6, -1, 6)
x_array = np.linspace(-2, 2, 100)
aa, xx = np.meshgrid(a_array, x_array)
ww = aa*xx**2
y_range = [np.min(ww, axis=0).max(), 0]

drawing(a_array, x_array, ww, y_range, 'a')

# y = ax**2 
# a = [1, 2, 3, 4, 5, 6]
# -2 <= x <= 2
a_array = np.linspace(1, 6, 6)
x_array = np.linspace(-2, 2, 100)
aa, xx = np.meshgrid(a_array, x_array)
ww = aa*xx**2
y_range = [0, np.max(ww, axis=0).min()]

drawing(a_array, x_array, ww, y_range, 'a')

# f(x) = a(x-h)**2 + k, a != 0
# a = 1
# k = 0
# h = [-4, -3, -2, -1, 0, 1, 2, 3, 4]
# -5 <= x <=5
h_array = np.linspace(-4, 4, 9)
x_array = np.linspace(-5, 5, 100)
hh, xx = np.meshgrid(h_array, x_array)
ww = 1 * (xx - hh) **2 + 0
y_range = [0, 10]

drawing(h_array, x_array, ww, y_range, 'h', step=1)

# f(x) = a(x-h)**2 + k, a != 0
# a = 1
# h = 0
# k = [-4, -3, -2, -1, 0, 1, 2, 3, 4]
# -5 <= x <=5
k_array = np.linspace(-4, 4, 9)
x_array = np.linspace(-5, 5, 100)
kk, xx = np.meshgrid(k_array, x_array)
ww = 1 * (xx - 0)**2 + kk
y_range = [-4.5, 4.5]

drawing(k_array, x_array, ww, y_range, 'k', step=1)
lang07123 commented 1 year ago

顺便一提, 希望作者能优化一下代码风格, 至少要遵守 PEP 8, 代码格式化工具 black 有很大帮助. 或者直接更换IDE, Pycharm(自带提醒与格式化)或者VSCode(插件Ruff + Pylint)都能很好的帮助优化代码风格.

Visualize-ML commented 1 year ago

谢谢你的建议,确实用的是Spyder。下一版将会统一调整为Jupyter Notebook。请继续批评指正。

On Fri, Jul 21, 2023 at 6:12 AM lang07123 @.***> wrote:

顺便一提, 所有代码的风格实在是让人难以忍受, 本来小白看这本书就跟天书一样, 看了代码更是脑袋疼, 至少也要遵守 PEP8 https://peps.python.org/pep-0008/呀, 我估计可能跟作者使用的IDE有关系, pycharm或vscode可能是更好的选择, 如果实在懒得换, 可能 black https://github.com/psf/black 也有很大帮助.

— Reply to this email directly, view it on GitHub https://github.com/Visualize-ML/Book3_Elements-of-Mathematics/issues/113#issuecomment-1645343079, or unsubscribe https://github.com/notifications/unsubscribe-auth/AZHC6V5YSLGTHJSFSLMY6RDXRJIXNANCNFSM6AAAAAA2SIRF4Q . You are receiving this because you are subscribed to this thread.Message ID: <Visualize-ML/Book3_Elements-of-Mathematics/issues/113/1645343079@ github.com>

Visualize-ML commented 1 year ago

谢谢你,本意是当大家自己补齐。下一版会采用你的建议,分子图绘制。请继续批评指正。

On Thu, Jul 20, 2023 at 11:29 PM lang07123 @.***> wrote:

[image: 图片] https://user-images.githubusercontent.com/7733095/255064336-111f7ab9-2374-4b1b-b991-36985ef26932.png 代码中只包含了 图13左, 并没有包含其它,

修改如下: (抱歉懒得合并代码了, 本地一堆修改)

###############

Authored by Weisheng Jiang

Book 3 | From Basic Arithmetic to Machine Learning

Published and copyrighted by Tsinghua University Press

Beijing, China, 2022

###############

Bk3_Ch11_02

import numpy as np import matplotlib.pyplot as plt

def drawing(coefficient, variable, result, y_range, lable, step=0.5): fig, ax = plt.subplots() colors = plt.cm.jet(np.linspace(0, 1, result.shape[1]))

for i in range(1, result.shape[1] + 1):
    plt.plot(
        variable,
        result[:, i-1],
        color = colors[i-1],
        label = F'${lable} = {coefficient[i-1]:.0f}$'
    )

plt.xlabel('x')
plt.ylabel('f(x)      ', rotation=0)
plt.legend()
plt.xticks(np.arange(variable.min(), variable.max() + step, step=step))
plt.yticks(np.arange(y_range[0], y_range[1] + step, step=step))
plt.axis('scaled')

ax.set_xlim(variable.min(), variable.max())
ax.set_ylim(y_range[0], y_range[1])
ax.spines[['top', 'right', 'bottom', 'left']].set_visible(False)
ax.grid(linestyle='--', linewidth=0.25, color=[0.5, 0.5, 0.5])

plt.show()

y = ax**2

a = [-6, -5, -4, -3, -2, -1]

-2 <= x <= 2

a_array = np.linspace(-6, -1, 6) x_array = np.linspace(-2, 2, 100) aa, xx = np.meshgrid(a_array, x_array) ww = aa*xx**2 y_range = [np.min(ww, axis=0).max(), 0]

drawing(a_array, x_array, ww, y_range, 'a')

y = ax**2

a = [1, 2, 3, 4, 5, 6]

-2 <= x <= 2

a_array = np.linspace(1, 6, 6) x_array = np.linspace(-2, 2, 100) aa, xx = np.meshgrid(a_array, x_array) ww = aa*xx**2 y_range = [0, np.max(ww, axis=0).min()]

drawing(a_array, x_array, ww, y_range, 'a')

f(x) = a(x-h)**2 + k, a != 0

a = 1

k = 0

h = [-4, -3, -2, -1, 0, 1, 2, 3, 4]

-5 <= x <=5

h_array = np.linspace(-4, 4, 9) x_array = np.linspace(-5, 5, 100) hh, xx = np.meshgrid(h_array, x_array) ww = 1 * (xx - hh) **2 + 0

y_range = [0, 10]

drawing(h_array, x_array, ww, y_range, 'h', step=1)

f(x) = a(x-h)**2 + k, a != 0

a = 1

h = 0

k = [-4, -3, -2, -1, 0, 1, 2, 3, 4]

-5 <= x <=5

k_array = np.linspace(-4, 4, 9) x_array = np.linspace(-5, 5, 100) kk, xx = np.meshgrid(k_array, x_array) ww = 1 * (xx - 0)**2 + kk

y_range = [-4.5, 4.5]

drawing(k_array, x_array, ww, y_range, 'k', step=1)

— Reply to this email directly, view it on GitHub https://github.com/Visualize-ML/Book3_Elements-of-Mathematics/issues/113, or unsubscribe https://github.com/notifications/unsubscribe-auth/AZHC6VY45P7IYEF3VB3MIHTXRHZQTANCNFSM6AAAAAA2SIRF4Q . You are receiving this because you are subscribed to this thread.Message ID: @.***>

lang07123 commented 1 year ago

谢谢你的建议,确实用的是Spyder。下一版将会统一调整为Jupyter Notebook。请继续批评指正。 On Fri, Jul 21, 2023 at 6:12 AM lang07123 @.***> wrote: 顺便一提, 所有代码的风格实在是让人难以忍受, 本来小白看这本书就跟天书一样, 看了代码更是脑袋疼, 至少也要遵守 PEP8 <https://peps.python.org/pep-0008/>呀, 我估计可能跟作者使用的IDE有关系, pycharm或vscode可能是更好的选择, 如果实在懒得换, 可能 black https://github.com/psf/black 也有很大帮助. — Reply to this email directly, view it on GitHub <#113 (comment)>, or unsubscribe https://github.com/notifications/unsubscribe-auth/AZHC6V5YSLGTHJSFSLMY6RDXRJIXNANCNFSM6AAAAAA2SIRF4Q . You are receiving this because you are subscribed to this thread.Message ID: <Visualize-ML/Book3_Elements-of-Mathematics/issues/113/1645343079@ github.com>

如果使用jupyter 好像 jupyterlab 会比 jupyter notebook 好一些... 具体没仔细对比过. 不过我第一次接触这个东西的时候... notebook 一堆的关于图像的报错..而且无法显示.... lab几乎没有遇到任何问题..让我一直对notebook 有抵触情绪... 而且lab的产品定位好像就是notebook的下一代产品, 有新不用旧~~

我是个数学小白, 但是算半个程序员, 所以当我看不懂你的公式或者说法的时候, 我会从代码反推. 但是当我一脑袋问号去看你的代码时候, 有一些不规范的代码会让我问号更多... 不知道我能不代表一部分读者...