Lädt...

🔧 🧠 Dockerize and Share Your ML Model: Logistic Regression with Iris Dataset


Nachrichtenbereich: 🔧 Programmierung
🔗 Quelle: dev.to

📦 Project Structure
Here’s what our folder looks like:

ml-docker-project/
├── Dockerfile
├── model.py
└── Iris.csv
🐳 Dockerfile Breakdown
Here’s the Dockerfile we’re using:

Use the official Python image as a base

FROM python:3.12-slim

Set working directory inside the container

WORKDIR /app

Copy all files from the local machine to the container

COPY . .

Install required Python packages

RUN pip install --no-cache-dir pandas scikit-learn matplotlib

Command to run your Python script

CMD ["python", "model.py"]

This will:

Use a slim Python base image
Copy your local files
Install all dependencies
Run model.py on container start
🧪 What model.py Does

The script:

from pandas import read_csv
from matplotlib import pyplot
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import train_test_split
import joblib

Step 1: Load dataset

filename = "Iris.csv"
data = read_csv(filename)

Step 2: Display data shape and preview

print("Shape of the dataset:", data.shape)
print("First 20 rows:\n", data.head(20))

Step 3: Plot and save histograms silently

data.hist()
pyplot.savefig("histograms.png")
pyplot.close() # Close the plot so it doesn't show up in prompt

Step 4: Plot and save density plots silently

data.plot(kind='density', subplots=True, layout=(3,3), sharex=False)
pyplot.savefig("density_plots.png")
pyplot.close()

Step 5: Convert to NumPy array and extract features/labels

array = data.values
X = array[:, 1:5] # Features: Sepal/Petal measurements
Y = array[:, 5] # Target: Species

Step 6: Split data into training (67%) and testing (33%)

test_size = 0.33
seed = 7
X_train, X_test, Y_train, Y_test = train_test_split(X, Y, test_size=test_size, random_state=seed)

Step 7: Create and train logistic regression model

model = LogisticRegression(max_iter=200)
model.fit(X_train, Y_train)

Step 8: Evaluate and display accuracy

result = model.score(X_test, Y_test)
print("Accuracy: {:.2f}%".format(result * 100))

Step 9: Save the trained model to a file

joblib.dump(model, "logistic_model.pkl")
Visualizes the data (and saves plots)

Trains a Logistic Regression model

Evaluates it

Saves the model as logistic_model.pkl

It’s a full training pipeline, ready to be reproduced in any environment that runs Docker. 💪

🔨 Step-by-Step: Build, Tag, and Push to DockerHub

1️⃣ Build the Docker Image
From your project directory:
docker build -t viratpk18/24mcr078:latest .

2️⃣ Log in to DockerHub
docker login

3️⃣ Push the Image
docker push viratpk18/24mcr078:latest

4️⃣ Then create the Dockerfile
Image description

📚 Resources

🐳 Docker for ML Projects
https://docs.docker.com/language/python/

🤖 scikit-learn Docs
https://scikit-learn.org/stable/

📊 matplotlib Gallery
https://matplotlib.org/stable/gallery/index.html

...

🔧 🧠 Dockerize and Share Your ML Model: Logistic Regression with Iris Dataset


📈 98.81 Punkte
🔧 Programmierung

🔧 🧠 Dockerize and Share Your ML Model: Logistic Regression with Iris Dataset


📈 98.81 Punkte
🔧 Programmierung

🔧 Machine Learning with Iris Dataset – Logistic Regression Model


📈 66.49 Punkte
🔧 Programmierung

🔧 Linear vs Logistic Regression: How to Choose the Right Regression Model for Your Data


📈 54.87 Punkte
🔧 Programmierung

📰 Turn Linear Regression into Logistic Regression


📈 45.85 Punkte
🔧 AI Nachrichten

🔧 สร้าง Logistic Regression Model อย่างง่ายโดยใช้ Python


📈 39.45 Punkte
🔧 Programmierung

🔧 How to Build a Logistic Regression Model: A Spam-filter Tutorial


📈 39.45 Punkte
🔧 Programmierung

📰 Create Powerful Model Explanations for Classification Problems with Logistic Regression


📈 39.45 Punkte
🔧 AI Nachrichten

🎥 Machine Learning and Logistic Regression


📈 34.66 Punkte
🎥 IT Security Video

🎥 Logistic Regression in ML Explained


📈 33.3 Punkte
🎥 IT Security Video

📰 One-vs-All Logistic Regression for Image Recognition in Python


📈 33.3 Punkte
🔧 AI Nachrichten

🔧 📊 Logistic Regression in a Nutshell


📈 33.3 Punkte
🔧 Programmierung

🔧 Performance Optimization in Logistic Regression Using Parameter Tuning


📈 33.3 Punkte
🔧 Programmierung

📰 Logistic Regression Models Comparison


📈 33.3 Punkte
🔧 AI Nachrichten

📰 Bayesian Logistic Regression in Python


📈 33.3 Punkte
🔧 AI Nachrichten

📰 Unlocking Insights: Building a Scorecard with Logistic Regression


📈 33.3 Punkte
🔧 AI Nachrichten

📰 Beyond Binary Classification — Breaking down Multiple Logistic Regression to its basics


📈 33.3 Punkte
🔧 AI Nachrichten

📰 Binary Logistic Regression in R


📈 33.3 Punkte
🔧 AI Nachrichten

🔧 Ann vs. Logistic Regression: When to Choose What


📈 33.3 Punkte
🔧 Programmierung

📰 Breaking down Logistic Regression to its basics


📈 33.3 Punkte
🔧 AI Nachrichten

🔧 🔍 Understanding Logistic Regression for Classification


📈 33.3 Punkte
🔧 Programmierung

📰 Logistic Regression in OpenCV


📈 33.3 Punkte
🔧 AI Nachrichten

📰 What is Logistic Regression? A Comprehensive Guide


📈 33.3 Punkte
📰 IT Nachrichten

📰 Logistic Regression for Image Classification Using OpenCV


📈 33.3 Punkte
🔧 AI Nachrichten

🔧 Logistic Regression


📈 33.3 Punkte
🔧 Programmierung

📰 Best Practices for Debugging Errors in Logistic Regression with Python


📈 33.3 Punkte
🔧 AI Nachrichten

📰 A Deeper Dive into Odds Ratios Using Logistic Regression


📈 33.3 Punkte
🔧 AI Nachrichten

📰 Introduction to Logistic Regression in PySpark


📈 33.3 Punkte
🔧 AI Nachrichten

🔧 Logistic Regression Unlocks Small LLMs as Explainable Tens-of-Shot Text Classifiers


📈 33.3 Punkte
🔧 Programmierung

📰 Training Logistic Regression with Cross-Entropy Loss in PyTorch


📈 33.3 Punkte
🔧 AI Nachrichten

📰 Logistic Regression, Explained: A Visual Guide with Code Examples for Beginners


📈 33.3 Punkte
🔧 AI Nachrichten