D Dev Duniya
Back to Journal
9 min read

Mastering Machine Learning Algorithms in Python 2025: A Step-by-Step Guide with Examples- Devduniya

Ashish Yadav

Ashish Yadav

Published on Dev Duniya
Keep exploring
Compare the details before you buy.

Machine learning is a rapidly growing field that has the potential to revolutionize the way we live and work. In this blog post, we will explore some of the most popular machine learning algorithms and provide example code to help you get started. Whether you are a beginner or an experienced developer, this post will give you a solid understanding of the key concepts and techniques used in machine learning.

From supervised learning algorithms like linear regression and logistic regression to unsupervised learning algorithms like k-means and hierarchical clustering, we will cover a wide range of topics and provide hands-on examples to help you understand how these algorithms work and how to implement them in practice. So, let's dive in and start learning about machine learning.

No.1: Linear Regression

Linear regression is a statistical method used to determine the relationship between a dependent variable and one or more independent variables. The goal is to find the best line of fit (the "regression line") through the data points. The equation of the line is typically represented as y = mx + b, where y is the dependent variable, x is the independent variable, m is the slope of the line, and b is the y-intercept. The slope represents the change in y for a change in x, and the y-intercept represents the value of y when x is 0. By finding the best line of fit, we can use the equation to make predictions about the value of the dependent variable for new values of the independent variable.
A method used to model the relationship between a dependent variable and one or more independent variables by fitting a linear equation to the observed data.

from sklearn.linear_model import LinearRegression
X = [[0, 1], [5, 1], [15, 2], [25, 5], [35, 11], [45, 15], [55, 34], [60, 35]]
y = [4, 5, 20, 14, 32, 22, 38, 43]

model = LinearRegression().fit(X, y)

r_sq = model.score(X, y)
print('coefficient of determination:', r_sq)

print('intercept:', model.intercept_)
print('slope:', model.coef_)

No.2: Logistic regression

Logistic regression is a statistical method used to predict a binary outcome (1 / 0, Yes / No, True / False) based on one or more independent variables. The model represents the probability of the outcome being 1, which is represented by a value between 0 and 1. The model is represented by an equation that uses the independent variables (also known as predictors) to calculate a probability. The equation typically uses a function called the logistic function, which produces a probability value between 0 and 1. Based on this probability, a threshold of 0.5 is used to classify the outcome as 0 or 1. Logistic regression is commonly used in fields such as medicine, finance, and social sciences.
A method used for classification tasks, which estimates the probability of a binary response based on one or more predictor variables.

from sklearn.linear_model import LogisticRegression

X = [[0, 1], [5, 1], [15, 2], [25, 5], [35, 11], [45, 15], [55, 34], [60, 35]]
y = [0, 0, 0, 1, 1, 1, 1, 1]

model = LogisticRegression().fit(X, y)

print('coefficients:', model.coef_)
print('intercept:', model.intercept_)
print('predicted probabilities:', model.predict_proba(X))

No.3: Decision tree

A decision tree is a flowchart-like tree structure that is used to represent decisions and their possible consequences. Each internal node of the tree represents a "test" on an attribute (e.g. "Is the temperature above 60 degrees?"), each branch represents the outcome of the test (e.g. "Yes" or "No"), and each leaf node represents a class label (e.g. "Play" or "Don't play"). The topmost node in the tree is the root node, and it is the starting point for all decisions.
A method used for classification and regression involves creating a tree-like model of decisions based on certain conditions.

from sklearn.tree import DecisionTreeClassifier

X = [[0, 1], [5, 1], [15, 2], [25, 5], [35, 11], [45, 15], [55, 34], [60, 35]]
y = [0, 0, 0, 1, 1, 1, 1, 1]

model = DecisionTreeClassifier().fit(X, y)

print('predictions:', model.predict(X))
print('feature importances:', model.feature_importances_)

No.4: Random forests

Random forests are a type of ensemble learning method for classification, regression, and other tasks, that operate by constructing a multitude of decision trees at training time and outputting the class that is the mode of the classes (classification) or mean prediction (regression) of the individual trees.
Here is an example of a random forest classifier in Python using the popular machine learning library sci-kit-learn:

from sklearn.ensemble import RandomForestClassifier

# Create the random forest classifier
rf = RandomForestClassifier(n_estimators=100)

# Train the classifier on the training data
rf.fit(X_train, y_train)

