python – 使用Spearman与Sklearn KNN的相关性进行模式匹配

我试图使用包含在用户定义的度量中的Spearman相关性来查找Scikit-learn中的最近邻居.出于某种原因,它仅在我的训练数据中的列数为5且k = 5时才有效.对于任何其他组合(例如,列数= 8和k = 6),它会给我以下错误. (此处列车和测试装置有4列,k = 4).如果我使用Pearson进行相关,它的效果非常好.有谁知道为什么会发生这种情况或如何解决它?谢谢.

from scipy.stats import spearmanr
def spearmancorr(x,y):
    rho, pval = spearmanr(x,y, axis=0)
    return rho * (-1)

from sklearn.neighbors import NearestNeighbors
nbrs = NearestNeighbors(n_neighbors=4, algorithm='ball_tree', metric=spearmancorr)
nbrs.fit(train)
dist, ind = nbrs.kneighbors(test)

SystemError                               Traceback (most recent call last)
<ipython-input-11-f04b508b1263> in <module>()
      5 for i in range(1):
      6     nbrs = NearestNeighbors(n_neighbors=4, algorithm='ball_tree', metric=spearmancorr)
----> 7     nbrs.fit(train)
      8     dist, ind = nbrs.kneighbors(test)
      9     print "for: " + funcs[i]

C:\Users\AppData\Local\Enthought\Canopy\User\lib\site-packages\sklearn\neighbors\base.pyc in fit(self, X, y)
    797             or [n_samples, n_samples] if metric='precomputed'.
    798         """
--> 799         return self._fit(X)

C:\Users\AppData\Local\Enthought\Canopy\User\lib\site-packages\sklearn\neighbors\base.pyc in _fit(self, X)
    238             self._tree = BallTree(X, self.leaf_size,
    239                                   metric=self.effective_metric_,
--> 240                                   **self.effective_metric_params_)
    241         elif self._fit_method == 'kd_tree':
    242             self._tree = KDTree(X, self.leaf_size,

SystemError: NULL result without error in PyObject_Call

解决方法:

这似乎是在sklearn 0.14.1之前发生的错误.尝试升级到更高版本或最新的0.18.1版本.

请参阅问题#2878#3032.

上一篇:双数组Trie树(DoubleArrayTrie)Java实现


下一篇:python – 使用.corr获取两列之间的相关性