# `sklearn.metrics.pairwise`.pairwise_kernels¶

`sklearn.metrics.pairwise.``pairwise_kernels`(X, Y=None, metric='linear', filter_params=False, n_jobs=1, **kwds)[源代码]

Compute the kernel between arrays X and optional array Y.

This method takes either a vector array or a kernel matrix, and returns a kernel matrix. If the input is a vector array, the kernels are computed. If the input is a kernel matrix, it is returned instead.

This method provides a safe way to take a kernel matrix as input, while preserving compatibility with many other algorithms that take a vector array.

If Y is given (default is None), then the returned matrix is the pairwise kernel between the arrays from both X and Y.

Valid values for metric are::
[‘rbf’, ‘sigmoid’, ‘polynomial’, ‘poly’, ‘linear’, ‘cosine’]

Read more in the User Guide.

Parameters: X : array [n_samples_a, n_samples_a] if metric == “precomputed”, or, [n_samples_a, n_features] otherwise Array of pairwise kernels between samples, or a feature array. Y : array [n_samples_b, n_features] A second feature array only if X has shape [n_samples_a, n_features]. metric : string, or callable The metric to use when calculating kernel between instances in a feature array. If metric is a string, it must be one of the metrics in pairwise.PAIRWISE_KERNEL_FUNCTIONS. If metric is “precomputed”, X is assumed to be a kernel matrix. Alternatively, if metric is a callable function, it is called on each pair of instances (rows) and the resulting value recorded. The callable should take two arrays from X as input and return a value indicating the distance between them. n_jobs : int The number of jobs to use for the computation. This works by breaking down the pairwise matrix into n_jobs even slices and computing them in parallel. If -1 all CPUs are used. If 1 is given, no parallel computing code is used at all, which is useful for debugging. For n_jobs below -1, (n_cpus + 1 + n_jobs) are used. Thus for n_jobs = -2, all CPUs but one are used. filter_params: boolean : Whether to filter invalid parameters or not. `**kwds` : optional keyword parameters Any further parameters are passed directly to the kernel function. K : array [n_samples_a, n_samples_a] or [n_samples_a, n_samples_b] A kernel matrix K such that K_{i, j} is the kernel between the ith and jth vectors of the given matrix X, if Y is None. If Y is not None, then K_{i, j} is the kernel between the ith array from X and the jth array from Y.

Notes

If metric is ‘precomputed’, Y is ignored and X is returned.
{"ft_lang_label":"__label__en","ft_lang_prob":0.776695,"math_prob":0.964768,"size":2523,"snap":"2022-40-2023-06","text_gpt3_token_len":608,"char_repetition_ratio":0.14807463,"word_repetition_ratio":0.024213076,"special_character_ratio":0.2413793,"punctuation_ratio":0.16432866,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9964955,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2023-02-09T08:26:54Z\",\"WARC-Record-ID\":\"<urn:uuid:9c2ce0c2-a24d-40b8-ac4c-ac10c7c655bf>\",\"Content-Length\":\"13556\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:396b7137-d0c6-449e-a526-be092744937a>\",\"WARC-Concurrent-To\":\"<urn:uuid:cd61c198-d199-47fe-9cc5-1b0c30d37b00>\",\"WARC-IP-Address\":\"185.199.110.153\",\"WARC-Target-URI\":\"http://lijiancheng0614.github.io/scikit-learn/modules/generated/sklearn.metrics.pairwise.pairwise_kernels.html\",\"WARC-Payload-Digest\":\"sha1:KQESGKSGYTKCBZ2WHG2M6EIKBV76HOEO\",\"WARC-Block-Digest\":\"sha1:JO47464KQBDGNONWNTD5CLEPSHEW2TQQ\",\"WARC-Identified-Payload-Type\":\"application/xhtml+xml\",\"warc_filename\":\"/cc_download/warc_2023/CC-MAIN-2023-06/CC-MAIN-2023-06_segments_1674764501555.34_warc_CC-MAIN-20230209081052-20230209111052-00816.warc.gz\"}"}