Supervised ML Introduction

Supervised machine learning is a type of artificial intelligence (AI) technique that involves teaching a computer model how to make predictions or decisions based on labeled examples. The term "supervised" comes from the fact that the model is guided and supervised throughout the learning process.

To understand supervised learning, imagine you want to teach a computer how to recognize different types of animals. You would start by showing the computer a set of labeled examples. For each example, you would provide an image of an animal along with the correct label indicating the type of animal it is, such as "cat," "dog," or "horse."

The computer then uses these labeled examples to learn patterns and relationships between the input data (the images) and the output labels (the animal types). It looks for features or characteristics in the images that are indicative of a specific animal type. This process is called training the model.

Once the model is trained, you can test it by giving it new, unlabeled images of animals that it hasn't seen before. The model will analyze the features of these new images and make predictions about the animal type. You can then compare the model's predictions with the correct labels to evaluate its accuracy.

The goal of supervised learning is to train the model in such a way that it can generalize its knowledge to make accurate predictions on unseen data. By providing the model with a large and diverse set of labeled examples, it can learn to recognize and classify animals (or any other type of data) based on the patterns it has discovered during training.

In summary, supervised machine learning involves training a computer model using labeled examples, enabling it to make predictions or decisions on new, unseen data based on what it has learned.

Python Example

from sklearn import datasets
from sklearn.model_selection import train_test_split
from sklearn.neighbors import KNeighborsClassifier

# Load the dataset of hand-written digits
digits = datasets.load_digits()

# Split the data into training and testing sets
X_train, X_test, y_train, y_test = train_test_split(digits.data, digits.target, test_size=0.2, random_state=42)

# Create a k-nearest neighbors classifier
knn = KNeighborsClassifier(n_neighbors=3)

# Train the model using the training data
knn.fit(X_train, y_train)

# Make predictions on the test data
predictions = knn.predict(X_test)

# Evaluate the accuracy of the model
accuracy = knn.score(X_test, y_test)
print("Accuracy:", accuracy)

Results

Accuracy: 0.9833333333333333

In this example, we first import the necessary libraries. We then load the hand-written digits dataset from scikit-learn. Next, we split the dataset into training and testing sets using the train_test_split function. The training set (X_train) consists of the input data (images of digits), and the corresponding target labels (y_train) indicate the actual digit value.

We create a k-nearest neighbors classifier (KNeighborsClassifier) and set it to use 3 nearest neighbors for classification. We then train the model by calling the fit method on the training data.

After training, we use the trained model to make predictions (predict) on the test data (X_test). Finally, we evaluate the accuracy of the model by comparing the predictions with the actual labels using the score method.

This is a basic example of supervised learning in Python, where we train a model to classify hand-written digits based on labeled examples and then use it to make predictions on unseen data.