# Python sklearn.metrics.pairwise.linear_kernel() Examples

The following are code examples for showing how to use sklearn.metrics.pairwise.linear_kernel(). They are from open source Python projects. You can vote up the examples you like or vote down the ones you don't like.

Example 1
 Project: MKLpy   Author: IvanoLauriola   File: komd.py    GNU General Public License v3.0 6 votes
```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 ```
Example 2
 Project: vec4ir   Author: lgalke   File: word2vec.py    MIT License 6 votes
```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 ```
Example 3
 Project: koolsla   Author: abdullahselek   File: tfidf.py    MIT License 6 votes
```def find_similarities(tfidf_matrix, index, top_n=5):
"""Use to find similarities.
Args:
tfidf_matrix (sparse matrix, [n_samples, n_features]): Tf-idf-weighted document-term matrix.
index (int): Dish id from dataset.
top_n (int): Max recommendation count, max value 30.
Returns:
indice, similarity value (dictionary): Indice, similarity value.
"""

# Find cosine similarities
cosine_similarities = linear_kernel(tfidf_matrix[index:index+1], tfidf_matrix).flatten()
# Prepare related indices
related_docs_indices = [i for i in cosine_similarities.argsort()[::-1] if i != index]
# Return dish indices and similarity values
return [(index, cosine_similarities[index]) for index in related_docs_indices][0:top_n] ```
Example 4
 Project: StrepHit   Author: Wikidata   File: rank_verbs.py    GNU General Public License v3.0 6 votes
```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 ```
Example 5
 Project: MKLpy   Author: IvanoLauriola   File: generator.py    GNU General Public License v3.0 5 votes
```def __init__(self, n=10,base=linear_kernel):
self.n = n
self.base = base ```
Example 6
 Project: numpy-ml   Author: ddbourgin   File: tests.py    GNU General Public License v3.0 5 votes
```def test_linear_kernel():
while True:
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") ```
Example 7
 Project: content   Author: demisto   File: FindSimilarIncidentsByText.py    MIT License 5 votes
```def get_similar_texts(text, other_texts):
vect = TfidfVectorizer(min_df=1, stop_words='english')
tfidf = vect.fit_transform([text] + other_texts)
similarity_vector = linear_kernel(tfidf[0:1], tfidf).flatten()
return similarity_vector[1:] ```
Example 8
 Project: linear_neuron   Author: uglyboxer   File: test_pairwise.py    MIT License 5 votes
```def test_kernel_symmetry():
# Valid kernels should be symmetric
rng = np.random.RandomState(0)
X = rng.random_sample((5, 4))
for kernel in (linear_kernel, polynomial_kernel, rbf_kernel,
sigmoid_kernel, cosine_similarity):
K = kernel(X, X)
assert_array_almost_equal(K, K.T, 15) ```
Example 9
 Project: linear_neuron   Author: uglyboxer   File: test_pairwise.py    MIT License 5 votes
```def test_kernel_sparse():
rng = np.random.RandomState(0)
X = rng.random_sample((5, 4))
X_sparse = csr_matrix(X)
for kernel in (linear_kernel, polynomial_kernel, rbf_kernel,
sigmoid_kernel, cosine_similarity):
K = kernel(X, X)
K2 = kernel(X_sparse, X_sparse)
assert_array_almost_equal(K, K2) ```
Example 10
 Project: linear_neuron   Author: uglyboxer   File: test_pairwise.py    MIT License 5 votes
```def test_linear_kernel():
rng = np.random.RandomState(0)
X = rng.random_sample((5, 4))
K = linear_kernel(X, X)
# the diagonal elements of a linear kernel are their squared norm
assert_array_almost_equal(K.flat[::6], [linalg.norm(x) ** 2 for x in X]) ```
Example 11
 Project: scikit-learn-graph   Author: nickgentoo   File: komd.py    GNU General Public License v3.0 5 votes
```def __kernel_definition__(self):
if self.Kf == 'rbf':
return lambda X,Y : rbf_kernel(X,Y,self.rbf_gamma)
if self.Kf == 'poly':
return lambda X,Y : polynomial_kernel(X, Y, degree=self.poly_deg, gamma=None, coef0=self.poly_coeff)
if self.Kf == None or self.Kf == 'linear':
return lambda X,Y : linear_kernel(X,Y) ```
Example 12
 Project: scikit-learn-graph   Author: nickgentoo   File: komd.py    GNU General Public License v3.0 5 votes
```def __kernel_definition__(self):
if self.Kf == 'rbf':
return lambda X,Y : rbf_kernel(X,Y,self.rbf_gamma)
if self.Kf == 'poly':
return lambda X,Y : polynomial_kernel(X, Y, degree=self.poly_deg, gamma=None, coef0=self.poly_coeff)
if self.Kf == None or self.Kf == 'linear':
return lambda X,Y : linear_kernel(X,Y) ```
Example 13
 Project: scikit-learn-graph   Author: nickgentoo   File: komd.py    GNU General Public License v3.0 5 votes
```def __kernel_definition__(self):
if self.Kf == 'rbf':
return lambda X,Y : rbf_kernel(X,Y,self.rbf_gamma)
if self.Kf == 'poly':
return lambda X,Y : polynomial_kernel(X, Y, degree=self.poly_deg, gamma=None, coef0=self.poly_coeff)
if self.Kf == None or self.Kf == 'linear':
return lambda X,Y : linear_kernel(X,Y) ```
Example 14
 Project: scikit-learn-graph   Author: nickgentoo   File: komd.py    GNU General Public License v3.0 5 votes
