ML之LiR:使用线性回归LiR回归模型在披萨数据集上拟合(train)、价格回归预测(test)
生活随笔
收集整理的這篇文章主要介紹了
ML之LiR:使用线性回归LiR回归模型在披萨数据集上拟合(train)、价格回归预测(test)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
ML之LiR:使用線性回歸LiR回歸模型在披薩數據集上擬合(train)、價格回歸預測(test)
?
?
?
?
目錄
輸出結果
設計思路
核心代碼
?
?
?
?
?
?
輸出結果
?
設計思路
?
?
核心代碼
r= LinearRegression() r.fit(X_train, y_train)x = np.linspace(0, 26, 100) x = x.reshape(xx.shape[0], 1) y = r.predict(x) class LinearRegression(LinearModel, RegressorMixin):"""Ordinary least squares Linear Regression.Parameters----------fit_intercept : boolean, optional, default Truewhether to calculate the intercept for this model. If setto False, no intercept will be used in calculations(e.g. data is expected to be already centered).normalize : boolean, optional, default FalseThis parameter is ignored when ``fit_intercept`` is set to False.If True, the regressors X will be normalized before regression bysubtracting the mean and dividing by the l2-norm.If you wish to standardize, please use:class:`sklearn.preprocessing.StandardScaler` before calling ``fit`` onan estimator with ``normalize=False``.copy_X : boolean, optional, default TrueIf True, X will be copied; else, it may be overwritten.n_jobs : int, optional, default 1The number of jobs to use for the computation.If -1 all CPUs are used. This will only provide speedup forn_targets > 1 and sufficient large problems.Attributes----------coef_ : array, shape (n_features, ) or (n_targets, n_features)Estimated coefficients for the linear regression problem.If multiple targets are passed during the fit (y 2D), thisis a 2D array of shape (n_targets, n_features), while if onlyone target is passed, this is a 1D array of length n_features.intercept_ : arrayIndependent term in the linear model.Notes-----From the implementation point of view, this is just plain OrdinaryLeast Squares (scipy.linalg.lstsq) wrapped as a predictor object."""def __init__(self, fit_intercept=True, normalize=False, copy_X=True, n_jobs=1):self.fit_intercept = fit_interceptself.normalize = normalizeself.copy_X = copy_Xself.n_jobs = n_jobsdef fit(self, X, y, sample_weight=None):"""Fit linear model.Parameters----------X : numpy array or sparse matrix of shape [n_samples,n_features]Training datay : numpy array of shape [n_samples, n_targets]Target values. Will be cast to X's dtype if necessarysample_weight : numpy array of shape [n_samples]Individual weights for each sample.. versionadded:: 0.17parameter *sample_weight* support to LinearRegression.Returns-------self : returns an instance of self."""n_jobs_ = self.n_jobsX, y = check_X_y(X, y, accept_sparse=['csr', 'csc', 'coo'], y_numeric=True, multi_output=True)if sample_weight is not None and np.atleast_1d(sample_weight).ndim > 1:raise ValueError("Sample weights must be 1D array or scalar")X, y, X_offset, y_offset, X_scale = self._preprocess_data(X, y, fit_intercept=self.fit_intercept, normalize=self.normalize, copy=self.copy_X, sample_weight=sample_weight)if sample_weight is not None:# Sample weight can be implemented via a simple rescaling.X, y = _rescale_data(X, y, sample_weight)if sp.issparse(X):if y.ndim < 2:out = sparse_lsqr(X, y)self.coef_ = out[0]self._residues = out[3]else:# sparse_lstsq cannot handle y with shape (M, K)outs = Parallel(n_jobs=n_jobs_)(delayed(sparse_lsqr)(X, :j]ravel()) for y[.j in range(y.shape[1]))self.coef_ = np.vstack(out[0] for out in outs)self._residues = np.vstack(out[3] for out in outs)else:self.coef_, self._residues, self.rank_, self.singular_ = linalg.lstsq(X, y)self.coef_ = self.coef_.Tif y.ndim == 1:self.coef_ = np.ravel(self.coef_)self._set_intercept(X_offset, y_offset, X_scale)return self?
?
?
總結
以上是生活随笔為你收集整理的ML之LiR:使用线性回归LiR回归模型在披萨数据集上拟合(train)、价格回归预测(test)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: ML之DT:基于DT决策树算法(交叉验证
- 下一篇: ML之LiR2PolyR:使用线性回归L