小编use*_*922的帖子

sklearn 中使用自定义距离度量进行聚类

我正在尝试为聚类实现自定义距离度量。代码片段如下所示:

import numpy as np
from sklearn.cluster import KMeans, DBSCAN, MeanShift

def distance(x, y):
    # print(x, y) -> This x and y aren't one-hot vectors and is the source of this question
    match_count = 0.
    for xi, yi in zip(x, y):
        if float(xi) == 1. and xi == yi:
            match_count += 1
    return match_count

def custom_metric(x, y):
    # x, y are two vectors
    # distance(.,.) calculates count of elements when both xi and yi are True
    return distance(x, y)


vectorized_text …
Run Code Online (Sandbox Code Playgroud)

python cluster-analysis python-3.x scikit-learn

5
推荐指数
1
解决办法
4213
查看次数

如何在Tensorflow中更新2D张量的子集?

我想用值0更新2D张量中的索引。所以数据是2D张量,其第二行第二列索引值将被0代替。但是,我遇到类型错误。有人可以帮我吗?

TypeError:“ ScatterUpdate”操作的输入“ ref”需要输入左值

data = tf.Variable([[1,2,3,4,5], [6,7,8,9,0], [1,2,3,4,5]])
data2 = tf.reshape(data, [-1])
sparse_update = tf.scatter_update(data2, tf.constant([7]), tf.constant([0]))
#data = tf.reshape(data, [N,S])
init_op = tf.initialize_all_variables()

sess = tf.Session()
sess.run([init_op])
print "Values before:", sess.run([data])
#sess.run([updated_data_subset])
print "Values after:", sess.run([sparse_update])
Run Code Online (Sandbox Code Playgroud)

python neural-network deep-learning tensorflow

3
推荐指数
1
解决办法
4866
查看次数