BharatYadav00 commited on
Commit
f17e759
·
verified ·
1 Parent(s): fcdc10d
Files changed (1) hide show
  1. iris_flower_classifier.py +54 -0
iris_flower_classifier.py ADDED
@@ -0,0 +1,54 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # -*- coding: utf-8 -*-
2
+ """Iris_Flower_Classifier.ipynb
3
+
4
+ Automatically generated by Colab.
5
+
6
+ Original file is located at
7
+ https://colab.research.google.com/drive/1ddsKnOLQk_nPeF9zu0Qr9yTsvmg-0D8S
8
+ """
9
+
10
+ import gradio as gr
11
+ import numpy as np
12
+ from sklearn.datasets import load_iris
13
+ from sklearn.model_selection import train_test_split
14
+ from sklearn.preprocessing import StandardScaler
15
+ from sklearn.ensemble import RandomForestClassifier
16
+ from sklearn.pipeline import make_pipeline
17
+
18
+ # Load the Iris dataset
19
+ iris = load_iris()
20
+ X = iris.data
21
+ y = iris.target
22
+ feature_names = iris.feature_names
23
+ target_names = iris.target_names
24
+
25
+ # Split the data into training and testing sets
26
+ X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
27
+
28
+ # Create and train a RandomForest model
29
+ model = make_pipeline(StandardScaler(), RandomForestClassifier())
30
+ model.fit(X_train, y_train)
31
+
32
+ # Define the prediction function
33
+ def predict_iris(sepal_length, sepal_width, petal_length, petal_width):
34
+ feature_values = np.array([sepal_length, sepal_width, petal_length, petal_width]).reshape(1, -1)
35
+ prediction = model.predict(feature_values)
36
+ return target_names[prediction[0]]
37
+
38
+ # Create a Gradio interface
39
+ interface = gr.Interface(
40
+ fn=predict_iris,
41
+ inputs=[
42
+ gr.Slider(minimum=float(X[:, 0].min()), maximum=float(X[:, 0].max()), value=float(np.mean(X[:, 0])), label="Sepal Length (cm)"), # Changed 'default' to 'value'
43
+ gr.Slider(minimum=float(X[:, 1].min()), maximum=float(X[:, 1].max()), value=float(np.mean(X[:, 1])), label="Sepal Width (cm)"), # Changed 'default' to 'value'
44
+ gr.Slider(minimum=float(X[:, 2].min()), maximum=float(X[:, 2].max()), value=float(np.mean(X[:, 2])), label="Petal Length (cm)"), # Changed 'default' to 'value'
45
+ gr.Slider(minimum=float(X[:, 3].min()), maximum=float(X[:, 3].max()), value=float(np.mean(X[:, 3])), label="Petal Width (cm)") # Changed 'default' to 'value'
46
+ ],
47
+ outputs="text",
48
+ title="Iris Flower Classifier",
49
+ description="Select the features of the iris flower to predict its species."
50
+ )
51
+
52
+ # Launch the interface
53
+ if __name__ == "__main__":
54
+ interface.launch()