sklearn.neighbors.NeighborhoodComponentsAnalysis?
class sklearn.neighbors.NeighborhoodComponentsAnalysis(n_components=None, *, init='auto', warm_start=False, max_iter=50, tol=1e-05, callback=None, verbose=0, random_state=None)
[源碼]
鄰域成分分析
鄰域成分分析(NCA)是一種用于度量學習的機器學習算法。 它以監督方式學習線性變換,以提高變換空間中隨機最近鄰規則的分類精度。
在用戶指南中閱讀更多內容。
參數 | 說明 |
---|---|
n_components | int, default=None 投影空間的首選尺寸。 如果為None,它將設置為n_features。 |
init | {‘auto’, ‘pca’, ‘lda’, ‘identity’, ‘random’} or ndarray of shape (n_features_a, n_features_b), default=’auto’ 線性變換的初始化。 可能的選項是“自動”,“ pca”,“ lda”,“identity”,“random”和形狀為numpy的數組(n_features_a,n_features_b)。 ‘auto’ 根據n_components,將選擇最合理的初始化。 如果n_components <= n_classes個,我們使用“ lda”,因為它使用標簽信息。 如果不是,但n_components <min(n_features,n_samples),則我們使用“ pca”,因為它將數據投影到有意義的方向(方差較大的方向)。 否則,我們僅使用“身份”。 ‘pca’ 傳遞給fit的輸入的n_components主成分將用于初始化轉換。 (請參閱PCA] ‘lda’ min(n_components,n_classes)傳遞給fit的輸入的大多數判別分量將用于初始化轉換。 (如果n_components> n_classes,則其余組件將為零。)(請參閱LinearDiscriminantAnalysis) ‘identity’ 如果n_components嚴格小于傳遞給fit的輸入的維數,則單位矩陣將被截斷為前n_components行 ‘random’ 初始轉換將是形狀的隨機數組(n_components,n_features)。 每個值均從標準正態分布中采樣。 numpy array n_features_b必須匹配傳遞給fit的輸入的維數,并且n_features_a必須小于或等于該值。 如果n_components不為None,則n_features_a必須匹配。* |
warm_start | bool, default=False 如果之前已調用True和fit,則將先前調用fit的解用作初始線性變換(n_components和init將被忽略)。 |
max_iter | int, default=50 優化中的最大迭代次數。 |
tol | float, default=1e-5 優化的收斂容限。 |
callback | callable, default=None 如果不為None,則在優化程序的每次迭代之后都將調用此函數,并以當前解(展平的變換矩陣)和迭代次數為參數。 如果要檢查或存儲每次迭代后發現的轉換,這可能很有用。 |
verbose | int, default=0 如果為0,則不會打印任何進度消息。 如果為1,則將進度消息打印到標準輸出。 如果> 1,則將打印進度消息,并將scipy.optimize.minimize的disp參數設置為詳細-2。 |
random_state | int or numpy.RandomState, default=None 偽隨機數生成器對象或種子(如果為int)。 如果init ='random',則random_state用于初始化隨機變換。 如果init ='pca',則在初始化轉換時將random_state作為參數傳遞給PCA。 在多個函數調用之間傳遞int以獲得可重復的結果。 參見:term:詞匯表<random_state>。 |
屬性 | 說明 |
---|---|
components_ | ndarray of shape (n_components, n_features) 擬合期間學習到的線性變換。 |
n_iter_ | int 計算優化程序執行的迭代次數。 |
random_state_ | numpy.RandomState 初始化期間使用的偽隨機數生成器對象。 |
參考資料
J. Goldberger, G. Hinton, S. Roweis, R. Salakhutdinov. “Neighbourhood Components Analysis”. Advances in Neural Information Processing Systems. 17, 513-520, 2005. http://www.cs.nyu.edu/~roweis/papers/ncanips.pdf
Wikipedia entry on Neighborhood Components Analysis https://en.wikipedia.org/wiki/Neighbourhood_components_analysis
示例
>>> from sklearn.neighbors import KNeighborsClassifier
>>> from sklearn.datasets import load_iris
>>> from sklearn.model_selection import train_test_split
>>> X, y = load_iris(return_X_y=True)
>>> X_train, X_test, y_train, y_test = train_test_split(X, y,
... stratify=y, test_size=0.7, random_state=42)
>>> nca = NeighborhoodComponentsAnalysis(random_state=42)
>>> nca.fit(X_train, y_train)
NeighborhoodComponentsAnalysis(...)
>>> knn = KNeighborsClassifier(n_neighbors=3)
>>> knn.fit(X_train, y_train)
KNeighborsClassifier(...)
>>> print(knn.score(X_test, y_test))
0.933333...
>>> knn.fit(nca.transform(X_train), y_train)
KNeighborsClassifier(...)
>>> print(knn.score(nca.transform(X_test), y_test))
0.961904...
方法
方法 | 說明 |
---|---|
fit (X, y) |
根據給定的訓練數據擬合模型。 |
fit_transform (X[, y]) |
適合數據,然后對其進行轉換。 |
get_params ([deep]) |
獲取此估計量的參數。 |
set_params (**params) |
設置此估算器的參數。 |
transform (X) |
將學習到的轉換應用于給定數據。 |
__init__(n_components=None, *, init='auto', warm_start=False, max_iter=50, tol=1e-05, callback=None, verbose=0, random_state=None)
[源碼]
初始化, 請參閱help(type())以獲得準確的說明
fit(X, y)
[源碼]
根據給定的訓練數據擬合模型。
參數 | 說明 |
---|---|
X | array-like of shape (n_samples, n_features) 訓練樣本 |
y | array-like of shape (n_samples,) 相應的訓練標簽 |
返回值 | 說明 |
---|---|
self | object 返回訓練有素的NeighborhoodComponentsAnalysis模型。 |
fit_transform(X, y=None, **fit_params)
[源碼]
適合數據,然后對其進行轉換。
使用可選參數fit_params將轉換器擬合到X和y,并返回X的轉換版本。
參數 | 說明 |
---|---|
X | {array-like, sparse matrix, dataframe} of shape (n_samples, n_features) |
y | ndarray of shape (n_samples,), default=None 目標值。 |
**fit_params | dict 其他擬合參數 |
返回值 | 說明 |
---|---|
X_new | ndarray array of shape (n_samples, n_features_new) 轉換后的數組。 |
get_params(deep=True)
[源碼]
獲取此估計量的參數
參數 | 說明 |
---|---|
deep | bool, default=True 如果為True,則將返回此估算器和作為估算器的所包含子對象的參數。 |
返回值 | 說明 |
---|---|
params | mapping of string to any 參數名稱映射到其值。 |
set_params(**params)
[源碼]
設置此估算器的參數。
該方法適用于簡單的估計器以及嵌套對象(例如管道)。 后者的參數格式為
參數 | 說明 |
---|---|
**params | dict 估算器參數 |
返回值 | 說明 |
---|---|
self | object 估算器實例。 |
transform(X)
[源碼]
將學習到的轉換應用于給定數據。
參數 | 說明 |
---|---|
X | array-like of shape (n_samples, n_features) 數據樣本 |
返回值 | 說明 |
---|---|
X_embedded | 形狀的ndarray(n_samples,n_components) 數據樣本已轉換。 |
提出:
NotFittedError
如果之前尚未調用過fit
。