【发布时间】:2015-08-27 19:07:51
【问题描述】:
我正在处理一个中等大小的数据集 (shape=(14013L, 46L))。
我想用knn 平滑每个样本。
我正在训练我的模型:
NearestNeighbors(n_neighbors, algorithm='ball_tree',
metric=sklearn.metrics.pairwise.cosine_distances)
平滑如下:
def smooth(x,nbrs,data,alpha):
"""
input:
alpha: the smoothing factor
nbrs: trained NearestNeighbors from sklearn
data: the original data
(since NearestNeighbors returns only the index and not the samples)
x: what we want to smooth
output:
smoothed x with its nearest neighbours
"""
distances, indices = nbrs.kneighbors(x)
distances = map(lambda z:abs(-z+1),distances)[0]
norm = sum(distances)
if norm == 0:
"No neighbours were found."
return x
distances = map(lambda z: (1-alpha)*z/norm ,distances)
indices = map(lambda z: data[z],indices)[0]
other = np.array([indices[i] * distances[i] for i in range(len(distances))])
z = x * alpha
z = z.reshape((1,z.shape[0]))
smoothed = sum(np.concatenate((other,z),axis=0))
return smoothed
我的问题:
- 怎么可能找不到邻居?(我在我的数据集上遇到过这种情况,因此出现了
if条件) - 拟合(“训练”)需要 18 秒,但平滑约 1000 个样本需要 20 多分钟。如果平滑过程更短,我愿意得到不太准确的结果。可能吗?为了实现它,我应该更改哪个parameters?
【问题讨论】:
-
我不知道knn在Python中是怎么用的,但是你在哪里传参数呢?还是您允许他们获取默认值?
-
@gsamaras 看到第一个代码块 -
NearestNeighbors的初始化 -
我已经看过了。但是,我可以假设您使用的是默认参数,对吧?
-
@gsamaras 是的,任何未说明的参数都被赋予其默认值,您可以在documentation中看到这些
-
是的,我看到了链接,谢谢,好问题顺便说一句,+1。
标签: python scikit-learn smooth nearest-neighbor