```def __kernel_definition__(self):
if self.Kf == 'rbf':
return lambda X,Y : rbf_kernel(X,Y,self.rbf_gamma)
if self.Kf == 'poly':
return lambda X,Y : polynomial_kernel(X, Y, degree=self.poly_deg, gamma=None, coef0=self.poly_coeff)
if self.Kf == None or self.Kf == 'linear':
return lambda X,Y : linear_kernel(X,Y) ```
Example 15
 Project: scikit-learn-graph   Author: nickgentoo   File: komd.py    GNU General Public License v3.0 5 votes
```def __kernel_definition__(self):
if self.Kf == 'rbf':
return lambda X,Y : rbf_kernel(X,Y,self.rbf_gamma)
if self.Kf == 'poly':
return lambda X,Y : polynomial_kernel(X, Y, degree=self.poly_deg, gamma=None, coef0=self.poly_coeff)
if self.Kf == None or self.Kf == 'linear':
return lambda X,Y : linear_kernel(X,Y) ```
Example 16
 Project: scikit-learn-graph   Author: nickgentoo   File: komd.py    GNU General Public License v3.0 5 votes
```def __kernel_definition__(self):
if self.Kf == 'rbf':
return lambda X,Y : rbf_kernel(X,Y,self.rbf_gamma)
if self.Kf == 'poly':
return lambda X,Y : polynomial_kernel(X, Y, degree=self.poly_deg, gamma=None, coef0=self.poly_coeff)
if self.Kf == None or self.Kf == 'linear':
return lambda X,Y : linear_kernel(X,Y) ```
Example 17
 Project: scikit-learn-graph   Author: nickgentoo   File: komd.py    GNU General Public License v3.0 5 votes
```def __kernel_definition__(self):
if self.Kf == 'rbf':
return lambda X,Y : rbf_kernel(X,Y,self.rbf_gamma)
if self.Kf == 'poly':
return lambda X,Y : polynomial_kernel(X, Y, degree=self.poly_deg, gamma=None, coef0=self.poly_coeff)
if self.Kf == None or self.Kf == 'linear':
return lambda X,Y : linear_kernel(X,Y) ```
Example 18
 Project: scikit-learn-graph   Author: nickgentoo   File: komd.py    GNU General Public License v3.0 5 votes
```def __kernel_definition__(self):
if self.Kf == 'rbf':
return lambda X,Y : rbf_kernel(X,Y,self.rbf_gamma)
if self.Kf == 'poly':
return lambda X,Y : polynomial_kernel(X, Y, degree=self.poly_deg, gamma=None, coef0=self.poly_coeff)
if self.Kf == None or self.Kf == 'linear':
return lambda X,Y : linear_kernel(X,Y) ```
Example 19
 Project: scikit-learn-graph   Author: nickgentoo   File: komd.py    GNU General Public License v3.0 5 votes
```def __kernel_definition__(self):
if self.Kf == 'rbf':
return lambda X,Y : rbf_kernel(X,Y,self.rbf_gamma)
if self.Kf == 'poly':
return lambda X,Y : polynomial_kernel(X, Y, degree=self.poly_deg, gamma=None, coef0=self.poly_coeff)
if self.Kf == None or self.Kf == 'linear':
return lambda X,Y : linear_kernel(X,Y) ```
Example 20
 Project: scikit-learn-graph   Author: nickgentoo   File: komd.py    GNU General Public License v3.0 5 votes
```def __kernel_definition__(self):
if self.Kf == 'rbf':
return lambda X,Y : rbf_kernel(X,Y,self.rbf_gamma)
if self.Kf == 'poly':
return lambda X,Y : polynomial_kernel(X, Y, degree=self.poly_deg, gamma=None, coef0=self.poly_coeff)
if self.Kf == None or self.Kf == 'linear':
return lambda X,Y : linear_kernel(X,Y) ```
Example 21
 Project: scikit-learn-graph   Author: nickgentoo   File: komd.py    GNU General Public License v3.0 5 votes
```def __kernel_definition__(self):
if self.Kf == 'rbf':
return lambda X,Y : rbf_kernel(X,Y,self.rbf_gamma)
if self.Kf == 'poly':
return lambda X,Y : polynomial_kernel(X, Y, degree=self.poly_deg, gamma=None, coef0=self.poly_coeff)
if self.Kf == None or self.Kf == 'linear':
return lambda X,Y : linear_kernel(X,Y) ```
Example 22
 Project: scikit-learn-graph   Author: nickgentoo   File: komd.py    GNU General Public License v3.0 5 votes
```def __kernel_definition__(self):
if self.Kf == 'rbf':
return lambda X,Y : rbf_kernel(X,Y,self.rbf_gamma)
if self.Kf == 'poly':
return lambda X,Y : polynomial_kernel(X, Y, degree=self.poly_deg, gamma=None, coef0=self.poly_coeff)
if self.Kf == None or self.Kf == 'linear':
return lambda X,Y : linear_kernel(X,Y) ```
Example 23
 Project: scikit-learn-graph   Author: nickgentoo   File: komd.py    GNU General Public License v3.0 5 votes
