Comparison supervised models

To compare the performance of various algorithms on the load_iris dataset, we first need to import the necessary libraries and load the dataset. The load_iris dataset is available in scikit-learn, so make sure you have it installed. Let's proceed with the comparison:


import numpy as np
import pandas as pd
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.metrics import accuracy_score
from sklearn.tree import DecisionTreeClassifier
from sklearn.ensemble import RandomForestClassifier
from sklearn.gaussian_process import GaussianProcessClassifier
from sklearn.linear_model import LinearRegression, Ridge, SGDClassifier, Perceptron
from sklearn.naive_bayes import GaussianNB
from sklearn.svm import SVC

# Load the iris dataset
data = load_iris()
X, y = data.data, data.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)

# Scale the features (not required for all algorithms, but helps some)
scaler = StandardScaler()
X_train = scaler.fit_transform(X_train)
X_test = scaler.transform(X_test)

# Initialize the classifiers
classifiers = {
    "Decision Tree": DecisionTreeClassifier(),
    "Random Forest": RandomForestClassifier(),
    "Gaussian Process": GaussianProcessClassifier(),
    "Linear Regression": LinearRegression(),
    "Ridge Regression": Ridge(),
    "Stochastic Gradient Descent": SGDClassifier(),
    "Perceptron": Perceptron(),
    "Naive Bayes": GaussianNB(),
    "Support Vector Machine": SVC(),
}

# Train and evaluate each classifier
results = {}
for name, clf in classifiers.items():
    clf.fit(X_train, y_train)
    y_pred = np.round(clf.predict(X_test), 0)
    print(y_test)
    print(y_pred)
    accuracy = accuracy_score(y_test, y_pred)
    results[name] = accuracy

# Sort results in descending order of accuracy
sorted_results = {k: v for k, v in sorted(results.items(), key=lambda item: item[1], reverse=True)}

# Display the results
df = pd.DataFrame.from_dict(sorted_results, orient="index", columns=["Accuracy"])
print(df)
      

Results

Results:


                             Accuracy
Decision Tree                1.000000
Random Forest                1.000000
Gaussian Process             1.000000
Linear Regression            1.000000
Ridge Regression             1.000000
Stochastic Gradient Descent  1.000000
Naive Bayes                  1.000000
Support Vector Machine       1.000000
Perceptron                   0.966667
      

Now, let's discuss the results:

The accuracy of each algorithm on the load_iris dataset will vary depending on the random seed used for train-test split and other factors. However, we can expect some trends based on the typical behavior of these algorithms on this type of dataset.

From the results, we can see that ensemble methods like Random Forest and Gaussian Process classifiers tend to perform well on the load_iris dataset due to their ability to capture complex relationships in the data.

Support Vector Machine (SVM) is another strong performer that often works well with well-separated classes, as is the case in the load_iris dataset.

Naive Bayes classifiers can perform surprisingly well on some datasets, particularly when features are mostly independent, as the name suggests. However, in more complex datasets, its performance might not be as good.

On the other hand, Linear Regression and Ridge Regression are not suitable for classification tasks like this, so their inclusion here is not as relevant.

Stochastic Gradient Descent and Perceptron might not be the best choices for this dataset since they are primarily used for different types of problems or have some limitations in their capabilities.

Overall, the most appropriate algorithms for the load_iris dataset are likely to be Random Forest, Gaussian Process, and Support Vector Machine. It's important to note that hyperparameter tuning and further experimentation could potentially improve the performance of any of these algorithms.

Remember that different datasets might yield different results, and it's always a good practice to cross-validate and fine-tune the hyperparameters to obtain the best possible performance for your specific problem.