Python sklearn.preprocessing.PolynomialFeatures() Examples

The following are 30 code examples of sklearn.preprocessing.PolynomialFeatures(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module sklearn.preprocessing , or try the search function .
Example #1
Source File: test_sklearn_polynomial_features_converter.py    From sklearn-onnx with MIT License 7 votes vote down vote up
def test_model_polynomial_features_float_degree_2(self):
        X = np.array([[1.2, 3.2, 1.3, -5.6], [4.3, -3.2, 5.7, 1.0],
                      [0, 3.2, 4.7, -8.9]])
        model = PolynomialFeatures(degree=2).fit(X)
        model_onnx = convert_sklearn(
            model,
            "scikit-learn polynomial features",
            [("input", FloatTensorType([None, X.shape[1]]))],
        )
        self.assertTrue(model_onnx is not None)
        dump_data_and_model(
            X.astype(np.float32),
            model,
            model_onnx,
            basename="SklearnPolynomialFeaturesFloatDegree2",
            allow_failure="StrictVersion(onnxruntime.__version__)"
                          " <= StrictVersion('0.2.1')",
        ) 
Example #2
Source File: test_sklearn_polynomial_features_converter.py    From sklearn-onnx with MIT License 6 votes vote down vote up
def test_model_polynomial_features_float_degree_4(self):
        X = np.array([[1.2, 3.2, 3.1, 1.3], [4.3, 3.2, 0.5, 1.3],
                      [3.2, 4.7, 5.4, 7.1]])
        model = PolynomialFeatures(degree=4).fit(X)
        model_onnx = convert_sklearn(
            model,
            "scikit-learn polynomial features",
            [("input", FloatTensorType([None, X.shape[1]]))],
        )
        self.assertTrue(model_onnx is not None)
        dump_data_and_model(
            X.astype(np.float32),
            model,
            model_onnx,
            basename="SklearnPolynomialFeaturesFloatDegree4-Dec4",
            allow_failure="StrictVersion(onnxruntime.__version__)"
                          " <= StrictVersion('0.2.1')",
        ) 
Example #3
Source File: test_data.py    From dask-ml with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def test_transformed_shape(self):
        # checks if the transformed objects have the correct columns
        a = dpp.PolynomialFeatures()
        a.fit(X)
        n_cols = len(a.get_feature_names())
        # dask array
        assert a.transform(X).shape[1] == n_cols
        # numpy array
        assert a.transform(X.compute()).shape[1] == n_cols
        # dask dataframe
        assert a.transform(df).shape[1] == n_cols
        # pandas dataframe
        assert a.transform(df.compute()).shape[1] == n_cols
        X_nan_rows = df.values
        df_none_divisions = X_nan_rows.to_dask_dataframe(columns=df.columns)
        # dask array with nan rows
        assert a.transform(X_nan_rows).shape[1] == n_cols
        # dask data frame with nan rows
        assert a.transform(df_none_divisions).shape[1] == n_cols 
Example #4
Source File: test_sklearn_polynomial_features_converter.py    From sklearn-onnx with MIT License 6 votes vote down vote up
def test_model_polynomial_features_int_degree_2(self):
        X = np.array([
            [1, 3, 4, 0],
            [2, 3, 4, 1],
            [1, -4, 3, 7],
            [3, 10, -9, 5],
            [1, 0, 10, 5],
        ])
        model = PolynomialFeatures(degree=2).fit(X)
        model_onnx = convert_sklearn(
            model,
            "scikit-learn polynomial features",
            [("input", Int64TensorType([None, X.shape[1]]))],
        )
        self.assertTrue(model_onnx is not None)
        dump_data_and_model(
            X.astype(np.int64),
            model,
            model_onnx,
            basename="SklearnPolynomialFeaturesIntDegree2",
            allow_failure="StrictVersion(onnxruntime.__version__)"
                          " <= StrictVersion('0.2.1')",
        ) 
Example #5
Source File: test_sklearn_polynomial_features_converter.py    From sklearn-onnx with MIT License 6 votes vote down vote up
def test_model_polynomial_features_float_degree_3(self):
        X = np.array([[1.2, 3.2, 1.2], [4.3, 3.2, 4.5], [3.2, 4.7, 1.1]])
        model = PolynomialFeatures(degree=3).fit(X)
        model_onnx = convert_sklearn(
            model,
            "scikit-learn polynomial features",
            [("input", FloatTensorType([None, X.shape[1]]))],
        )
        self.assertTrue(model_onnx is not None)
        dump_data_and_model(
            X.astype(np.float32),
            model,
            model_onnx,
            basename="SklearnPolynomialFeaturesFloatDegree3",
            allow_failure="StrictVersion(onnxruntime.__version__)"
                          " <= StrictVersion('0.2.1')",
        ) 
Example #6
Source File: sindy.py    From sparsereg with MIT License 6 votes vote down vote up
def fit(self, x, y=None):
        if y is not None:
            xdot = y
        else:
            xdot = self.derivative.transform(x)

        if self.operators is not None:
            feature_transformer = SymbolicFeatures(
                exponents=np.linspace(1, self.degree, self.degree), operators=self.operators
            )
        else:
            feature_transformer = PolynomialFeatures(degree=self.degree, include_bias=False)

        steps = [
            ("features", feature_transformer),
            ("model", STRidge(alpha=self.alpha, threshold=self.threshold, **self.kw)),
        ]
        self.model = MultiOutputRegressor(Pipeline(steps), n_jobs=self.n_jobs)
        self.model.fit(x, xdot)

        self.n_input_features_ = self.model.estimators_[0].steps[0][1].n_input_features_
        self.n_output_features_ = self.model.estimators_[0].steps[0][1].n_output_features_
        return self 
Example #7
Source File: test_sklearn_polynomial_features_converter.py    From sklearn-onnx with MIT License 6 votes vote down vote up
def test_model_polynomial_features_int_degree_3(self):
        X = np.array([
            [1, 3, 33],
            [4, 1, -11],
            [3, 7, -3],
            [3, 5, 4],
            [1, 0, 3],
            [5, 4, 9],
        ])
        model = PolynomialFeatures(degree=3).fit(X)
        model_onnx = convert_sklearn(
            model,
            "scikit-learn polynomial features",
            [("input", Int64TensorType([None, X.shape[1]]))],
        )
        self.assertTrue(model_onnx is not None)
        dump_data_and_model(
            X.astype(np.int64),
            model,
            model_onnx,
            basename="SklearnPolynomialFeaturesIntDegree3",
            allow_failure="StrictVersion(onnxruntime.__version__)"
                          " <= StrictVersion('0.2.1')",
        ) 
Example #8
Source File: test_preprocessing.py    From pandas-ml with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def test_objectmapper(self):
        df = pdml.ModelFrame([])
        self.assertIs(df.preprocessing.Binarizer, pp.Binarizer)
        self.assertIs(df.preprocessing.FunctionTransformer,
                      pp.FunctionTransformer)
        self.assertIs(df.preprocessing.Imputer, pp.Imputer)
        self.assertIs(df.preprocessing.KernelCenterer, pp.KernelCenterer)
        self.assertIs(df.preprocessing.LabelBinarizer, pp.LabelBinarizer)
        self.assertIs(df.preprocessing.LabelEncoder, pp.LabelEncoder)
        self.assertIs(df.preprocessing.MultiLabelBinarizer, pp.MultiLabelBinarizer)
        self.assertIs(df.preprocessing.MaxAbsScaler, pp.MaxAbsScaler)
        self.assertIs(df.preprocessing.MinMaxScaler, pp.MinMaxScaler)
        self.assertIs(df.preprocessing.Normalizer, pp.Normalizer)
        self.assertIs(df.preprocessing.OneHotEncoder, pp.OneHotEncoder)
        self.assertIs(df.preprocessing.PolynomialFeatures, pp.PolynomialFeatures)
        self.assertIs(df.preprocessing.RobustScaler, pp.RobustScaler)
        self.assertIs(df.preprocessing.StandardScaler, pp.StandardScaler) 
Example #9
Source File: ABuMLCreater.py    From abu with GNU General Public License v3.0 6 votes vote down vote up
def polynomial_regression(self, assign=True, degree=2, **kwargs):
        """
        有监督学习回归器,使用:
            make_pipeline(PolynomialFeatures(degree), LinearRegression(**kwargs))

        :param assign: 是否保存实例后的LinearRegression对象,默认True,self.reg = reg
        :param degree: 多项式拟合参数,默认2
        :param kwargs: 由make_pipeline(PolynomialFeatures(degree), LinearRegression(**kwargs))
                       即关键字参数**kwargs全部传递给LinearRegression做为构造参数

        :return: 实例化的回归对象
        """
        reg = make_pipeline(PolynomialFeatures(degree), LinearRegression(**kwargs))
        if assign:
            self.reg = reg
        return reg 
Example #10
Source File: c10.py    From abu with GNU General Public License v3.0 6 votes vote down vote up
def sample_1031_3():
    """
    10.3.1_3 猪老三使用回归预测股价:PolynomialFeatures
    :return:
    """
    train_x, train_y_regress, train_y_classification, pig_three_feature, \
    test_x, test_y_regress, test_y_classification, kl_another_word_feature_test = sample_1031_1()

    from sklearn.pipeline import make_pipeline
    from sklearn.preprocessing import PolynomialFeatures
    from sklearn.linear_model import LinearRegression

    # pipeline套上 degree=3 + LinearRegression
    estimator = make_pipeline(PolynomialFeatures(degree=3),
                              LinearRegression())
    # 继续使用regress_process,区别是estimator变了
    regress_process(estimator, train_x, train_y_regress, test_x,
                    test_y_regress)
    plt.show() 
Example #11
Source File: difference_calc.py    From code-jam-5 with MIT License 6 votes vote down vote up
def evaluate_timestamp(self, timestamp):
        """
        Gets datetime object and calculates as a prediction or as an
        interpolation

        - timestamp: datetime object (date_1/date_2 in `calculate`)
        > Returns float of prediction or interpolation
        """

        if (
            datetime.date(1993, 1, 15) > timestamp.date()
            or datetime.date(2019, 2, 7) < timestamp.date()
        ):
            # Perform some data preparation before being
            # able to pass it to the model
            return self.poly_model.predict(
                PolynomialFeatures(degree=3).fit_transform(
                    np.array([timestamp.timestamp()]).reshape(1, -1)
                )
            )[0][0]

        return self.model(timestamp.timestamp()) 
Example #12
Source File: poly_interpolation.py    From LSTM-Crypto-Price-Prediction with MIT License 6 votes vote down vote up
def poly_inter(self, data):
        # define x values for data points
        X = np.linspace(0, data.shape[0] - 1, data.shape[0])[:, np.newaxis]
        
        # define pipeline and fit model
        model = make_pipeline(PolynomialFeatures(self.degree), Ridge())
        model.fit(X, data)

        if self.plot: plot_poly(X, model.predict(X), data)
        
        # predict next interpolated value
        last = model.predict(np.array([[data.shape[0] - 1]]))
        pred = model.predict(np.array([[data.shape[0]]]))

        # return slope of last point
        return pred[0]/last[0] 
Example #13
Source File: test_data.py    From dask-ml with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_transform_array(self):
        a = dpp.PolynomialFeatures()
        b = spp.PolynomialFeatures()

        # pass numpy array to fit_transform
        res_a1 = a.fit_transform(X.compute())
        # pass dask array to fit_transform
        res_a2 = a.fit_transform(X).compute()
        res_b = b.fit_transform(X.compute())
        assert_eq_ar(res_a1, res_b)
        assert_eq_ar(res_a2, res_b) 
Example #14
Source File: feature_generators.py    From MAST-ML with MIT License 5 votes vote down vote up
def __init__(self, features=None, degree=2, interaction_only=False, include_bias=True):
        self.features = features
        self.SPF = SklearnPolynomialFeatures(degree, interaction_only, include_bias) 
Example #15
Source File: difference_calc.py    From code-jam-5 with MIT License 5 votes vote down vote up
def _fit_poly_model(self):
        """
        Create a mathematical model to estimate any data between
        before 1993 and after 2019. This process takes some time,
        but will only be called if no model exists.

        < Returns `<sklearn.linear_model.LinearRegression'>` class to
          eval all data in range
        """

        def flatten(l):
            return [item for sublist in l for item in sublist]

        X = np.array(
            flatten(
                np.linspace(726188400, 1551999600, 1376352).reshape((-1, 1))
            )
        )
        y = np.array(flatten(np.array([self.model(x) for x in X])))

        polynomial_features = PolynomialFeatures(degree=3)
        x_poly = polynomial_features.fit_transform(X.reshape(-1, 1))

        poly_model = LinearRegression()
        poly_model.fit(x_poly, y.reshape(-1, 1))

        with open(self.poly_file, "wb") as fid:
            pickle.dump(poly_model, fid)

        return poly_model 
Example #16
Source File: twosls.py    From DeepIV with MIT License 5 votes vote down vote up
def fit_twosls(x, z, t, y):
    '''
    Two stage least squares with polynomial basis function.
    '''
    params = dict(poly__degree=range(1,4),
                  ridge__alpha=np.logspace(-5, 5, 11))
    pipe = Pipeline([('poly', PolynomialFeatures()),
                        ('ridge', Ridge())])
    stage_1 = GridSearchCV(pipe, param_grid=params, cv=5)
    if z.shape[1] > 0:
        X = np.concatenate([x,z], axis=1)
    else:
        X = z
    stage_1.fit(X,t)
    t_hat = stage_1.predict(X)
    print("First stage paramers: " + str(stage_1.best_params_ ))

    pipe2 = Pipeline([('poly', PolynomialFeatures()),
                        ('ridge', Ridge())])
    stage_2 = GridSearchCV(pipe2, param_grid=params, cv=5)
    X2 = np.concatenate([x,t_hat], axis=1)
    stage_2.fit(X2, y)
    print("Best in sample score: %f" % stage_2.score(X2, y))
    print("Second stage paramers: " + str(stage_2.best_params_  ))

    def g_hat(x,z,t):
        X_new = np.concatenate([x, t], axis=1)
        return stage_2.predict(X_new)
    return g_hat 
Example #17
Source File: convex_opt.py    From oboe with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def predict(self, size):
        """Predict runtime of all model settings on a dataset of given size.
        
        Args:
            size(np.array): Size of the dataset to fit runtime onto.
        Returns:
            predictions (np.array): The predicted runtime.
        """
        if self.model_name == 'LinearRegression':
            size_test = np.append(size, np.log(size[0]))
            size_test_poly = PolynomialFeatures(self.degree).fit_transform([size_test])
            predictions = np.zeros(self.n_models)
            for i in range(self.n_models):
                predictions[i] = self.models[i].predict(size_test_poly)[0]
    
        elif self.model_name == 'KNeighborsRegressor':
            predictions = np.zeros(self.n_models)
            for i in range(self.n_models):
                predictions[i] = self.models[i].predict(np.array(size).reshape(1, -1))[0]
        
#        # TO BE REMOVED: sanity check
#
#        size_check = (1000, 10)
#        size_check = np.append(size, np.log(size[0]))
#        size_check_poly = PolynomialFeatures(self.degree).fit_transform([size_check])
#        print(size_check_poly)
#        for i in range(self.n_models):
#            print(self.models[i].predict(size_check_poly)[0])

        return predictions 
Example #18
Source File: convex_opt.py    From oboe with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def fit(self, sizes, sizes_index, runtimes, runtimes_index):
        """Fit polynomial regression on pre-recorded runtimes on datasets."""
        # assert sizes.shape[0] == runtimes.shape[0], "Dataset sizes and runtimes must be recorded on same datasets."
        for i in set(runtimes_index).difference(set(sizes_index)):
            dataset = openml.datasets.get_dataset(int(i))
            data_numeric, data_labels, categorical, _ = dataset.get_data(target=dataset.default_target_attribute)
            if len(sizes) == 0:
                sizes = np.array([data_numeric.shape])
                sizes_index = np.array(i)
            else:
                sizes = np.concatenate((sizes, np.array([data_numeric.shape])))
                sizes_index = np.append(sizes_index, i)

        sizes_train = np.array([sizes[list(sizes_index).index(i), :] for i in runtimes_index])
        sizes_log = np.concatenate((sizes_train, np.log(sizes_train[:, 0]).reshape(-1, 1)), axis=1)
        sizes_train_poly = PolynomialFeatures(self.degree).fit_transform(sizes_log)

        # train independent regression model to predict each runtime of each model setting
        for i in range(self.n_models):
            runtime = runtimes[:, i]
            no_nan_indices = np.where(np.invert(np.isnan(runtime)))[0]
            runtime_no_nan = runtime[no_nan_indices]
            
            
            if self.model_name == 'LinearRegression':
                sizes_train_poly_no_nan = sizes_train_poly[no_nan_indices]
                self.models[i] = LinearRegression().fit(sizes_train_poly_no_nan, runtime_no_nan)
            elif self.model_name == 'KNeighborsRegressor':
                sizes_train_no_nan = sizes_train[no_nan_indices]
                def metric(a, b):
                    coefficients = [1, 100]
                    return np.sum(np.multiply((a - b) ** 2, coefficients))
                        
                def weights(distances):
                    return distances

                neigh = KNeighborsRegressor(n_neighbors=5, metric=metric, weights=weights)
                self.models[i] = neigh.fit(sizes_train_no_nan, runtime_no_nan)
#            print(self.models[i].coef_)
#            print(self.models[i].intercept_)
            # self.models[i] = Lasso().fit(sizes_train_poly, runtime) 
Example #19
Source File: test_data.py    From dask-ml with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_df_transform_index(self, daskify):
        frame = copy(df)
        if not daskify:
            frame = frame.compute()
        frame = frame.sample(frac=1.0)
        res_df = dpp.PolynomialFeatures(
            preserve_dataframe=True, degree=1
        ).fit_transform(frame)
        assert_eq_df(res_df.iloc[:, 1:], frame, check_dtype=False) 
Example #20
Source File: test_data.py    From dask-ml with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_transformer_params(self):
        pf = dpp.PolynomialFeatures(degree=3, interaction_only=True, include_bias=False)
        pf.fit(X)
        assert pf._transformer.degree == pf.degree
        assert pf._transformer.interaction_only is pf.interaction_only
        assert pf._transformer.include_bias is pf.include_bias 
Example #21
Source File: poiRegression.py    From python-urbanPlanning with MIT License 5 votes vote down vote up
def polynomialReg(X_pReg,y_pReg,predFeat=False):    
    X=X_pReg
    y=y_pReg
    lr=LinearRegression()
    pr=LinearRegression()
    quadratic=PolynomialFeatures(degree=2) #加入多项式项,degree为多项式的次数
    X_quad=quadratic.fit_transform(X)
    
    lr.fit(X,y)
    X_fit=np.arange(0,8,1)[:,np.newaxis]
    y_lin_fit=lr.predict(X_fit)
    
    pr.fit(X_quad,y)
    y_quad_fit=pr.predict(quadratic.fit_transform(X_fit))    
#    print(X_fit,y_quad_fit)
    
    plt.scatter(X,y,label='training points')
    plt.plot(X_fit,y_lin_fit,label='linear fit',linestyle='--')
    plt.plot(X_fit,y_quad_fit,label='quadratic fit')
    plt.legend(loc='upper left')
    plt.show()
    
    print(pr.coef_) #根据degree参数设置数,返回的coef_系数数量不同.
    
    if type(predFeat).__module__=='numpy':
        return pr.predict(quadratic.fit_transform(predFeat)) 
Example #22
Source File: test_data.py    From dask-ml with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_array_transform(self):
        a = dpp.PolynomialFeatures()
        b = spp.PolynomialFeatures()

        res_a = a.fit_transform(X)
        res_b = b.fit_transform(X.compute())
        assert_estimator_equal(a, b)
        assert dask.is_dask_collection(res_a)
        assert_eq_ar(res_a, res_b) 
Example #23
Source File: preprocess.py    From KDDCup2019_admin with MIT License 5 votes vote down vote up
def polyfeatures(X):
    poly = PolynomialFeatures(degree=2, include_bias=False, interaction_only=False)
    X_poly = poly.fit_transform(X)
    X = pd.DataFrame(X_poly, columns=poly.get_feature_names())
    return X 
Example #24
Source File: optimizerlib.py    From nevergrad with MIT License 5 votes vote down vote up
def learn_on_k_best(archive: utils.Archive[utils.MultiValue], k: int) -> ArrayLike:
    """Approximate optimum learnt from the k best.

    Parameters
    ----------
    archive: utils.Archive[utils.Value]
    """
    items = list(archive.items_as_arrays())
    dimension = len(items[0][0])

    # Select the k best.
    first_k_individuals = [x for x in sorted(items, key=lambda indiv: archive[indiv[0]].get_estimation("pessimistic"))[:k]]
    assert len(first_k_individuals) == k

    # Recenter the best.
    middle = np.array(sum(p[0] for p in first_k_individuals) / k)
    normalization = 1e-15 + np.sqrt(np.sum((first_k_individuals[-1][0] - first_k_individuals[0][0])**2))
    y = [archive[c[0]].get_estimation("pessimistic") for c in first_k_individuals]
    X = np.asarray([(c[0] - middle) / normalization for c in first_k_individuals])

    # We need SKLearn.
    from sklearn.linear_model import LinearRegression
    from sklearn.preprocessing import PolynomialFeatures
    polynomial_features = PolynomialFeatures(degree=2)
    X2 = polynomial_features.fit_transform(X)

    # Fit a linear model.
    model = LinearRegression()
    model.fit(X2, y)

    # Find the minimum of the quadratic model.
    optimizer = OnePlusOne(parametrization=dimension, budget=dimension * dimension + dimension + 500)
    try:
        optimizer.minimize(lambda x: float(model.predict(polynomial_features.fit_transform(np.asarray([x])))))
    except ValueError:
        raise InfiniteMetaModelOptimum("Infinite meta-model optimum in learn_on_k_best.")

    minimum = optimizer.provide_recommendation().value
    if np.sum(minimum**2) > 1.:
        raise InfiniteMetaModelOptimum("huge meta-model optimum in learn_on_k_best.")
    return middle + normalization * minimum 
Example #25
Source File: main.py    From Harmonica with MIT License 5 votes vote down vote up
def get_features(x,degree):
        print("Extending feature vectors with degree "+str(degree)+" ..")
        featureExtender = PolynomialFeatures(degree, interaction_only=True)   # This function generates low degree monomials. It's a little bit slow though. You may write your own function using recursion.
        tmp=[]
        for current_x in x:
            tmp.append(featureExtender.fit_transform(np.array(current_x).reshape(1, -1))[0].tolist())   # Extend feature vectors with monomials
        return tmp 
Example #26
Source File: skl_utils.py    From kaggle-HomeDepot with MIT License 5 votes vote down vote up
def fit(self, X, y):
        sdim, fdim = X.shape
        for i in range(self.n_estimators):
            ridge = Ridge(alpha=self.alpha, normalize=self.normalize, random_state=self.random_state)
            fidx = self._random_feature_idx(fdim, self.random_state+i*100)
            sidx = self._random_sample_idx(sdim, self.random_state+i*10)
            X_tmp = X[sidx][:,fidx]
            if self.poly:
                X_tmp = PolynomialFeatures(degree=2).fit_transform(X_tmp)[:,1:]
            ridge.fit(X_tmp, y[sidx])
            self.ridge_list[i] = ridge
            self.feature_idx_list[i] = fidx
        return self 
Example #27
Source File: skl_utils.py    From kaggle-HomeDepot with MIT License 5 votes vote down vote up
def predict(self, X):
        y_pred = np.zeros((X.shape[0], self.n_estimators))
        for i in range(self.n_estimators):
            fidx = self.feature_idx_list[i]
            ridge = self.ridge_list[i]
            X_tmp = X[:,fidx]
            if self.poly:
                X_tmp = PolynomialFeatures(degree=2).fit_transform(X_tmp)[:,1:]
            y_pred[:,i] = ridge.predict(X_tmp)
        y_pred = np.mean(y_pred, axis=1)
        return y_pred 
Example #28
Source File: measures.py    From nolds with MIT License 5 votes vote down vote up
def poly_fit(x, y, degree, fit="RANSAC"):
  # check if we can use RANSAC
  if fit == "RANSAC":
    try:
      # ignore ImportWarnings in sklearn
      with warnings.catch_warnings():
        warnings.simplefilter("ignore", ImportWarning)
        import sklearn.linear_model as sklin
        import sklearn.preprocessing as skpre
    except ImportError:
      warnings.warn(
        "fitting mode 'RANSAC' requires the package sklearn, using"
        + " 'poly' instead",
        RuntimeWarning)
      fit = "poly"

  if fit == "poly":
    return np.polyfit(x, y, degree)
  elif fit == "RANSAC":
    model = sklin.RANSACRegressor(sklin.LinearRegression(fit_intercept=False))
    xdat = np.asarray(x)
    if len(xdat.shape) == 1:
      # interpret 1d-array as list of len(x) samples instead of
      # one sample of length len(x)
      xdat = xdat.reshape(-1, 1)
    polydat = skpre.PolynomialFeatures(degree).fit_transform(xdat)
    try:
      model.fit(polydat, y)
      coef = model.estimator_.coef_[::-1]
    except ValueError:
      warnings.warn(
        "RANSAC did not reach consensus, "
        + "using numpy's polyfit",
        RuntimeWarning)
      coef = np.polyfit(x, y, degree)
    return coef
  else:
    raise ValueError("invalid fitting mode ({})".format(fit)) 
Example #29
Source File: test_partial_dependence.py    From Mastering-Elasticsearch-7.0 with MIT License 5 votes vote down vote up
def test_partial_dependence_easy_target(est, power):
    # If the target y only depends on one feature in an obvious way (linear or
    # quadratic) then the partial dependence for that feature should reflect
    # it.
    # We here fit a linear regression_data model (with polynomial features if
    # needed) and compute r_squared to check that the partial dependence
    # correctly reflects the target.

    rng = np.random.RandomState(0)
    n_samples = 100
    target_variable = 2
    X = rng.normal(size=(n_samples, 5))
    y = X[:, target_variable]**power

    est.fit(X, y)

    averaged_predictions, values = partial_dependence(
        est, features=[target_variable], X=X, grid_resolution=1000)

    new_X = values[0].reshape(-1, 1)
    new_y = averaged_predictions[0]
    # add polynomial features if needed
    new_X = PolynomialFeatures(degree=power).fit_transform(new_X)

    lr = LinearRegression().fit(new_X, new_y)
    r2 = r2_score(new_y, lr.predict(new_X))

    assert r2 > .99 
Example #30
Source File: model.py    From WechatJump with MIT License 5 votes vote down vote up
def train_polynomial_regression_model(self, degree):
        """训练多项式回归模型"""
        poly_feat = PolynomialFeatures(degree=degree)
        x_tranformed = poly_feat.fit_transform(self.dataset_X)
        linear = LinearRegression()
        linear.fit(x_tranformed, self.dataset_Y)
        self.predict = lambda x: linear.predict(poly_feat.transform(x))[0]