Simple Logistic Regression Example

Binary Classification?, Keras functions: Sequential, Dense, Layer, compile(), fit(), evaluate(), predict()


import numpy as np
import keras

X = np.array([    # Features
        #x1    x2
        [0.0, 0.0],
    [0.0, 1.0],
    [1.0, 0.0],
    [1.0, 1.0],
    [0.2, 0.3],
    [0.8, 0.9],
    [0.1, 0.2],
    [0.9, 0.7],
])
y = np.array([    # label(y)
    0,
    0,
    0,
    1,
    0,
    1,
    0,
    1,
])
model = keras.Sequential([
    keras.Input(shape=(2,)),              # Two input features(x)
    keras.layers.Dense(8, activation="relu"),
    keras.layers.Dense(1, activation="sigmoid"),  # Layer giving Logisitic regression(ie Sigmoid Function)
])
# Make model ready for training
model.compile(
    optimizer="adam",
    loss="binary_crossentropy",
    metrics=["accuracy"],
)
model.fit(
    X,
    y,
    epochs=50,
    batch_size=2,
    verbose=0,
)
# Evaluate the model on the training data (or test data)
loss, accuracy = model.evaluate(X, y, verbose=0)
print(f"Loss: {loss:.4f}")                #0.6871
print(f"Accuracy: {accuracy * 100:.2f}%") #50.00%

# New unseen data points
X_new = np.array([
    [0.0, 0.1],  # Looks like class 0
    [0.9, 0.9],  # Looks like class 1
])

# Get raw probabilities
predictions = model.predict(X_new, verbose=0)
print("Predictions:", predictions)    #[[0.49071455][0.46283707]]