```def __kernel_definition__(self):
if self.Kf == 'rbf':
return lambda X,Y : rbf_kernel(X,Y,self.rbf_gamma)
if self.Kf == 'poly':
return lambda X,Y : polynomial_kernel(X, Y, degree=self.poly_deg, gamma=None, coef0=self.poly_coeff)
if self.Kf == None or self.Kf == 'linear':
return lambda X,Y : linear_kernel(X,Y) ```
Example 24
 Project: scikit-learn-graph   Author: nickgentoo   File: komd.py    GNU General Public License v3.0 5 votes
```def __kernel_definition__(self):
if self.Kf == 'rbf':
return lambda X,Y : rbf_kernel(X,Y,self.rbf_gamma)
if self.Kf == 'poly':
return lambda X,Y : polynomial_kernel(X, Y, degree=self.poly_deg, gamma=None, coef0=self.poly_coeff)
if self.Kf == None or self.Kf == 'linear':
return lambda X,Y : linear_kernel(X,Y) ```
Example 25
 Project: scikit-learn-graph   Author: nickgentoo   File: komd.py    GNU General Public License v3.0 5 votes
```def __kernel_definition__(self):
if self.Kf == 'rbf':
return lambda X,Y : rbf_kernel(X,Y,self.rbf_gamma)
if self.Kf == 'poly':
return lambda X,Y : polynomial_kernel(X, Y, degree=self.poly_deg, gamma=None, coef0=self.poly_coeff)
if self.Kf == None or self.Kf == 'linear':
return lambda X,Y : linear_kernel(X,Y) ```
Example 26
 Project: scikit-learn-graph   Author: nickgentoo   File: komd.py    GNU General Public License v3.0 5 votes
```def __kernel_definition__(self):
if self.Kf == 'rbf':
return lambda X,Y : rbf_kernel(X,Y,self.rbf_gamma)
if self.Kf == 'poly':
return lambda X,Y : polynomial_kernel(X, Y, degree=self.poly_deg, gamma=None, coef0=self.poly_coeff)
if self.Kf == None or self.Kf == 'linear':
return lambda X,Y : linear_kernel(X,Y) ```
Example 27
 Project: scikit-learn-graph   Author: nickgentoo   File: komd.py    GNU General Public License v3.0 5 votes
```def __kernel_definition__(self):
if self.Kf == 'rbf':
return lambda X,Y : rbf_kernel(X,Y,self.rbf_gamma)
if self.Kf == 'poly':
return lambda X,Y : polynomial_kernel(X, Y, degree=self.poly_deg, gamma=None, coef0=self.poly_coeff)
if self.Kf == None or self.Kf == 'linear':
return lambda X,Y : linear_kernel(X,Y) ```
Example 28
 Project: scikit-learn-graph   Author: nickgentoo   File: komd.py    GNU General Public License v3.0 5 votes
```def __kernel_definition__(self):
if self.Kf == 'rbf':
return lambda X,Y : rbf_kernel(X,Y,self.rbf_gamma)
if self.Kf == 'poly':
return lambda X,Y : polynomial_kernel(X, Y, degree=self.poly_deg, gamma=None, coef0=self.poly_coeff)
if self.Kf == None or self.Kf == 'linear':
return lambda X,Y : linear_kernel(X,Y) ```
Example 29
 Project: scikit-learn-graph   Author: nickgentoo   File: komd.py    GNU General Public License v3.0 5 votes
```def __kernel_definition__(self):
if self.Kf == 'rbf':
return lambda X,Y : rbf_kernel(X,Y,self.rbf_gamma)
if self.Kf == 'poly':
return lambda X,Y : polynomial_kernel(X, Y, degree=self.poly_deg, gamma=None, coef0=self.poly_coeff)
if self.Kf == None or self.Kf == 'linear':
return lambda X,Y : linear_kernel(X,Y) ```
Example 30
 Project: scikit-learn-graph   Author: nickgentoo   File: komd.py    GNU General Public License v3.0 5 votes
```def __kernel_definition__(self):
if self.Kf == 'rbf':
return lambda X,Y : rbf_kernel(X,Y,self.rbf_gamma)
if self.Kf == 'poly':
return lambda X,Y : polynomial_kernel(X, Y, degree=self.poly_deg, gamma=None, coef0=self.poly_coeff)
if self.Kf == None or self.Kf == 'linear':
return lambda X,Y : linear_kernel(X,Y) ```
Example 31
 Project: scikit-learn-graph   Author: nickgentoo   File: komd.py    GNU General Public License v3.0 5 votes
```def __kernel_definition__(self):
if self.Kf == 'rbf':
return lambda X,Y : rbf_kernel(X,Y,self.rbf_gamma)
if self.Kf == 'poly':
return lambda X,Y : polynomial_kernel(X, Y, degree=self.poly_deg, gamma=None, coef0=self.poly_coeff)
if self.Kf == None or self.Kf == 'linear':
return lambda X,Y : linear_kernel(X,Y) ```
Example 32
 Project: scikit-learn-graph   Author: nickgentoo   File: komd.py    GNU General Public License v3.0 5 votes
```def __kernel_definition__(self):
if self.Kf == 'rbf':
return lambda X,Y : rbf_kernel(X,Y,self.rbf_gamma)
if self.Kf == 'poly':
return lambda X,Y : polynomial_kernel(X, Y, degree=self.poly_deg, gamma=None, coef0=self.poly_coeff)
if self.Kf == None or self.Kf == 'linear':
return lambda X,Y : linear_kernel(X,Y) ```
Example 33
 Project: scikit-learn-graph   Author: nickgentoo   File: komd.py    GNU General Public License v3.0 5 votes
```def __kernel_definition__(self):
if self.Kf == 'rbf':
return lambda X,Y : rbf_kernel(X,Y,self.rbf_gamma)
if self.Kf == 'poly':
return lambda X,Y : polynomial_kernel(X, Y, degree=self.poly_deg, gamma=None, coef0=self.poly_coeff)
if self.Kf == None or self.Kf == 'linear':
return lambda X,Y : linear_kernel(X,Y) ```
Example 34
 Project: scikit-learn-graph   Author: nickgentoo   File: komd.py    GNU General Public License v3.0 5 votes
```def __kernel_definition__(self):
if self.Kf == 'rbf':
return lambda X,Y : rbf_kernel(X,Y,self.rbf_gamma)
if self.Kf == 'poly':
return lambda X,Y : polynomial_kernel(X, Y, degree=self.poly_deg, gamma=None, coef0=self.poly_coeff)
if self.Kf == None or self.Kf == 'linear':
return lambda X,Y : linear_kernel(X,Y) ```
Example 35
 Project: scikit-learn-graph   Author: nickgentoo   File: komd.py    GNU General Public License v3.0 5 votes
```def __kernel_definition__(self):
if self.Kf == 'rbf':
return lambda X,Y : rbf_kernel(X,Y,self.rbf_gamma)
if self.Kf == 'poly':
return lambda X,Y : polynomial_kernel(X, Y, degree=self.poly_deg, gamma=None, coef0=self.poly_coeff)
if self.Kf == None or self.Kf == 'linear':
return lambda X,Y : linear_kernel(X,Y) ```
Example 36
 Project: scikit-learn-graph   Author: nickgentoo   File: komd.py    GNU General Public License v3.0 5 votes
```def __kernel_definition__(self):
if self.Kf == 'rbf':
return lambda X,Y : rbf_kernel(X,Y,self.rbf_gamma)
if self.Kf == 'poly':
return lambda X,Y : polynomial_kernel(X, Y, degree=self.poly_deg, gamma=None, coef0=self.poly_coeff)
if self.Kf == None or self.Kf == 'linear':
return lambda X,Y : linear_kernel(X,Y) ```
Example 37
 Project: scikit-learn-graph   Author: nickgentoo   File: komd.py    GNU General Public License v3.0 5 votes
```def __kernel_definition__(self):
if self.Kf == 'rbf':
return lambda X,Y : rbf_kernel(X,Y,self.rbf_gamma)
if self.Kf == 'poly':
return lambda X,Y : polynomial_kernel(X, Y, degree=self.poly_deg, gamma=None, coef0=self.poly_coeff)
if self.Kf == None or self.Kf == 'linear':
return lambda X,Y : linear_kernel(X,Y) ```
Example 38
 Project: scikit-learn-graph   Author: nickgentoo   File: komd.py    GNU General Public License v3.0 5 votes
```def __kernel_definition__(self):
if self.Kf == 'rbf':
return lambda X,Y : rbf_kernel(X,Y,self.rbf_gamma)
if self.Kf == 'poly':
return lambda X,Y : polynomial_kernel(X, Y, degree=self.poly_deg, gamma=None, coef0=self.poly_coeff)
if self.Kf == None or self.Kf == 'linear':
return lambda X,Y : linear_kernel(X,Y) ```
Example 39
 Project: scikit-learn-graph   Author: nickgentoo   File: komd.py    GNU General Public License v3.0 5 votes
```def __kernel_definition__(self):
if self.Kf == 'rbf':
return lambda X,Y : rbf_kernel(X,Y,self.rbf_gamma)
if self.Kf == 'poly':
return lambda X,Y : polynomial_kernel(X, Y, degree=self.poly_deg, gamma=None, coef0=self.poly_coeff)
if self.Kf == None or self.Kf == 'linear':
return lambda X,Y : linear_kernel(X,Y) ```
Example 40
 Project: scikit-learn-graph   Author: nickgentoo   File: komd.py    GNU General Public License v3.0 5 votes
```def __kernel_definition__(self):
if self.Kf == 'rbf':
return lambda X,Y : rbf_kernel(X,Y,self.rbf_gamma)
if self.Kf == 'poly':
return lambda X,Y : polynomial_kernel(X, Y, degree=self.poly_deg, gamma=None, coef0=self.poly_coeff)
if self.Kf == None or self.Kf == 'linear':
return lambda X,Y : linear_kernel(X,Y) ```
Example 41
 Project: Weiss   Author: WangWenjun559   File: test_pairwise.py    Apache License 2.0 5 votes
```def test_kernel_symmetry():
# Valid kernels should be symmetric
rng = np.random.RandomState(0)
X = rng.random_sample((5, 4))
for kernel in (linear_kernel, polynomial_kernel, rbf_kernel,
sigmoid_kernel, cosine_similarity):
K = kernel(X, X)
assert_array_almost_equal(K, K.T, 15) ```
Example 42
 Project: Weiss   Author: WangWenjun559   File: test_pairwise.py    Apache License 2.0 5 votes
```def test_kernel_sparse():
rng = np.random.RandomState(0)
X = rng.random_sample((5, 4))
X_sparse = csr_matrix(X)
for kernel in (linear_kernel, polynomial_kernel, rbf_kernel,
sigmoid_kernel, cosine_similarity):
K = kernel(X, X)
K2 = kernel(X_sparse, X_sparse)
assert_array_almost_equal(K, K2) ```
Example 43
 Project: Weiss   Author: WangWenjun559   File: test_pairwise.py    Apache License 2.0 5 votes