# Predict on the test data
predictions = rf.predict(X_test)

# Calculate the accuracy
accuracy = rf.score(X_test, y_test)
print("Accuracy:", accuracy)
The main parameters of the RandomForestClassifier class are:

n_estimators: The number of trees in the forest.
criterion: The function to measure the quality of a split.
max_depth: The maximum depth of the tree.
min_samples_split: The minimum number of samples required to split an internal node.
min_samples_leaf: The minimum number of samples required to be at a leaf node.
max_features: The number of features to consider when looking for the best split.

No.5: SVM (Support Vector Machines)

Support Vector Machines (SVMs) are a type of supervised learning algorithm that can be used for classification and regression problems. The goal of an SVM is to find the best boundary (or "hyperplane") that separates the different classes in the data. This boundary is chosen so that it maximizes the margin, which is the distance between the boundary and the closest data points from each class. These closest data points are called support vectors.

SVMs can also handle non-linearly separable data by using something called a kernel trick, which transforms the data into a higher dimensional space, where a linear boundary can be found. The most commonly used kernel is the radial basis function (RBF) kernel.
A method used for classification tasks, where the goal is to find the hyperplane in an N-dimensional space that maximally separates the two classes.

from sklearn import svm
X = [[0, 0], [1, 1]]
y = [0, 1]
model = svm.SVC(kernel='linear')
model.fit(X, y)
prediction = model.predict([[2., 2.]])
print(prediction)

No.6: K-Nearest Neighbors (KNN)

KNN is a simple and easy-to-implement supervised machine learning algorithm. It can be used for classification and regression. The idea behind KNN is to find a group of points in the training data that are similar to the new data point, and then predict the label of the new data point based on the labels of these similar points.
Here is some Python code to implement KNN for classification:

from sklearn.neighbors import KNeighborsClassifier

# Create a KNN classifier with 3 neighbors
knn = KNeighborsClassifier(n_neighbors=3)

# Fit the classifier to the training data
knn.fit(X_train, y_train)

# Predict the labels for the test data
y_pred = knn.predict(X_test)

# Calculate the accuracy of the predictions
accuracy = knn.score(X_test, y_test)

In this code, X_train and y_train are the training data and labels, respectively, and X_test and y_test are the test data and labels. The fit function is used to train the KNN classifier on the training data, and the predict function is used to predict the labels of the test data. The score function is used to calculate the accuracy of the predictions.

KNN has a few hyperparameters that you can tune to improve the performance of the model. The most important hyperparameter is the number of neighbors (n_neighbors), which determines how many similar points to consider when making a prediction. Increasing the value of n_neighbors will make the model more conservative, as it will consider more points when making a prediction, but it may also make the model more prone to overfitting.

No.7: K-Means Clustering

K-means is a clustering algorithm that divides a group of data points into k clusters, where k is a user-defined parameter. The algorithm works by iteratively assigning each data point to the cluster with the nearest mean, and then updating the cluster means.
Here is a simple implementation of K-means in Python:

from sklearn.cluster import KMeans
import numpy as np

# generate some random data
data = np.random.rand(100, 3)

# create the k-means model
kmeans = KMeans(n_clusters=3)

# fit the model to the data
kmeans.fit(data)

# get the cluster labels
labels = kmeans.predict(data)

# get the cluster centers
centers = kmeans.cluster_centers_

This code will create a k-means model with 3 clusters, fit the model to the data, and then predict the cluster labels for the data points. The cluster centers are also obtained.

Note that this is just a simple example, and there are many parameters that can be adjusted in the KMeans model, such as the number of iterations and the distance metric used to calculate the distance between data points and cluster means.

No.8: Naive Bayes

Naive Bayes is a simple and effective classification algorithm that is based on the Bayes Theorem. It is called "naive" because it makes a strong assumption about the independence of the features.
In machine learning, we are given a set of training examples and we want to build a model that can predict the label of a new example. In the case of the Naive Bayes algorithm, the model predicts the label (e.g., spam or not spam) based on the features (e.g., words in the email).
Here is some pseudocode for the Naive Bayes algorithm:

for each label L in the set of labels:
    calculate the prior probability of L: P(L)
    for each feature F in the set of features:
        calculate the likelihood of F given L: P(F|L)
    calculate the posterior probability of L given the features: P(L|F)
predict the label with the highest posterior probability

