[np-ml] Linear Regression
1.[np-ml] Ridge Regression
2.[np-ml] Linear Regression
算法描述
A weighted linear least-squares regression model.
Notes
In weighted linear least-squares regression, a real-valued target vector,
where
Under the model, the maximum-likelihood estimate for the regression coefficients,
where
When
算法实现
直接按照解析解进行更新:
import numpy as np def fit(X, y, fit_intercept=True, weights=None): N = X.shape[0] weights = np.ones(N) if weights is None else np.atleast_1d(weights) weights = np.squeeze(weights) if weights.size > 1 else weights err_str = f"`weights` must have shape ({N},), but got {weights.shape}" assert weights.shape == (N,), err_str # scale X and y by the weight associated with each examples W = np.diag(np.sqrt(weights)) X, y = W @ X, W @ y # convert X to a design matrix if we are fitting an intercept if fit_intercept: X = np.c_[np.sqrt(weights), X] sigma_inv = np.linalg.pinv(X.T @ X) beta = np.atleast_2d(sigma_inv @ X.T @ y) return sigma_inv, beta
SGD方式进行模型参数更新:
import numpy as np from numpy import ndarray def main(X: ndarray, y: ndarray, num_iteration=1000, learning_rate=1e-2, weights=None): _, input_dim = X.shape # initialize weights weights = weights or np.random.randn(input_dim, 1) for iteration in range(num_iteration): # (batch_size, 1) y_pred = X @ weights # (batch_size, 1) diff = y_pred - y # (input_dim, 1) grad = X.T @ diff weights -= learning_rate * grad return weights def test_main(): A = np.array([[1, 1, 0]]).T X = np.random.rand(5, 3) y = X @ A weights = main(X, y, num_iteration=10000, learning_rate=1e-2) assert np.allclose(A, weights, rtol=1e-4), f"{weights=}\n but expect {A=}" if __name__ == "__main__": test_main()
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库
· SQL Server 2025 AI相关能力初探
· 为什么 退出登录 或 修改密码 无法使 token 失效