grid_to_graph#
- sklearn.feature_extraction.image.grid_to_graph(n_x, n_y, n_z=1, *, mask=None, return_as=<class 'scipy.sparse._coo.coo_matrix'>, dtype=<class 'int'>)[source]#
像素到像素连接的图。
如果两个体素连接,则存在边。
更多信息请参阅用户指南。
- 参数:
- n_xint
X轴的维度。
- n_yint
Y轴的维度。
- n_zint, 默认值=1
Z轴的维度。
- mask形状为 (n_x, n_y, n_z) 的 ndarray, dtype=bool, 默认值=None
图像的可选掩码,仅考虑部分像素。
- return_asnp.ndarray 或稀疏矩阵类,默认值=sparse.coo_matrix
用于构建返回的邻接矩阵的类。
- dtypedtype, 默认值=int
返回的稀疏矩阵的数据类型。默认情况下是int。
- 返回值:
- graphnp.ndarray 或稀疏矩阵类
计算出的邻接矩阵。
示例
>>> import numpy as np >>> from sklearn.feature_extraction.image import grid_to_graph >>> shape_img = (4, 4, 1) >>> mask = np.zeros(shape=shape_img, dtype=bool) >>> mask[[1, 2], [1, 2], :] = True >>> graph = grid_to_graph(*shape_img, mask=mask) >>> print(graph) <COOrdinate sparse matrix of dtype 'int64' with 2 stored elements and shape (2, 2)> Coords Values (0, 0) 1 (1, 1) 1