Exemple #1
0
def test_linear_regressor_predict_not_fitted():
    """Test of `LinearRegressor` class with prediction without fit."""
    lr = LinearRegressor(learning_rate=0.1, iterations=1, standardize=False)

    X = np.array([1])

    with pytest.raises(NotFitted):
        lr.predict(X)
Exemple #2
0
def test_linear_regressor_score_not_fitted():
    """Test of `LinearRegressor` class with score calculation without fit."""
    lr = LinearRegressor(learning_rate=0.1, iterations=1, standardize=False)

    X = np.array([1])
    y = np.array([1])

    with pytest.raises(NotFitted):
        lr.score(X, y)
Exemple #3
0
def test_linear_regressor_dataset_inconsistancy():
    """Test of `LinearRegressor` with dataset inconsistancy."""
    lr = LinearRegressor(learning_rate=0.1, iterations=1)

    X = np.array([[1], [1]])
    y = np.array([[1]])

    with pytest.raises(InvalidInput):
        lr.fit(X, y)
Exemple #4
0
def test_linear_regressor_normal():
    """Test of `normal` method of `LinearRegressor` class."""
    lr = LinearRegressor(learning_rate=0.1, iterations=1, standardize=False)

    X = np.array([1])
    y = np.array([1])

    lr.normal(X, y)

    assert lr.score(X, y) == 1
Exemple #5
0
def test_linear_regressor_standardized():
    """Test of `LinearRegressor` class with standardization."""
    lr = LinearRegressor(learning_rate=0.1, iterations=1, standardize=True)

    X = np.array([1])
    y = np.array([1])

    lr.fit(X, y)

    assert lr.score(X, y) == 1
Exemple #6
0
def test_linear_regressor_history_disabled():
    """Test of `LinearRegressor` when history disabled."""
    lr = LinearRegressor(learning_rate=0.1, iterations=1, history=False)

    assert lr.history is None

    X = np.array([1])
    y = np.array([1])

    lr.fit(X, y)

    assert lr.history is None
Exemple #7
0
def test_linear_regressor_history_enabled():
    """Test of `LinearRegressor` when history enabled."""
    lr = LinearRegressor(learning_rate=0.1, iterations=1, history=True)

    assert lr.history == []

    X = np.array([1])
    y = np.array([1])

    lr.fit(X, y)

    assert lr.history is not None
Exemple #8
0
# Create the input features and target data frame
df = pd.DataFrame(houses.data, columns=houses.feature_names)
df["target"] = pd.Series(houses.target)

# Select the input feature
X = df["RM"]
X = X.values.reshape(X.shape[0], 1)

# Select the target
y = df["target"]
y = y.values.reshape(y.shape[0], 1)

# Train the model
lr = LinearRegressor(optimizer=MBGD(learning_rate=0.05,
                                    iterations=70,
                                    history=True),
                     standardize=True)
lr.fit(X, y)

# Plot the results
figure, (a0, a1) = plt.subplots(1,
                                2,
                                num="Linear Regression",
                                figsize=(16, 9),
                                gridspec_kw={"width_ratios": [3, 1]})
figure.suptitle("Linear Regression on Boston house prices dataset",
                fontsize=16)

a0.plot(X, y, "b.")
a0.plot(
    range(3, 10),