1. 程式人生 > >以一元及二元函式為例,通過多項式的函式影象觀察其擬合效能;以及對用多項式作目標函式進行機器學習時的一些理解。

以一元及二元函式為例,通過多項式的函式影象觀察其擬合效能;以及對用多項式作目標函式進行機器學習時的一些理解。

 先給出程式碼:

import numpy as np
from mpl_toolkits.mplot3d import Axes3D
from matplotlib import pyplot as plt
from datetime import datetime

t0 = datetime.now()
# 實數範圍內非負數有偶次方根,任何實數有奇次方根;但python中負數沒有奇次方根和偶次方根。
x = np.arange(-10, 10.1, 1)  

y1 = x ** 1; y2 = x ** 3; y3 = x ** 5
y4 = x ** 2; y5 = x ** 4; y6 = x ** (-1)
y7 = x ** (-3); y8 = x ** (-2); y9 = x ** (-4)

plt.subplot(331)
plt.title('y = x')
plt.plot(x, y1)
plt.subplot(332)
plt.title('y = x^3')
plt.plot(x, y2)
plt.subplot(333)
plt.title('y = x^5')
plt.plot(x, y3)
plt.subplot(334)
plt.title('y = x^2')
plt.plot(x, y4)
plt.subplot(335)
plt.title('y = x^4')
plt.plot(x, y5)
plt.subplot(336)
plt.title('y = x^(-1)')
plt.plot(x, y6)
plt.subplot(337)
plt.title('y = x^(-3)')
plt.plot(x, y7)
plt.subplot(338)
plt.title('y = x^(-2)')
plt.plot(x, y8)
plt.subplot(339)
plt.title('y = x^(-4)')
plt.plot(x, y9)
plt.show()

y10 = x ** (3/2); y11 = x ** (5/2); y12 = x ** (2/5)
y13 = x ** (4/5); y14 = x ** (-3/2); y15 = x ** (-5/2)
y16 = x ** (-2/5); y17 = (-x) ** (3/2); y18 = -(x ** (3/2))

plt.subplot(331)
plt.title('y = x^(3/2)')
plt.plot(x, y10)
plt.subplot(332)
plt.title('y = x^(5/2)')
plt.plot(x, y11)
plt.subplot(333)
plt.title('y = x^(2/5)')
plt.plot(x, y12)
plt.subplot(334)
plt.title('y = x^(4/5)')
plt.plot(x, y13)
plt.subplot(335)
plt.title('y = x^(-3/2)')
plt.plot(x, y14)
plt.subplot(336)
plt.title('y = x^(-5/2)')
plt.plot(x, y15)
plt.subplot(337)
plt.title('y = x^(-2/5)')
plt.plot(x, y16)
plt.subplot(338)
plt.title('y = (-x)^(3/2)')
plt.plot(x, y17)
plt.subplot(339)
plt.title('y = -(x^(3/2))')
plt.plot(x, y18)
plt.show()

y19 = x**2 + x; y20 = x**2 + 100*x; y21 = x**3 + x**2 + x
y22 = x**3 + 100*x**2 + x; y23 = x**4 + x**3 + x**2 + x; y24 = x**4 + 100*x**3 + x**2 + x

plt.subplot(231)
plt.title('y = x^(3/2)')
plt.plot(x, y19)
plt.subplot(232)
plt.title('y = x^(5/2)')
plt.plot(x, y20)
plt.subplot(233)
plt.title('y = x^(2/5)')
plt.plot(x, y21)
plt.subplot(234)
plt.title('y = x^(4/5)')
plt.plot(x, y22)
plt.subplot(235)
plt.title('y = x^(-3/2)')
plt.plot(x, y23)
plt.subplot(236)
plt.title('y = x^(-5/2)')
plt.plot(x, y24)
plt.show()

W = np.arange(-100, 100+0.1, 10)
B = np.arange(-100, 100+0.1, 10)
W, B = np.meshgrid(W, B)  
n = 11
for a in range(1, n, 3):
    for b in range(1, n, 3):
        for c in range(1, n, 3):
            fig = plt.figure()
            ax = Axes3D(fig)
            Z = W ** a + B ** b - W * B * c
            plt.xlabel('W')
            plt.ylabel('B')
            ax.plot_surface(W, B, Z, rstride=1, cstride=1, cmap='rainbow')
            plt.show()

t1 = datetime.now()
print('總耗時:', t1-t0)

 

從前兩張圖可以看到當多項式為一元函式且沒有常數項時,其自變數的冪指數依次為正奇數、正偶數、負奇數、負偶數、正假分數、正真分數、負假分數、負真分數時的函式影象。其中當冪指數為正負奇數時,函式影象關於原點對稱,為奇函式;當冪指數為正負偶數時,函式影象關於縱座標對稱,為偶函式;當冪指數為正假分數時,函式單調遞增且二階導數大於0;當冪指數為正真分數時,函式單調遞增且二階導數小於0;當冪指數為負假分數、負真分數時,函式單調遞減且二階導數小於0。

從第三張圖並結合其程式碼可以知道,多項式中哪一項分配的權重越大,函式就越會表現出那一項的性質;一般地,如果每一項權重相等,則會表現出最高次項的特徵,因為高次項天然具有更大的影響力。就像在梯度下降類的演算法中,高次項天然具有更高的權重。所以應當慎用高次項,避免模型過擬合。

 

從二元函式的以上影象可以知道,如果再進行疊加,例如第一種型別的函式和第五種型別的函式疊加,或者第二種型別的函式和第四種類型的函式疊加,則函式可以擬合的資料型別會大大增加,可以分類的資料型別也會大大增加。

通過以上分析觀察可以推知,如果多項式中自變數的次數沒有限制,則其可以擬合及分類的資料型別非常多。但因為高次項天然具有更高的權重,在使用梯度下降類演算法迭代自變數的係數時會產生巨大的跳躍,容易導致演算法不收斂;就算收斂了,最終得到的多項式也容易過擬合。所以多項式最好不用高次項,如果要用,則自變數最值的絕對值不能過大;然後對自變數的係數進行迭代的時候最好加上一些降低過擬合的方法如權重衰減等。