Here is some example Python code for training a Naive Bayes classifier and making predictions:

from sklearn.naive_bayes import MultinomialNB

# create the classifier
clf = MultinomialNB()

# train the classifier with the training data
clf.fit(X_train, y_train)

# make predictions on the test data
predictions = clf.predict(X_test)

# evaluate the performance of the classifier
accuracy = clf.score(X_test, y_test)

Here, X_train and y_train are the features and labels of the training data, respectively, and X_test and y_test are the features and labels of the test data. The fit function trains the classifier on the training data, and the predict function makes predictions on the test data. The score function calculates the accuracy of the predictions, which is the percentage of predictions that are correct.

In conclusion, we have covered a wide range of machine learning algorithms in this blog post and provided example codes to help you understand how they work and how to implement them in practice. From supervised learning algorithms like linear regression and logistic regression to unsupervised learning algorithms like k-means and hierarchical clustering, we have explored the key concepts and techniques used in machine learning. We hope that this post has provided you with a solid foundation for further learning and experimentation with machine learning.

Conclusion:

Remember, machine learning is a rapidly growing field with endless possibilities, so keep learning, experimenting, and exploring new algorithms. We recommend you keep practicing with different datasets and see how these algorithms perform on them. Also, don't forget to check out the documentation, tutorials, and sample codes from the various Machine Learning libraries available online.

If you have any queries related to this article, then you can ask in the comment section, we will contact you soon, and Thank you for reading this article.

Follow me to receive more useful content:

Instagram | Twitter | Linkedin | Youtube

Thank you

Keep reading

12 latest articles

The newest stories from Dev Duniya, excluding the article you are reading.

All articles →

From the product catalog

Explore device specifications

Specification cards update automatically from published catalog products, grouped by brand.

Apple specifications

Explore Apple devices →
Apple Apple MacBook Pro 14-inch
Apple Apple MacBook Pro 14-inch specifications

Experience unrivaled performance with the latest Apple MacBook Pro, available in 14-inch and 16-inch models powered by the revolutionary M6, M5 Pro, and M5 Max chips. Built for Apple Intelligence, these powerhouse laptops feature advanced Thunderbolt 5 ports, Wi-Fi 7, and optional Nano-texture displays for minimal glare. Maximize your creative potential with up to 128GB of unified memory in a sleek Space Black or Silver finish.

Display size
14-inch
Screen Type
Standard display (low reflectivity) or Nano-texture display (reduces reflections and glare in bright lighting)
External Display Support
Support for up to three external displays (M5 Pro) / Support for up to four external displays (M5 Max)
M6 Chip
12‑core CPU, 12‑core GPU, Dual 16‑core Neural Engine
See all specifications →
Apple Apple MacBook Pro 16-inch
Apple Apple MacBook Pro 16-inch specifications

Experience unrivaled performance with the latest Apple MacBook Pro, available in 14-inch and 16-inch models powered by the revolutionary M6, M5 Pro, and M5 Max chips. Built for Apple Intelligence, these powerhouse laptops feature advanced Thunderbolt 5 ports, Wi-Fi 7, and optional Nano-texture displays for minimal glare. Maximize your creative potential with up to 128GB of unified memory in a sleek Space Black or Silver finish.

Display size
16-inch
Screen Type
Standard display (low reflectivity) or Nano-texture display (reduces reflections and glare in bright lighting)
External Display Support
Support for up to three external displays (M5 Pro) Support for up to four external displays (M5 Max)
M5 Pro Chip
18‑core CPU, 20‑core GPU, 16‑core Neural Engine
See all specifications →
Apple Apple iPhone Duo
Apple Apple iPhone Duo specifications

Apple iPhone Duo is a new-generation iPhone model focused on a sleek design, powerful performance, and everyday usability. Explore iPhone Duo specifications, features, display, cameras, battery, and expected price in the USA. Get the latest information about Apple iPhone Duo and its key features. Compare iPhone Duo with other popular Apple iPhone models.

Size (inches)
Open: 4.64 x 6.48 x 0.21 Closed: 4.64 x 3.31 x 0.44
Weight (ounces)
8.96
Display size (inches)
Inner: 7.6-inch (diagonal) all-screen OLED folding display Outer: 5.4-inch (diagonal) all-screen OLED display
Resolution (pixels)
Inner: 1878-by-2670-pixel resolution at 430 ppi Outer: 1398-by-2034-pixel resolution at 460 ppi
See all specifications →
Apple Apple iPhone 18 Pro Max
Apple Apple iPhone 18 Pro Max specifications

