Logistic Regression Model Accuracy on Iris Dataset

  • Share this:

Code introduction


This code defines a function named `generate_random_model` that trains a logistic regression model on the Iris dataset and returns the model's accuracy.


Technology Stack : The code uses the following packages and technologies: Prefect, scikit-learn, and the Iris dataset.

Code Type : Function

Code Difficulty :


                
                    
import random
from prefect import task
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import train_test_split
from sklearn.datasets import load_iris

def generate_random_model():
    # Load iris dataset
    iris = load_iris()
    X, y = iris.data, iris.target
    
    # Split the dataset 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)
    
    # Create a Logistic Regression model
    model = LogisticRegression(max_iter=200)
    
    # Train the model
    model.fit(X_train, y_train)
    
    # Predict the labels for the test set
    predictions = model.predict(X_test)
    
    # Evaluate the model
    accuracy = model.score(X_test, y_test)
    
    return accuracy