使用多项式特征缩放特征

时间:2019-04-02 12:01:53

标签: python-3.x machine-learning knn non-linear-regression

我了解sklearn的多项式回归的概念以及PolynomialFeatures的使用。

但是要具体了解使用PolynomialFeature扩展的概念,我只想询问一个场景。

在这里,我想考虑采用KNN而不是回归的多项式特征。

这就是我们使用多项式特征进行回归的方式。

from sklearn.preprocessing import PolynomialFeatures

X_train, X_test, y_train, y_test = train_test_split(X_T, y_T, random_state = 0)
poly = PolynomialFeatures(degree=3)
X_T_poly = poly.fit_transform(X_T)
X_train, X_test, y_train, y_test = train_test_split(X_T_poly, y_T, random_state = 0)
fin = LinearRegression().fit(X_train, y_train)

现在,如果我这样做:

from sklearn.preprocessing import PolynomialFeatures

X_train, X_test, y_train, y_test = train_test_split(X_T, y_T, random_state = 0)
poly = PolynomialFeatures(degree=3)
X_T_poly = poly.fit_transform(X_T)
X_train, X_test, y_train, y_test = train_test_split(X_T_poly, y_T, random_state = 0)
knn = KNeighborsClassifier(n_neighbors = n)
knn.fit(X_train, y_train)

以前,我们正在做回归(我知道了。好的),后来我们使用了KNN。

  1. 在这里应用KNN并正确(首先)是正确的吗?

  2. 在KNN情况下如何转换要素?

进入 .fit_transform()后,我发现了这一点:

该函数与系数和截距无关。

def fit_transform(self, X, y=None, **fit_params):
    """Fit to data, then transform it.
    Fits transformer to X and y with optional parameters fit_params
    and returns a transformed version of X.
    Parameters
    ----------
    X : numpy array of shape [n_samples, n_features]
        Training set.
    y : numpy array of shape [n_samples]
        Target values.
    Returns
    -------
    X_new : numpy array of shape [n_samples, n_features_new]
        Transformed array.
    """
    # non-optimized default implementation; override when a better
    # method is possible for a given clustering algorithm
    if y is None:
        # fit method of arity 1 (unsupervised transformation)
        return self.fit(X, **fit_params).transform(X)
    else:
        # fit method of arity 2 (supervised transformation)
return self.fit(X, y, **fit_params).transform(X)

那么可以像上面那样在KNN上使用PolynomialFeatures这样的做法吗?

0 个答案:

没有答案