分類
節點分類演算法。
屬性 標籤_
提供圖形中每個節點的標籤。
擴散
- 類別 sknetwork.classification.DiffusionClassifier(n_iter: int = 10, centering: bool = True, scale: float = 5)[來源碼]
節點分類透過熱傳遞。
對於每個標籤,節點的溫度對應於具有這個標籤的機率。
- 參數:
n_iter (int) – 傳輸的迭代次數(離散時間)。
centering (bool) – 如果
True
,在分類前將每個標籤的溫度移至其平均值(預設)。scale (float) – 在 softmax 前套用於溫度的乘法因子(預設 = 5)。僅在 centering 為
True
時使用。
- 變數:
labels (np.ndarray, shape (n_labels,)) – 節點的標籤。
probs (sparse.csr_matrix, shape (n_row, n_labels)) – 在標籤上的機率分配。
labels_row (np.ndarray) – 列的標籤,針對二部圖。
labels_col (np.ndarray) – 欄的標籤,針對二部圖。
probs_row (sparse.csr_matrix, shape (n_row, n_labels)) – 列標籤上的機率分配,針對二部圖。
probs_col (sparse.csr_matrix, shape (n_col, n_labels)) – 欄標籤上的機率分配,針對二部圖。
範例
>>> from sknetwork.data import karate_club >>> diffusion = DiffusionClassifier() >>> graph = karate_club(metadata=True) >>> adjacency = graph.adjacency >>> labels_true = graph.labels >>> labels = {0: labels_true[0], 33: labels_true[33]} >>> labels_pred = diffusion.fit_predict(adjacency, labels) >>> round(np.mean(labels_pred == labels_true), 2) 0.97
參考文件
Zhu, X., Lafferty, J., & Rosenfeld, R. (2005). Semi-supervised learning with graphs (Doctoral dissertation, Carnegie Mellon University, language technologies institute, school of computer science).
- fit(input_matrix: csr_matrix | ndarray, labels: ndarray | list | dict | None = None, labels_row: ndarray | list | dict | None = None, labels_col: ndarray | list | dict | None = None, force_bipartite: bool = False) DiffusionClassifier [source]
計算 Dirichlet 問題的解(平衡狀態下溫度)。
- 參數:
input_matrix (sparse.csr_matrix, np.ndarray) – 圖形的鄰接矩陣或雙鄰接矩陣。
labels (dict, np.ndarray) – 已知標籤(字典或整數向量)。負值標籤會被忽略。
labels_row (dict, np.ndarray) – 雙部分圖的列標籤。負值標籤會被忽略。
標籤_列(字典, np.ndarray)– 二部圖之列標籤。忽略負值。
強制二部(布林值)– 若
True
,則將輸入矩陣視為二鄰接矩陣(預設值 =False
)。
- 傳回值:
這個物件
- 傳回值型別:
- fit_predict(*args, **kwargs) ndarray
將演算法配適到資料,並傳回標籤。與
fit
方法的參數相同。- 傳回值:
標籤– 標籤。
- 傳回值型別:
np.ndarray
- fit_predict_proba(*args, **kwargs) ndarray
將演算法配適到資料,並傳回標籤的機率分配。與
fit
方法的參數相同。- 傳回值:
機率– 各標籤的機率。
- 傳回值型別:
np.ndarray
- fit_transform(*args, **kwargs) csr_matrix
將演算法配適到資料,並以稀疏格式傳回標籤的機率分配。與
fit
方法的參數相同。- 傳回值:
機率– 標籤的機率分配。
- 傳回值型別:
sparse.csr_matrix
- get_params()
以字典型態取得參數。
- 傳回值:
參數– 演算法的參數。
- 傳回值型別:
dict
- predict(columns: 布林值 = False) ndarray
回傳演算法預測的標籤。
- 參數:
columns (布林值) – 如果為
True
,則回傳欄位的預測值。- 傳回值:
標籤– 標籤。
- 傳回值型別:
np.ndarray
- predict_proba(columns=False) ndarray
回傳此演算法預測的標籤機率分布。
- 參數:
columns (布林值) – 如果為
True
,則回傳欄位的預測值。- 傳回值:
機率– 標籤的機率分配。
- 傳回值型別:
np.ndarray
- set_params(params: dict) 演算法
設定演算法參數。
- 參數:
params (dict) – 演算法參數。
- 傳回值:
這個物件
- 傳回值型別:
演算法
- transform(columns=False) csr_matrix
回傳稀疏格式的標籤機率分布。
- 參數:
columns (布林值) – 如果為
True
,則回傳欄位的預測值。- 傳回值:
機率– 標籤的機率分配。
- 傳回值型別:
sparse.csr_matrix
最近鄰居
- 類別 sknetwork.classification.NNClassifier(近鄰數: int = 3, 內嵌方法: BaseEmbedding | 無 = 無, 正規化: bool = 真實)[來源]
內嵌空間中透過 K 近鄰分類節點。
- 參數:
近鄰數 (int) – 近鄰數。
內嵌方法 (
BaseEmbedding
) – 內嵌方法用於表示向量空間中的節點。如果無
(預設值),使用身分。正規化 (bool) – 如果
真實
,套用正規化,使在內嵌空間中所有向量的範數為 1。
- 變數:
labels (np.ndarray, shape (n_labels,)) – 節點的標籤。
probs (sparse.csr_matrix, shape (n_row, n_labels)) – 在標籤上的機率分配。
labels_row (np.ndarray) – 列的標籤,針對二部圖。
labels_col (np.ndarray) – 欄的標籤,針對二部圖。
probs_row (sparse.csr_matrix, shape (n_row, n_labels)) – 列標籤上的機率分配,針對二部圖。
probs_col (sparse.csr_matrix, shape (n_col, n_labels)) – 欄標籤上的機率分配,針對二部圖。
範例
>>> from sknetwork.classification import NNClassifier >>> from sknetwork.data import karate_club >>> classifier = NNClassifier(n_neighbors=1) >>> graph = karate_club(metadata=True) >>> adjacency = graph.adjacency >>> labels_true = graph.labels >>> labels = {0: labels_true[0], 33: labels_true[33]} >>> labels_pred = classifier.fit_predict(adjacency, labels) >>> round(np.mean(labels_pred == labels_true), 2) 0.82
- fit(input_matrix: csr_matrix | ndarray, labels: ndarray | list | dict | None = None, labels_row: ndarray | list | dict | None = None, labels_col: ndarray | list | dict | None = None) NNClassifier [來源]
節點分類為在嵌入空間中的 k-最近鄰居。
- 參數:
input_matrix (sparse.csr_matrix, np.ndarray) – 圖形的鄰接矩陣或雙鄰接矩陣。
標籤 (np.ndarray, 字典) – 已知標籤。忽略負值。
labels_row (np.ndarray, 字典) – 二部圖中已知列標籤。
labels_col (np.ndarray, 字典) – 二部圖中已知欄標籤。
- 傳回值:
這個物件
- 傳回值型別:
KNN
- fit_predict(*args, **kwargs) ndarray
將演算法配適到資料,並傳回標籤。與
fit
方法的參數相同。- 傳回值:
標籤– 標籤。
- 傳回值型別:
np.ndarray
- fit_predict_proba(*args, **kwargs) ndarray
將演算法配適到資料,並傳回標籤的機率分配。與
fit
方法的參數相同。- 傳回值:
機率– 各標籤的機率。
- 傳回值型別:
np.ndarray
- fit_transform(*args, **kwargs) csr_matrix
將演算法配適到資料,並以稀疏格式傳回標籤的機率分配。與
fit
方法的參數相同。- 傳回值:
機率– 標籤的機率分配。
- 傳回值型別:
sparse.csr_matrix
- get_params()
以字典型態取得參數。
- 傳回值:
參數– 演算法的參數。
- 傳回值型別:
dict
- predict(columns: bool = False) ndarray
回傳演算法預測的標籤。
- 參數:
columns (布林值) – 如果為
True
,則回傳欄位的預測值。- 傳回值:
標籤– 標籤。
- 傳回值型別:
np.ndarray
- predict_proba(columns=False) ndarray
回傳此演算法預測的標籤機率分布。
- 參數:
columns (布林值) – 如果為
True
,則回傳欄位的預測值。- 傳回值:
機率– 標籤的機率分配。
- 傳回值型別:
np.ndarray
- set_params(params: dict) Algorithm
設定演算法參數。
- 參數:
params (dict) – 演算法參數。
- 傳回值:
這個物件
- 傳回值型別:
演算法
- transform(columns=False) csr_matrix
回傳稀疏格式的標籤機率分布。
- 參數:
columns (布林值) – 如果為
True
,則回傳欄位的預測值。- 傳回值:
機率– 標籤的機率分配。
- 傳回值型別:
sparse.csr_matrix
傳遞
- 類別 sknetwork.classification.Propagation(n_iter: float = -1, node_order: str | None = None, weighted: bool = True)[來源]
對標籤傳遞進行節點分類。
- 參數:
n_iter (float) – 最大迭代次數(-1 表示無限次)。
node_order (str) –
'random'
:節點標籤會以隨機順序更新。'increasing'
:節點標籤會依據(in)權重遞增順序更新。'decreasing'
:節點標籤會依據(in)權重遞減順序更新。否則,節點標籤會依據索引順序更新。
weighted (bool) – 若
True
,每個鄰點的表決權重會與邊緣權重成正比。否則,所有表決權重都為 1。
- 變數:
labels (np.ndarray, shape (n_labels,)) – 節點的標籤。
probs (sparse.csr_matrix, shape (n_row, n_labels)) – 在標籤上的機率分配。
labels_row (np.ndarray) – 列的標籤,針對二部圖。
labels_col (np.ndarray) – 欄的標籤,針對二部圖。
probs_row (sparse.csr_matrix, shape (n_row, n_labels)) – 列標籤上的機率分配,針對二部圖。
probs_col (sparse.csr_matrix, shape (n_col, n_labels)) – 欄標籤上的機率分配,針對二部圖。
範例
>>> from sknetwork.classification import Propagation >>> from sknetwork.data import karate_club >>> propagation = Propagation() >>> graph = karate_club(metadata=True) >>> adjacency = graph.adjacency >>> labels_true = graph.labels >>> labels = {0: labels_true[0], 33: labels_true[33]} >>> labels_pred = propagation.fit_predict(adjacency, labels) >>> np.round(np.mean(labels_pred == labels_true), 2) 0.94
參考文件
Raghavan, U. N., Albert, R., & Kumara, S. (2007). 近似線性時間演算法用於偵測大型網路社群結構。 Physical review E, 76(3), 036106.
- fit(input_matrix: csr_matrix | ndarray, labels: ndarray | list | dict | None = None, labels_row: ndarray | list | dict | None = None, labels_col: ndarray | list | dict | None = None) Propagation [source]
對標籤傳遞進行節點分類。
- 參數:
input_matrix (sparse.csr_matrix, np.ndarray) – 圖形的鄰接矩陣或雙鄰接矩陣。
標籤(陣列、清單或字典)– 已知的標籤。忽略負值。
標籤_列(陣列、清單或字典)– 二部圖中已知列標籤。
標籤_行(陣列、清單或字典)– 二部圖中已知行標籤。
- 傳回值:
這個物件
- 傳回值型別:
- fit_predict(*args, **kwargs) ndarray
將演算法配適到資料,並傳回標籤。與
fit
方法的參數相同。- 傳回值:
標籤– 標籤。
- 傳回值型別:
np.ndarray
- fit_predict_proba(*args, **kwargs) ndarray
將演算法配適到資料,並傳回標籤的機率分配。與
fit
方法的參數相同。- 傳回值:
機率– 各標籤的機率。
- 傳回值型別:
np.ndarray
- fit_transform(*args, **kwargs) csr_matrix
將演算法配適到資料,並以稀疏格式傳回標籤的機率分配。與
fit
方法的參數相同。- 傳回值:
機率– 標籤的機率分配。
- 傳回值型別:
sparse.csr_matrix
- get_params()
以字典型態取得參數。
- 傳回值:
參數– 演算法的參數。
- 傳回值型別:
dict
- predict(columns: bool = False) ndarray
回傳演算法預測的標籤。
- 參數:
columns (布林值) – 如果為
True
,則回傳欄位的預測值。- 傳回值:
標籤– 標籤。
- 傳回值型別:
np.ndarray
- predict_proba(columns=False) ndarray
回傳此演算法預測的標籤機率分布。
- 參數:
columns (布林值) – 如果為
True
,則回傳欄位的預測值。- 傳回值:
機率– 標籤的機率分配。
- 傳回值型別:
np.ndarray
- set_params(params: dict) Algorithm
設定演算法參數。
- 參數:
params (dict) – 演算法參數。
- 傳回值:
這個物件
- 傳回值型別:
演算法
- transform(columns=False) csr_matrix
回傳稀疏格式的標籤機率分布。
- 參數:
columns (布林值) – 如果為
True
,則回傳欄位的預測值。- 傳回值:
機率– 標籤的機率分配。
- 傳回值型別:
sparse.csr_matrix
PageRank
- 類別 sknetwork.classification.PageRankClassifier(阻尼係數: 浮點數 = 0.85, 解算器: 字串 = 'piteration', 反覆次數: 整數 = 10, 容許誤差: 浮點數 = 0.0, 工作執行緒: 整數 | 無 = 無, 詳細資料: 布林值 = 錯誤)[來源]
透過多個個人化 PageRank 進行節點分類。
- 參數:
阻尼係數 (浮點數) – 繼續隨機漫步的機率。
解算器 (字串) – 要使用的解算器:‘piteration’、‘diteration’、‘bicgstab’、‘lanczos’。
反覆次數 (整數) – 一些解算器的反覆次數,例如
'piteration'
或'diteration'
。容許誤差 (浮點數) – 一些解算器的收斂容許誤差,例如
'bicgstab'
或'lanczos'
。
- 變數:
labels (np.ndarray, shape (n_labels,)) – 節點的標籤。
probs (sparse.csr_matrix, shape (n_row, n_labels)) – 在標籤上的機率分配。
labels_row (np.ndarray) – 列的標籤,針對二部圖。
labels_col (np.ndarray) – 欄的標籤,針對二部圖。
probs_row (sparse.csr_matrix, shape (n_row, n_labels)) – 列標籤上的機率分配,針對二部圖。
probs_col (sparse.csr_matrix, shape (n_col, n_labels)) – 欄標籤上的機率分配,針對二部圖。
範例
>>> from sknetwork.classification import PageRankClassifier >>> from sknetwork.data import karate_club >>> pagerank = PageRankClassifier() >>> graph = karate_club(metadata=True) >>> adjacency = graph.adjacency >>> labels_true = graph.labels >>> labels = {0: labels_true[0], 33: labels_true[33]} >>> labels_pred = pagerank.fit_predict(adjacency, labels) >>> np.round(np.mean(labels_pred == labels_true), 2) 0.97
參考文件
Lin, F., & Cohen, W. W. (2010). 使用極少數標籤的網路資料半監督分類。 在 IEEE 社群網路分析與探勘國際會議。
- fit(input_matrix: csr_matrix | ndarray, labels: ndarray | dict | None = None, labels_row: ndarray | dict | None = None, labels_col: ndarray | dict | None = None) RankClassifier
調整資料演算法。
- 參數:
input_matrix – 圖形的鄰接矩陣或雙鄰接矩陣。
labels – 已知標籤(字典或陣列;忽略負值)。
labels_row – 已知行和列標籤(雙部圖形用)。
labels_col – 已知行和列標籤(雙部圖形用)。
- 傳回值:
這個物件
- 傳回值型別:
RankClassifier
- fit_predict(*args, **kwargs) ndarray
將演算法配適到資料,並傳回標籤。與
fit
方法的參數相同。- 傳回值:
標籤– 標籤。
- 傳回值型別:
np.ndarray
- fit_predict_proba(*args, **kwargs) ndarray
將演算法配適到資料,並傳回標籤的機率分配。與
fit
方法的參數相同。- 傳回值:
機率– 各標籤的機率。
- 傳回值型別:
np.ndarray
- fit_transform(*args, **kwargs) csr_matrix
將演算法配適到資料,並以稀疏格式傳回標籤的機率分配。與
fit
方法的參數相同。- 傳回值:
機率– 標籤的機率分配。
- 傳回值型別:
sparse.csr_matrix
- get_params()
以字典型態取得參數。
- 傳回值:
參數– 演算法的參數。
- 傳回值型別:
dict
- predict(columns: bool = False) ndarray
回傳演算法預測的標籤。
- 參數:
columns (布林值) – 如果為
True
,則回傳欄位的預測值。- 傳回值:
標籤– 標籤。
- 傳回值型別:
np.ndarray
- predict_proba(columns=False) ndarray
回傳此演算法預測的標籤機率分布。
- 參數:
columns (布林值) – 如果為
True
,則回傳欄位的預測值。- 傳回值:
機率– 標籤的機率分配。
- 傳回值型別:
np.ndarray
- set_params(params: dict) Algorithm
設定演算法參數。
- 參數:
params (dict) – 演算法參數。
- 傳回值:
這個物件
- 傳回值型別:
演算法
- transform(columns=False) csr_matrix
回傳稀疏格式的標籤機率分布。
- 參數:
columns (布林值) – 如果為
True
,則回傳欄位的預測值。- 傳回值:
機率– 標籤的機率分配。
- 傳回值型別:
sparse.csr_matrix
測量標準
- sknetwork.classification.get_accuracy_score(labels_true: ndarray, labels_pred: ndarray) float [原始碼]
傳回正確標記的樣本比例。忽略負面標記。
- 參數:
labels_true (np.ndarray) – 正確標記。
labels_pred (np.ndarray) – 預測標記。
- 傳回值:
accuracy – 0 到 1 之間的分數。
- 傳回值型別:
float
範例
>>> import numpy as np >>> labels_true = np.array([0, 0, 1, 1]) >>> labels_pred = np.array([0, 0, 0, 1]) >>> round(get_accuracy_score(labels_true, labels_pred), 2) 0.75
- sknetwork.classification.get_f1_score(labels_true: ndarray, labels_pred: ndarray, return_precision_recall: bool = False) float | Tuple[float, float, float] [原始碼]
傳回二元分類的 f1 分數。忽略負面標記。
- 參數:
labels_true (np.ndarray) – 正確標記。
labels_pred (np.ndarray) – 預測標記。
return_precision_recall (bool) – 若為
True
,也會傳回準確度與召回率。
- 傳回值:
score, [precision, recall] – F1 分數(介於 0 到 1 之間)。選擇性地,也會傳回準確度與召回率。
- 傳回值型別:
np.ndarray
範例
>>> import numpy as np >>> labels_true = np.array([0, 0, 1, 1]) >>> labels_pred = np.array([0, 0, 0, 1]) >>> round(get_f1_score(labels_true, labels_pred), 2) 0.67
- sknetwork.classification.get_f1_scores(labels_true: ndarray, labels_pred: ndarray, return_precision_recall: bool = False) ndarray | Tuple[ndarray, ndarray, ndarray] [source]
傳回多標籤分類的 f1 分數(每個標籤一個)。忽略負標籤。
- 參數:
labels_true (np.ndarray) – 正確標記。
labels_pred (np.ndarray) – 預測標記。
return_precision_recall (bool) – 如果
True
,還會傳回準確率與召回率。
- 傳回值:
scores, [precisions, recalls] – F1 分數(介於 0 與 1 之間)。選擇性地,也會傳回 F1 準確率與召回率。
- 傳回值型別:
np.ndarray
範例
>>> import numpy as np >>> labels_true = np.array([0, 0, 1, 1]) >>> labels_pred = np.array([0, 0, 0, 1]) >>> np.round(get_f1_scores(labels_true, labels_pred), 2) array([0.8 , 0.67])
- sknetwork.classification.get_average_f1_score(labels_true: ndarray, labels_pred: ndarray, average: str = 'macro') float [source]
傳回多標籤分類的平均 f1 分數。忽略負標籤。
- 參數:
labels_true (np.ndarray) – 正確標記。
labels_pred (np.ndarray) – 預測標記。
average (str) – 平均方法。可以是
'macro'
(預設)、'micro'
或'weighted'
。
- 傳回值:
score – 平均 F1 分數(介於 0 與 1 之間)。
- 傳回值型別:
float
範例
>>> import numpy as np >>> labels_true = np.array([0, 0, 1, 1]) >>> labels_pred = np.array([0, 0, 0, 1]) >>> round(get_average_f1_score(labels_true, labels_pred), 2) 0.73
- sknetwork.classification.取得混淆矩陣(實際標籤: NumPy 陣列, 預測標籤: NumPy 陣列) 稀疏矩陣 [來源]
以稀疏矩陣格式傳回混淆矩陣 (實際標籤在列,預測標籤在欄)。忽略負標籤。
- 參數:
labels_true (np.ndarray) – 正確標記。
labels_pred (np.ndarray) – 預測標記。
- 傳回值:
混淆矩陣 – 混淆矩陣。
- 傳回值型別:
sparse.csr_matrix
範例
>>> import numpy as np >>> labels_true = np.array([0, 0, 1, 1]) >>> labels_pred = np.array([0, 0, 0, 1]) >>> get_confusion_matrix(labels_true, labels_pred).toarray() array([[2, 0], [1, 1]])