|
7 | 7 | # |
8 | 8 | # http://www.apache.org/licenses/LICENSE-2.0 |
9 | 9 | # |
10 | | -# Unless required by applicable law or agreed to in writing, |
| 10 | +# Unless required by applicable law or agreed to in writing, software |
11 | 11 | # distributed under the License is distributed on an "AS IS" BASIS, |
12 | 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
13 | 13 | # See the License for the specific language governing permissions and |
14 | 14 | # limitations under the License. |
15 | 15 | # ============================================================================== |
16 | 16 |
|
17 | 17 | import numpy as np |
| 18 | +from scipy import sparse |
| 19 | +from sklearn.metrics.pairwise import polynomial_kernel as sklearn_poly_kernel |
18 | 20 |
|
19 | 21 | from onedal.primitives import poly_kernel as onedal_poly_kernel |
20 | 22 |
|
21 | 23 |
|
22 | | -def poly_kernel(X, Y=None, gamma=1.0, coef0=0.0, degree=3, queue=None): |
| 24 | +def poly_kernel( |
| 25 | + X, |
| 26 | + Y=None, |
| 27 | + degree=3, |
| 28 | + gamma=None, |
| 29 | + coef0=1, |
| 30 | + queue=None, |
| 31 | +): |
23 | 32 | """ |
24 | | - Sklearnex interface for the polynomial kernel using oneDAL backend. |
| 33 | + Compute the polynomial kernel using the oneDAL backend when possible. |
25 | 34 |
|
26 | | - K(x, y) = (gamma * <x, y> + coef0) ** degree |
27 | | - for each pair of rows x in X and y in Y. |
| 35 | + Falls back to scikit-learn's ``polynomial_kernel`` for unsupported cases |
| 36 | + such as sparse inputs. |
28 | 37 |
|
29 | 38 | Parameters |
30 | 39 | ---------- |
31 | 40 | X : array-like of shape (n_samples_X, n_features) |
32 | 41 | Input feature array. |
33 | 42 |
|
34 | 43 | Y : array-like of shape (n_samples_Y, n_features), default=None |
35 | | - Optional second feature array. If None, Y = X. |
36 | | -
|
37 | | - gamma : float, default=1.0 |
38 | | - Scaling factor for the inner product. |
39 | | -
|
40 | | - coef0 : float, default=0.0 |
41 | | - Constant term added to scaled inner product. |
| 44 | + Optional second feature array. If None, ``Y = X``. |
42 | 45 |
|
43 | 46 | degree : int, default=3 |
44 | 47 | Degree of the polynomial kernel. |
45 | 48 |
|
| 49 | + gamma : float, default=None |
| 50 | + Scaling factor for the inner product. If None, ``1 / n_features`` is used. |
| 51 | +
|
| 52 | + coef0 : float, default=1 |
| 53 | + Constant term added to the scaled inner product. |
| 54 | +
|
46 | 55 | queue : SyclQueue or None, default=None |
47 | 56 | Optional SYCL queue for device execution. |
48 | 57 |
|
49 | 58 | Returns |
50 | 59 | ------- |
51 | 60 | kernel_matrix : ndarray of shape (n_samples_X, n_samples_Y) |
52 | | - Polynomial kernel Gram matrix. |
| 61 | + Computed polynomial kernel Gram matrix. |
53 | 62 | """ |
| 63 | + # Fall back to sklearn if sparse input |
| 64 | + if sparse.issparse(X) or (Y is not None and sparse.issparse(Y)): |
| 65 | + return sklearn_poly_kernel(X, Y, degree=degree, gamma=gamma, coef0=coef0) |
| 66 | + |
| 67 | + # Handle gamma default like sklearn |
| 68 | + if gamma is None: |
| 69 | + gamma = 1.0 / X.shape[1] |
| 70 | + |
| 71 | + # Use oneDAL accelerated path |
54 | 72 | return onedal_poly_kernel( |
55 | 73 | X, Y=Y, gamma=gamma, coef0=coef0, degree=degree, queue=queue |
56 | 74 | ) |
0 commit comments