Apple iPhone 18 Pro Max is a high-end flagship smartphone designed for powerful performance and advanced photography. Explore iPhone 18 Pro Max specifications, features, display, cameras, battery, and expected price in the USA. Discover the latest upgrades and key features of Apple’s largest Pro iPhone. Compare iPhone 18 Pro Max with other premium flagship smartphones.

Size (inches)
6.43 x 3.07 x 0.34
Weight (ounces)
8.78
Display size (inches)
6.9-inch (diagonal) all-screen OLED display
Resolution (pixels)
2868-by-1320-pixel resolution at 460 ppi
See all specifications →
Apple Apple iPhone 18 Pro
Apple Apple iPhone 18 Pro specifications

Apple iPhone 18 Pro is a premium flagship smartphone with advanced performance and an upgraded camera system. Explore iPhone 18 Pro specifications, features, display, cameras, battery, and expected price in the USA. Get the latest details about Apple’s new Pro model and its key upgrades. Compare iPhone 18 Pro features with other flagship smartphones.

Size (inches)
5.91 x 2.83 x 0.34
Weight (ounces)
7.44
Display size (inches)
6.3-inch (diagonal) all-screen OLED display
Resolution (pixels)
2622-by-1206-pixel resolution at 460 ppi
See all specifications →

Google specifications

Explore Google devices →
Google Google Pixel 12
Google Google Pixel 12 specifications

Upgrade to the Google Pixel 12, an advanced 5G AI smartphone. Experience seamless performance powered by the custom Google Tensor G7 processor. Enjoy a stunning 6.3-inch OLED Actua display and take flawless photos with the powerful 50MP dual-camera system. Built with an adaptive all-day battery and seamless Google Gemini AI integration, it is the perfect unlocked device for everyday brilliance.

Size (inches)
6.02 x 2.83 x 0.34
Weight (ounces)
6.95
Display size (inches)
6.3-inch (diagonal) OLED display
Resolution (pixels)
1080 x 2424 pixels
See all specifications →
Google Google Pixel 12 Pro
Google Google Pixel 12 Pro specifications

Discover the new Google Pixel 12 Pro, the ultimate 5G AI smartphone. Powered by the next-generation Google Tensor G7 chip and built-in Gemini AI, it delivers blazing-fast performance. Capture professional-grade photos with the advanced 50MP Pro Triple Camera system featuring 5x optical zoom. Complete with a stunning 6.3-inch Super Actua display, all-day adaptive battery, and a premium titanium frame. Upgrade to the smartest Android phone today.

Size (inches)
6.02 x 2.83 x 0.33
Weight (ounces)
7.3
Display size (inches)
6.31-inch LTPO OLED display
Resolution (pixels)
2856 x 1280 pixels
See all specifications →
Google Google Pixel 12 Pro XL
Google Google Pixel 12 Pro XL specifications

Go bigger with the Google Pixel 12 Pro XL, the ultimate large-screen 5G AI smartphone. It boasts a massive 6.8-inch OLED display and the lightning-fast Google Tensor G7 chip. Capture master-level photography using the 50MP Pro Triple Camera setup. Equipped with a massive 5500mAh battery for extended endurance and fast 67W charging, this unlocked flagship redefines premium power.

Size (inches)
6.43 x 3.07 x 0.34
Weight (ounces)
8.18
Display size (inches)
6.8-inch LTPO OLED Super Actua display
Resolution (pixels)
2992 x 1344 pixels
See all specifications →
Google Google Pixel 12 Pro Fold
Google Google Pixel 12 Pro Fold specifications

Transform your mobile experience with the Google Pixel 12 Pro Fold. This unlocked 5G AI foldable smartphone unfolds into a gorgeous 8-inch dual-UTG inner OLED display for next-level multitasking and entertainment. Powered by the Google Tensor G7 chip, it features flexible triple-camera photography, advanced Gemini AI, and a highly durable folding hinge design.

Size (inches) - Unfolded
6.24 x 5.64 x 0.16
Size (inches) - Folded
6.24 x 2.87 x 0.35
Weight (ounces)
7.58
Main Display
8.0-inch Foldable LTPO OLED, 120Hz
See all specifications →

Samsung specifications

Explore Samsung devices →