Example #1
0
def test_mse_tree():

    X, y = boston_housing_data()
    X_train, X_test, y_train, y_test = train_test_split(X,
                                                        y,
                                                        test_size=0.3,
                                                        random_state=123,
                                                        shuffle=True)

    tree = DecisionTreeRegressor(random_state=123)
    avg_expected_loss, avg_bias, avg_var = bias_variance_decomp(
        tree, X_train, y_train, X_test, y_test, loss='mse', random_seed=123)

    assert round(avg_expected_loss, 3) == 31.536
    assert round(avg_bias, 3) == 14.096
    assert round(avg_var, 3) == 17.440
def test_mse_tree():

    X, y = boston_housing_data()
    X_train, X_test, y_train, y_test = train_test_split(X, y,
                                                        test_size=0.3,
                                                        random_state=123,
                                                        shuffle=True)

    tree = DecisionTreeRegressor(random_state=123)
    avg_expected_loss, avg_bias, avg_var = bias_variance_decomp(
            tree, X_train, y_train, X_test, y_test,
            loss='mse',
            random_seed=123)

    assert round(avg_expected_loss, 3) == 31.917
    assert round(avg_bias, 3) == 13.814
    assert round(avg_var, 3) == 18.102
Example #3
0
def test_mse_bagging():

    X, y = boston_housing_data()
    X_train, X_test, y_train, y_test = train_test_split(X,
                                                        y,
                                                        test_size=0.3,
                                                        random_state=123,
                                                        shuffle=True)

    tree = DecisionTreeRegressor(random_state=123)
    bag = BaggingRegressor(base_estimator=tree,
                           n_estimators=10,
                           random_state=123)

    avg_expected_loss, avg_bias, avg_var = bias_variance_decomp(
        bag, X_train, y_train, X_test, y_test, loss='mse', random_seed=123)

    assert round(avg_expected_loss, 2) == 20.24, avg_expected_loss
    assert round(avg_bias, 2) == 15.63, avg_bias
    assert round(avg_var, 2) == 4.61, avg_var
Example #4
0
def test_keras():

    import tensorflow as tf
    X, y = boston_housing_data()
    X_train, X_test, y_train, y_test = train_test_split(X,
                                                        y,
                                                        test_size=0.3,
                                                        random_state=123,
                                                        shuffle=True)

    model = tf.keras.Sequential([
        tf.keras.layers.Dense(32, activation=tf.nn.relu),
        tf.keras.layers.Dense(1)
    ])

    optimizer = tf.keras.optimizers.Adam()
    model.compile(loss='mean_squared_error', optimizer=optimizer)

    model.fit(X_train, y_train, epochs=10)
    model.predict(X_test)
def test_mse_bagging():

    X, y = boston_housing_data()
    X_train, X_test, y_train, y_test = train_test_split(X, y,
                                                        test_size=0.3,
                                                        random_state=123,
                                                        shuffle=True)

    tree = DecisionTreeRegressor(random_state=123)
    bag = BaggingRegressor(base_estimator=tree,
                           n_estimators=100,
                           random_state=123)

    avg_expected_loss, avg_bias, avg_var = bias_variance_decomp(
            bag, X_train, y_train, X_test, y_test,
            loss='mse',
            random_seed=123)

    assert round(avg_expected_loss, 3) == 18.593
    assert round(avg_bias, 3) == 15.354
    assert round(avg_var, 3) == 3.239
Example #6
0
def test_regressor():
    X, y = boston_housing_data()
    reg1 = Lasso(random_state=1)
    reg2 = Ridge(random_state=1)

    X_train, X_test, y_train, y_test = \
        train_test_split(X, y, test_size=0.25,
                         random_state=123)

    score1 = reg1.fit(X_train, y_train).score(X_test, y_test)
    score2 = reg2.fit(X_train, y_train).score(X_test, y_test)

    assert round(score1, 2) == 0.66, score1
    assert round(score2, 2) == 0.68, score2

    t, p = paired_ttest_kfold_cv(estimator1=reg1,
                                 estimator2=reg2,
                                 X=X, y=y,
                                 random_seed=1)

    assert round(t, 3) == -0.549, t
    assert round(p, 3) == 0.596, p
def test_regressor():
    X, y = boston_housing_data()
    reg1 = Lasso(random_state=1)
    reg2 = Ridge(random_state=1)

    X_train, X_test, y_train, y_test = \
        train_test_split(X, y, test_size=0.25,
                         random_state=123)

    score1 = reg1.fit(X_train, y_train).score(X_test, y_test)
    score2 = reg2.fit(X_train, y_train).score(X_test, y_test)

    assert round(score1, 2) == 0.66, score1
    assert round(score2, 2) == 0.68, score2

    t, p = paired_ttest_5x2cv(estimator1=reg1,
                              estimator2=reg2,
                              X=X, y=y,
                              random_seed=1)

    assert round(t, 3) == -0.599, t
    assert round(p, 3) == 0.575, p
