Machine Learning: Beyond The basics and Types

Mabtoorulshafiq
The Modern Scientist
5 min readFeb 11, 2023
Machine Learning: Beyond The basics and Types

Nowadays! People are obsessed with the term Machine Learning. It is a Sub-section of Artificial Intelligence, that uses Mathematical Models to predict future possibilities. Instead of Using Multiple If-Else Conditions, You can Just Train Your Machine learning model and get efficient and accurate results.

In this Article, We will discuss 4 Main Types of Machine Learning Used to predict specific kinds of information by using specific kinds of data.

1- Supervised Learning:

Supervised learning is a type of machine learning where the algorithms are trained on labelled data to make predictions or take actions based on input data. It’s called “supervised” because the algorithm is guided by a supervisor, in the form of labelled examples, to produce the correct output.

In this type of learning, the algorithms learn from the relationship between the input and output data and then use this understanding to make predictions on unseen data. This can be contrasted with unsupervised learning, where the algorithms work with unlabeled data and try to identify patterns or structures in the data without guidance.

Supervised learning has a wide range of applications, including image classification, speech recognition, natural language processing, and predictive maintenance in the industry. It also plays a critical role in artificial intelligence and deep learning, where large neural networks are trained on vast amounts of data to achieve state-of-the-art results in various tasks.

Code:

# Import Libraries
import numpy as np
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.neighbors import KNeighborsClassifier

# Load Sample Dataset
iris = load_iris()

# Data Splitting
X_train, X_test, Y_train, Y_test = train_test_split(iris.data, iris.target, test_size=0.2, random_state=42)

# Train the model
model = KNeighborsClassifier(n_neighbors=3)
model.fit(X_train, Y_train)

# Model Evaluation
accuracy = model.score(X_test, Y_test)
print("Accuracy:", accuracy)

# Making Prediction
new_data = np.array([[5.1, 3.5, 1.4, 0.2]])
prediction = model.predict(new_data)
print("Prediction:", prediction)

2- Unsupervised Learning:

Unsupervised learning is a type of machine learning where the algorithms work with unlabeled data to identify patterns or structures in the data without guidance. Unlike supervised learning, where the algorithms are trained on labelled data to make predictions or take actions, unsupervised learning algorithms have to find their way to uncover the hidden structure of the data.

One of the main applications of unsupervised learning is clustering, where the algorithms group similar data points together and form clusters. Clustering can be used for a wide range of tasks, such as market segmentation, image segmentation, and anomaly detection. Another popular unsupervised learning method is dimensionality reduction, which reduces the number of features in the data while preserving important information.

Unsupervised learning is also an important part of deep learning, where unsupervised algorithms are used to pre-train the layers of neural networks before fine-tuning them using supervised learning. This has been shown to improve the performance of the networks on various tasks and is a key factor in the success of deep learning in recent years.

Code:

import numpy as np
import matplotlib.pyplot as plt
from sklearn.datasets import make_blobs
from sklearn.cluster import KMeans

# Generate Data
X, y = make_blobs(n_samples=200, centers=3, random_state=42)

# Training
model = KMeans(n_clusters=3)
model.fit(X)

# Getting Labels
labels = model.labels_

# Plotting data
plt.scatter(X[:, 0], X[:, 1], c=labels)
plt.show()

3-Semi-Supervised Learning:

Semi-Supervised learning is a type of Machine Learning that is in between Machine Learning and Deep Learning. It combines the strengths of both approaches by using both labelled and unlabeled data to improve the performance of the model.

The main idea behind Semi-Supervised Learning is to leverage the large amounts of unlabeled data that are often available to improve the model’s performance. This can be particularly useful in situations where obtaining labelled data is difficult, expensive, or time-consuming. For example, in Image Classification, it may be difficult to obtain labelled images for every possible class, but large amounts of unlabeled images may still be available.

In semi-supervised learning, the model is first trained on a small amount of labelled data, and then the model is used to make predictions on the unlabeled data. The model’s predictions on the unlabeled data can then be used to update the model’s parameters, which can help to improve its performance.

Code:

import numpy as np
import matplotlib.pyplot as plt
from sklearn.datasets import make_classification
from sklearn.model_selection import train_test_split
from sklearn.semi_supervised import LabelPropagation

# Generate Random Dataset
X, y = make_classification(n_classes=2, random_state=0)

# Split the data
X_labeled, X_unlabeled, y_labeled, y_unlabeled = train_test_split(X, y, test_size=0.5, stratify=y, random_state=0)

# Model Initialization
model = LabelPropagation(kernel='rbf', gamma=20)

# Model Fitting
model.fit(X_labeled, y_labeled)

# Predict Data
y_pred = model.predict(X_unlabeled)

# Plotting Results
plt.scatter(X_labeled[:, 0], X_labeled[:, 1], c=y_labeled, cmap='viridis')
plt.scatter(X_unlabeled[:, 0], X_unlabeled[:, 1], c=y_pred, cmap='viridis', alpha=0.5)
plt.show()

4- Reinforcement Learning:

Reinforcement learning is a type of machine learning that focuses on training agents to make decisions by taking actions in an environment to maximize a reward signal. It is inspired by the trial-and-error process that animals and humans use to learn, where they try different actions to achieve a desired outcome.

In Reinforcement learning, an agent interacts with an environment by taking action and receiving feedback in the form of rewards or penalties.

Reinforcement learning has been successfully applied to a wide range of tasks, including game playing (e.g., chess, Go, and video games), robotics, autonomous vehicles, recommendation systems, and even finance. For example, in the game of chess, the agent’s state could be the current configuration of the board, and the actions could be moves made by the pieces. The reward signal could be positive for winning the game, negative for losing the game, and zero for a draw.

Code:

import gym
import numpy as np

# Nodel Initialize
env = gym.make("CartPole-v1")

# Initialize weights
weights = np.random.rand(4) * 2 - 1

# Learning Rate
learning_rate = 0.1

# Discount Factor
discount_factor = 0.95

# Learning Performance
for episode in range(1000):
done = False
observation = env.reset()
total_reward = 0

while not done:
# compute the action based on the current observation and the policy weights
dot_product = np.dot(observation, weights)
action = 0 if dot_product < 0 else 1

# take the action and observe the new state and reward
observation, reward, done, _ = env.step(action)
total_reward += reward

# compute the gradient of the policy based on the observation
gradient = observation if action == 1 else -observation

# update the policy weights based on the gradient and the reward
weights += learning_rate * gradient * reward

print("Episode {}: Total Reward = {}".format(episode, total_reward))

# Evaluation
observation = env.reset()
done = False
total_reward = 0
while not done:
# compute the result
dot_product = np.dot(observation, weights)
action = 0 if dot_product < 0 else 1

# Taking the Action
observation, reward, done, _ = env.step(action)
total_reward += reward

print("Final Total Reward: {}".format(total_reward))

--

--