Spaces:
Sleeping
Sleeping
Create A\\app.py
Browse files- A////app.py +69 -0
A////app.py
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import numpy as np
|
| 2 |
+
from sklearn.datasets import load_iris
|
| 3 |
+
from sklearn.model_selection import train_test_split
|
| 4 |
+
from sklearn.linear_model import LogisticRegression
|
| 5 |
+
from sklearn.metrics import accuracy_score
|
| 6 |
+
|
| 7 |
+
# Load the Iris dataset
|
| 8 |
+
iris = load_iris()
|
| 9 |
+
|
| 10 |
+
# Split the dataset into features (X) and target (y)
|
| 11 |
+
X = iris.data
|
| 12 |
+
y = iris.target
|
| 13 |
+
|
| 14 |
+
# Split the data into training and testing sets
|
| 15 |
+
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
|
| 16 |
+
|
| 17 |
+
# Create a Logistic Regression model
|
| 18 |
+
model = LogisticRegression(max_iter=200)
|
| 19 |
+
|
| 20 |
+
# Fit the model to the training data
|
| 21 |
+
model.fit(X_train, y_train)
|
| 22 |
+
|
| 23 |
+
# Make predictions on the test data
|
| 24 |
+
y_pred = model.predict(X_test)
|
| 25 |
+
|
| 26 |
+
# Calculate the accuracy of the model
|
| 27 |
+
accuracy = accuracy_score(y_test, y_pred)
|
| 28 |
+
|
| 29 |
+
print(f'Model Accuracy on Test Data: {accuracy*100:.2f}%')
|
| 30 |
+
|
| 31 |
+
# Define a function to make predictions based on user input
|
| 32 |
+
def predict_iris(sepal_length, sepal_width, petal_length, petal_width):
|
| 33 |
+
# Convert input to a numpy array
|
| 34 |
+
input_data = np.array([sepal_length, sepal_width, petal_length, petal_width])
|
| 35 |
+
|
| 36 |
+
# Reshape the input to match the model's input shape
|
| 37 |
+
input_data = input_data.reshape(1, -1)
|
| 38 |
+
|
| 39 |
+
# Make a prediction using the trained model
|
| 40 |
+
prediction = model.predict(input_data)
|
| 41 |
+
|
| 42 |
+
# Get the name of the predicted class
|
| 43 |
+
prediction_name = iris.target_names[prediction[0]]
|
| 44 |
+
|
| 45 |
+
return prediction_name
|
| 46 |
+
|
| 47 |
+
# User input loop for continuous predictions
|
| 48 |
+
while True:
|
| 49 |
+
try:
|
| 50 |
+
# Get user input for flower features
|
| 51 |
+
sepal_length = float(input("Enter Sepal Length (cm): "))
|
| 52 |
+
sepal_width = float(input("Enter Sepal Width (cm): "))
|
| 53 |
+
petal_length = float(input("Enter Petal Length (cm): "))
|
| 54 |
+
petal_width = float(input("Enter Petal Width (cm): "))
|
| 55 |
+
|
| 56 |
+
# Make prediction and display results
|
| 57 |
+
prediction = predict_iris(sepal_length, sepal_width, petal_length, petal_width)
|
| 58 |
+
print(f"\nPredicted Iris Class: {prediction}")
|
| 59 |
+
|
| 60 |
+
except ValueError:
|
| 61 |
+
print("\nInvalid input. Please enter numerical values for flower features.")
|
| 62 |
+
|
| 63 |
+
# Ask user if they want to continue
|
| 64 |
+
choice = input("\nDo you want to make another prediction? (y/n): ")
|
| 65 |
+
if choice.lower() != 'y':
|
| 66 |
+
break
|
| 67 |
+
|
| 68 |
+
print("\nExiting the program.")
|
| 69 |
+
|