用實例告訴你什麼叫“過擬合”與“欠擬合”

用實例告訴你什麼叫“過擬合”與“欠擬合”

有位同事最近用術語“欠擬合”來指代命名實體識別(NER)模型,該模型缺少應該標記的實體。

我得糾正一下。 這實際上並不是欠擬合,但是我明白為何有人會這麼想。

那麼,對於這個問題而言,什麼是不合適的,或者是過度擬合的呢?

讓我們訓練一些欠缺數據並擬合過度的模型!

我們將從使用sklearn的“ make_classification”功能生成數據集開始。 每個數據點將具有2個要素(因此易於繪製)和一個標籤。

from sklearn.datasets import make_classification
# We didn't need to display all params but I like to see defaults
# I've edited some of these
X,y = make_classification(
n_samples=30,
n_features=2,
n_informative=2,
n_redundant=0,
n_repeated=0,
n_classes=2,
n_clusters_per_class=2,
weights=None,
flip_y=0.01,
class_sep=1.0,
hypercube=True,
shift=0.0,
scale=1.0,
shuffle=True,
random_state=None
)
# Split examples by class (positive/negative) to give diff colors
pos_feat0 = []

pos_feat1 = []
neg_feat0 = []
neg_feat1 = []
for idx,klass in enumerate(y):
if klass == 1:
pos_feat0.append(X[idx][0])
pos_feat1.append(X[idx][1])
else:
neg_feat0.append(X[idx][0])
neg_feat1.append(X[idx][1])

# And plot them
import matplotlib.pyplot as plt
plt.scatter(pos_feat0,pos_feat1, c='blue')
plt.scatter(neg_feat0,neg_feat1, c='red'))
用實例告訴你什麼叫“過擬合”與“欠擬合”

大功告成。 我們得到數據了。

現在,我們將介紹欠擬合和過擬合的定義,然後有目的地選擇將數據欠擬合和過擬合的算法。

欠擬合

根據維基百科:

當統計模型無法充分捕獲數據的基礎結構時,就會發生欠擬合。

翻譯: 模型在數據中無法找到一個可靠的模式。 這並不意味著沒有模式。 只是該模型找不到正確的模式。

from sklearn.linear_model import SGDClassifier
model = SGDClassifier()
model.fit(X, y)
# set min and max values for the x and y axes
x_min, x_max = X[:, 0].min() - 1, X[:, 0].max() + 1
y_min, y_max = X[:, 1].min() - 1, X[:, 1].max() + 1
a = np.arange(x_min, x_max, 0.1)
b = np.arange(y_min, y_max, 0.1)
# build a grid of each unique combination of x and y
xx, yy = np.meshgrid(a, b)
# make predictions for every combination of x and y on that grid
Z = model.predict(np.c_[xx.ravel(), yy.ravel()])
Z = Z.reshape(xx.shape)
# draw the classification boundary
plt.contourf(xx, yy, Z, alpha=0.4)
# adds the points from our training data
plt.scatter(X[:, 0], X[:, 1], c=y, s=20, edgecolor='k')
plt.show()
用實例告訴你什麼叫“過擬合”與“欠擬合”

該模型並不擅長繪製決策邊界,它無法利用特徵來確定示例的類型。

過擬合

根據維基百科釋義:

過於緊密或完全對應於特定數據集的分析結果,由此可能無法擬合其它數據或可靠地預測未來的觀察結果。

翻譯: 該模型學習輸入的示例,但不能推廣到其它示例。

from sklearn.tree import DecisionTreeClassifier
model = DecisionTreeClassifier(max_depth=4)
model.fit(X, y)
# set min and max values for the x and y axes
x_min, x_max = X[:, 0].min() - 1, X[:, 0].max() + 1
y_min, y_max = X[:, 1].min() - 1, X[:, 1].max() + 1
a = np.arange(x_min, x_max, 0.1)
b = np.arange(y_min, y_max, 0.1)
# build a grid of each unique combination of x and y
xx, yy = np.meshgrid(a, b)
# make predictions for every combination of x and y on that grid
Z = model.predict(np.c_[xx.ravel(), yy.ravel()])
Z = Z.reshape(xx.shape)
# draw the classification boundary
plt.contourf(xx, yy, Z, alpha=0.4)
# adds the points in our training data
plt.scatter(X[:, 0], X[:, 1], c=y, s=20, edgecolor='k')
plt.show()
用實例告訴你什麼叫“過擬合”與“欠擬合”

漂亮。 又一個糟糕的模型。 它在目標示例的周圍繪製了邊界,但是其發現的模式毫無意義,很可能在新的示例。

讓我們擬合數據、尋找樂趣吧!

odel import LinearRegression,LogisticRegression
model = LogisticRegression()model.fit(X, y)# set min and max values for the x and y axes
x_min, x_max = X[:, 0].min() - 1, X[:, 0].max() + 1
y_min, y_max = X[:, 1].min() - 1, X[:, 1].max() + 1
a = np.arange(x_min, x_max, 0.1)
b = np.arange(y_min, y_max, 0.1)# build a grid of each unique combination of x and y
xx, yy = np.meshgrid(a, b)# make predictions for every combination of x and y on that grid
Z = model.predict(np.c_[xx.ravel(), yy.ravel()])
Z = Z.reshape(xx.shape)# draw the classification boundary
plt.contourf(xx, yy, Z, alpha=0.4)# adds the points in our training data
plt.scatter(X[:, 0], X[:, 1], c=y, s=20, edgecolor='k')plt.title(''Underfitting')plt.show()'Underfitting')plt.show()
用實例告訴你什麼叫“過擬合”與“欠擬合”

雖然不完美,但比前兩種方法好多了。

現在你看到了。 欠擬合,過擬合,還有計劃擬合。

我們有意選擇了一個簡單的雙特徵數據集,因此你可以在圖表上看到決策邊界。

具有成千上萬個特徵的真實示例需要採用更數字化的方法來衡量欠擬合和過擬合,這就是下次我要講到的了~


分享到:


相關文章: