ML之LiR:使用线性回归LiR回归模型在披萨数据集上拟合(train)、价格回归预测(test)


板凳老迟到
板凳老迟到 2022-09-19 15:27:53 46903
分类专栏: 资讯

ML之LiR:使用线性回归LiR回归模型在披萨数据集上拟合(train)、价格回归预测(test)

目录

输出结果

设计思路

核心代码


输出结果

设计思路

核心代码

  1. r= LinearRegression()
  2. r.fit(X_train, y_train)
  3. x = np.linspace(0, 26, 100)
  4. x = x.reshape(xx.shape[0], 1)
  5. y = r.predict(x)
  1. class LinearRegression(LinearModel, RegressorMixin):
  2. """
  3. Ordinary least squares Linear Regression.
  4. Parameters
  5. ----------
  6. fit_intercept : boolean, optional, default True
  7. whether to calculate the intercept for this model. If set
  8. to False, no intercept will be used in calculations
  9. (e.g. data is expected to be already centered).
  10. normalize : boolean, optional, default False
  11. This parameter is ignored when ``fit_intercept`` is set to
  12. False.
  13. If True, the regressors X will be normalized before
  14. regression by
  15. subtracting the mean and dividing by the l2-norm.
  16. If you wish to standardize, please use
  17. :class:`sklearn.preprocessing.StandardScaler` before
  18. calling ``fit`` on
  19. an estimator with ``normalize=False``.
  20. copy_X : boolean, optional, default True
  21. If True, X will be copied; else, it may be overwritten.
  22. n_jobs : int, optional, default 1
  23. The number of jobs to use for the computation.
  24. If -1 all CPUs are used. This will only provide speedup for
  25. n_targets > 1 and sufficient large problems.
  26. Attributes
  27. ----------
  28. coef_ : array, shape (n_features, ) or (n_targets, n_features)
  29. Estimated coefficients for the linear regression problem.
  30. If multiple targets are passed during the fit (y 2D), this
  31. is a 2D array of shape (n_targets, n_features), while if only
  32. one target is passed, this is a 1D array of length
  33. n_features.
  34. intercept_ : array
  35. Independent term in the linear model.
  36. Notes
  37. -----
  38. From the implementation point of view, this is just plain
  39. Ordinary
  40. Least Squares (scipy.linalg.lstsq) wrapped as a predictor
  41. object.
  42. """
  43. def __init__(self, fit_intercept=True, normalize=False,
  44. copy_X=True,
  45. n_jobs=1):
  46. self.fit_intercept = fit_intercept
  47. self.normalize = normalize
  48. self.copy_X = copy_X
  49. self.n_jobs = n_jobs
  50. def fit(self, X, y, sample_weight=None):
  51. """
  52. Fit linear model.
  53. Parameters
  54. ----------
  55. X : numpy array or sparse matrix of shape [n_samples,
  56. n_features]
  57. Training data
  58. y : numpy array of shape [n_samples, n_targets]
  59. Target values. Will be cast to X's dtype if necessary
  60. sample_weight : numpy array of shape [n_samples]
  61. Individual weights for each sample
  62. .. versionadded:: 0.17
  63. parameter *sample_weight* support to
  64. LinearRegression.
  65. Returns
  66. -------
  67. self : returns an instance of self.
  68. """
  69. n_jobs_ = self.n_jobs
  70. X, y = check_X_y(X, y, accept_sparse=['csr', 'csc', 'coo'],
  71. y_numeric=True, multi_output=True)
  72. if sample_weight is not None and np.atleast_1d
  73. (sample_weight).ndim > 1:
  74. raise ValueError("Sample weights must be 1D array
  75. or scalar")
  76. X, y, X_offset, y_offset, X_scale = self._preprocess_data(
  77. X, y, fit_intercept=self.fit_intercept, normalize=self.
  78. normalize,
  79. copy=self.copy_X, sample_weight=sample_weight)
  80. if sample_weight is not None:
  81. Sample weight can be implemented via a simple
  82. rescaling.
  83. X, y = _rescale_data(X, y, sample_weight)
  84. if sp.issparse(X):
  85. if y.ndim < 2:
  86. out = sparse_lsqr(X, y)
  87. self.coef_ = out[0]
  88. self._residues = out[3]
  89. else:
  90. sparse_lstsq cannot handle y with shape (M, K)
  91. outs = Parallel(n_jobs=n_jobs_)(
  92. delayed(sparse_lsqr)(X, :j]ravel()) for y[.
  93. j in range(y.shape[1]))
  94. self.coef_ = np.vstack(out[0] for out in outs)
  95. self._residues = np.vstack(out[3] for out in outs)
  96. else:
  97. self.coef_, self._residues, self.rank_, self.singular_ =
  98. linalg.lstsq(X, y)
  99. self.coef_ = self.coef_.T
  100. if y.ndim == 1:
  101. self.coef_ = np.ravel(self.coef_)
  102. self._set_intercept(X_offset, y_offset, X_scale)
  103. return self

网站声明:如果转载,请联系本站管理员。否则一切后果自行承担。

本文链接:https://www.xckfsq.com/news/show.html?id=3282
赞同 0
评论 0 条
板凳老迟到L0
粉丝 0 发表 8 + 关注 私信
上周热门
如何使用 StarRocks 管理和优化数据湖中的数据?  2951
【软件正版化】软件正版化工作要点  2872
统信UOS试玩黑神话:悟空  2833
信刻光盘安全隔离与信息交换系统  2728
镜舟科技与中启乘数科技达成战略合作,共筑数据服务新生态  1261
grub引导程序无法找到指定设备和分区  1226
华为全联接大会2024丨软通动力分论坛精彩议程抢先看!  165
2024海洋能源产业融合发展论坛暨博览会同期活动-海洋能源与数字化智能化论坛成功举办  163
点击报名 | 京东2025校招进校行程预告  163
华为纯血鸿蒙正式版9月底见!但Mate 70的内情还得接着挖...  159
本周热议
我的信创开放社区兼职赚钱历程 40
今天你签到了吗? 27
如何玩转信创开放社区—从小白进阶到专家 15
信创开放社区邀请他人注册的具体步骤如下 15
方德桌面操作系统 14
用抖音玩法闯信创开放社区——用平台宣传企业产品服务 13
我有15积分有什么用? 13
如何让你先人一步获得悬赏问题信息?(创作者必看) 12
2024中国信创产业发展大会暨中国信息科技创新与应用博览会 9
中央国家机关政府采购中心:应当将CPU、操作系统符合安全可靠测评要求纳入采购需求 8

加入交流群

请使用微信扫一扫!