K-means
Sometimes we have data without labels and want to discover groups within it. This is called clustering.
K-means is a clustering algorithm that groups nearby points together. Each group is represented by a point called a centroid.
K-means alternates between two simple steps:
- assign every point to its nearest centroid;
- move each centroid to the mean of the points assigned to it.
We use for the cluster assigned to example , and for the centroid of cluster .
The first step assigns each point to its nearest centroid.
The second step moves each centroid to the mean of the points assigned to it.
We repeat these steps until the assignments stop changing, or until the centroids move by only a very small amount.

K-means alternates between assigning points to their nearest centroid and moving each centroid to the mean of its assigned points.
A practical issue occurs if a cluster receives no points. Its mean is then undefined, so an implementation must keep or reinitialize that centroid before continuing.
K-means tries to keep every point close to its assigned centroid.
Its objective, called the distortion, is:
Smaller means the clusters are more compact.
Each assignment step and centroid-update step can only decrease or leave it unchanged.
K-means therefore eventually reaches a solution where the assignments stop changing.
However, this solution may only be a local minimum.
Different initial centroids can produce different final clusters.

Different initial centroids can lead to very different solutions. In practice, we run K-means several times and keep the solution with the smallest distortion.
For this reason, we usually run K-means several times with different initial centroids and keep the solution with the smallest .
Because K-means uses squared Euclidean distance, feature scaling matters. A feature with a much larger numerical scale can dominate the distance calculation.
K-means also works best when clusters are reasonably compact and roughly spherical. It can perform poorly for elongated clusters, clusters with very different densities, or categorical features.
The number of clusters must also be chosen separately.
Principal Component Analysis
Principal Component Analysis, or PCA, finds directions along which the data varies the most.
We can use these directions to represent high-dimensional data using fewer dimensions while preserving as much variation as possible.
Before applying ordinary PCA, we center the data so that each feature has mean zero.
For feature , compute its mean:
Then center the feature:
If feature scales should not influence PCA, we can additionally standardize each feature.
Define:
Then:
A constant feature has zero variance and cannot be standardized.
Below, refers to the centered data, or standardized data if we choose to standardize.
Maximum-Variance Directions
The first principal component is the direction along which the projected data has the largest variance.
Let be a unit vector representing a direction.
We want to choose the direction for which the projected points are spread out the most.

PCA chooses directions along which the projected data is as spread out as possible.
For a unit vector , the coordinate of a point along this direction is:

The scalar tells us how far the point lies along the direction .
The variance of the projected data is therefore proportional to:
So the first principal component is:
This objective can be written using the covariance matrix.
See derivation
Since is a scalar, its square can be written as .
Define:
Therefore:
Because the data has been centered, is the sample covariance matrix under this convention.
We now need to find the unit vector that maximizes:
subject to:
Using Lagrange optimization gives an eigenvalue problem.
See derivation
Define the Lagrangian:
Differentiate with respect to :
Setting the derivative equal to gives:
Therefore:
So every stationary direction is an eigenvector of the covariance matrix .
For an eigenvector , the corresponding eigenvalue is the variance of the data along that direction:
because:
The stationary directions are eigenvectors, but we still need to determine which eigenvector gives the largest variance.
Let:
be an orthonormal eigenbasis of , with eigenvalues:
See derivation
Any unit vector can be written as:
where:
Then:
Therefore, the variance is maximized by an eigenvector corresponding to the largest eigenvalue.
The first principal component is:
The second principal component is the orthogonal direction with the next largest variance:
and so on.
If the largest eigenvalue is repeated, any unit vector in its eigenspace is optimal.

The principal components are eigenvectors of the covariance matrix. The first points in the direction of greatest variance, while later components capture the largest remaining orthogonal directions of variation.
If we want to reduce the data to dimensions, we keep the top principal components:
A centered point is then represented by:
where:
is the lower-dimensional representation of .
In practice, we choose the eigenvectors corresponding to the largest eigenvalues.
These can be computed from an eigendecomposition of the covariance matrix or directly from the centered data using singular value decomposition.
Independent Component Analysis
Independent Component Analysis, or ICA, tries to recover independent signals that have been mixed together.
A classic example is the cocktail party problem: several people speak at the same time, and several microphones record different mixtures of their voices.
We represent the original sources by:
and the observed mixtures by:
The mixing process is:
where:
is the mixing matrix.
The matrix tells us how much of each source appears in each observed signal.
ICA tries to reverse this mixing.
After centering the observations, ICA learns an unmixing matrix such that:
If the model is correct:
and the recovered coordinates are as statistically independent as possible.

Several independent sources are mixed into the observed signals. ICA learns a transformation that tries to separate those mixtures back into the original sources.
The key assumption is that the original sources are statistically independent.
If source has density , independence means that the joint density factorizes:
So knowing one source does not provide information about the value of another.

Mixing transforms the independent source coordinates into new directions. ICA tries to recover the directions that make the transformed coordinates independent again.
Likelihood and Learning
To learn using likelihood, we need the probability density of the observed mixture .
Since:
we can use the change-of-variables formula.
Change of Variables in Probability
For an invertible transformation:
the density transforms as:
The determinant term accounts for how the transformation expands or contracts volume.
Using the factorized source density:
the likelihood of one observation becomes:
where is row of .
ICA needs a model for the distribution of each source.
One simple non-Gaussian choice is the logistic distribution, whose CDF is:
and whose density is:
We now choose so that the recovered coordinates are likely under these independent source distributions.
For centered observations:
the log-likelihood is:
The first term rewards recovered coordinates that look like samples from the assumed source distributions.
The log-determinant term comes from the change of variables and accounts for how the transformation changes volume.
To write the gradient compactly, define the recovered source vector:
and define:
Each component of is the derivative of with respect to .
Differentiating the likelihood gives the gradient.
See derivation
The sigmoid satisfies:
For one recovered coordinate:
Using .
Therefore, the source-density terms contribute:
For the determinant term:
Because the log-determinant appears once for each of the observations, its contribution is:
Therefore:
The batch gradient-ascent update is therefore:
or:
Instead of using all examples in every update, we can update using one example at a time.
For stochastic gradient ascent:
Limitations
Even with unlimited data, ICA cannot always recover the original sources uniquely.
Permutation: ICA can recover the sources, but not their original ordering.
Swapping two recovered sources gives an equally valid solution:
where is a permutation matrix.
Scale and sign: in generic ICA, the scale of a source cannot be determined uniquely.
Multiplying one recovered source by a nonzero constant can be compensated by changing the corresponding mixing coefficient.
A normalization convention can fix the magnitude, but the sign may still be ambiguous.
The fixed logistic source model used above removes the free scale ambiguity because the source scale has been specified in advance, although its symmetry still allows a sign flip.
Gaussian components: ICA relies on non-Gaussian structure to identify the mixing directions.
If several independent sources are Gaussian, rotations of those Gaussian sources can produce the same joint distribution.
Their individual directions therefore cannot be identified uniquely.
For this reason, standard ICA is identifiable only when at most one source is Gaussian.
After standardizing Gaussian source variances, rotations within their Gaussian subspace remain indistinguishable:
for an orthogonal transformation acting within that Gaussian subspace.

Non-Gaussian sources contain directional structure that can reveal the mixing directions. A Gaussian subspace is rotationally symmetric, so different rotations cannot be distinguished.
The likelihood above also assumes that the observations are IID.
If observations have important temporal dependence, as in time-series data, ICA requires additional modeling.