```def test_linear_kernel():
rng = np.random.RandomState(0)
X = rng.random_sample((5, 4))
K = linear_kernel(X, X)
# the diagonal elements of a linear kernel are their squared norm
assert_array_almost_equal(K.flat[::6], [linalg.norm(x) ** 2 for x in X]) ```
Example 44
 Project: soft_tfidf   Author: potatochip   File: soft_tfidf.py    Apache License 2.0 5 votes
```def retrieve(self, query, best_matches=False):
"""retrieves the most similar document in the corpus to the query

Args:
query (unicode): the document in the corpus we are looking for
best_matches (bool, default False): whether to return 5 of the best
matches instead of just the top one

Returns:
unicode: the string that is closest to the query
"""
query = unicode(query)
t_bag = query.split()
sims, terms = zip(*[self._best_match(t) for t in t_bag])
transformed = self.vectorizer.transform([' '.join(terms)])
dist_matrix = sp.csr_matrix(
(sims, ([0]*len(terms), [self.column_lookup[t] for t in terms])),
transformed.shape)
comparison_vector = normalize(transformed.multiply(dist_matrix))
cos_sim = linear_kernel(self.matrix, comparison_vector).ravel()
if best_matches:
partial_sort_indexes = np.argpartition(cos_sim, np.arange(-5, 0, 1), axis=0)
closest_idx = reversed(partial_sort_indexes[-5:])
return operator.itemgetter(*closest_idx)(self.corpus)
closest_idx = np.argmax(cos_sim)
return self.corpus[closest_idx] ```
Example 45
 Project: wine-ml-on-aws-lambda   Author: pierreant   File: test_pairwise.py    Apache License 2.0 5 votes
```def test_kernel_symmetry():
# Valid kernels should be symmetric
rng = np.random.RandomState(0)
X = rng.random_sample((5, 4))
for kernel in (linear_kernel, polynomial_kernel, rbf_kernel,
laplacian_kernel, sigmoid_kernel, cosine_similarity):
K = kernel(X, X)
assert_array_almost_equal(K, K.T, 15) ```
Example 46
 Project: wine-ml-on-aws-lambda   Author: pierreant   File: test_pairwise.py    Apache License 2.0 5 votes
```def test_kernel_sparse():
rng = np.random.RandomState(0)
X = rng.random_sample((5, 4))
X_sparse = csr_matrix(X)
for kernel in (linear_kernel, polynomial_kernel, rbf_kernel,
laplacian_kernel, sigmoid_kernel, cosine_similarity):
K = kernel(X, X)
K2 = kernel(X_sparse, X_sparse)
assert_array_almost_equal(K, K2) ```
Example 47
 Project: wine-ml-on-aws-lambda   Author: pierreant   File: test_pairwise.py    Apache License 2.0 5 votes
```def test_linear_kernel():
rng = np.random.RandomState(0)
X = rng.random_sample((5, 4))
K = linear_kernel(X, X)
# the diagonal elements of a linear kernel are their squared norm
assert_array_almost_equal(K.flat[::6], [linalg.norm(x) ** 2 for x in X]) ```
Example 48
 Project: ALiPy   Author: NUAA-AL   File: multi_label.py    BSD 3-Clause "New" or "Revised" License 5 votes
```def __init__(self, X, y, **kwargs):
# K: kernel matrix
super(QueryMultiLabelQUIRE, self).__init__(X, y)
self.lmbda = kwargs.pop('lambda', 1.)
self.kernel = kwargs.pop('kernel', 'rbf')
if self.kernel == 'rbf':
self.K = rbf_kernel(X=X, Y=X, gamma=kwargs.pop('gamma', 1.))
elif self.kernel == 'poly':
self.K = polynomial_kernel(X=X,
Y=X,
coef0=kwargs.pop('coef0', 1),
degree=kwargs.pop('degree', 3),
gamma=kwargs.pop('gamma', 1.))
elif self.kernel == 'linear':
self.K = linear_kernel(X=X, Y=X)
elif hasattr(self.kernel, '__call__'):
self.K = self.kernel(X=np.array(X), Y=np.array(X))
else:
raise NotImplementedError

if not isinstance(self.K, np.ndarray):
raise TypeError('K should be an ndarray')
if self.K.shape != (len(X), len(X)):
raise ValueError(
'Kernel should have size (%d, %d)' % (len(X), len(X)))
self._nsamples, self._nclass = self.y.shape
self.L = np.linalg.pinv(self.K + self.lmbda * np.eye(len(X))) ```
Example 49
 Project: ALiPy   Author: NUAA-AL   File: query_labels.py    BSD 3-Clause "New" or "Revised" License 5 votes
```def __init__(self, X, y, train_idx, **kwargs):
# K: kernel matrix
#
X = np.asarray(X)[train_idx]
y = np.asarray(y)[train_idx]
self._train_idx = np.asarray(train_idx)

self.y = np.array(y)
self.lmbda = kwargs.pop('lambda', 1.)
self.kernel = kwargs.pop('kernel', 'rbf')
if self.kernel == 'rbf':
self.K = rbf_kernel(X=X, Y=X, gamma=kwargs.pop('gamma', 1.))
elif self.kernel == 'poly':
self.K = polynomial_kernel(X=X,
Y=X,
coef0=kwargs.pop('coef0', 1),
degree=kwargs.pop('degree', 3),
gamma=kwargs.pop('gamma', 1.))
elif self.kernel == 'linear':
self.K = linear_kernel(X=X, Y=X)
elif hasattr(self.kernel, '__call__'):
self.K = self.kernel(X=np.array(X), Y=np.array(X))
else:
raise NotImplementedError

if not isinstance(self.K, np.ndarray):
raise TypeError('K should be an ndarray')
if self.K.shape != (len(X), len(X)):
raise ValueError(
'kernel should have size (%d, %d)' % (len(X), len(X)))
self.L = np.linalg.inv(self.K + self.lmbda * np.eye(len(X))) ```
Example 50
 Project: vec4ir   Author: lgalke   File: base.py    MIT License 5 votes
```def query(self, query, k=None, indices=None, return_scores=False, sort=True):
if self._fit_X is None:
raise NotFittedError
q = super().transform([query])
if indices is not None:
fit_X = self._fit_X[indices]
else:
fit_X = self._fit_X
# both fit_X and q are l2-normalized
D = linear_kernel(q, fit_X)
ind = argtopk(D[0], k) if sort else np.arange(D.shape[1])
if return_scores:
return ind, D[0,ind]
else:
return ind ```
Example 51
 Project: vec4ir   Author: lgalke   File: doc2vec.py    MIT License 5 votes
```def query(self, query, k=None, indices=None):
if indices is not None:
dvs = self.inferred_docvecs[indices]
else:
dvs = self.inferred_docvecs

analyzed_query = self.analyzer(query)
qv = self.model.infer_vector(analyzed_query).reshape(1, -1)
qv = normalize(qv, copy=False)

dists = linear_kernel(qv, dvs)[0]

ind = argtopk(dists)

return ind ```
Example 52
 Project: EasyMKL   Author: jmikko   File: komd.py    GNU General Public License v3.0 5 votes
```def __kernel_definition__(self):
if self.Kf == 'rbf':
return lambda X,Y : rbf_kernel(X,Y,self.rbf_gamma)
if self.Kf == 'poly':
return lambda X,Y : polynomial_kernel(X, Y, degree=self.poly_deg, gamma=None, coef0=self.poly_coeff)
if self.Kf == None or self.Kf == 'linear':
return lambda X,Y : linear_kernel(X,Y) ```
Example 53
 Project: sklearn-rvm   Author: Mind-the-Pineapple   File: rvc_em.py    BSD 3-Clause "New" or "Revised" License 5 votes
```def _apply_kernel(self, x, y):
"""Apply the selected kernel function to the data."""
if self.kernel == 'linear':
phi = linear_kernel(x, y)
elif self.kernel == 'rbf':
phi = rbf_kernel(x, y, self.gamma)
elif self.kernel == 'poly':
phi = polynomial_kernel(x, y, self.degree, self.gamma, self.coef0)
elif callable(self.kernel):
phi = self.kernel(x, y)
if len(phi.shape) != 2:
raise ValueError(
"Custom kernel function did not return 2D matrix"
)
if phi.shape[0] != x.shape[0]:
raise ValueError(
"Custom kernel function did not return matrix with rows"
" equal to number of data points."""
)
else:
raise ValueError("Kernel selection is invalid.")

if self.bias_used:
phi = np.append(phi, np.ones((phi.shape[0], 1)), axis=1)

return phi ```
Example 54
 Project: entity2vec   Author: D2KLab   File: scorer.py    Apache License 2.0 5 votes
```def similarity_function(vec1,vec2, similarity):

#compute cosine similarity or other similarities

v1 = np.array(vec1)

v2 = np.array(vec2)

if len(v1)*len(v2) == 0: #any of the two is 0
global count
count +=1

return 0

else:

if similarity == 'cosine':

return cosine_similarity([v1],[v2])[0][0] #returns a double array [[sim]]

elif similarity == 'softmax':

return np.exp(np.dot(v1,v2)) #normalization is useless for relative comparisons

elif similarity == 'linear_kernel':
return linear_kernel(v1,v2)[0][0]

elif similarity == 'euclidean':
return euclidean_distances(v1,v2)[0][0]
else:
raise NameError('Choose a valid similarity function') ```
Example 55
 Project: morpy   Author: iurykrieger   File: ContentEngine.py    GNU General Public License v3.0 5 votes
```def __init__(self):
start = time.time()
self.item_service = ItemService()
self.data = pd.DataFrame(list(self.item_service.get_rec_data()))
self.tfidf = TfidfVectorizer(
analyzer='word',
ngram_range=(1, 3),
min_df=0,
smooth_idf=False,
stop_words='english')
self.tfidf_matrix = self.tfidf.fit_transform(
self.data['concated_attrs'])
self.cosine_similarities = linear_kernel(
self.tfidf_matrix, self.tfidf_matrix)
info("Training data ingested in %s seconds." % (time.time() - start)) ```
Example 56
 Project: Movie-Recommendation   Author: pncnmnp   File: content_based.py    MIT License 5 votes
```def tfidf(self, df):
"""
param: df - movies pandas DataFrame

return: cosine similarity matrix based on overview and description
"""
tfidf = TfidfVectorizer(
analyzer="word", stop_words=stopwords.words("english"), ngram_range=(1, 2)
)
tfidf_mat = tfidf.fit_transform(df["overview"])
cosine_sim = linear_kernel(tfidf_mat, tfidf_mat)
return cosine_sim ```
Example 57
 Project: WTK   Author: BorgwardtLab   File: kernels.py    MIT License 5 votes
