UnsupervisedLearning

  • Supervised Learning:

    • Learn from data labeled with the "right answers".
    • 从带有“正确答案”的有标签数据中进行学习。
  • Unsupervised Learning:

    • Find something interesting in unlabeled data.
    • 无标签数据中找到有趣的模式。

这是机器学习中两种主要学习方式的简明描述。监督学习依赖于已标注的数据来学习,而无监督学习则在未标注的数据中找寻规律结构

Clustering

  • "This is a particular type of unsupervised learning, called a clustering algorithm"
  • “这是一种特殊类型的无监督学习,称为聚类算法。”

本质就是:Group similar data points together

  1. 新闻中的关键词提取(Keyword Extraction)
    • 通过无监督学习的方法(例如聚类),可以从大量新闻文本中提取出代表性的关键词。关键词提取是利用文本数据中隐藏的模式,找到最能代表文章主题的词汇。
import matplotlib.pyplot as plt
from wordcloud import WordCloud

# 示例文本数据:模拟新闻文本的关键词提取
text = """
economy growth market inflation interest rate GDP government investment unemployment policy industry technology 
finance trade banking innovation regulation energy production export import housing agriculture services stock consumer 
demand supply business revenue tax budget stimulus crisis development jobs employment recession recovery wages salary 
"""

# 生成词云
wordcloud = WordCloud(width=800, height=400, background_color='white', colormap='viridis').generate(text)

# 绘制词云图
plt.figure(figsize=(10, 6))
plt.imshow(wordcloud, interpolation='bilinear')
plt.axis('off')
plt.title('Keyword Extraction from News Articles (Unsupervised Learning Example)', fontsize=16)
plt.show()

png

如上图:能清晰的观察到无监督学习获取的关键词,能精准的定位某个时间内的新闻文本大概会讲些什么

  1. DNA序列相似度(DNA Sequence Similarity)
    • 使用无监督学习的模式检测方法,可以对不同的 DNA 序列进行比较,从中找到相似性。无监督学习可以帮助发现不同 DNA 片段之间的相似模式,这对基因分析和生物信息学非常重要。
import matplotlib.pyplot as plt
import numpy as np

# 定义两段 DNA 序列的相似性矩阵(随机生成用于示例)
np.random.seed(42)
dna_sequences = ['Sequence A', 'Sequence B', 'Sequence C', 'Sequence D']
similarity_matrix = np.random.rand(4, 4)

# 使矩阵对称,表示对称的相似性
similarity_matrix = (similarity_matrix + similarity_matrix.T) / 2
np.fill_diagonal(similarity_matrix, 1)  # 对角线设为 1,表示与自身完全相似

# 绘制热力图展示 DNA 序列相似度
plt.figure(figsize=(8, 6))
plt.imshow(similarity_matrix, cmap='viridis', interpolation='nearest')
plt.colorbar(label='Similarity Score')
plt.xticks(ticks=np.arange(len(dna_sequences)), labels=dna_sequences)
plt.yticks(ticks=np.arange(len(dna_sequences)), labels=dna_sequences)
plt.title('DNA Sequence Similarity Heatmap', fontsize=16)
plt.xlabel('DNA Sequences', fontsize=12)
plt.ylabel('DNA Sequences', fontsize=12)

# 为每个矩阵单元添加数值标注
for i in range(len(dna_sequences)):
    for j in range(len(dna_sequences)):
        plt.text(j, i, f"{similarity_matrix[i, j]:.2f}", ha='center', va='center', color='white')

plt.show()

png

如上图:能清晰的观察到无监督学习通过DNA片段的相似度,分为来多个区块,快速的区分各个区块的关系

举个例子:有很多人我们通过对他们的特征映射到二维坐标轴中,形成了下面的图

  • X轴是特征的映射坐标
  • Y轴是特征的映射坐标

我们并不给予映射具体含义,实际的机器学习中,每一个映射轴一定是有含义

# 绘制没有分区的原始数据点分布图
plt.figure(figsize=(10, 6))

# 绘制原始数据点,所有点用相同的颜色和标记
plt.scatter(data_points[:, 0], data_points[:, 1], s=100, color='gray', edgecolor='black', alpha=0.6)

# 图表设置
plt.xlabel('X Axis', fontsize=12)
plt.ylabel('Y Axis', fontsize=12)
plt.title('Original Data Points Distribution', fontsize=16)
plt.grid(True, linestyle='--', alpha=0.7)
plt.show()

png

无监督学习最后会将上面的图中的点 , 分类聚合为几个Cluster(集合,簇)

import matplotlib.pyplot as plt
from sklearn.cluster import KMeans
import numpy as np

# 定义数据点,根据图片中的小人位置大致估算坐标
data_points = np.array([
    [2, 8], [3, 8], [2.5, 7.5], [3.5, 7], [2.2, 7.8], [3, 7.5],
    [6, 9], [7, 8.5], [6.5, 8.8], [7.5, 9], [6.8, 8.7], [7.2, 9.1],
    [9, 4], [9.5, 3.5], [10, 4.5], [9.8, 4.2],
    [3, 2], [2.5, 1.5], [3.5, 1.8], [3.2, 2.2]
])

# 使用 KMeans 聚类
kmeans = KMeans(n_clusters=4, random_state=42)
kmeans.fit(data_points)
labels = kmeans.labels_
centroids = kmeans.cluster_centers_

# 绘制聚类结果
plt.figure(figsize=(10, 6))

# 绘制不同簇的数据点
colors = ['red', 'blue', 'green', 'orange']
for i in range(4):
    cluster_points = data_points[labels == i]
    plt.scatter(cluster_points[:, 0], cluster_points[:, 1], s=100, color=colors[i], label=f'Cluster {i+1}', alpha=0.6)

# 绘制质心
plt.scatter(centroids[:, 0], centroids[:, 1], s=200, c='black', marker='X', label='Centroids')

# 图表设置
plt.xlabel('X Axis', fontsize=12)
plt.ylabel('Y Axis', fontsize=12)
plt.title('KMeans Clustering of Data Points', fontsize=16)
plt.legend()
plt.grid(True, linestyle='--', alpha=0.7)
plt.show()

png

总的来说:无监督学习算法获取没有标签的数据并尝试自动将它们分到集群中。

Anomaly detection

  • Find unusual data points

Dimensionality reduction

  • Compress data using fewer numbers
  • 将大数据集压缩为一个小数据集(降维度)

Anomaly detection 和 Dimensionality reductio后续会单独写一份笔记