#76: Scikit-learn 73:Supervised Learning 51: Gaussian Process

Поділитися
Вставка
  • Опубліковано 31 жов 2024

КОМЕНТАРІ • 15

  • @undertaker7523
    @undertaker7523 8 місяців тому +1

    Hello, thank you for this series.
    I have a question. Why do we have 3 different functions we sample our priors from? Is the idea that we're sampling each point 3 times and we're using our functions to generate the 3 values at each point? In practice, do we expect that sampling the same point multiple times will result in different values due to normally distributed noise? Are we capturing the mean of those 3 output values at the sample point and determining the mean of those points?

    • @learndataa
      @learndataa  8 місяців тому

      Thanks for watching!
      (1) Why do we have 3 different functions we sample our priors from?
      - Because we want to explore different possibilities or hypotheses about the underlying data generating process. Each sampled function represents a possible function that could describe the data.
      (2) Is the idea that we're sampling each point 3 times and using our functions to generate the 3 values at each point?
      - Yes, exactly. When we sample three functions from the Gaussian Process prior, we are effectively generating three sets of function values at each point in the input space. These values represent different possible outcomes or predictions for the target variable.
      (3) In practice, do we expect that sampling the same point multiple times will result in different values due to normally distributed noise?
      - Yes, that's correct. In Gaussian Process regression, the function values are distributed according to a multivariate Gaussian distribution. This implies that for the same input point, we would expect different function values in general due to the randomness introduced by the Gaussian noise.
      (4) Are we capturing the mean of those 3 output values at the sample point and determining the mean of those points?
      - Yes, in practice, we can capture the mean of the sampled function values at each point to estimate the mean function of the Gaussian Process. This mean function represents the expected value or average behavior of the target variable at each point in the input space. Additionally, we can also compute other statistics such as the variance to assess uncertainty in the predictions.
      Thus, sampling multiple functions from the Gaussian Process prior allows us to explore different hypotheses about the data, and by observing the variations in function values across samples, we can estimate the mean function and assess uncertainty in our predictions.
      Hope it helps!

  • @h3602
    @h3602 10 місяців тому +1

    Thanks for the video

    • @learndataa
      @learndataa  10 місяців тому

      You are welcome. Thanks for watching.

    • @HeeMan007
      @HeeMan007 10 місяців тому +1

      @@learndataa How to optimise the kernel in sklearn for best prediction?

    • @learndataa
      @learndataa  10 місяців тому +1

      It depends. Below are few examples. Hope it helps.
      General steps:
      - Start Simple: Begin with basic kernels and observe how well they capture the data patterns.
      - Data Exploration: Understand the characteristics of your data. For example, does it have smooth regions, sharp transitions, periodic patterns, etc.?
      - Combination: Combine different kernels to capture multiple aspects of the data.
      - Optimization: Use hyperparameter optimization techniques to find the best combination.
      #####################################
      # Example 1: Smooth and Linear Trends
      #####################################
      import numpy as np
      import matplotlib.pyplot as plt
      from sklearn.gaussian_process import GaussianProcessRegressor
      from sklearn.gaussian_process.kernels import RBF, WhiteKernel
      # Generate synthetic data with a linear increasing trend
      np.random.seed(42)
      X = np.sort(10 * np.random.rand(100, 1), axis=0)
      y = 2 * X.ravel() + np.random.normal(0, 0.5, X.shape[0])
      # Define the RBF and White kernels for the linear trend
      linear_trend_kernel = 1.0 * RBF(length_scale=1.0) + WhiteKernel(noise_level=1.0)
      # Create the GaussianProcessRegressor with the linear trend kernel
      gp = GaussianProcessRegressor(kernel=linear_trend_kernel, n_restarts_optimizer=10)
      # Fit the Gaussian process to the data
      gp.fit(X, y)
      # Generate test data for prediction
      X_test = np.linspace(0, 10, 1000)[:, np.newaxis]
      # Make predictions with the Gaussian process
      y_pred, sigma = gp.predict(X_test, return_std=True)
      # Plot the results
      plt.figure(figsize=(10, 6))
      plt.scatter(X, y, c='r', label='Observations')
      plt.plot(X_test, y_pred, 'k', label='Prediction')
      plt.fill_between(X_test.ravel(), y_pred - 1.96 * sigma, y_pred + 1.96 * sigma, alpha=0.2, color='k')
      plt.title('Linear Increasing Trend')
      plt.xlabel('Input')
      plt.ylabel('Output')
      plt.legend()
      plt.show()
      #####################################
      # Example 2: Periodic and Random Noise
      #####################################
      import numpy as np
      import matplotlib.pyplot as plt
      from sklearn.gaussian_process import GaussianProcessRegressor
      from sklearn.gaussian_process.kernels import ExpSineSquared, WhiteKernel
      # Generate synthetic data with a periodic component and random noise
      np.random.seed(42)
      X = np.sort(5 * np.random.rand(100, 1), axis=0)
      y = np.sin(X).ravel() + np.random.normal(0, 0.1, X.shape[0])
      # Define the ExpSineSquared kernel for the periodic component
      periodic_kernel = ExpSineSquared(length_scale=1.0, periodicity=1.0)
      # Define the White kernel for random noise
      noise_kernel = WhiteKernel(noise_level=0.1)
      # Combine the kernels (sum of kernels)
      combined_kernel = periodic_kernel + noise_kernel
      # Create the GaussianProcessRegressor with the combined kernel
      gp = GaussianProcessRegressor(kernel=combined_kernel, n_restarts_optimizer=10)
      # Fit the Gaussian process to the data
      gp.fit(X, y)
      # Generate test data for prediction
      X_test = np.linspace(0, 5, 1000)[:, np.newaxis]
      # Make predictions with the Gaussian process
      y_pred, sigma = gp.predict(X_test, return_std=True)
      # Plot the results
      plt.figure(figsize=(10, 6))
      plt.scatter(X, y, c='r', label='Observations')
      plt.plot(X_test, y_pred, 'k', label='Prediction')
      plt.fill_between(X_test.ravel(), y_pred - 1.96 * sigma, y_pred + 1.96 * sigma, alpha=0.2, color='k')
      plt.title('Periodic Pattern with Random Noise')
      plt.xlabel('Input')
      plt.ylabel('Output')
      plt.legend()
      plt.show()
      #####################################
      # Example 3: Exponential Growth and Sudden Change
      #####################################
      import numpy as np
      import matplotlib.pyplot as plt
      from sklearn.gaussian_process import GaussianProcessRegressor
      from sklearn.gaussian_process.kernels import ExpSineSquared, RationalQuadratic
      # Generate synthetic data with exponential growth and a sudden change
      np.random.seed(42)
      X = np.sort(5 * np.random.rand(100, 1), axis=0)
      y = 2 * np.exp(0.5 * X).ravel() + np.random.normal(0, 0.1, X.shape[0])
      # Define the ExpSineSquared kernel for the exponential growth
      exp_growth_kernel = ExpSineSquared(length_scale=1.0, periodicity=1.0)
      # Define the RationalQuadratic kernel for the sudden change
      sudden_change_kernel = RationalQuadratic(length_scale=1.0, alpha=1.0)
      # Combine the kernels (product of kernels)
      combined_kernel = exp_growth_kernel * sudden_change_kernel
      # Create the GaussianProcessRegressor with the combined kernel
      gp = GaussianProcessRegressor(kernel=combined_kernel, n_restarts_optimizer=10)
      # Fit the Gaussian process to the data
      gp.fit(X, y)
      # Generate test data for prediction
      X_test = np.linspace(0, 5, 1000)[:, np.newaxis]
      # Make predictions with the Gaussian process
      y_pred, sigma = gp.predict(X_test, return_std=True)
      # Plot the results
      plt.figure(figsize=(10, 6))
      plt.scatter(X, y, c='r', label='Observations')
      plt.plot(X_test, y_pred, 'k', label='Prediction')
      plt.fill_between(X_test.ravel(), y_pred - 1.96 * sigma, y_pred + 1.96 * sigma, alpha=0.2, color='k')
      plt.title('Exponential Growth with Sudden Change')
      plt.xlabel('Input')
      plt.ylabel('Output')
      plt.legend()
      plt.show()
      #####################################
      # Example 4: Complicated trend
      #####################################
      import numpy as np
      import matplotlib.pyplot as plt
      from sklearn.gaussian_process import GaussianProcessRegressor
      from sklearn.gaussian_process.kernels import RBF, ExpSineSquared, Matern, ConstantKernel as C
      # Generate synthetic data with a complex pattern
      np.random.seed(42)
      X = np.sort(10 * np.random.rand(100, 1), axis=0)
      y = np.sin(2 * np.pi * X) + np.cos(4 * np.pi * X) + np.random.normal(0, 0.2, (X.shape[0], 1))
      # Define a combination of five kernels with different length scales
      combined_kernel = C(1.0, (1e-1, 1e1)) * RBF(1.0, (1e-1, 1e1)) + \
      C(1.0, (1e-1, 1e1)) * ExpSineSquared(1.0, 1.0, (1e-1, 1e1)) + \
      Matern(length_scale=1.0, nu=2.5) + \
      RBF(1.0, (1e-1, 1e1)) * ExpSineSquared(1.0, 1.0, (1e-1, 1e1))
      # Create the GaussianProcessRegressor with the combined kernel
      gp = GaussianProcessRegressor(kernel=combined_kernel, n_restarts_optimizer=10)
      # Fit the Gaussian process to the data
      gp.fit(X, y)
      # Generate test data for prediction
      X_test = np.linspace(0, 10, 100)[:, np.newaxis] # Ensure X_test has the same number of samples as X
      # Make predictions with the Gaussian process
      y_pred, sigma = gp.predict(X_test, return_std=True)
      # Plot the results
      plt.figure(figsize=(10, 6))
      plt.scatter(X, y, c='r', label='Observations')
      plt.plot(X_test, y_pred, 'k', label='Prediction')
      plt.fill_between(X_test.ravel(), y_pred - 1.96 * sigma, y_pred + 1.96 * sigma, alpha=0.2, color='k')
      plt.title('Combined Kernels with Adjusted Hyperparameters')
      plt.xlabel('Input')
      plt.ylabel('Output')
      plt.legend()
      plt.show()

  • @arulprakash5589
    @arulprakash5589 5 місяців тому +1

    Thanks for the video.
    I have a question about scikit learn GP.
    I have multiple observations of the heart pressure traces. Can it be fitted to a single Gaussian Process to capture the uncertainty among multiple observations.
    I need multiple observations to be fitted to a single GP. But When I use scikit learn to fit, I am getting mean and covariance matrix for each pressure trace !!
    Thank you :)

    • @learndataa
      @learndataa  3 місяці тому

      Appreciate your support. It means a lot. Thanks for watching.
      To answer your question, I've tried to put together a code below. Hope it helps!
      import numpy as np
      import matplotlib.pyplot as plt
      from sklearn.gaussian_process import GaussianProcessRegressor
      from sklearn.gaussian_process.kernels import RBF, ConstantKernel as C
      # Create data
      np.random.seed(42)
      X1 = np.linspace(0, 10, 100).reshape(-1, 1) # 100 time points for observation 1
      X2 = np.linspace(0, 10, 100).reshape(-1, 1) # 100 time points for observation 2
      X3 = np.linspace(0, 10, 100).reshape(-1, 1) # 100 time points for observation 3
      # Pressure waves
      y1 = np.sin(X1).ravel() + np.random.normal(0, 0.1, X1.shape[0])
      y2 = np.sin(X2 - 1).ravel() + np.random.normal(0, 0.1, X2.shape[0])
      y3 = np.sin(X3 + 1).ravel() + np.random.normal(0, 0.1, X3.shape[0])
      # Put it together
      y = np.concatenate([y1, y2, y3])
      X_combined = np.vstack([X1, X2, X3])
      # Create a kernel: Constant kernel * RBF kernel
      kernel = C(1.0, (1e-4, 1e1)) * RBF(1.0, (1e-4, 1e1))
      # Initialize and fit GP
      gp = GaussianProcessRegressor(kernel=kernel, n_restarts_optimizer=10)
      gp.fit(X_combined, y)
      # Predict
      mean, covariance = gp.predict(X1, return_cov=True)
      # SD and COV
      std_dev = np.sqrt(np.diag(covariance))
      # Plot
      plt.figure(figsize=(10, 6))
      # Original data
      plt.plot(X1, y1, 'r.', markersize=10, label='Observation 1')
      plt.plot(X2, y2, 'g.', markersize=10, label='Observation 2')
      plt.plot(X3, y3, 'b.', markersize=10, label='Observation 3')
      # Predicted GP mean
      plt.plot(X1, mean, 'k-', label='GP Mean')
      # CI of GP
      plt.fill_between(X1.ravel(), mean - 1.96 * std_dev, mean + 1.96 * std_dev, color='gray', alpha=0.2, label='95% Confidence Interval')
      plt.title('Gaussian Process Regression on Multiple Observations')
      plt.xlabel('Time')
      plt.ylabel('Pressure')
      plt.legend()
      plt.show()

  • @user-ce3ip5lx9t
    @user-ce3ip5lx9t 3 місяці тому +1

    thanks a lot! is the jupyter notebook available?

    • @learndataa
      @learndataa  3 місяці тому +1

      All the notebooks from scikit-learn docs are available at link below:
      github.com/learndataa/scikit-learn-docs/tree/main/notebooks/auto_examples/gaussian_process
      While these notebooks may not be exactly those in the video, the are well commented.
      Hope it helps!

    • @user-ce3ip5lx9t
      @user-ce3ip5lx9t 3 місяці тому

      @@learndataa great and thanks a lot!!!

    • @user-ce3ip5lx9t
      @user-ce3ip5lx9t 3 місяці тому

      @@learndataahowever I get an error using your link 😕

    • @learndataa
      @learndataa  3 місяці тому

      @@user-ce3ip5lx9t I was able to recreate the error when not logged in to Github. Could you try logging in?

    • @user-ce3ip5lx9t
      @user-ce3ip5lx9t 3 місяці тому

      @@learndataa in github I see your scikit learn repository but it appears empty to me

  • @ganesanelumalai3596
    @ganesanelumalai3596 2 роки тому +1

    Thank you for the video. Can you please explain with a dataset? Import a dataset, calculate mean and standard deviation, and visualize the GP with mean, mean+, mean-, and standard deviation?