2.0 KiB
2.0 KiB
In [7]:
from sklearn import linear_model
import numpy as np
# Form the coefficient matrix of the linear system
X_transpose = np.array([[1, 1], [1, 2], [2, 2], [2, 3]])
# The underlying linear function is y = 1 * x_1 + 2 * x_2 + epsilon
epsilon = np.random.rand(4,)
y = np.dot(X_transpose, np.array([1, 2])) + epsilon
# Various regression models
#clf = linear_model.LinearRegression()
#clf = linear_model.Ridge(alpha=0.1)
clf = linear_model.Lasso(alpha=0.1)
# Fit the model to the data
clf.fit(X_transpose, y)
# Print the regression coefficients
print(clf.coef_)
[0.15631421 2.24887843]
In [ ]: