<jupyter_start><jupyter_text>回归<jupyter_code># 对于回归问题，我们可以使用linear GAM
from pygam import LinearGAM, s, f
from pygam.datasets import wage
X, y = wage(return_X_y=True)
X.shape
y.shape
## model
gam = LinearGAM(s(0) + s(1) + f(2))
gam
gam.gridsearch(X, y)

## plotting
plt.figure();
fig, axs = plt.subplots(1,3);

titles = ['year', 'age', 'education']
for i, ax in enumerate(axs):
    XX = gam.generate_X_grid(term=i)
    ax.plot(XX[:, i], gam.partial_dependence(term=i, X=XX))
    ax.plot(XX[:, i], gam.partial_dependence(term=i, X=XX, width=.95)[1], c='r', ls='--')
    if i == 0:
        ax.set_ylim(-30,30)
    ax.set_title(titles[i]);
# Even though our model allows coefficients, our smoothing penalty reduces us to just 19 effective degrees of freedom
gam.summary()
# 使用LinearGAMs，我们可以查看预测区间
from pygam import LinearGAM
from pygam.datasets import mcycle
X, y = mcycle(return_X_y=True)
X.shape
y.shape
gam = LinearGAM(n_splines=25).gridsearch(X, y)
XX = gam.generate_X_grid(term=0, n=500)
plt.plot(XX, gam.predict(XX), 'r--')
plt.plot(XX, gam.prediction_intervals(XX, width=.95), color='b', ls='--')

plt.scatter(X, y, facecolor='gray', edgecolors='none')
plt.title('95% prediction interval')
# 根据后验分布进行模拟
# continuing last example with the mcycle dataset
for response in gam.sample(X, y, quantity='y', n_draws=50, sample_at_X=XX):
    plt.scatter(XX, response, alpha=.03, color='k')
plt.plot(XX, gam.predict(XX), 'r--')
plt.plot(XX, gam.prediction_intervals(XX, width=.95), color='b', ls='--')
plt.title('draw samples from the posterior of the coefficients')
# 可以使用惩罚和约束来将我们的先验知识加入模型中，从而控制过拟合
from pygam import LinearGAM, s
from pygam.datasets import hepatitis
-可用惩罚
-二阶导数平滑（数值特征默认）
-L2平滑（分类特征默认）

-可用约束
-单调递增/递减平滑
-凸/凹平滑
-周期平滑

我们可以通过使用单调和凹约束将直觉注入到模型中
X, y = hepatitis(return_X_y=True)
print(f'X shape: {X.shape}')
print(f'y shape: {y.shape}')
gam1 = LinearGAM(s(0, constraints='monotonic_inc')).fit(X, y)
gam2 = LinearGAM(s(0, constraints='concave')).fit(X, y)

fig, ax = plt.subplots(1, 2)
ax[0].plot(X, y, label='data')
ax[0].plot(X, gam1.predict(X), label='monotonic fit')
ax[0].legend()

ax[1].plot(X, y, label='data')
ax[1].plot(X, gam2.predict(X), label='concave fit')
ax[1].legend()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.17903733693207377, 'fraction_numerical': 0.0206927575348628, 'mean_word_length': 5.658682634730539, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '573259', 'license': 'MIT', 'n_tokens_mistral': 1055, 'n_tokens_neox': 1036, 'n_words': 203, 'path': 'pyGAM_practice1.ipynb', 'repo_name': 'Alex2Yang97/gam_model', 'size': '2223'}