```def linear_kernel(subsequences_1, subsequences_2):
'''
Calculates the linear kernel between two time series using their
corresponding set of subsequences.
'''

K_lin = pairwise.linear_kernel(subsequences_1, subsequences_2)
n = subsequences_1.shape[0]
m = subsequences_2.shape[0]

return np.sum(K_lin)/(n*m) ```
Example 58
 Project: WTK   Author: BorgwardtLab   File: __init__.py    MIT License 5 votes
```def linear_kernel(subsequences_1, subsequences_2):
'''
Calculates the linear kernel between two time series using their
corresponding set of subsequences.
'''

K_lin = pairwise.linear_kernel(subsequences_1, subsequences_2)
n = subsequences_1.shape[0]
m = subsequences_2.shape[0]

return np.sum(K_lin)/(n*m) ```
Example 59
 Project: ALiPy   Author: NUAA-AL   File: query_labels.py    BSD 3-Clause "New" or "Revised" License 4 votes
```def __init__(self, X, y, beta=1000, gamma=0.1, rho=1, **kwargs):
try:
import cvxpy
self._cvxpy = cvxpy
except:
raise ImportError("This method need cvxpy to solve the QP problem."
"Please refer to https://www.cvxpy.org/install/index.html "
"install cvxpy manually before using.")

# K: kernel matrix
super(QueryInstanceBMDR, self).__init__(X, y)
ul = unique_labels(self.y)
if len(ul) != 2:
warnings.warn("This query strategy is implemented for binary classification only.",
category=FunctionWarning)
if len(ul) == 2 and {1, -1} != set(ul):
y_temp = np.array(copy.deepcopy(self.y))
y_temp[y_temp == ul[0]] = 1
y_temp[y_temp == ul[1]] = -1
self.y = y_temp

self._beta = beta
self._gamma = gamma
self._rho = rho

# calc kernel
self._kernel = kwargs.pop('kernel', 'rbf')
if self._kernel == 'rbf':
self._K = rbf_kernel(X=X, Y=X, gamma=kwargs.pop('gamma_ker', 1.))
elif self._kernel == 'poly':
self._K = polynomial_kernel(X=X,
Y=X,
coef0=kwargs.pop('coef0', 1),
degree=kwargs.pop('degree', 3),
gamma=kwargs.pop('gamma_ker', 1.))
elif self._kernel == 'linear':
self._K = linear_kernel(X=X, Y=X)
elif hasattr(self._kernel, '__call__'):
self._K = self._kernel(X=np.array(X), Y=np.array(X))
else:
raise NotImplementedError

if not isinstance(self._K, np.ndarray):
raise TypeError('K should be an ndarray')
if self._K.shape != (len(X), len(X)):
raise ValueError(
'kernel should have size (%d, %d)' % (len(X), len(X))) ```
Example 60
 Project: ALiPy   Author: NUAA-AL   File: query_labels.py    BSD 3-Clause "New" or "Revised" License 4 votes
```def __init__(self, X, y, mu=0.1, gamma=0.1, rho=1, lambda_init=0.1, lambda_pace=0.01, **kwargs):
try:
import cvxpy
self._cvxpy = cvxpy
except:
raise ImportError("This method need cvxpy to solve the QP problem."
"Please refer to https://www.cvxpy.org/install/index.html "
"install cvxpy manually before using.")

# K: kernel matrix
super(QueryInstanceSPAL, self).__init__(X, y)
ul = unique_labels(self.y)
if len(unique_labels(self.y)) != 2:
warnings.warn("This query strategy is implemented for binary classification only.",
category=FunctionWarning)
if len(ul) == 2 and {1, -1} != set(ul):
y_temp = np.array(copy.deepcopy(self.y))
y_temp[y_temp == ul[0]] = 1
y_temp[y_temp == ul[1]] = -1
self.y = y_temp

self._mu = mu
self._gamma = gamma
self._rho = rho
self._lambda_init = lambda_init
self._lambda_pace = lambda_pace
self._lambda = lambda_init

# calc kernel
self._kernel = kwargs.pop('kernel', 'rbf')
if self._kernel == 'rbf':
self._K = rbf_kernel(X=X, Y=X, gamma=kwargs.pop('gamma_ker', 1.))
elif self._kernel == 'poly':
self._K = polynomial_kernel(X=X,
Y=X,
coef0=kwargs.pop('coef0', 1),
degree=kwargs.pop('degree', 3),
gamma=kwargs.pop('gamma_ker', 1.))
elif self._kernel == 'linear':
self._K = linear_kernel(X=X, Y=X)
elif hasattr(self._kernel, '__call__'):
self._K = self._kernel(X=np.array(X), Y=np.array(X))
else:
raise NotImplementedError

if not isinstance(self._K, np.ndarray):
raise TypeError('K should be an ndarray')
if self._K.shape != (len(X), len(X)):
raise ValueError(
'kernel should have size (%d, %d)' % (len(X), len(X))) ```
Example 61
 Project: trial2rev   Author: evidence-surveillance   File: bot.py    MIT License 4 votes
