Python 如何使用Scikit-learn生成用于双聚类的数组
在本教程中,我们将学习如何使用Python的Scikit-learn(Sklearn)生成具有恒定块对角结构和块棋盘结构的数组以进行双聚类。
生成具有恒定块对角结构的数组
为了生成具有恒定块对角结构的双聚类数组,我们可以按照以下步骤进行:
步骤1 - 导入sklearn.datasets.make_biclusters和matplotlib。
步骤2 - 设置图形大小。
步骤3 - 创建数据点,即数据、行和列。
步骤4 - 创建一个绘图器来显示具有恒定块对角结构的数组。
步骤5 - 提供标题。
示例
在下面的示例中,我们将生成一个形状为(500, 500)且含有6个聚类的数组。
# Importing libraries
from sklearn.datasets import make_biclusters
# Matplotlib for plotting the array with constant diagonal structure
from matplotlib import pyplot as plt
# Set the figure size
plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True
# Creating the bi-cluster Test Datasets using sklearn.datasets.make_biclusters
data, rows, columns = make_biclusters(
shape=(500, 500), n_clusters=6, noise=5, shuffle=False, random_state=0
)
plt.matshow(data, cmap=plt.cm.Reds)
plt.title("An array\nwith constant block diagonal structure for biclustering")
plt.show()
输出
将产生以下输出−
生成具有块状棋盘结构的数组
为进行双聚类生成具有块状棋盘结构的数组,可以采取以下步骤−
步骤 1 −导入库 sklearn.datasets.make_checkerboard 和 matplotlib。
步骤 2 −设置图形大小。
步骤 3 −创建数据点,即 数据,行 和 列。
步骤 4 −创建一个绘图器以展示具有常量块对角线结构的数组。
步骤 5 −提供标题。
示例
在下面给出的示例中,我们将生成一个形状为(600, 600)的数组,其中聚类数为(4, 3)。
# Importing libraries
from sklearn.datasets import make_checkerboard
# Matplotlib for plotting the array with block chekerboard structure
from matplotlib import pyplot as plt
# Set the figure size
plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True
# Creating the Test Datasets using sklearn.datasets.make_checkerboard
n_clusters = (4, 3)
data, rows, columns = make_checkerboard(
shape=(600, 600), n_clusters=n_clusters, noise=10, shuffle=False, random_state=0
)
plt.matshow(data, cmap=plt.cm.Greens)
plt.title("An array\nwith block checkerboard structure for biclustering")
plt.show()
输出
它将产生以下输出: