You can download this code by clicking the button below.
This code is now available for download.
This function uses the LogisticRegression model from the scikit-learn library to train a dataset and evaluate its accuracy on a test set.
Technology Stack : scikit-learn, LogisticRegression, train_test_split, accuracy_score
Code Type : Machine learning
Code Difficulty : Intermediate
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score
def random_logistic_regression(X, y, test_size=0.2):
"""
Trains a logistic regression model and evaluates its accuracy on a test set.
"""
# Split the data into training and testing sets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=test_size, random_state=42)
# Initialize the logistic regression model
model = LogisticRegression()
# Train the model on the training data
model.fit(X_train, y_train)
# Make predictions on the test data
y_pred = model.predict(X_test)
# Calculate the accuracy of the model
accuracy = accuracy_score(y_test, y_pred)
return accuracy