Algebra for Data Science
Algebra is one of the foundational mathematical disciplines used in data science. It provides methods for representing numerical relationships, solving equations, transforming variables, and understanding how changes in input features affect model predictions.
For example, an e-commerce company may analyse the relationship between advertising expenditure and sales revenue. A mathematical equation can represent this relationship, while a function can estimate expected sales for a particular advertising budget.
Algebra in data science involves using variables, equations, functions, and mathematical transformations to represent relationships between numerical features and target outcomes. These concepts form the foundation of regression analysis, feature engineering, statistical modelling, and machine learning optimisation.
What Are Variables and Constants in Algebra?
A variable is a mathematical symbol representing a quantity that can change.
Variables are commonly represented using letters such as \(x\), \(y\), and \(z\).
A constant represents a fixed numerical value within a particular mathematical expression.
Consider the equation:
\[ y=3x+5 \]
Here:
- \(x\) represents the independent variable.
- \(y\) represents the dependent variable.
- \(3\) represents the coefficient of \(x\).
- \(5\) represents the constant term.
For example, suppose \(x\) represents the number of products sold and \(y\) represents a numerical business metric.
For \(x=10\):
\[ y=3(10)+5=35 \]
The equation describes how the output changes as the input changes.
Independent and Dependent Variables
In predictive modelling, input variables are commonly called features, while the variable being predicted is called the target.
Consider a dataset containing:
| Advertising Spend (₹) | Sales Revenue (₹) |
|---|---|
| 1,000 | 5,000 |
| 2,000 | 8,000 |
| 3,000 | 11,000 |
| 4,000 | 14,000 |
Advertising expenditure may be treated as an input feature, while sales revenue is the target variable.
A regression model can estimate the mathematical relationship between them.
However, a statistical association does not automatically establish that advertising expenditure caused the observed revenue changes.
What Are Algebraic Expressions and Equations?
An algebraic expression combines variables, constants, and mathematical operations.
Examples include:
\[ 3x+5 \]
\[ x^2+2x+1 \]
An equation states that two mathematical expressions are equal.
For example:
\[ 2x+10=50 \]
Solving the equation involves isolating the unknown variable.
\[ 2x=40 \]
\[ x=20 \]
In data science, equations are used to calculate derived features, estimate unknown parameters, and represent relationships between observations.
Linear vs Nonlinear Equations
A linear equation represents a straight-line relationship.
\[ y=mx+c \]
Here, \(m\) is the slope and \(c\) is the intercept.
A nonlinear equation contains relationships that cannot generally be represented by a straight line.
For example:
\[ y=x^2+3x+2 \]
Nonlinear relationships are common in real-world datasets.
For example, increasing advertising expenditure may improve sales initially, but the additional benefit may decrease as spending grows.
What Are Functions in Mathematics?
A function defines a relationship that maps each valid input to exactly one output.
It is commonly represented as:
\[ y=f(x) \]
For example:
\[ f(x)=2x+10 \]
For an input of \(x=5\):
\[ f(5)=2(5)+10=20 \]
Functions are important because machine learning models are mathematical functions that transform input features into predictions.
Types of Functions Used in Data Science
| Function Type | Mathematical Form | Data Science Application |
|---|---|---|
| Linear | \(f(x)=mx+c\) | Linear regression |
| Quadratic | \(f(x)=ax^2+bx+c\) | Modelling curved relationships |
| Exponential | \(f(x)=a^x\) | Growth and decay modelling |
| Logarithmic | \(f(x)=\log(x)\) | Feature transformations |
| Sigmoid | \(f(x)=1/(1+e^{-x})\) | Logistic regression |
| Polynomial | \(f(x)=a_0+a_1x+\cdots+a_nx^n\) | Polynomial regression |
The appropriate function depends on the relationship being modelled.
What Are Logarithms in Data Science?
A logarithm is the inverse of exponentiation.
Consider:
\[ 2^3=8 \]
The equivalent logarithmic expression is:
\[ \log_2(8)=3 \]
The logarithm answers the question: To what power must the base be raised to obtain the given value?
Two logarithmic forms are particularly important in data science.
Natural Logarithm: Uses Euler's number \(e\), approximately 2.71828, as its base.
\[ \ln(x)=\log_e(x) \]
Common Logarithm: Uses base 10.
\[ \log_{10}(x) \]
Why Are Logarithmic Transformations Useful?
Real-world data frequently contains highly skewed numerical variables.
For example, customer purchase values might be:
₹200, ₹500, ₹800, ₹1,200, and ₹50,000.
The unusually large purchase creates a long right tail.
A logarithmic transformation compresses larger values relative to smaller values.
For positive values:
\[ x_{\text{transformed}}=\ln(x) \]
A common alternative for non-negative data is:
\[ x_{\text{transformed}}=\ln(1+x) \]
The log1p transformation is useful because it is defined when \(x=0\).
Python Example of Logarithmic Transformation
import numpy as nppurchase_values = np.array([ 200, 500, 800, 1200, 50000])transformed_values = np.log1p(purchase_values)print(np.round(transformed_values, 2))Output:
[ 5.3 6.22 6.69 7.09 10.82]The transformation reduces the relative numerical differences between large and small observations.
However, logarithmic transformation does not automatically make a distribution normal or improve every machine learning model.
What Is Feature Engineering in Algebra?
Feature engineering involves creating or transforming variables to represent useful information.
Consider a retail dataset containing product prices and quantities.
Total transaction value can be calculated using:
\[ Transaction\ Value=Price\times Quantity \]
Similarly, average order value can be calculated as:
\[ AOV=\frac{Total\ Revenue}{Number\ of\ Orders} \]
Python implementation:
revenue = 150000orders = 75average_order_value = revenue / ordersprint(average_order_value)Output:
2000.0The resulting feature represents average revenue per order.
Feature engineering can help models identify patterns that are difficult to capture from individual raw variables.
How Is Algebra Used in Linear Regression?
Linear regression models the relationship between input variables and a continuous target.
For a single feature:
\[ \hat{y}=\beta_0+\beta_1x \]
Here:
- \(\hat{y}\) represents the predicted value.
- \(\beta_0\) represents the intercept.
- \(\beta_1\) represents the regression coefficient.
- \(x\) represents the input feature.
Consider a hypothetical sales prediction model:
\[ \hat{y}=5000+3x \]
Suppose \(x\) represents advertising expenditure in rupees.
For an advertising budget of ₹10,000:
\[ \hat{y}=5000+3(10000) \]
\[ \hat{y}=35000 \]
The model predicts ₹35,000 in sales revenue.
The coefficient represents the estimated change in the prediction associated with a one-unit increase in advertising expenditure, assuming the specified model is appropriate.
What Are the Practical Applications of Algebra in Data Science?
Algebra supports several common data science tasks.
- Regression Modelling: Represent relationships between input features and numerical targets.
- Feature Engineering: Calculate derived variables such as profit margin and transaction value.
- Data Transformation: Apply logarithmic, polynomial, or other mathematical transformations.
- Optimisation: Define equations describing prediction errors.
- Machine Learning: Express model predictions as mathematical functions.
A strong understanding of algebra helps explain how machine learning models convert numerical features into predictions.
Common Mistakes in Algebra for Data Science
- Confusing an independent variable with a dependent variable.
- Assuming every relationship between variables is linear.
- Applying logarithms to zero or negative values without an appropriate transformation.
- Interpreting regression coefficients as causal effects without supporting evidence.
- Ignoring units when constructing derived numerical features.
Data Science Tutorial for Beginners
Vaishali Ardhana








