import numpy as np import pandas as pd from sklearn.model_selection import train_test_split from sklearn.linear_model import LogisticRegression from sklearn.metrics import accuracy_score, classification_report, confusion_matrix import matplotlib.pyplot as plt from sklearn.datasets import load_iris data = load_iris() X = data.data y = data.target binary_indices = np.where(y != 2) X = X[binary_indices] y = y[binary_indices] X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42) model = LogisticRegression() model.fit(X_train, y_train) y_pred = model.predict(X_test) accuracy = accuracy_score(y_test, y_pred) print(f'Accuracy: {accuracy:.2f}') print('Classification Report:') print(classification_report(y_test, y_pred)) print('Confusion Matrix:') print(confusion_matrix(y_test, y_pred)) X_train_2D = X_train[:, :2] X_test_2D = X_test[:, :2] model_2D = LogisticRegression() model_2D.fit(X_train_2D, y_train) h = .02 x_min, x_max = X_train_2D[:, 0].min() - 1, X_train_2D[:, 0].max() + 1 y_min, y_max = X_train_2D[:, 1].min() - 1, X_train_2D[:, 1].max() + 1 xx, yy = np.meshgrid(np.arange(x_min, x_max, h), np.arange(y_min, y_max, h)) Z = model_2D.predict(np.c_[xx.ravel(), yy.ravel()]) Z = Z.reshape(xx.shape) plt.contourf(xx, yy, Z, alpha=0.8) plt.scatter(X_train_2D[:, 0], X_train_2D[:, 1], c=y_train, edgecolor='k', marker='o') plt.xlabel('Feature 1') plt.ylabel('Feature 2') plt.title('Decision Boundary') plt.show()