A simple machine learning example using Python using scikit-learn

A simple machine learning example using Python and the scikit-learn library for the classification of the Iris dataset. The Iris dataset is a classic dataset containing measurements of iris flowers and their species. We will use a Decision Tree classifier to classify the species based on the measurements.

from sklearn import datasets
from sklearn.model_selection import train_test_split
from sklearn.tree import DecisionTreeClassifier
from sklearn.metrics import accuracy_score

# Load the Iris dataset
iris = datasets.load_iris()
X = iris.data
y = iris.target

# Split the dataset into train and test sets (70% train, 30% test)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)

# Create a Decision Tree classifier and train it on the train set
clf = DecisionTreeClassifier()
clf.fit(X_train, y_train)

# Make predictions on the test set
y_pred = clf.predict(X_test)

# Calculate the accuracy of the classifier
accuracy = accuracy_score(y_test, y_pred)

print(f"Accuracy of the Decision Tree classifier: {accuracy:.2f}")

This code will output the accuracy of the Decision Tree classifier on the Iris dataset. We can experiment with other classifiers and their parameters to see how the results change.

Leave a Reply

Your email address will not be published. Required fields are marked *