-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNew Year and Hurry.py
61 lines (51 loc) · 1.39 KB
/
New Year and Hurry.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# -*- coding: utf-8 -*-
"""
Created on Mon Aug 23 11:23:08 2021
@author: Easin
"""
'''
import sklearn
import numpy as np
import matplotlib.pyplot as plt
from sklearn import linear_model
reg = linear_model.RidgeCV(alphas=np.logspace(-6, 6, 13))
#alphas = np.logspace(-6, 6, 13)
#print(alphas)
fit = reg.fit([[0, 0], [0, 0], [1, 1]], [0, .1, 1])
alpha_coef = reg.alpha_
print(fit)
print(alpha_coef)
'''
print(__doc__)
import numpy as np
import matplotlib.pyplot as plt
from sklearn import linear_model
# X is the 10x10 Hilbert matrix
X = 1. / (np.arange(1, 11) + np.arange(0, 10)[:, np.newaxis])
print(X)
y = np.ones(10)
print(y)
print( np.arange(0, 10))#[:, np.newaxis])
# #############################################################################
# Compute paths
n_alphas = 200
alphas = np.logspace(-10, -2, n_alphas)
print(alphas)
coefs = []
for a in alphas:
ridge = linear_model.Ridge(alpha=a, fit_intercept=False)
ridge.fit(X, y)
coefs.append(ridge.coef_)
print(coefs)
print(len(coefs[0]))
# #############################################################################
# Display results
ax = plt.gca()
ax.plot(alphas, coefs)
ax.set_xscale('log')
ax.set_xlim(ax.get_xlim()[::-1]) # reverse axis
plt.xlabel('alpha')
plt.ylabel('weights')
plt.title('Ridge coefficients as a function of the regularization')
plt.axis('tight')
plt.show()