def test_linear_classifier_predict_not_fitted():
    """Test of `LinearClassifier` class with prediction without fit."""
    lc = LinearClassifier(learning_rate=0.1, iterations=1, standardize=False)

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

    with pytest.raises(NotFitted):
        lc.predict(X)
def test_linear_classifier_dataset_inconsistancy():
    """Test of `LinearClassifier` with dataset inconsistancy."""
    lc = LinearClassifier(learning_rate=0.1, iterations=1)

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

    with pytest.raises(InvalidInput):
        lc.fit(X, y)
def test_linear_classifier_score_not_fitted():
    """Test of `LinearClassifier` class with score calculation without fit."""
    lc = LinearClassifier(learning_rate=0.1, iterations=1, standardize=False)

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

    with pytest.raises(NotFitted):
        lc.score(X, y) == 1
def test_linear_classifier_multiclass():
    """Test of `LinearClassifier` with multiclass."""
    lc = LinearClassifier(learning_rate=0.1, iterations=2)

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

    lc.fit(X, y)

    assert lc.score(X, y) == 1
def test_linear_classifier_predict():
    """Test of `LinearClassifier` class with a prediction."""
    lc = LinearClassifier(learning_rate=0.1, iterations=1, standardize=True)

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

    lc.fit(X, y)

    assert np.equal(lc.predict(np.array([0])), np.array([0]))
def test_linear_classifier_standardized():
    """Test of `LinearClassifier` class with standardization."""
    lc = LinearClassifier(learning_rate=0.1, iterations=1, standardize=True)

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

    lc.fit(X, y)

    assert lc.score(X, y) == 1
def test_linear_classifier_history_disabled():
    """Test of `LinearClassifier` when history disabled."""
    lc = LinearClassifier(learning_rate=0.1, iterations=1, history=False)

    assert lc.history is None

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

    lc.fit(X, y)

    assert lc.history is None
def test_linear_classifier_history_enabled():
    """Test of `LinearClassifier` when history enabled."""
    lc = LinearClassifier(learning_rate=0.1, iterations=1, history=True)

    assert lc.history == []

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

    lc.fit(X, y)

    assert lc.history is not None
Exemplo n.º 9
0
# Create the input features and target data frame
df = pd.DataFrame(data=cancer["data"], columns=cancer["feature_names"])
df["target"] = pd.Series(cancer.target)

X = df[["mean radius", "mean texture"]]
X = X.values

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

# Normalize the features
standardize = Standardization()
X = standardize(X)

# Train the model
lc = LinearClassifier(optimizer=SGD(iterations=15, history=True), standardize=False)
lc.fit(X, y)

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

M = X[df["target"] == 0]
B = X[df["target"] == 1]
Exemplo n.º 10
0
if __name__ == "__main__":
    # Load the Boston house prices dataset
    iris = datasets.load_iris()

    # Create the input features and target data frame
    df = pd.DataFrame(iris.data, columns=iris.feature_names)
    df["target"] = pd.Series(iris.target)

    # Select the input features
    X = df[iris.feature_names].values

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

    X_train, y_train, X_test, y_test = split_dataset(X, y)

    # Alchina regression
    print("--- Alchina ---")
    lr_alchina = LinearClassifier(learning_rate=0.05,
                                  iterations=2000,
                                  standardize=True)
    train_and_report(lr_alchina, X_train, y_train, X_test, y_test)

    # Scikit-learn regression
    print("--- Scikit-learn ---")
    lr_skitlearn = LogisticRegression(solver="liblinear", multi_class="ovr")
    train_and_report(lr_skitlearn, X_train, target_reshape(y_train), X_test,
                     target_reshape(y_test))