如何利用tf.contrib.learn.KMeansClustering在Tensorflow中实现K-均值文本聚类
float值工作,但我需要text的解决方案。代码:
hw_frame = pd.read_csv('./hw-data.txt', delim_whitespace=True,header=None,
names=['Index', 'Height'])
hw_frame.drop('Index', 1, inplace=True)
print (hw_frame.head(5))
def input_fn():
return tf.constant(hw_frame.as_matrix(), tf.float32, hw_frame.shape), None
tf.logging.set_verbosity(tf.logging.ERROR)
kmeans = tf.contrib.learn.KMeansClustering(num_clusters=2,
relative_tolerance=0.0001)
_ = kmeans.fit(input_fn=input_fn)
clusters = kmeans.clusters()
print(clusters)
assignments = list(kmeans.predict_cluster_idx(input_fn=input_fn))发布于 2017-08-10 06:44:34
这个问题很模糊,没有附加一些实际的输入,但我会尝试一下。
K-均值是一种对象聚类方法,这意味着为了形成聚类,需要在不同的对象之间建立一些有意义的“距离”度量。浮动“对象”可以通过子运算建立一个距离:取差值的绝对值,这就是距离。不幸的是,字符串不能有意义地被减除。
但并非一切都失去了,唉。输入单词嵌入到救援中,这可以有意义地将一个单词(字符串)嵌入到一个数字向量中,这样,相同的单词就会有相同的向量。同样,可以对相同维数(长度)的向量进行有意义的比较,以提取距离度量。
所以我想你需要做的是从网络上得到一个经过预先训练的word2vec模型,并把你的话通过它,然后把它们插入到csv中。另外,你也可以自己训练一个,但是除非你有大量的数据(清洁数据提醒你),否则我不会为这个选择而烦恼。
祝你好运
https://stackoverflow.com/questions/45606072
复制相似问题