【问题标题】:scikit learn Type error only integer arrays with one element can be converted to an indexscikit learn 类型错误只有一个元素的整数数组可以转换为索引
【发布时间】:2016-08-09 12:21:30
【问题描述】:

我在调用 cosine_similarity 时收到以下错误

numerator = sum(a*b for a,b in zip(x,y))
TypeError: only integer arrays with one element can be converted to an index

我正在尝试从 CountVectorizer 返回的文档关键字矩阵中获取关键字关键字共现矩阵。

我觉得cosine_similarity 不喜欢我传递它的数据类型,但我不确定到底是什么问题。这里,nscipy.sparse.csc.csc_matrix 类型,yscipy.sparse.csr.csr_matrix 类型

documents = (
    "The sky is blue",
    "The sun is bright",
    "The sun in the sky is bright",
    "We can see the shining sun, the bright sun"
)

countvectorizer = CountVectorizer()
y =  countvectorizer.fit_transform(documents)
n  = y.T.dot(y) 
x = n.tocsr()
x = x.toarray()
numpy.fill_diagonal(x, 0) 

result = cosine_similarity(x, "None")

【问题讨论】:

  • 首先猜测是稀疏表示会导致问题,您是否尝试将矩阵转换为非稀疏表示?
  • @ncfirth 非稀疏是指 numpy 数组?
  • 是的[更多字符]
  • @ncfirth 嗨..但这就是我在点积运算后所做的(使用 toarray() 将矩阵转换为数组)
  • 您能提供一些示例数据吗?

标签: python scikit-learn cosine-similarity


【解决方案1】:

使用sklearncosine_similarity,此代码段运行并返回一个合理的答案。

from sklearn.feature_extraction.text import CountVectorizer
from sklearn.metrics.pairwise import distance_metrics

documents = (
    "The sky is blue",
    "The sun is bright",
    "The sun in the sky is bright",
    "We can see the shining sun, the bright sun"
)

countvectorizer = CountVectorizer()
y =  countvectorizer.fit_transform(documents)
n  = y.T.dot(y) 
x = n.tocsr()
x = x.toarray()
np.fill_diagonal(x, 0) 
cosine_similarity = distance_metrics()['cosine']
result = cosine_similarity(x, x)

【讨论】:

    猜你喜欢
    • 2012-09-08
    • 2016-01-13
    • 2019-11-07
    • 2021-07-21
    • 2018-05-29
    • 2021-12-24
    • 1970-01-01
    • 2021-09-25
    • 1970-01-01
    相关资源
    最近更新 更多