Unsupervised learning is a type of machine learning where the model is trained on unlabeled data. Unlike supervised learning, there are no predefined outputs; the goal is to uncover hidden patterns, groupings, or structures within the data. Unsupervised learning is commonly used for exploratory data analysis and feature extraction. This chapter covers key unsupervised learning techniques, including clustering, dimensionality reduction, and association rule learning.

#### Clustering Techniques: k-Means, Hierarchical Clustering

**Clustering** is a technique used to group similar data points together. It is commonly used in market segmentation, image compression, and anomaly detection.

**k-Means Clustering**:

**k-Means**is one of the most popular clustering algorithms. It partitions the data into k clusters, with each data point assigned to the cluster with the nearest mean value.**Algorithm**:- Choose the number of clusters (k).
- Initialize k centroids randomly.
- Assign each data point to the nearest centroid.
- Recalculate the centroids based on the current cluster assignments.
- Repeat steps 3 and 4 until convergence.

**Example**:

```
from sklearn.cluster import KMeans
model = KMeans(n_clusters=3)
model.fit(X)
labels = model.predict(X)
```

**Hierarchical Clustering**:

**Hierarchical Clustering**creates a tree-like structure of clusters, known as a dendrogram. It can be agglomerative (bottom-up approach) or divisive (top-down approach).**Agglomerative Clustering**starts with each data point as its own cluster and merges the closest pairs of clusters iteratively until only one cluster remains.

**Example**:

```
from sklearn.cluster import AgglomerativeClustering
model = AgglomerativeClustering(n_clusters=3)
labels = model.fit_predict(X)
```

Hierarchical clustering is useful for cases where the number of clusters is not predefined, and it provides a visual representation of the cluster hierarchy.

#### Dimensionality Reduction: PCA, t-SNE

**Dimensionality Reduction** is the process of reducing the number of input variables or features in a dataset while retaining as much information as possible. This is particularly useful for visualizing high-dimensional data and speeding up machine learning algorithms.

**Principal Component Analysis (PCA)**:

**PCA**is a linear dimensionality reduction technique that transforms the data into a set of linearly uncorrelated components, ordered by the amount of variance they explain.**Key Concepts**:**Eigenvalues and Eigenvectors**: Used to identify the principal components.**Explained Variance Ratio**: Indicates how much variance is captured by each principal component.

**Example**:

```
from sklearn.decomposition import PCA
model = PCA(n_components=2)
X_reduced = model.fit_transform(X)
```

**t-Distributed Stochastic Neighbor Embedding (t-SNE)**:

**t-SNE**is a nonlinear dimensionality reduction technique that is particularly well-suited for visualizing high-dimensional data in 2 or 3 dimensions.**Key Concepts**:**Perplexity**: A parameter that balances attention between local and global aspects of the data.**Gradient Descent**: Used to minimize the difference between the input and output distances.

**Example**:

```
from sklearn.manifold import TSNE
model = TSNE(n_components=2, perplexity=30)
X_embedded = model.fit_transform(X)
```

t-SNE is often used for visualizing clusters or patterns in data, especially in applications like image and text analysis.

#### Association Rule Learning

**Association Rule Learning** is used to discover interesting relationships, patterns, or associations among a set of items in large datasets. It is commonly used in market basket analysis, where the goal is to identify products frequently bought together.

**Key Concepts**:

**Support**: The proportion of transactions that contain a particular itemset.**Confidence**: The likelihood of finding the consequent in transactions that contain the antecedent.**Lift**: The ratio of the observed support to that expected if the two rules were independent.

**Example**:

```
from mlxtend.frequent_patterns import apriori, association_rules
frequent_itemsets = apriori(df, min_support=0.1, use_colnames=True)
rules = association_rules(frequent_itemsets, metric="lift", min_threshold=1)
```

Association rule learning is valuable in retail, web usage mining, and recommendation systems.

Unsupervised learning techniques provide powerful tools for exploring and understanding data without predefined labels. Clustering, dimensionality reduction, and association rule learning are essential techniques that help uncover patterns, simplify data, and generate actionable insights.

##### Tags

#UnsupervisedLearning #Clustering #kMeans #HierarchicalClustering #DimensionalityReduction #PCA #tSNE #AssociationRuleLearning #MarketBasketAnalysis #MachineLearning #DataScience #DataExploration #PatternRecognition #FeatureExtraction