question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

AttributeError of compound kernel of Gaussian processes

See original GitHub issue

Describe the bug

I am trying to use GaussianProcess classifier or regressor using the compound kernel (comprised of RBF and white kernels). Fit method of Gaussian process generates an error regarding its kernel, declaring that ‘CompoundKernel’ object has no attribute ‘k1’. I regenerated the error using the following simpler code:

Steps/Code to Reproduce

import numpy as np
import pandas as pd
from sklearn.gaussian_process import GaussianProcessRegressor
from sklearn.gaussian_process.kernels import CompoundKernel, WhiteKernel, RBF
X1 = np.random.exponential(range(0,10), size = 10)
X2 = np.random.poisson(range(0,10), size = 10)
y = np.random.normal(size = 10)
df = pd.DataFrame()
df["X1"] = X1
df["X2"] = X2
df["y"] = y
df1 = df.iloc[:,0:2]
df2 = df.iloc[:,2]

kernel = CompoundKernel([WhiteKernel(noise_level=2), RBF(length_scale=3)])
gaus = GaussianProcessRegressor(kernel = kernel)
gaus.fit(df1, df2) # executing this line generates the error

Expected Results

AttributeError                            Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_2056/1607188154.py in <module>
     15 kernel = CompoundKernel([WhiteKernel(noise_level=2), RBF(length_scale=3)])
     16 gaus = GaussianProcessRegressor(kernel = kernel)
---> 17 gaus.fit(df1, df2) # executing this line generates the error
......
......
    540             The non-fixed, log-transformed hyperparameters of the kernel
    541         """
--> 542         k_dims = self.k1.n_dims
    543         for i, kernel in enumerate(self.kernels):
    544             kernel.theta = theta[i * k_dims:(i + 1) * k_dims]

AttributeError: 'CompoundKernel' object has no attribute 'k1'

Actual Results

The code works well without using the compound kernel (e.g., RBF only), and the way I built the compound kernel is exactly according to the provided format of sklearn. Not sure why I receive this attribute error.

Versions

System:
    python: 3.7.12 (default, Sep 10 2021, 00:21:48)  [GCC 7.5.0]
executable: /usr/bin/python3
   machine: Linux-5.4.104+-x86_64-with-Ubuntu-18.04-bionic

Python dependencies:
          pip: 21.1.3
   setuptools: 57.4.0
      sklearn: 1.0.1
        numpy: 1.19.5
        scipy: 1.4.1
       Cython: 0.29.24
       pandas: 1.1.5
   matplotlib: 3.2.2
       joblib: 1.1.0
threadpoolctl: 3.0.0

Built with OpenMP: True

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:8 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
glemaitrecommented, Dec 17, 2021

this CompoundKernel module seems to generate error for a classification problem in the same manner

I was probably not clear, you should not use it even in classification. Basically, it is only used in the internal of scikit-learn:

In [1]: from sklearn.datasets import load_iris
   ...: from sklearn.gaussian_process import GaussianProcessClassifier
   ...: from sklearn.gaussian_process.kernels import RBF
   ...: X, y = load_iris(return_X_y=True)
   ...: kernel = 1.0 * RBF(1.0)
   ...: gpc = GaussianProcessClassifier(kernel=kernel,
   ...:         random_state=0).fit(X, y)
In [2]: gpc.kernel_
Out[2]: CompoundKernel(7.71, 1.36, 5.26, 0.669, 6.13, 1.15)
In [3]: gpc.kernel_.get_params()
Out[3]: 
{'kernels': [47.2**2 * RBF(length_scale=3.92),
  13.8**2 * RBF(length_scale=1.95),
  21.4**2 * RBF(length_scale=3.15)]}

Here we indeed have 3 kernels, because the model is trained in a 1-vs-rest manner, thus 1 estimator + 1 kernel for each. But it is transparent to the user.

0reactions
marcozzxx810commented, Jan 14, 2022

take

Read more comments on GitHub >

github_iconTop Results From Across the Web

scikit learn - 'CompoundKernel' object has no attribute 'k1'
'CompoundKernel' object has no attribute 'k1' - error of compound kernel in gaussian process inside ensemble learning · scikit-learn ...
Read more >
sklearn.gaussian_process.GaussianProcessClassifier
Gaussian process classification (GPC) based on Laplace approximation. The implementation is based on Algorithm ... Also kernel cannot be a CompoundKernel .
Read more >
Gaussian processes (3/3) - exploring kernels
A kernel (or covariance function) describes the covariance of the Gaussian process random variables. Together with the mean function the kernel completely ...
Read more >
Kernel Cookbook
If you're looking for software to implement Gaussian process models, I recommend GPML ... However, if you want to construct an interesting composite...
Read more >
A be cannot found parameter positional that - CSDN
"""Kernels for Gaussian process regression and classification. ... from sklearn.gaussian_process.kernels import CompoundKernel ... raise AttributeError(.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found