import matplotlib.pyplot as plt from mpl_toolkits.mplot3d import Axes3D # For 3D plotting from sklearn.decomposition import PCA from sklearn.datasets import load_iris from sklearn.linear_model import LogisticRegression from sklearn.model_selection import train_test_split from sklearn.metrics import accuracy_score, classification_report # Load dataset data = load_iris() X = data.data y = data.target # Split the data into training and test sets X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=0) # Initialize and train logistic regression model for multiclass classification logreg = LogisticRegression(multi_class='multinomial', solver='lbfgs', max_iter=200) logreg.fit(X_train, y_train) # Predict and evaluate y_pred = logreg.predict(X_test) print("Accuracy:", accuracy_score(y_test, y_pred)) print(classification_report(y_test, y_pred)) # Use PCA to reduce to 3D for visualization pca = PCA(n_components=3) X_test_3d = pca.fit_transform(X_test) # Plotting in 3D fig = plt.figure(figsize=(10, 7)) ax = fig.add_subplot(111, projection='3d') # Scatter plot with true labels scatter = ax.scatter(X_test_3d[:, 0], X_test_3d[:, 1], X_test_3d[:, 2], c=y_test, cmap='viridis', edgecolor='k', s=50) ax.set_title("3D Plot of Classified Data (True Labels)") ax.set_xlabel("PCA Component 1") ax.set_ylabel("PCA Component 2") ax.set_zlabel("PCA Component 3") # Color bar for classes legend1 = ax.legend(*scatter.legend_elements(), title="Classes") ax.add_artist(legend1) plt.show()