|
|
|
"""Iris_Flower_Classifier.ipynb |
|
|
|
Automatically generated by Colab. |
|
|
|
Original file is located at |
|
https://colab.research.google.com/drive/1ddsKnOLQk_nPeF9zu0Qr9yTsvmg-0D8S |
|
""" |
|
|
|
import gradio as gr |
|
import numpy as np |
|
from sklearn.datasets import load_iris |
|
from sklearn.model_selection import train_test_split |
|
from sklearn.preprocessing import StandardScaler |
|
from sklearn.ensemble import RandomForestClassifier |
|
from sklearn.pipeline import make_pipeline |
|
|
|
|
|
iris = load_iris() |
|
X = iris.data |
|
y = iris.target |
|
feature_names = iris.feature_names |
|
target_names = iris.target_names |
|
|
|
|
|
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42) |
|
|
|
|
|
model = make_pipeline(StandardScaler(), RandomForestClassifier()) |
|
model.fit(X_train, y_train) |
|
|
|
|
|
def predict_iris(sepal_length, sepal_width, petal_length, petal_width): |
|
feature_values = np.array([sepal_length, sepal_width, petal_length, petal_width]).reshape(1, -1) |
|
prediction = model.predict(feature_values) |
|
return target_names[prediction[0]] |
|
|
|
|
|
interface = gr.Interface( |
|
fn=predict_iris, |
|
inputs=[ |
|
gr.Slider(minimum=float(X[:, 0].min()), maximum=float(X[:, 0].max()), value=float(np.mean(X[:, 0])), label="Sepal Length (cm)"), |
|
gr.Slider(minimum=float(X[:, 1].min()), maximum=float(X[:, 1].max()), value=float(np.mean(X[:, 1])), label="Sepal Width (cm)"), |
|
gr.Slider(minimum=float(X[:, 2].min()), maximum=float(X[:, 2].max()), value=float(np.mean(X[:, 2])), label="Petal Length (cm)"), |
|
gr.Slider(minimum=float(X[:, 3].min()), maximum=float(X[:, 3].max()), value=float(np.mean(X[:, 3])), label="Petal Width (cm)") |
|
], |
|
outputs="text", |
|
title="Iris Flower Classifier", |
|
description="Select the features of the iris flower to predict its species." |
|
) |
|
|
|
|
|
if __name__ == "__main__": |
|
interface.launch(inline=False) |