sklearn.metrics.pairwise.linear_kernel(X, Y=None, dense_output=True)[source]#

Compute the linear kernel between X and Y.

Read more in the User Guide.

X{array-like, sparse matrix} of shape (n_samples_X, n_features)

A feature array.

Y{array-like, sparse matrix} of shape (n_samples_Y, n_features), default=None

An optional second feature array. If None, uses Y=X.

dense_outputbool, default=True

Whether to return dense output even when the input is sparse. If False, the output is sparse if both input arrays are sparse.

Added in version 0.20.

kernelndarray of shape (n_samples_X, n_samples_Y)

The Gram matrix of the linear kernel, i.e. X @ Y.T.


>>> from sklearn.metrics.pairwise import linear_kernel
>>> X = [[0, 0, 0], [1, 1, 1]]
>>> Y = [[1, 0, 0], [1, 1, 0]]
>>> linear_kernel(X, Y)
array([[0., 0.],
       [1., 2.]])