两个不同长度的数组或向量之间的距离?

2024-05-16 07:47:36 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个程序来预测一个积极或消极的审查使用kNN算法。在对我的复习训练集做了一袋单词之后,我希望找到向量/数组之间的距离。但是我不能使用欧几里德距离(),因为向量都是不同的距离。如何找到不同长度向量之间的距离?你知道吗

from sklearn.metrics.pairwise import euclidean_distances
from sklearn.metrics.pairwise import pairwise_distances
import numpy as np
import math
import re
import random

def divide_chunks(l, n):

    for i in range(0, len(l),n):
        yield l[i:i + n]

with open(r"D:\Desktop\\1565964985_2925534_train_file.data", "r") as f:
    data_lines = f.readlines()

sentiments = list()
reviews = list()

for i, line in enumerate(data_lines):
    s = ''.join(re.findall("^[+1]*[-1]*[0]*", line))
    r = line.replace(s, '').strip()
    #print('line:{} \n\t sentiment: {} \n\t review: {}'.format(i, s, r))
    sentiments.append(s)
    reviews.append(r)

n = 1
x = list(divide_chunks(reviews, n))
print(x[0])

count = CountVectorizer()
docs = np.array(x[0])
bag = count.fit_transform(docs)
print(bag.toarray())

docs = np.array(x[1])
bag1 = count.fit_transform(docs)
print(bag1.toarray())

euclidean_distances(bag, bag1)

f.close()

错误和回溯:

[[1 1 1 1 2 1 2 2 1 2 1 1 2 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 1 1 1 1 2 1 3 1
  1 2 1 1 1 1 1 1 1 1 1 1 1 1 2 6 1 1 2 1 1 1 1 1 1 5 1 1 1 1 2 2 1 1 1 1
  2 1]]
[[1 1 3 3 1 1 1 1 1 1 2 1 1 2 2 1 1 1 1 1 1 2 1 1 6 1 1 1 2 1 6 1 1 1 4 3
  1 1 1 1 1 3 4 1 1 1 1 1 1 1 1 4 1 1 3 1 1 1 1 3 1 2 1 1 1 1 1 2 1 1 1 2
  1 2 1 1 1 5 1 1 2 1 1 1 8 1 4 1 1 1 1 3 2 1 3 1 1 2 1 1]]
Traceback (most recent call last):
  File "D:/Users/Not_J/PycharmProjects/untitled/HW1_Durand.py", line 82, in <module>
    euclidean_distances(bag, bag1)
  File "D:\Users\Not_J\PycharmProjects\HW1_Durand\venv\lib\site-packages\sklearn\metrics\pairwise.py", line 232, in euclidean_distances
    X, Y = check_pairwise_arrays(X, Y)
  File "D:\Users\Not_J\PycharmProjects\HW1_Durand\venv\lib\site-packages\sklearn\metrics\pairwise.py", line 125, in check_pairwise_arrays
    X.shape[1], Y.shape[1]))
ValueError: Incompatible dimension for X and Y matrices: X.shape[1] == 74 while Y.shape[1] == 100 ```

Tags: inimport距离docslinesklearn向量metrics