ML之4PolyR:利用四次多项式回归4PolyR模型+两种正则化(Lasso/Ridge)在披萨数据集上拟合(train)、价格回归预测(test)

简介: ML之4PolyR:利用四次多项式回归4PolyR模型+两种正则化(Lasso/Ridge)在披萨数据集上拟合(train)、价格回归预测(test)

输出结

image.png

image.png

image.png

 

设计思

image.png

image.png

 

核心代

lasso_poly4 = Lasso()

lasso_poly4.fit(X_train_poly4, y_train)

ridge_poly4 = Ridge()

ridge_poly4.fit(X_train_poly4, y_train)


class Lasso(ElasticNet):

   """Linear Model trained with L1 prior as regularizer (aka the Lasso)

 

   The optimization objective for Lasso is::

 

   (1 / (2 * n_samples)) * ||y - Xw||^2_2 + alpha * ||w||_1

 

   Technically the Lasso model is optimizing the same objective function as

   the Elastic Net with ``l1_ratio=1.0`` (no L2 penalty).

 

   Read more in the :ref:`User Guide <lasso>`.

 

   Parameters

   ----------

   alpha : float, optional

   Constant that multiplies the L1 term. Defaults to 1.0.

   ``alpha = 0`` is equivalent to an ordinary least square, solved

   by the :class:`LinearRegression` object. For numerical

   reasons, using ``alpha = 0`` with the ``Lasso`` object is not advised.

   Given this, you should use the :class:`LinearRegression` object.

 

   fit_intercept : boolean

   whether to calculate the intercept for this model. If set

   to false, no intercept will be used in calculations

   (e.g. data is expected to be already centered).

 

   normalize : boolean, optional, default False

   This parameter is ignored when ``fit_intercept`` is set to False.

   If True, the regressors X will be normalized before regression by

   subtracting the mean and dividing by the l2-norm.

   If you wish to standardize, please use

   :class:`sklearn.preprocessing.StandardScaler` before calling ``fit``

   on an estimator with ``normalize=False``.

 

   precompute : True | False | array-like, default=False

   Whether to use a precomputed Gram matrix to speed up

   calculations. If set to ``'auto'`` let us decide. The Gram

   matrix can also be passed as argument. For sparse input

   this option is always ``True`` to preserve sparsity.

 

   copy_X : boolean, optional, default True

   If ``True``, X will be copied; else, it may be overwritten.

 

   max_iter : int, optional

   The maximum number of iterations

 

   tol : float, optional

   The tolerance for the optimization: if the updates are

   smaller than ``tol``, the optimization code checks the

   dual gap for optimality and continues until it is smaller

   than ``tol``.

 

   warm_start : bool, optional

   When set to True, reuse the solution of the previous call to fit as

   initialization, otherwise, just erase the previous solution.

 

   positive : bool, optional

   When set to ``True``, forces the coefficients to be positive.

 

   random_state : int, RandomState instance or None, optional, default

    None

   The seed of the pseudo random number generator that selects a

    random

   feature to update.  If int, random_state is the seed used by the random

   number generator; If RandomState instance, random_state is the

    random

   number generator; If None, the random number generator is the

   RandomState instance used by `np.random`. Used when ``selection`` ==

   'random'.

 

   selection : str, default 'cyclic'

   If set to 'random', a random coefficient is updated every iteration

   rather than looping over features sequentially by default. This

   (setting to 'random') often leads to significantly faster convergence

   especially when tol is higher than 1e-4.

 

   Attributes

   ----------

   coef_ : array, shape (n_features,) | (n_targets, n_features)

   parameter vector (w in the cost function formula)

 

   sparse_coef_ : scipy.sparse matrix, shape (n_features, 1) | \

   (n_targets, n_features)

   ``sparse_coef_`` is a readonly property derived from ``coef_``

 

   intercept_ : float | array, shape (n_targets,)

   independent term in decision function.

 

   n_iter_ : int | array-like, shape (n_targets,)

   number of iterations run by the coordinate descent solver to reach

   the specified tolerance.

 

   Examples

   --------

   >>> from sklearn import linear_model

   >>> clf = linear_model.Lasso(alpha=0.1)

   >>> clf.fit([[0,0], [1, 1], [2, 2]], [0, 1, 2])

   Lasso(alpha=0.1, copy_X=True, fit_intercept=True, max_iter=1000,

   normalize=False, positive=False, precompute=False,

    random_state=None,

   selection='cyclic', tol=0.0001, warm_start=False)

   >>> print(clf.coef_)

   [ 0.85  0.  ]

   >>> print(clf.intercept_)

   0.15

 

   See also

   --------

   lars_path

   lasso_path

   LassoLars

   LassoCV

   LassoLarsCV

   sklearn.decomposition.sparse_encode

 

   Notes

   -----

   The algorithm used to fit the model is coordinate descent.

 

   To avoid unnecessary memory duplication the X argument of the fit

    method

   should be directly passed as a Fortran-contiguous numpy array.

   """

   path = staticmethod(enet_path)

   def __init__(self, alpha=1.0, fit_intercept=True, normalize=False,

       precompute=False, copy_X=True, max_iter=1000,

       tol=1e-4, warm_start=False, positive=False,

       random_state=None, selection='cyclic'):

       super(Lasso, self).__init__(alpha=alpha, l1_ratio=1.0,

        fit_intercept=fit_intercept, normalize=normalize,

        precompute=precompute, copy_X=copy_X, max_iter=max_iter, tol=tol,

        warm_start=warm_start, positive=positive, random_state=random_state,

        selection=selection)

