Skip to content
Laviyx Learn

Try

Machine Learning course menu

Introduction to Machine Learning for Beginners (With Python)

What machine learning is, how it differs from writing rules by hand, supervised, unsupervised and reinforcement learning, regression vs classification, where ML is used, and a first working model in scikit-learn.

Beginner4 min readUpdated
On this page

Machine learning (ML) is a way of building programs that learn patterns from examples instead of following rules a programmer wrote by hand.

Suppose you want to flag suspicious transactions. The traditional approach is to write rules: "if the amount is over ₹50,000 and the country is new, flag it". Real fraud is too varied for a short list of rules. With machine learning, you give the computer thousands of past transactions labelled "fraud" or "not fraud", and an algorithm finds the patterns that separate them. The result, a model, can then score new transactions.

A first model in a few lines#

Here is a complete example with scikit-learn, Python's standard machine learning library. It learns to recognise three species of iris flower from four measurements of their petals and sepals:

Python
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.tree import DecisionTreeClassifier

iris = load_iris()
X_train, X_test, y_train, y_test = train_test_split(
    iris.data, iris.target, test_size=0.3, random_state=42, stratify=iris.target)

model = DecisionTreeClassifier(random_state=42)
model.fit(X_train, y_train)                    # learn from examples

print("accuracy on unseen flowers:", round(model.score(X_test, y_test), 3))
new_flower = [[5.1, 3.5, 1.4, 0.2]]            # sepal and petal measurements in cm
print("prediction:", iris.target_names[model.predict(new_flower)[0]])
Output
accuracy on unseen flowers: 0.933
prediction: setosa

Three ideas are already here: the model learns from training data (fit), it is evaluated on data it has never seen (score on the test set), and it predicts for new inputs (predict). The rest of the course builds on these three steps.

Types of machine learning#

TypeLearns fromTypical tasks
Supervised learningexamples with answers (labels)predicting prices, detecting spam, diagnosing disease
Unsupervised learningexamples without answersgrouping customers, compressing data, finding anomalies
Reinforcement learningrewards from trial and errorgame playing, robotics, recommendation strategies

Most real-world ML, and most of this course, is supervised learning.

Regression and classification#

Supervised problems come in two kinds, depending on what you predict:

  • Regression predicts a number: a house price, tomorrow's demand, delivery time.
  • Classification predicts a category: spam or not, which product category, which digit is in an image.

The iris example is classification (three species). Predicting the flower's petal length from its other measurements would be regression.

Where ML is used#

  • Recommendations: products, videos and songs you might like.
  • Fraud and anomaly detection in payments and logins.
  • Forecasting demand, sales and traffic.
  • Computer vision: reading documents, quality inspection, medical images.
  • Language: search, translation, chatbots and large language models.

What ML cannot do#

A model is only as good as its data. If the training data is too small, unrepresentative or biased, the model will be wrong, sometimes in harmful ways (for example a loan model trained on past decisions that were themselves unfair). ML finds correlations, not reasons, and it can fail silently when the world changes. Measuring, monitoring and questioning models is part of the job, and this course covers how.

What you need#

  • Python, and comfort with NumPy and pandas. If those are new, take the Python for Data course first.
  • Some maths helps (averages, basic algebra); the lessons explain the ideas you need as they come up.

The examples were run with scikit-learn 1.7, NumPy 2.3 and pandas 2.3, and the deep learning lessons with PyTorch. Every output shown is the real output of the code.

Key takeaway#

Next: The Machine Learning Workflow.

Finished this lesson?