ML之LiR&2PolyR:使用线性回归LiR、二次多项式回归2PolyR模型在披萨数据集上拟合(train)、价格回归预测(test)

目录

输出结果

设计思路

核心代码


输出结果

设计思路

核心代码

poly2 = PolynomialFeatures(degree=2)
X_train_poly2 = poly2.fit_transform(X_train)r_poly2 = LinearRegression()
r_poly2.fit(X_train_poly2, y_train)
poly2 = r_poly2.predict(xx_poly2) 
class PolynomialFeatures(BaseEstimator, TransformerMixin):"""Generate polynomial and interaction features.Generate a new feature matrix consisting of all polynomial combinationsof the features with degree less than or equal to the specified degree.For example, if an input sample is two dimensional and of the form[a, b], the degree-2 polynomial features are [1, a, b, a^2, ab, b^2].Parameters----------degree : integerThe degree of the polynomial features. Default = 2.interaction_only : boolean, default = FalseIf true, only interaction features are produced: features that areproducts of at most ``degree`` *distinct* input features (so not``x[1] ** 2``, ``x[0] * x[2] ** 3``, etc.).include_bias : booleanIf True (default), then include a bias column, the feature in whichall polynomial powers are zero (i.e. a column of ones - acts as anintercept term in a linear model).Examples-------->>> X = np.arange(6).reshape(3, 2)>>> Xarray([[0, 1],[2, 3],[4, 5]])>>> poly = PolynomialFeatures(2)>>> poly.fit_transform(X)array([[  1.,   0.,   1.,   0.,   0.,   1.],[  1.,   2.,   3.,   4.,   6.,   9.],[  1.,   4.,   5.,  16.,  20.,  25.]])>>> poly = PolynomialFeatures(interaction_only=True)>>> poly.fit_transform(X)array([[  1.,   0.,   1.,   0.],[  1.,   2.,   3.,   6.],[  1.,   4.,   5.,  20.]])Attributes----------powers_ : array, shape (n_output_features, n_input_features)powers_[i, j] is the exponent of the jth input in the ith output.n_input_features_ : intThe total number of input features.n_output_features_ : intThe total number of polynomial output features. The number of outputfeatures is computed by iterating over all suitably sized combinationsof input features.Notes-----Be aware that the number of features in the output array scalespolynomially in the number of features of the input array, andexponentially in the degree. High degrees can cause overfitting.See :ref:`examples/linear_model/plot_polynomial_interpolation.py<sphx_glr_auto_examples_linear_model_plot_polynomial_interpolation.py>`"""def __init__(self, degree=2, interaction_only=False, include_bias=True):self.degree = degreeself.interaction_only = interaction_onlyself.include_bias = include_bias@staticmethoddef _combinations(n_features, degree, interaction_only, include_bias):comb = combinations if interaction_only else combinations_w_rstart = int(not include_bias)return chain.from_iterable(comb(range(n_features), i) for i in range(start, degree + 1))@propertydef powers_(self):check_is_fitted(self, 'n_input_features_')combinations = self._combinations(self.n_input_features_, self.degree, self.interaction_only, self.include_bias)return np.vstack(np.bincount(c, minlength=self.n_input_features_) for c in combinations)def get_feature_names(self, input_features=None):"""Return feature names for output featuresParameters----------input_features : list of string, length n_features, optionalString names for input features if available. By default,"x0", "x1", ... "xn_features" is used.Returns-------output_feature_names : list of string, length n_output_features"""powers = self.powers_if input_features is None:input_features = ['x%d' % i for i in range(powers.shape[1])]feature_names = []for row in powers:inds = np.where(row)[0]if len(inds):name = " ".join("%s^%d" % (input_features[ind], exp) if exp != 1 else input_features[ind] for (ind, exp) in zip(inds, row[inds]))else:name = "1"feature_names.append(name)return feature_namesdef fit(self, X, y=None):"""Compute number of output features.Parameters----------X : array-like, shape (n_samples, n_features)The data.Returns-------self : instance"""n_samples, n_features = check_array(X).shapecombinations = self._combinations(n_features, self.degree, self.interaction_only, self.include_bias)self.n_input_features_ = n_featuresself.n_output_features_ = sum(1 for _ in combinations)return selfdef transform(self, X):"""Transform data to polynomial featuresParameters----------X : array-like, shape [n_samples, n_features]The data to transform, row by row.Returns-------XP : np.ndarray shape [n_samples, NP]The matrix of features, where NP is the number of polynomialfeatures generated from the combination of inputs."""check_is_fitted(self, ['n_input_features_', 'n_output_features_'])X = check_array(X, dtype=FLOAT_DTYPES)n_samples, n_features = X.shapeif n_features != self.n_input_features_:raise ValueError("X shape does not match training shape")# allocate output dataXP = np.empty((n_samples, self.n_output_features_), dtype=X.dtype)combinations = self._combinations(n_features, self.degree, self.interaction_only, self.include_bias)for i, c in enumerate(combinations)::i]XP[ = X[:c].prod(1)return XP