```def basicbot2(review_id=None, sess_id=None):
"""
use document similarity to recommend trials for a review based on similarity to current included trials
@param review_id: PMID of review
@param sess_id: session ID if transitting progress via websocket
"""
if sess_id:
socketio = SocketIO(message_queue='amqp://localhost')
conn = dblib.create_con(VERBOSE=True)
cur = conn.cursor()
cur.execute("SELECT nct_id FROM review_rtrial WHERE relationship = 'included' AND review_id = %s;",
(review_id,))
trials = cur.fetchall()
if len(trials) < 1:
print 'no trials for basicbot2'
conn.close()
return False
if trials:
cur.execute(
"delete from votes where link_id in (select id from review_rtrial where review_id = %s) and user_id = %s;",
(review_id, 10))
conn.commit()
cur.execute("delete from review_rtrial where upvotes = 0 and downvotes = 0 and user_id = 10;")
conn.commit()
conn.close()
if sess_id:
socketio.emit('basicbot2_update', {'msg': 'triggering basicbot2'}, room=sess_id)
trials = list(zip(*trials)[0])
ix = np.isin(ids, trials)
trial_indices = np.where(ix)[0]
if sess_id:
socketio.emit('basicbot2_update', {'msg': 'vectorizing stuff'}, room=sess_id)
trial_vecs = tfidf_matrix[trial_indices, :]
cos_sim = linear_kernel(trial_vecs, tfidf_matrix)
if sess_id:
socketio.emit('basicbot2_update', {'msg': 'calculating cosine similarity'}, room=sess_id)
final = cos_sim.sum(axis=0)
top = np.argpartition(final, -100)[-100:]
top_ranked = set(ids[np.array(top)]) - set(ids[trial_indices])
if sess_id:
socketio.emit('basicbot2_update', {'msg': 'inserting basicbot 2 predictions'}, room=sess_id)
for nct_id in top_ranked:
crud.review_trial(review_id, nct_id, False, 'relevant', 'basicbot2', 10)
if sess_id:
socketio.emit('basicbot2_update', {'msg': 'basicbot2 complete!'}, room=sess_id) ```
Example 62
 Project: trial2rev   Author: evidence-surveillance   File: bot.py    MIT License 4 votes
```def basicbot2_freetext(review_id=None, sess_id=None):
"""
use document similarity to recommend trials for a review based on similarity to current included trials
@param review_id: PMID of review
@param sess_id: session ID if transitting progress via websocket
"""
if sess_id:
socketio = SocketIO(message_queue='amqp://localhost')
conn = dblib.create_con(VERBOSE=True)
cur = conn.cursor()
cur.execute("SELECT nct_id FROM freetext_review_rtrial WHERE review_id = %s;",
(review_id,))
trials = cur.fetchall()
conn.close()
if len(trials) < 1:
print 'no trials for basicbot2'
conn.close()

return []
if trials:
if sess_id:
socketio.emit('basicbot2_update', {'msg': 'triggering basicbot2'}, room=sess_id)
trials = list(zip(*trials)[0])
ix = np.isin(ids, trials)
trial_indices = np.where(ix)[0]
if sess_id:
socketio.emit('basicbot2_update', {'msg': 'vectorizing stuff'}, room=sess_id)
trial_vecs = tfidf_matrix[trial_indices, :]
cos_sim = linear_kernel(trial_vecs, tfidf_matrix)
if sess_id:
socketio.emit('basicbot2_update', {'msg': 'calculating cosine similarity'}, room=sess_id)
final = cos_sim.sum(axis=0)
top = np.argpartition(final, -100)[-100:]
top_ranked = set(ids[np.array(top)]) - set(ids[trial_indices])

return list(top_ranked)
# if sess_id:
#     socketio.emit('basicbot2_update', {'msg': 'inserting basicbot 2 predictions'}, room=sess_id)
# for nct_id in top_ranked:
#     crud.review_trial(review_id, nct_id, False, 'relevant', 'basicbot2', 10)
# if sess_id:
#     socketio.emit('basicbot2_update', {'msg': 'basicbot2 complete!'}, room=sess_id) ```
Example 63
 Project: LibRecommender   Author: massquantity   File: similarities.py    MIT License 4 votes
```def sk_sim(data, n_users, n_items, min_support=5, sparse=True):
if sparse:
user_indices = []
item_indices = []
values = []
for i, u_ratings in data.items():
for u, r in u_ratings.items():
user_indices.append(u)
item_indices.append(i)
values.append(r)
m = csr_matrix((np.array(values), (np.array(user_indices), np.array(item_indices))), dtype=np.float32)
assert issparse(m)
else:
m = np.zeros((n_users, n_items))
for i, u_ratings in data.items():
for u, r in u_ratings.items():
m[u, i] = r

sim = cosine_similarity(m, dense_output=False)
sim = sim.tolil()

if min_support > 1:
print("warning: allow min_support > 1 could be slow for calculating similarity matrix")
t0 = time.time()
dot = linear_kernel(m, dense_output=False)
dot = dot.tolil()
#    print("dot: ", [i for i in dot.data if len(i) != 0], dot.getnnz())
s = lil_matrix((n_users, n_users), dtype=np.float32)
for u in range(n_users):
for e, freq in enumerate(dot.data[u]):
index = dot.rows[u][e]
if freq >= min_support and index != u:
#    print(u, index, freq)
s[u, index] = sim[u, index]
#    s[u, u] = 1.0
print("lil time: {:.4f}, lil_elements: {}".format(time.time() - t0, s.getnnz()))
return s

if min_support > 0 and not sparse:
item_user_sklearn = {k: list(v.keys()) for k, v in data.items()}
num = sk_num(n_users, item_user_sklearn)
indices = np.where(num < min_support)
sim[indices[0], indices[1]] = 0.0
print("diag sim, ", min(sim.diagonal()))
print("non-zero items: {}, shape: {}".format(sim.getnnz(), sim.shape))

return sim ```