######################################################

#########################

# Functions for CV with paths functions


 

相关文章
|
自然语言处理 API 区块链
多语言TRX投资理财系统/波场trx自动归集钱包智能合约开发部署
多语言TRX投资理财系统/波场trx自动归集钱包智能合约开发部署
|
存储 人机交互 数据库
如何数据库设计?
本文介绍了数据库设计的四种方法和基本步骤。直观设计法依赖设计者经验,规范设计法(如新奥尔良法)遵循软件工程原理,分为需求分析、概念设计、逻辑设计和物理设计四个阶段。计算机辅助设计法借助软件工具,自动化设计法则通过人机会话自动生成数据库。设计步骤包括需求分析、概念结构设计、逻辑结构设计、物理结构设计、数据库实施和运行维护。需求分析是关键,概念结构设计是基础,逻辑和物理设计涉及数据模型转换和存储优化,而运行维护是持续改进的过程。
663 0
如何数据库设计?
|
11月前
|
消息中间件 中间件 Kafka
MQ四兄弟:如何实现延时消息
本文介绍了几种常见的消息队列系统(RabbitMQ、RocketMQ、Kafka和Pulsar)实现延时消息的方式。RabbitMQ通过死信队列或延时插件实现;RocketMQ内置延时消息支持,可通过设置`delayTimeLevel`属性实现;Kafka不直接支持延时消息,但可以通过时间戳、延时Topic、Kafka Streams等方法间接实现;Pulsar自带延时消息功能,提供`deliverAfter`和`deliverAt`两种方式。每种方案各有优劣,适用于不同的应用场景。
1165 0
|
开发框架 前端开发 JavaScript
React、Vue.js 和 Angular主流前端框架和选择指南
在当今的前端开发领域,选择合适的框架对于项目的成功至关重要。本文将介绍几个主流的前端框架——React、Vue.js 和 Angular,探讨它们各自的特点、开发场景、优缺点,并提供选择框架的建议。
349 6
分享一些在 1688 上找一件代发商品的技巧
在1688上找一件代发商品需明确自身需求与定位,筛选可靠供应商,研究商品信息,利用精准搜索和平台推荐,关注活动,并与供应商充分沟通,确保合作顺畅。
|
机器学习/深度学习 PyTorch 测试技术
Ultralytics YOLOv5简介
Ultralytics YOLOv5简介
|
IDE Java Shell
Java的开发环境的搭建
Java的开发环境的搭建
452 4
|
分布式计算 DataWorks 大数据
DataWorks产品使用合集之DataWorks和MaxCompute存在差异如何解决
DataWorks作为一站式的数据开发与治理平台,提供了从数据采集、清洗、开发、调度、服务化、质量监控到安全管理的全套解决方案,帮助企业构建高效、规范、安全的大数据处理体系。以下是对DataWorks产品使用合集的概述,涵盖数据处理的各个环节。
292 0
|
Java
IDEA常用插件之代码规范检查
IDEA常用插件之代码规范检查
944 0
|
算法
LeetCode 热题 HOT 100题解 (easy级别)(一)
LeetCode 热题 HOT 100题解 (easy级别)
584 0