RAM*_*RAM 3 python regression machine-learning linear-regression scikit-learn
在获得 Lambda 的最佳值后,我正在尝试套索回归,现在问题是,我想获得系数(权重向量),因为我想将它们与岭回归的权重进行比较。
lasso = Lasso(alpha=optimal_lmbda, fit_intercept=True, random_state=1142, max_iter=5000)
lasso.fit(X_train, y_train)
y_pred_lasso = lasso.predict(X_test)
Run Code Online (Sandbox Code Playgroud)
在Sklearn中的python中拟合套索回归后如何获得系数(权重向量)?
只需按照文档 sklearn.linear_model.Lasso
# Build lasso and fit
lasso = Lasso(...)
lasso.fit(...)
# Read out attributes
coeffs = lasso.coef_ # dense np.array
coeffs = lasso.sparse_coef_ # sparse matrix
coeffs = lasso.intercept_ # probably also relevant
Run Code Online (Sandbox Code Playgroud)
lasso.coef_和lasso.sparse_coef_
type,如在线评论和文档说明。numpy-array或稀疏矩阵(来自scipy.sparse)。后者是相关的,如果你有很多变量,而且其中很多都是零,因为强正则化(已知 l1 会影响很多零)。那么存储 0 是没有用的,这就是稀疏数据结构的用途。中没有密集向量类型scipy,因此使用稀疏矩阵。内容是一样的!| 归档时间: |
|
| 查看次数: |
9796 次 |
| 最近记录: |