我有一个带有8 GB RAM和Intel Core i5处理器的Lenovo IdeaPad笔记本电脑。我每100个尺寸都有60k数据点。我想做knn,为此,我正在运行LMNN算法找到一个Mahalanobis Metric。
问题是在我的Ubuntu上出现了2个小时的运行空白屏幕。我没有得到什么问题!我的记忆是否已满?
因此,有什么方法可以优化我的代码吗?
我的数据集:数据
我的LMNN实现:
import numpy as np
import sys
from modshogun import LMNN, RealFeatures, MulticlassLabels
from sklearn.datasets import load_svmlight_file
def main():
# Get training file name from the command line
traindatafile = sys.argv[1]
# The training file is in libSVM format
tr_data = load_svmlight_file(traindatafile);
Xtr = tr_data[0].toarray(); # Converts sparse matrices to dense
Ytr = tr_data[1]; # The trainig labels
# Cast data to Shogun format to work with LMNN
features = RealFeatures(Xtr.T)
labels = MulticlassLabels(Ytr.astype(np.float64))
# Number of target neighbours per example - tune this using validation
k = 18
# Initialize the LMNN package
lmnn = LMNN(features, labels, k)
init_transform = np.eye(Xtr.shape[1])
# Choose an appropriate timeout
lmnn.set_maxiter(200000)
lmnn.train(init_transform)
# Let LMNN do its magic and return a linear transformation
# corresponding to the Mahalanobis metric it has learnt
L = lmnn.get_linear_transform()
M = np.matrix(np.dot(L.T, L))
# Save the model for use in testing phase
# Warning: do not change this file name
np.save("model.npy", M)
if __name__ == '__main__':
main()
精确的k-nn具有可扩展性问题。
Scikit-learn具有有关在这种情况下要做什么的文档页面(缩放策略)(许多算法具有partial_fit
方法,但不幸的是KNN没有它)。
如果您接受一些准确性以换取速度,则可以运行大约最近的邻居之类的东西。