简体   繁体   English

将稀疏矩阵拟合到kNN模型时出现MemoryError

[英]MemoryError while fitting a sparse matrix to kNN model

While running the below code I am having a MemoryError: from the last line. 在运行以下代码时,我在最后一行遇到了MemoryError:

from sklearn.neighbors import KNeighborsClassifier
clf = KNeighborsClassifier(n_neighbors=7)
clf.fit(train_X, y_train)
y_pred_clf = clf.predict(test_X)

The test_X is a <10852x112 sparse matrix of type '<class 'numpy.float64'>' with 97668 stored elements in Compressed Sparse Row format> test_X<10852x112 sparse matrix of type '<class 'numpy.float64'>' with 97668 stored elements in Compressed Sparse Row format><10852x112 sparse matrix of type '<class 'numpy.float64'>' with 97668 stored elements in Compressed Sparse Row format>

Any suggestions? 有什么建议么?

One way is to use batches of the data and the second one is to use a different algorithm for the KNN model: 一种方法是使用一批数据, 第二种方法是对KNN模型使用不同的算法:

clf = KNeighborsClassifier(n_neighbors=5,algorithm='kd_tree').fit(X_train, Y_train)
y_pred_clf = clf.predict(test_X)

The model by default is algorithm='brute' and brute false take too much memory. 默认情况下,该模型为algorithm ='brute',而蛮横的false占用过多内存。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM