我在编译下面的代码时遇到错误,我不知道为什么会得到此错误
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.metrics.pairwise import cosine_similarity
vectorizer=TfidfVectorizer( ngram_range=(1,1))
similarity = []
for i in range(0,len(data)):
j = a['question1'].iloc[i]
k = a['question2'].iloc[i]
inputs = []
inputs.append(j)
inputs.append(k)
try:
inputs = (vectorizer.fit_transform(inputs))
except ValueError:
inputs=[]
inputs.append(0)
inputs.append(0)
j = inputs[0]
k =inputs[1]
j = np.reshape(j,(1,-1))
k = np.reshape(k,(1,-1))
similarity.append(cosine_similarity(j, k))
我遇到错误
NotImplementedError: Reshaping not implemented for csr_matrix.
答案 0 :(得分:0)
TfidfVectorizer.transform()
返回一个稀疏矩阵。因此,无法对此进行重塑。首先将其转换为密集数组。试试:
try:
inputs = vectorizer.fit_transform(inputs).toarray()
except ValueError:
inputs=[]
inputs.append(0)
inputs.append(0)