Пример #1
0
    def test_fwls_regressor(self):
        feature_func = lambda x: np.ones(x.shape)
        bclf = LinearRegression()
        clfs = [
            RandomForestRegressor(n_estimators=50, random_state=1),
            GradientBoostingRegressor(n_estimators=25, random_state=1),
            Ridge(random_state=1)
        ]

        # Friedman1
        X, y = datasets.make_friedman1(n_samples=1200,
                                       random_state=1,
                                       noise=1.0)
        X_train, y_train = X[:200], y[:200]
        X_test, y_test = X[200:], y[200:]

        sr = FWLSRegressor(bclf,
                           clfs,
                           feature_func,
                           n_folds=3,
                           verbose=0,
                           oob_score_flag=True)
        sr.fit(X_train, y_train)
        mse = mean_squared_error(y_test, sr.predict(X_test))
        assert_less(mse, 6.0)
Пример #2
0
    def test_fwls_regressor(self):
        feature_func = lambda x: np.ones(x.shape)
        bclf = LinearRegression()
        clfs = [RandomForestRegressor(n_estimators=50, random_state=1),
                GradientBoostingRegressor(n_estimators=25, random_state=1),
                Ridge(random_state=1)]

        # Friedman1
        X, y = datasets.make_friedman1(n_samples=1200,
                                       random_state=1,
                                       noise=1.0)
        X_train, y_train = X[:200], y[:200]
        X_test, y_test = X[200:], y[200:]

        sr = FWLSRegressor(bclf,
                              clfs,
                              feature_func,
                              n_folds=3,
                              verbose=0,
                              oob_score_flag=True)
        sr.fit(X_train, y_train)
        mse = mean_squared_error(y_test, sr.predict(X_test))
        assert_less(mse, 6.0)
Пример #3
0
boston = datasets.load_boston()
X = preprocessing.StandardScaler().fit_transform(boston.data)
Y = boston.target

X_train = X[:200]
Y_train = Y[:200]
X_test = X[200:]
Y_test = Y[200:]

breg = LinearRegression()
regs = [RandomForestRegressor(n_estimators=50, random_state=1),
        GradientBoostingRegressor(n_estimators=25, random_state=1),
        Ridge(),
        ExtraTreesRegressor(n_estimators=50),
        ]
feature_func = lambda x: np.c_[np.ones((x.shape[0], 1)),
                               x[:, 1].reshape((x.shape[0], 1)),
                               x[:, 6].reshape((x.shape[0], 1)),]

sr = FWLSRegressor(breg,
                   regs,
                   feature_func,
                   n_folds=3,
                   verbose=0,
                   oob_score_flag=False)

sr.fit(X_train, Y_train)
score = metrics.mean_squared_error(sr.predict(X_test), Y_test)
print ("MSE of stacked regressor: %f" % score)
boston = datasets.load_boston()
X = preprocessing.StandardScaler().fit_transform(boston.data)
Y = boston.target

X_train = X[:200]
Y_train = Y[:200]
X_test = X[200:]
Y_test = Y[200:]

breg = LinearRegression()
regs = [RandomForestRegressor(n_estimators=50, random_state=1),
        GradientBoostingRegressor(n_estimators=25, random_state=1),
        Ridge(),
        ExtraTreesRegressor(n_estimators=50),
        ]
feature_func = lambda x: np.c_[np.ones((x.shape[0], 1)),
                               x[:, 1].reshape((x.shape[0], 1)),
                               x[:, 6].reshape((x.shape[0], 1)),]

sr = FWLSRegressor(breg,
                   regs,
                   feature_func,
                   n_folds=3,
                   verbose=0,
                   oob_score_flag=False)

sr.fit(X_train, Y_train)
score = metrics.mean_squared_error(sr.predict(X_test), Y_test)
print ("MSE of stacked regressor: %f" % score)