我一直在关注此链接上的代码以查找输入 X 和 Y 之间的相似性度量:
def similarity(X, Y, method):
X = np.mat(X)
Y = np.mat(Y)
N1, M = np.shape(X)
N2, M = np.shape(Y)
method = method[:3].lower()
if method=='smc': # SMC
X,Y = binarize(X,Y);
sim = ((X*Y.T)+((1-X)*(1-Y).T))/M
return sim
def binarize(X,Y=None):
''' Force binary representation of the matrix, according to X>median(X) '''
if Y==None:
X = np.matrix(X)
Xmedians = np.ones((np.shape(X)[0],1)) * np.median(X,0)
Xflags = X>Xmedians
X[Xflags] = 1; X[~Xflags] = 0
return X
else:
X = np.matrix(X); Y = np.matrix(Y);
XYmedian= np.median(np.bmat('X; Y'),0)
Xmedians = np.ones((np.shape(X)[0],1)) * XYmedian
Xflags = X>Xmedians
X[Xflags] = 1; X[~Xflags] = 0
Ymedians = np.ones((np.shape(Y)[0],1)) * XYmedian
Yflags = Y>Ymedians
Y[Yflags] = 1; Y[~Yflags] = 0
return [X,Y]
但是,它假设输入 X 和 Y 应该分别是N1 * M
和N2 * M
维矩阵。我对如何将可变长度句子的输入转换为所需的输入格式感到困惑。
另外,如果有人能建议我找到其他方法,我将不胜感激。