本文整理汇总了Python中sklearn.metrics.pairwise.linear_kernel方法的典型用法代码示例。如果您正苦于以下问题:Python pairwise.linear_kernel方法的具体用法?Python pairwise.linear_kernel怎么用?Python pairwise.linear_kernel使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类sklearn.metrics.pairwise
的用法示例。
在下文中一共展示了pairwise.linear_kernel方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __kernel_definition__
# 需要导入模块: from sklearn.metrics import pairwise [as 别名]
# 或者: from sklearn.metrics.pairwise import linear_kernel [as 别名]
def __kernel_definition__(self):
"""Select the kernel function
Returns
-------
kernel : a callable relative to selected kernel
"""
if hasattr(self.kernel, '__call__'):
return self.kernel
if self.kernel == 'rbf' or self.kernel == None:
return lambda X,Y : rbf_kernel(X,Y,self.rbf_gamma)
if self.kernel == 'poly':
return lambda X,Y : polynomial_kernel(X, Y, degree=self.degree, gamma=self.rbf_gamma, coef0=self.coef0)
if self.kernel == 'linear':
return lambda X,Y : linear_kernel(X,Y)
if self.kernel == 'precomputed':
return lambda X,Y : X
示例2: get_topk_docs_scores
# 需要导入模块: from sklearn.metrics import pairwise [as 别名]
# 或者: from sklearn.metrics.pairwise import linear_kernel [as 别名]
def get_topk_docs_scores(self, query):
"""
:param query: question as string
:return: the top k articles with each of their paragraphs seperated by '###' as python list of strings
"""
qeury = self.stem_string(query)
query_tfidf = self.vectorizer.transform([query])
similarities_raw = linear_kernel(self.tfidf_matrix, query_tfidf)
similarities = []
for s in similarities_raw:
similarities.append(s[0])
indices_sorted = np.argsort(similarities)[::-1] # reverse order
top_docs = []
docs_scores = []
i = 0
while i < min(self.k, len(self.docs)):
doc = self.docs[indices_sorted[i]]
top_docs.append(doc)
docs_scores.append(similarities[indices_sorted[i]])
i += 1
norm_cst = np.sum(np.asarray(docs_scores))
docs_scores = np.asarray(docs_scores)
docs_scores = docs_scores / norm_cst
return top_docs, docs_scores
示例3: get_topk_docs
# 需要导入模块: from sklearn.metrics import pairwise [as 别名]
# 或者: from sklearn.metrics.pairwise import linear_kernel [as 别名]
def get_topk_docs(self, query):
"""
:param query: question as string
:return: the top k articles with each of their paragraphs seperated by '###' as python list of strings
"""
qeury = self.stem_string(query)
query_tfidf = self.vectorizer.transform([query])
similarities_raw = linear_kernel(self.tfidf_matrix, query_tfidf)
similarities = []
for s in similarities_raw:
similarities.append(s[0])
indices_sorted = np.argsort(similarities)[::-1] # reverse order
top_docs = []
scores = []
i = 0
while i < min(self.k, len(self.docs)):
doc = self.docs[indices_sorted[i]]
top_docs.append(doc)
i += 1
norm_cst = np.sum(np.asarray(scores))
return top_docs
示例4: test_linear_kernel
# 需要导入模块: from sklearn.metrics import pairwise [as 别名]
# 或者: from sklearn.metrics.pairwise import linear_kernel [as 别名]
def test_linear_kernel(N=1):
np.random.seed(12345)
i = 0
while i < N:
N = np.random.randint(1, 100)
M = np.random.randint(1, 100)
C = np.random.randint(1, 1000)
X = np.random.rand(N, C)
Y = np.random.rand(M, C)
mine = LinearKernel()(X, Y)
gold = sk_linear(X, Y)
np.testing.assert_almost_equal(mine, gold)
print("PASSED")
i += 1
示例5: query
# 需要导入模块: from sklearn.metrics import pairwise [as 别名]
# 或者: from sklearn.metrics.pairwise import linear_kernel [as 别名]
def query(self, query, k=None, indices=None, return_scores=False, sort=True):
centroids = self.centroids
if centroids is None:
raise NotFittedError
if indices is not None:
centroids = centroids[indices]
q = self.vect.transform([query])
q = normalize(q, copy=False)
D = linear_kernel(q, centroids) # l2 normalized, so linear kernel
# ind = np.argsort(D[0, :])[::-1] # similarity metric, so reverse
# if k is not None: # we could use our argtopk in the first place
# ind = ind[:k]
# print(ind)
ind = argtopk(D[0], k) if sort else np.arange(D.shape[1])
if return_scores:
return ind, D[0, ind]
else:
return ind
示例6: get_similarity_scores
# 需要导入模块: from sklearn.metrics import pairwise [as 别名]
# 或者: from sklearn.metrics.pairwise import linear_kernel [as 别名]
def get_similarity_scores(verb_token, vectorizer, tf_idf_matrix):
""" Compute the cosine similarity score of a given verb token against the input corpus TF/IDF matrix.
:param str verb_token: Surface form of a verb, e.g., *born*
:param sklearn.feature_extraction.text.TfidfVectorizer vectorizer: Vectorizer
used to transform verbs into vectors
:return: cosine similarity score
:rtype: ndarray
"""
verb_token_vector = vectorizer.transform([verb_token])
# Here the linear kernel is the same as the cosine similarity, but faster
# cf. http://scikit-learn.org/stable/modules/metrics.html#cosine-similarity
scores = linear_kernel(verb_token_vector, tf_idf_matrix)
logger.debug("Corpus-wide TF/IDF scores for '%s': %s" % (verb_token, scores))
logger.debug("Average TF/IDF score for '%s': %f" % (verb_token, average(scores)))
return scores
示例7: getSimilarities
# 需要导入模块: from sklearn.metrics import pairwise [as 别名]
# 或者: from sklearn.metrics.pairwise import linear_kernel [as 别名]
def getSimilarities(id, recommendations, plotsTFIDF, verbose):
start = time.time()
# Generate cosine similarities
cosineSimilarities = linear_kernel(plotsTFIDF, plotsTFIDF)
# Get similarity scores for the input movie
scores = list(enumerate(cosineSimilarities[id]))
# Sort into descending order the scores
sortedScores = sorted(scores, key=lambda x: x[1], reverse=True)
# Get the number of the recommendations asked
movieRecommendations = sortedScores[1:recommendations + 1]
# Get the indices of the recommendation movies
movieIndices = [i[0] for i in movieRecommendations]
if (verbose):
printGreen(
'✔ Found Similarities\t{0:.1f}s'.format(time.time() - start))
return movieIndices
示例8: removeSimilarSentences
# 需要导入模块: from sklearn.metrics import pairwise [as 别名]
# 或者: from sklearn.metrics.pairwise import linear_kernel [as 别名]
def removeSimilarSentences(generatedSentences, originalSentences, stopwords,threshold=0.80,):
docs=[]
for sent, sim in generatedSentences:
docs.append(sent)
docs.extend(originalSentences)
bow_matrix = StemmedTfidfVectorizer(stop_words=stopwords).fit_transform(docs)
normalized = TfidfTransformer().fit_transform(bow_matrix)
#simMatrix = (normalized[0:] * normalized[0:].T).A
simindices=[]
#print 'Num original, ', len(originalSentences)
for i in xrange(len(generatedSentences)):
simGeneratedScores = linear_kernel(normalized[i], normalized[len(generatedSentences):]).flatten()
if(max(simGeneratedScores) >= threshold):
simindices.append(i)
#print simindices
finalGen=[sentence for k,sentence in enumerate(generatedSentences) if k not in simindices]
#print len(generatedSentences), len(finalGen)
return finalGen
示例9: monotone_conjunctive_kernel
# 需要导入模块: from sklearn.metrics import pairwise [as 别名]
# 或者: from sklearn.metrics.pairwise import linear_kernel [as 别名]
def monotone_conjunctive_kernel(X,Z=None,c=2):
L = linear_kernel(X,Z)
return binom(L,c)
示例10: monotone_disjunctive_kernel
# 需要导入模块: from sklearn.metrics import pairwise [as 别名]
# 或者: from sklearn.metrics.pairwise import linear_kernel [as 别名]
def monotone_disjunctive_kernel(X,Z=None,d=2):
L = linear_kernel(X,Z)
n = X.shape[1]
XX = np.dot(X.sum(axis=1).reshape(X.shape[0],1), np.ones((1,Z.shape[0])))
TT = np.dot(Z.sum(axis=1).reshape(Z.shape[0],1), np.ones((1,X.shape[0])))
N_x = n - XX
N_t = n - TT
N_xz = N_x - TT.T + L
N_d = binom(n, d)
N_x = binom(N_x,d)
N_t = binom(N_t,d)
N_xz = binom(N_xz,d)
return (N_d - N_x - N_t.T + N_xz)
示例11: tanimoto_kernel
# 需要导入模块: from sklearn.metrics import pairwise [as 别名]
# 或者: from sklearn.metrics.pairwise import linear_kernel [as 别名]
def tanimoto_kernel(X,Z=None):#?
L = linear_kernel(X,Z)
xx = np.linalg.norm(X,axis=1)
tt = np.linalg.norm(T,axis=1)
pass
示例12: test_HPK_train
# 需要导入模块: from sklearn.metrics import pairwise [as 别名]
# 或者: from sklearn.metrics.pairwise import linear_kernel [as 别名]
def test_HPK_train(self):
Ktr = self.Xtr @ self.Xtr.T
self.assertTrue(matNear(Ktr,pairwise_sk.linear_kernel(self.Xtr)))
self.assertTrue(matNear(
pairwise_mk.homogeneous_polynomial_kernel(self.Xtr, degree=4),
pairwise_sk.polynomial_kernel(self.Xtr, degree=4, gamma=1, coef0=0)))
self.assertTrue(matNear(
pairwise_mk.homogeneous_polynomial_kernel(self.Xtr, degree=5),
pairwise_sk.polynomial_kernel(self.Xtr, degree=5, gamma=1, coef0=0)))
self.assertTrue(matNear(Ktr**3, pairwise_sk.polynomial_kernel(self.Xtr, degree=3, gamma=1, coef0=0)))
self.assertTrue(matNear(
pairwise_mk.homogeneous_polynomial_kernel(self.Xtr, self.Xtr, degree=3),
pairwise_sk.polynomial_kernel(self.Xtr, self.Xtr, degree=3, gamma=1, coef0=0)))
示例13: test_numpy
# 需要导入模块: from sklearn.metrics import pairwise [as 别名]
# 或者: from sklearn.metrics.pairwise import linear_kernel [as 别名]
def test_numpy(self):
Xtr = self.Xtr.numpy()
self.assertTrue(matNear(
pairwise_mk.polynomial_kernel(Xtr, degree=4, gamma=0.1, coef0=2),
pairwise_sk.polynomial_kernel(Xtr, degree=4, gamma=0.1, coef0=2)))
self.assertTrue(matNear(
pairwise_mk.linear_kernel(Xtr),
pairwise_sk.linear_kernel(Xtr)))
示例14: test_kernel_normalization
# 需要导入模块: from sklearn.metrics import pairwise [as 别名]
# 或者: from sklearn.metrics.pairwise import linear_kernel [as 别名]
def test_kernel_normalization(self):
K = self.X @ self.X.T
Kn_torch = preprocessing.kernel_normalization(K)
Kn_numpy = preprocessing.kernel_normalization(K.numpy())
self.assertAlmostEqual(Kn_torch.max().item(), 1., places=6)
self.assertAlmostEqual(Kn_torch.diag().min().item(), 1., places=6)
self.assertEqual(Kn_torch.shape, (5,5))
self.assertTrue(matNear(Kn_torch, Kn_numpy))
self.assertEqual(type(Kn_torch), torch.Tensor)
self.assertEqual(type(Kn_numpy), torch.Tensor)
linear = pairwise_mk.linear_kernel(preprocessing.normalization(self.X))
self.assertTrue(matNear(Kn_torch, linear, eps=1e-7))
示例15: test_lambda
# 需要导入模块: from sklearn.metrics import pairwise [as 别名]
# 或者: from sklearn.metrics.pairwise import linear_kernel [as 别名]
def test_lambda(self):
funcs = [pairwise_mk.linear_kernel, lambda X,Z : (X @ Z.T)**2]
KLtr = [pairwise_mk.linear_kernel(self.Xtr), pairwise_mk.homogeneous_polynomial_kernel(self.Xtr)]
KLte = [pairwise_mk.linear_kernel(self.Xte, self.Xtr), pairwise_mk.homogeneous_polynomial_kernel(self.Xte, self.Xtr)]
KLtr_g = Lambda_generator(self.Xtr, kernels=funcs)
KLte_g = Lambda_generator(self.Xte, self.Xtr, kernels=funcs)
self.assertTrue(matNear(average(KLtr), average(KLtr_g)))
self.assertTrue(matNear(average(KLte), average(KLte_g)))
self.assertTrue(matNear(KLtr[1], KLtr_g[1]))
self.assertTrue(matNear(KLte[0], KLte_g[0]))