Machine Learning

Study with Machine Learning

Training and Deploying an Unsupervised Learning Model with K-means and Flask

In this article, we will explore the training of an unsupervised machine learning model using the K-means algorithm and then expose this model through a Flask API. The focus of this article is for an audience that already works with technology but is not an expert in machine learning.

Unsupervised Learning

Unsupervised learning is a set of machine learning techniques that analyze and cluster data without a predefined "answer key." Unsupervised algorithms seek patterns and structures in the data without using prior output labels. The goal is to find natural groupings, relationships, or patterns in the data.

K-means Algorithm

The K-means algorithm is a popular unsupervised learning technique that relies on grouping data into K distinct clusters, where K is a predetermined number of clusters. The algorithm aims to minimize the sum of the distances between data points and cluster centroids by iteratively adjusting the centroid positions until the clusters stabilize.

Iris Dataset

The Iris dataset is a popular dataset consisting of 150 samples of iris flowers from three different species: Iris setosa, Iris versicolor, and Iris virginica. Each sample contains four features (sepal length and width, petal length and width). Our goal is to use the K-means algorithm to cluster the samples into three clusters, corresponding to the three iris species.

Training the K-means Model

Let's start by training our K-means model on the Iris dataset. We will use the scikit-learn library in Python to train the model. First, we load the Iris dataset, create a DataFrame, and train the K-means model with K=3.

# Import necessary libraries
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.cluster import KMeans
from sklearn.datasets import load_iris

# Load the Iris dataset
iris = load_iris()
data = iris.data
target = iris.target

# Create a DataFrame with the data and columns
iris_df = pd.DataFrame(data, columns=iris.feature_names)

# Train the K-means model with K=3
kmeans = KMeans(n_clusters=3, random_state=42)
kmeans.fit(iris_df)

Exporting the Trained Model

To save and load the trained model, we will use the joblib library. First, we save the trained model to a file.

import joblib

# Save the trained model to a file
joblib.dump(kmeans, 'kmeans_model.joblib')

Creating a Flask API

Now that we have trained and saved our model, we can create a Flask API to expose the model and make predictions.

from flask import Flask, request, jsonify
import numpy as np
import joblib

app = Flask(__name__)

# Load the trained model
kmeans = joblib.load('kmeans_model.joblib')

@app.route('/predict', methods=['POST'])
def predict():
    input_data = request.json
    features = [input_data['sepal_length'], input_data['sepal_width'], input_data['petal_length'], input_data['petal_width']]
    features = np.array(features).reshape(1, -1)
    cluster = kmeans.predict(features)
    return jsonify({'cluster': int(cluster[0])})

if __name__ == '__main__':
    app.run(debug=True)

Next, start the Flask server by running the app.py file.

python app.py

Now, the API is available at http://localhost:5000/predict. We can make POST requests to this URL with a JSON containing the Iris feature values (sepal length and width, petal length and width) and get the predicted cluster as a response.

Example POST request using curl on Linux

Here is an example of how to make a POST request to the API using the curl command on Linux:

bashCopy code
curl -X POST -H "Content-Type: application/json" -d '{"sepal_length": 5.1, "sepal_width": 3.5, "petal_length": 1.4, "petal_width": 0.2}' http://localhost:5000/predict

This command sends a POST request to the API with the Content-Type: application/json header and the data in JSON format.

Conclusion

In this article, we explored the training and deployment of an unsupervised learning model using the K-means algorithm and the Iris dataset. We demonstrated how to train the model, export it, and create a Flask API to expose the model and make predictions.


April 02, 2023

Background image credits forSigmund

Copyright © Gatsby-starter-grayscale 2019