<jupyter_start><jupyter_text>Copyright (C)                                                        2016 - 2019 <NAME>(<EMAIL>)   https://www.cnblogs.com/pinard  Permission given to modify the code as long as you keep this declaration at the top                               用scikit-learn和pandas学习Ridge回归https://www.cnblogs.com/pinard/p/6023000.html<jupyter_code>import numpy as np
import matplotlib.pyplot as plt
from sklearn import linear_model
%matplotlib inline
print np.arange(1, 11)
print np.arange(0, 10)[:, np.newaxis]   
print np.arange(1, 11) + np.arange(0, 10)[:, np.newaxis]
# X is a 10x10 matrix
X = 1. / (np.arange(1, 11) + np.arange(0, 10)[:, np.newaxis])
# y is a 10 x 1 vector
y = np.ones(10)

n_alphas = 200
# alphas count is 200, 都在10的-10次方和10的-2次方之间
alphas = np.logspace(-10, -2, n_alphas)
print alphas
clf = linear_model.Ridge(fit_intercept=False)
coefs = []
# 循环200次
for a in alphas:
    #设置本次循环的超参数
    clf.set_params(alpha=a)
    #针对每个alpha做ridge回归
    clf.fit(X, y)
    # 把每一个超参数alpha对应的theta存下来
    coefs.append(clf.coef_)
ax = plt.gca()

ax.plot(alphas, coefs)
#将alpha的值取对数便于画图
ax.set_xscale('log')
#翻转x轴的大小方向，让alpha从大到小显示
ax.set_xlim(ax.get_xlim()[::-1]) 
plt.xlabel('alpha')
plt.ylabel('weights')
plt.title('Ridge coefficients as a function of the regularization')
plt.axis('tight')
plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.13101406365655072, 'fraction_numerical': 0.046632124352331605, 'mean_word_length': 3.4768211920529803, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 3, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '166016', 'license': 'MIT', 'n_tokens_mistral': 617, 'n_tokens_neox': 589, 'n_words': 118, 'path': 'classic-machine-learning/ridge_regression.ipynb', 'repo_name': 'yefang008514/machinelearning', 'size': '1370'}