<jupyter_start><jupyter_text>多項式基底関数を過剰に大きな次元に設定する<jupyter_code>polydim = 100
phi = models.PolyBaseFunction(x, dims=polydim)
model = models.LinearRegression(y, phi)
model.fit_OLS()
xmin, xmax = x.min(), x.max()
xline = np.linspace(xmin, xmax, num=50)
philine = models.PolyBaseFunction(xline, dims=polydim)
plt.grid(color=gray)
plt.scatter(x, y, s=2, c="k", label="data")
plt.plot(xline, model.predict_OLS(philine), c="gray", label="prediction")
plt.ylim(y.min()-0.1, y.max()+0.1)
plt.xlabel("x")
plt.ylabel("y")
plt.legend()

plt.savefig("../draft/img/OverFitting.png", dpi=300)
plt.show()<jupyter_output><empty_output><jupyter_text>適切に正則化をするか，ベイズ法を用いれば，滑らかに内挿されるただし，いずれも追加されるパラメータを適切に調整する必要がある<jupyter_code>model.fit_Ridge(alpha=0.001) # 緩めの正則化
model.fit_Bayes(sigma2=0.1, prior_cov=10*np.eye(model.D)) # 緩めの事前分布で，観測誤差の分散は真値
plt.grid(color=gray)
plt.scatter(x, y, s=2, c="k")
plt.plot(xline, model.predict_Ridge(philine), label="Ridge", linestyle=":")
plt.plot(xline, model.predict_Bayes(philine)[0], label="Bayes", linestyle="-.")
plt.ylim(y.min()-0.1, y.max()+0.1)
plt.xlabel("x")
plt.ylabel("y")
plt.legend()
plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.202081526452732, 'fraction_numerical': 0.024284475281873375, 'mean_word_length': 8.459016393442623, 'pattern_counts': {'":': 1, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '182562', 'license': 'MIT', 'n_tokens_mistral': 628, 'n_tokens_neox': 597, 'n_words': 61, 'path': 'programs/OverFitting.ipynb', 'repo_name': 'hayato-n/AsakuraBook', 'size': '1153'}