ML之LiR2PolyR:使用线性回归LiR、二次多项式回归2PolyR模型在披萨数据集上拟合(train)、价格回归预测(test)相关推荐

  1. ML之LiR2PolyR4PolyR:使用线性回归LiR、二次多项式回归2PolyR、四次多项式回归4PolyR模型在披萨数据集上拟合(train)、价格回归预测(test)

    ML之LiR&2PolyR&4PolyR:使用线性回归LiR.二次多项式回归2PolyR.四次多项式回归4PolyR模型在披萨数据集上拟合(train).价格回归预测(test) 目录 ...

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

    ML之LiR:使用线性回归LiR回归模型在披萨数据集上拟合(train).价格回归预测(test) 目录 输出结果 设计思路 核心代码 输出结果 设计思路 核心代码 r= LinearRegressi ...

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

    ML之4PolyR:利用四次多项式回归4PolyR模型+两种正则化(Lasso/Ridge)在披萨数据集上拟合(train).价格回归预测(test) 目录 输出结果 设计思路 核心代码 输出结果 设 ...

  4. ML之LiR:利用LiR线性回归算法(自定义目标函数MSE和优化器GD)对Boston房价数据集(两特征+归一化)进行回归预测

    ML之LiR:利用LiR线性回归算法(自定义目标函数MSE和优化器GD)对Boston房价数据集(两特征+归一化)进行回归预测 目录 利用LiR线性回归算法(自定义目标函数MSE和优化器GD)对Bos ...

  5. ML之LiRSGDR:基于二种算法(LiR、SGDR)对Boston(波士顿房价)数据集(506,13+1)进行价格回归预测并对比各自性能

    ML之LiR&SGDR:基于二种算法(LiR.SGDR)对Boston(波士顿房价)数据集(506,13+1)进行价格回归预测并对比各自性能 目录 输出结果 设计思路 核心代码 输出结果 Bo ...

  6. ML之LiRLassoR:利用boston房价数据集(PCA处理)采用线性回归和Lasso套索回归算法实现房价预测模型评估

    ML之LiR&LassoR:利用boston房价数据集(PCA处理)采用线性回归和Lasso套索回归算法实现房价预测模型评估 目录 利用boston房价数据集(PCA处理)采用线性回归和Las ...

  7. 十二、案例:加利福尼亚房屋价值数据集(多元线性回归) Lasso 岭回归 分箱处理非线性问题 多项式回归

    案例:加利福尼亚房屋价值数据集(线性回归)& Lasso & 岭回归 & 分箱处理非线性问题 点击标题即可获取文章源代码和笔记 1. 导入需要的模块和库 from sklear ...

  8. 用Matlab求二次多项式,matlab二次多项式拟合

    用matlab做散点的二次曲线拟合_数学_自然科学_专业资料.例 对下面一组数据作二次多项式拟合 xi 0.1 0.2 0.4 0.5 0.6 0.7 0.8 0.9 1 yi 1.978 ..... ...

  9. 肤色检测算法 - 基于二次多项式混合模型的肤色检测

    1.二次多项式混合模型 二次多项式混合模型首先有SORIANO提出,此后CHIANG对此进行了改进.改进后的模型由两个R-G平面的二次多项式和一个圆方程构成: 在以上三个方程的基础上,肤色区域可以通过 ...

最新文章

  1. 我学Delphi心得与笔记-------在控件上如何禁用Ctrl+V
  2. python学习笔记四——数据类型
  3. Visual Studio控制台程序输出窗口一闪而过的解决方法
  4. Maven 中 plugins 和 pluginManagement的区别
  5. 区块链如何击败 AI、云计算成为最受欢迎技能?
  6. 计算机基础(二):嵌入式驱动、图像处理知识设备小结
  7. poj3262(Protecting the Flowers)贪心
  8. 虚拟机网卡还原默认设置
  9. MyBatis(三)------MyBatis的核心组件
  10. 阿里P8手把手教你!java私塾培训
  11. 华为模拟器eNSP下载与安装教程(面向小白)
  12. 如何使用Java以编程方式在Excel中创建数据透视表?
  13. Audio-预训练模型(一):概述
  14. A. Neko Finds Grapes-奇偶的性质及运用-Codeforces Round #554 (Div. 2)
  15. java菜鸟快速上手指南
  16. 区块链行业会多,饭局多,但我偏爱巴比特 | 巴比特乌镇大会
  17. # 将日期的符串(Sat Mar 21 08:00:00 CST 2020 )转换为指定字符串日期(yyyy-MM-dd HH:mm:ss)
  18. github如何上传代码到仓库(从本地上传代码到github)
  19. 小块渲染VS渐进式渲染
  20. 2020考研,老学长帮你规划

热门文章

  1. 打印dog信息java_java – 打印arraylist元素?
  2. maven打包到本地库
  3. 太阳能发电产业有望大发展
  4. github emoji 表情列表
  5. 调试node服务器-过程
  6. 用Navicat连接Oracle数据库时报错ORA-28547:connection to server failed, probable Oracle Net admin error...
  7. VMware介绍与网络的三种模式
  8. 破解CMOS SETUP密码诀窍
  9. 【图解篇】斯达康XV6700刷机超完整教程!!!
  10. 简述Linux虚拟内存管理