def test_regressor():
    X, y = boston_housing_data()
    reg1 = Lasso(random_state=1)
    reg2 = Ridge(random_state=1)

    X_train, X_test, y_train, y_test = \
        train_test_split(X, y, test_size=0.25,
                         random_state=123)

    score1 = reg1.fit(X_train, y_train).score(X_test, y_test)
    score2 = reg2.fit(X_train, y_train).score(X_test, y_test)

    assert round(score1, 2) == 0.66, score1
    assert round(score2, 2) == 0.68, score2

    f, p = combined_ftest_5x2cv(estimator1=reg1,
                                estimator2=reg2,
                                X=X, y=y,
                                random_seed=1)

    assert round(f, 3) == 3.211, f
    assert round(p, 3) == 0.105, p
def test_regressor():
    X, y = boston_housing_data()
    reg1 = Lasso(random_state=1)
    reg2 = Ridge(random_state=1)

    X_train, X_test, y_train, y_test = \
        train_test_split(X, y, test_size=0.25,
                         random_state=123)

    score1 = reg1.fit(X_train, y_train).score(X_test, y_test)
    score2 = reg2.fit(X_train, y_train).score(X_test, y_test)

    assert round(score1, 2) == 0.66, score1
    assert round(score2, 2) == 0.68, score2

    f, p = combined_ftest_5x2cv(estimator1=reg1,
                                estimator2=reg2,
                                X=X,
                                y=y,
                                random_seed=1)

    assert round(f, 3) == 3.211, f
    assert round(p, 3) == 0.105, p
Example #10
0
from mlxtend.regressor import StackingCVRegressor
from mlxtend.data import boston_housing_data
from sklearn.linear_model import LinearRegression, Ridge, Lasso
from sklearn.svm import SVR
from sklearn.model_selection import train_test_split, GridSearchCV, cross_val_score
from sklearn.metrics import mean_squared_error
import numpy as np
import matplotlib.pyplot as plt

x, y = boston_housing_data()
x = x[:100]
y = y[:100]
# 划分数据集
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.2)
# 初始化基模型
lr = LinearRegression()
svr_lin = SVR(kernel='linear', gamma='auto')
ridge = Ridge(random_state=2019, )
lasso = Lasso()
models = [lr, svr_lin, ridge, lasso]

print("base model")
for model in models:
    score = cross_val_score(model, x_train, y_train, cv=5)
    print(score.mean(), "+/-", score.std())
sclf = StackingCVRegressor(regressors=models, meta_regressor=lasso)
# 训练回归器
print("stacking model")
score = cross_val_score(sclf, x_train, y_train, cv=5)
print(score.mean(), "+/-", score.std())
Example #11
0
def test_import_boston_housing():
    X, y = boston_housing_data()
    assert (X.shape[0] == 506)
    assert (X.shape[1] == 13)
    assert (y.shape[0] == 506)
Example #12
0
def test_import_boston_housing():
    X, y = boston_housing_data()
    assert(X.shape[0] == 506)
    assert(X.shape[1] == 13)
    assert(y.shape[0] == 506)
# Sebastian Raschka 2014-2018
# mlxtend Machine Learning Library Extensions
# Author: Sebastian Raschka <sebastianraschka.com>
#
# License: BSD 3 clause


from mlxtend.regressor import LinearRegression
from mlxtend.data import boston_housing_data
import numpy as np
from numpy.testing import assert_almost_equal
from sklearn.base import clone

X, y = boston_housing_data()
X_rm = X[:, 5][:, np.newaxis]
X_rm_lstat = X[:, [5, -1]]

# standardized variables
X_rm_std = (X_rm - X_rm.mean(axis=0)) / X_rm.std(axis=0)
X_rm_lstat_std = ((X_rm_lstat - X_rm_lstat.mean(axis=0)) /
                  X_rm_lstat.std(axis=0))
y_std = (y - y.mean()) / y.std()


def test_univariate_normal_equation():
    w_exp = np.array([[9.1]])
    b_exp = np.array([-34.7])
    ne_lr = LinearRegression(minibatches=None)
    ne_lr.fit(X_rm, y)
    assert_almost_equal(ne_lr.w_, w_exp, decimal=1)
    assert_almost_equal(ne_lr.b_, b_exp, decimal=1)