Spaces:
Sleeping
Sleeping
| import numpy as np | |
| from sklearn.datasets import load_iris | |
| from sklearn.model_selection import train_test_split | |
| from sklearn.linear_model import LogisticRegression | |
| from sklearn.metrics import accuracy_score | |
| # Load the Iris dataset | |
| iris = load_iris() | |
| # Split the dataset into features (X) and target (y) | |
| X = iris.data | |
| y = iris.target | |
| # Split the data into training and testing sets | |
| X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42) | |
| # Create a Logistic Regression model | |
| model = LogisticRegression(max_iter=200) | |
| # Fit the model to the training data | |
| model.fit(X_train, y_train) | |
| # Make predictions on the test data | |
| y_pred = model.predict(X_test) | |
| # Calculate the accuracy of the model | |
| accuracy = accuracy_score(y_test, y_pred) | |
| print(f'Model Accuracy on Test Data: {accuracy*100:.2f}%') | |
| # Define a function to make predictions based on user input | |
| def predict_iris(sepal_length, sepal_width, petal_length, petal_width): | |
| # Convert input to a numpy array | |
| input_data = np.array([sepal_length, sepal_width, petal_length, petal_width]) | |
| # Reshape the input to match the model's input shape | |
| input_data = input_data.reshape(1, -1) | |
| # Make a prediction using the trained model | |
| prediction = model.predict(input_data) | |
| # Get the name of the predicted class | |
| prediction_name = iris.target_names[prediction[0]] | |
| return prediction_name | |
| # User input loop for continuous predictions | |
| while True: | |
| try: | |
| # Get user input for flower features | |
| sepal_length = float(input("Enter Sepal Length (cm): ")) | |
| sepal_width = float(input("Enter Sepal Width (cm): ")) | |
| petal_length = float(input("Enter Petal Length (cm): ")) | |
| petal_width = float(input("Enter Petal Width (cm): ")) | |
| # Make prediction and display results | |
| prediction = predict_iris(sepal_length, sepal_width, petal_length, petal_width) | |
| print(f"\nPredicted Iris Class: {prediction}") | |
| except ValueError: | |
| print("\nInvalid input. Please enter numerical values for flower features.") | |
| # Ask user if they want to continue | |
| choice = input("\nDo you want to make another prediction? (y/n): ") | |
| if choice.lower() != 'y': | |
| break | |
| print("\nExiting the program.") |