Apply machine learning using Machine learning with usage of Logistic Regression algorithm
- codeagle
- Aug 18, 2024
- 2 min read
Machine Learning can be used to predict various different types of outcomes. Applying logistic regression in TensorFlow involves creating a model that maps input features to binary outcomes (0 or 1) using a sigmoid activation function. Here’s a step-by-step guide on how to implement logistic regression in TensorFlow:

Step 1: Import Required Libraries
import tensorflow as tf
import numpy as np
from sklearn.model_selection import train_test_split
from sklearn.datasets import make_classification
from sklearn.preprocessing import StandardScaler
Step 2: Generate or Load the Dataset
For this example, we'll generate a synthetic dataset using make_classification from sklearn:
# Create a synthetic dataset
X, y = make_classification(n_samples=1000, n_features=10, n_classes=2, n_informative=5, random_state=42)
# Split the data into training and testing sets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
# Normalize the features
scaler = StandardScaler()
X_train = scaler.fit_transform(X_train)
X_test = scaler.transform(X_test)
Step 3: Define the Logistic Regression Model
# Define the logistic regression model using tf.keras
model = tf.keras.Sequential([
tf.keras.layers.Dense(1, input_shape=(X_train.shape[1],), activation='sigmoid')
])
# Compile the model
model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])
Step 4: Train the Model
# Train the model
history = model.fit(X_train, y_train, epochs=100, batch_size=32, validation_data=(X_test, y_test))
Step 5: Evaluate the Model
After training, you can evaluate the model's performance on the test data:
# Evaluate the model on the test data
loss, accuracy = model.evaluate(X_test, y_test)
print(f'Test Accuracy: {accuracy:.4f}')
Step 6: Make Predictions
You can use the trained model to make predictions on new data:
# Make predictions on the test data
predictions = model.predict(X_test)
predictions = (predictions > 0.5).astype(int) # Convert probabilities to binary output
Complete Example Code
Here's the entire code combined:
import tensorflow as tf
import numpy as np
from sklearn.model_selection import train_test_split
from sklearn.datasets import make_classification
from sklearn.preprocessing import StandardScaler
# Generate a synthetic dataset
X, y = make_classification(n_samples=1000, n_features=10, n_classes=2, n_informative=5, random_state=42)
# Split the data into training and testing sets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
# Normalize the features
scaler = StandardScaler()
X_train = scaler.fit_transform(X_train)
X_test = scaler.transform(X_test)
# Define the logistic regression model using tf.keras
model = tf.keras.Sequential([
tf.keras.layers.Dense(1, input_shape=(X_train.shape[1],), activation='sigmoid')
])
# Compile the model
model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])
# Train the model
history = model.fit(X_train, y_train, epochs=100, batch_size=32, validation_data=(X_test, y_test))
# Evaluate the model on the test data
loss, accuracy = model.evaluate(X_test, y_test)
print(f'Test Accuracy: {accuracy:.4f}')
# Make predictions on the test data
predictions = model.predict(X_test)
predictions = (predictions > 0.5).astype(int) # Convert probabilities to binary output
Explanation:
Model Definition: The model consists of a single Dense layer with one output unit and a sigmoid activation function, making it suitable for binary classification.
Loss Function: binary_crossentropy is used as the loss function for logistic regression.
Optimizer: adam is a popular choice due to its efficiency and ease of use.
Training: The model is trained over 100 epochs with a batch size of 32. The validation data is used to monitor the model's performance during training.
Evaluation: After training, the model's accuracy on the test set is calculated, and predictions are made on the test data.
This is a basic implementation of logistic regression using TensorFlow. You can customize the model, optimizer, or other aspects according to your needs.