# Scikit learn Gaussian – Everything you need to know

In this Python tutorial, we will learn How Scikit learn Gaussian works in python and we will also cover different examples related to Scikit learn Gaussian. And, we will cover these topics.

• Scikit learn Gaussian
• Scikit learn Gaussian mixture model
• Scikit learn Gaussian process
• Scikit learn Gaussian regression
• Scikit learn Gaussian regression example
• Scikit learn Gaussian Naive Bayes
• Scikit learn Gaussian Kernel
• Scikit learn Gaussian classifier
• Scikit learn Gaussian process classifier
• Scikit learn Gaussian process RBF Kernel

Before moving forward in this tutorial, we recommend you to read What is Scikit Learn in Python.

## Scikit learn Gaussian

In this section, we will learn about how Scikit learn Gaussian works in python.

• Scikit learn Gaussian is a supervised machine learning model. It is used to solve regression and classification problems.
• The Gaussian process is also defined as a finite group of a random variable that has multivariate distribution.

Code:

In the following code, we will import some libraries from which we can solve the regression problem.

• X = num.linspace(start=0, stop=12, num=1_000).reshape(-1, 1) is used to create a linespace.
• plot.plot(X, Y, label=r”$f(x) = x \sin(x)$”, linestyle=”dotted”) is used to plot the graph on the screen.
• plot.xlabel(“$x$”) is used to plot the xlabel.
• plot.ylabel(“$f(x)$”) is used to plot the y label.
• Gaussian = plot.title(“True Generative Process”) is used to give the title to the graph.
import numpy as num

X = num.linspace(start=0, stop=12, num=1_000).reshape(-1, 1)
Y = num.squeeze(X * num.sin(X))
import matplotlib.pyplot as plot

plot.plot(X, Y, label=r"$f(x) = x \sin(x)$", linestyle="dotted")
plot.legend()
plot.xlabel("$x$")
plot.ylabel("$f(x)$")
Gaussian = plot.title("True Generative Process")
• range = num.random.RandomState(1) is used to generate the random number.
• X_train, Y_train = X[training_indices], Y[training_indices] is used tocreate the training and testing data.
• gaussianprocess.fit(X_train, Y_train) is used to fit the model.
• plot.plot(X, Y, label=r”$f(x) = x \sin(x)$”, linestyle=”dotted”) is used to plot the graph.
• plot.scatter(X_train, Y_train, label=”Observations”) is used to plot the scatter plot.
• plot.xlabel(“$x$”) is used to plot x label.
• plot.ylabel(“$f(x)$”) is used to plot the y label.
• plot.title(“Gaussian Process Regression on noise-free dataset”) is used to give the title to the graph.
range = num.random.RandomState(1)
training_indices = range.choice(num.arange(Y.size), size=8, replace=False)
X_train, Y_train = X[training_indices], Y[training_indices]
from sklearn.gaussian_process import GaussianProcessRegressor
from sklearn.gaussian_process.kernels import RBF

kernel = 1 * RBF(length_scale=1.0, length_scale_bounds=(1e-2, 1e2))
gaussianprocess = GaussianProcessRegressor(kernel=kernel, n_restarts_optimizer=9)
gaussianprocess.fit(X_train, Y_train)
gaussianprocess.kernel_
mean_prediction, std_prediction = gaussian_process.predict(X, return_std=True)

plot.plot(X, Y, label=r"$f(x) = x \sin(x)$", linestyle="dotted")
plot.scatter(X_train, Y_train, label="Observations")
plot.plot(X, mean_prediction, label="Mean Prediction")
plot.fill_between(
X.ravel(),
mean_prediction - 1.98 * std_prediction,
mean_prediction + 1.98 * std_prediction,
alpha=0.7,
label=r"95% confidence interval",
)
plot.legend()
plot.xlabel("$x$")
plot.ylabel("$f(x)$")
Gaussian = plot.title("Gaussian Process Regression on noise-free dataset")

After running the above code, we get the following output in which we can see the Gaussian Process Regression on the noise-free dataset.

Also, check: Scikit-learn logistic regression

## Scikit learn Gaussian mixture model

In this section, we will learn about how Scikit learn Gaussian mixture model works in python.

• Scikit learn Gaussian mixture model is used to define the process which represent the probability distribution of the gaussian model.
• Gaussian Mixture also allow to evaluate the parameter of the model.

Code:

In the following code, we will import some libraries which it represents the probability distribution of the Gaussian model.

• num.random.seed(0) is used to generate the random sample of two components.
• shiftedgaussian = num.random.randn(n_samples, 2) + num.array([25, 25]) is used to generate spherical data.
• stretchedgaussian = num.dot(num.random.randn(n_samples, 2), c) is used to generate zero centered of stretched gaussian data.
• X_train = num.vstack([shiftedgaussian, stretchedgaussian]) is used to concentrate the two datasets into the final training set.
• classifier = mixture.GaussianMixture(n_components=2, covariance_type=”full”) is used to fit a gaussian mixture model with two components.
• cs=plot.contour(X,Y,Z,norm=LogNorm(vmin=2.0,vmax=1000.0),levels=num.logspace(0, 3, 10) ) is used to predict the score as a counter plot.
• plot.scatter(X_train[:, 0], X_train[:, 1], 0.10) is used to plot the scatter plot.
• plot.title(“Negative log-likelihood predicted by a Gaussian Mixture Method”) is used toplot the title on the graph.
• plot.axis(“tight”) is used to plot the axis on the graph.
import numpy as num
import matplotlib.pyplot as plot
from matplotlib.colors import LogNorm
from sklearn import mixture

n_samples = 350

num.random.seed(0)

shiftedgaussian = num.random.randn(n_samples, 2) + num.array([25, 25])

c = num.array([[0.0, -0.9], [3.5, 0.9]])
stretchedgaussian = num.dot(num.random.randn(n_samples, 2), c)

X_train = num.vstack([shiftedgaussian, stretchedgaussian])

classifier = mixture.GaussianMixture(n_components=2, covariance_type="full")
classifier.fit(X_train)

x = num.linspace(-25.0, 35.0)
y = num.linspace(-25.0, 45.0)
X, Y = num.meshgrid(x, y)
XX = num.array([X.ravel(), Y.ravel()]).T
Z = -classifier.score_samples(XX)
Z = Z.reshape(X.shape)

cs = plot.contour(
X, Y, Z, norm=LogNorm(vmin=2.0, vmax=1000.0), levels=num.logspace(0, 3, 10)
)
cb = plot.colorbar(cs, shrink=0.10, extend="both")
plot.scatter(X_train[:, 0], X_train[:, 1], 0.10)

plot.title("Negative log-likelihood predicted by a Gaussian Mixture Method")
plot.axis("tight")
plot.show()

Output:

After running the above code, we get the following output in which we can see that the Gaussian mixture method is predicated on the screen.

## Scikit learn Gaussian process

In this section, we will learn about how Scikit learn Gaussian process works in python.

• Scikit learn Gaussian processes works with the regression and classification both and with the help of this here we can create a discrete data structure.
• The discrete data structure is defined as data is in the discrete form.

Code:

In the following code, we will import some libraries from which we can create a discrete data structure with the help of the Gaussian process.

• self.baseline_similarity_bounds = baseline_similarity_bounds is used to create the baseline bounds.
• return sum([1.0 if c1 == c2 else self.baseline_similarity for c1 in s1 for c2 in s2]) is used return the kernel value between a pair of sequence.
• plot.figure(figsize=(8, 5)) is used to plot the figure on the screen.
• plot.xticks(num.arange(len(X)), X) is used to plot the x ticks on the graph.
• plot.yticks(num.arange(len(X)), X) is used to plot the y ticks on the graph.
• plot.title(“Sequence similarity Under kernel”) is used to give the title on the screen.
• gaussianprocess = GaussianProcessRegressor(kernel=kernel) is used to process the Gaussian regressor.
• gaussianprocess.fit(X[training_idx], Y[training_idx]) is used to fit the model.
• plot.bar(num.arange(len(X)), gp.predict(X), color=”r”, label=”prediction”) is used to plot the bar on the screen.
• plot.xticks(num.arange(len(X)), X) is used to plot the x ticks.
• plot.title(“Regression On Sequences”) is used to givethe title to the graph.
• gaussianprocess = GaussianProcessClassifier(kernel) is used to process the gaussian classifier.
• plot.figure(figsize=(8, 5)) is used to plot the figure on the screen.
import numpy as num
import matplotlib.pyplot as plot
from sklearn.gaussian_process.kernels import Kernel, Hyperparameter
from sklearn.gaussian_process.kernels import GenericKernelMixin
from sklearn.gaussian_process import GaussianProcessRegressor
from sklearn.gaussian_process import GaussianProcessClassifier
from sklearn.base import clone

class SequenceKernel(GenericKernelMixin, Kernel):

def __init__(self, baseline_similarity=0.6, baseline_similarity_bounds=(1e-5, 1)):
self.baseline_similarity = baseline_similarity
self.baseline_similarity_bounds = baseline_similarity_bounds

def hyperparameter_baseline_similarity(self):
return Hyperparameter(
"baseline_similarity", "numeric", self.baseline_similarity_bounds
)

def _f(self, s1, s2):
return sum(
[1.0 if c1 == c2 else self.baseline_similarity for c1 in s1 for c2 in s2]
)

def _g(self, s1, s2)
return sum([0.0 if c1 == c2 else 1.0 for c1 in s1 for c2 in s2])

if Y is None:
Y = X

return (
num.array([[self._f(x, y) for y in Y] for x in X]),
num.array([[[self._g(x, y)] for y in Y] for x in X]),
)
else:
return num.array([[self._f(x, y) for y in Y] for x in X])

def diag(self, X):
return num.array([self._f(x, x) for x in X])

def is_stationary(self):
return False

def clone_with_theta(self, theta):
cloned = clone(self)
cloned.theta = theta
return cloned

kernel = SequenceKernel()

X = num.array(["AGCT", "AGC", "AACT", "TAA", "AAA", "GAACA"])

K = kernel(X)
D = kernel.diag(X)

plot.figure(figsize=(8, 5))
plot.imshow(num.diag(D ** -0.5).dot(K).dot(num.diag(D ** -0.5)))
plot.xticks(num.arange(len(X)), X)
plot.yticks(num.arange(len(X)), X)
plot.title("Sequence similarity Under kernel")

"""
Regression

"""

X = num.array(["AGCT", "AGC", "AACT", "TAA", "AAA", "GAACA"])
Y = num.array([2.0, 2.0, 3.0, 3.0, 4.0, 4.0])

training_idx = [1, 2, 3, 4]
gaussianprocess = GaussianProcessRegressor(kernel=kernel)
gaussianprocess.fit(X[training_idx], Y[training_idx])

plot.figure(figsize=(8, 5))
plot.bar(num.arange(len(X)), gp.predict(X), color="r", label="prediction")
plot.bar(training_idx, Y[training_idx], width=0.2, color="y", alpha=1, label="training")
plot.xticks(num.arange(len(X)), X)
plot.title("Regression On Sequences")
plot.legend()

"""
Classification
"""

X_train = num.array(["AGCT", "CGA", "TAAC", "TCG", "CTTT", "TGCT"])
Y_train = num.array([True, True, True, False, False, False])

gaussianprocess  = GaussianProcessClassifier(kernel)
gaussianprocess.fit(X_train, Y_train)

X_test = ["AAA", "ATAG", "CTC", "CT", "C"]
Y_test = [True, True, False, False, False]

plot.figure(figsize=(8, 5))
plot.scatter(
num.arange(len(X_train)),
[1.0 if c else -1.0 for c in Y_train],
s=100,
marker="o",
edgecolor="none",
facecolor=(1, 0.75, 0),
label="training",
)
plot.scatter(
len(X_train) + num.arange(len(X_test)),
[1.0 if c else -1.0 for c in Y_test],
s=100,
marker="o",
edgecolor="none",
facecolor="b",
label="truth",
)
plot.scatter(
len(X_train) + num.arange(len(X_test)),
[1.0 if c else -1.0 for c in gp.predict(X_test)],
s=100,
marker="x",
edgecolor=(0, 2.0, 0.4),
linewidth=2,
label="prediction",
)
plot.xticks(num.arange(len(X_train) + len(X_test)), num.concatenate((X_train, X_test)))
plot.yticks([-1, 1], [False, True])
plot.title("Classification On Sequences")
plot.legend()

plot.show()

Output:

After running the above code, we get the following output in which we can see that the discrete data structure is drawn on the screen with the help of scikit learn Gaussian process.

## Scikit learn Gaussian regression

In this section, we will learn about how Scikit learn Gaussian Regression works in python.

Scikit learn Gaussian regression is defined as a non-parametric approach that creates waves in the region of machine learning.

Code:

In the following code, we will import some libraries from which we can create a gaussian regressor.

• x, y = make_friedman2(n_samples=550, noise=0, random_state=0) is used to make friedman.
• gaussianProcessregression=GaussianProcessRegressor(kernel=kernel,random_state=0).fit(x, y) is used to create a gaussian regressor and fit the model.
• gaussianProcessregression.score(x, y) is used to count the score.
from sklearn.datasets import make_friedman2
from sklearn.gaussian_process import GaussianProcessRegressor
from sklearn.gaussian_process.kernels import DotProduct, WhiteKernel
x, y = make_friedman2(n_samples=550, noise=0, random_state=0)
kernel = DotProduct() + WhiteKernel()
gaussianProcessregression = GaussianProcessRegressor(kernel=kernel,
random_state=0).fit(x, y)
gaussianProcessregression.score(x, y)

Output:

After running the above code, we get the following output in which we can see that the Gaussian regressor score is printed on the screen.

## Scikit learn Gaussian regression example

In this section, we will learn about Scikit learn Gaussian Regression example works in python.

• Scikit learn Gaussian as a finite group of a random variable that has multivariate distribution.
• Scikit learn Gaussian regression is defined as a bayesian approach that creates waves in the region.

Code:

In the following code, we will import some libraries from which we can create a regressor graph.

• x = num.linspace(0, 7, num=30).reshape(-1, 1) is used to create a linspace.
• y = target_generator(x, add_noise=False) is used to create a target generator.
• Gaussianregression= plot.ylabel(“y”) is used to plot the y label on the graph.
• range = num.random.RandomState(0) is used to generate the random number.
• plot.plot(x, y, label=”Expected signal”) is used to plot an expected signal.
• plot.xlabel(“x”) is used to plotthe x label.
• gaussianprocessregression = GaussianProcessRegressor(kernel=kernel, alpha=0.0) is used to create a gaussian regressor.
• y_mean, y_std = gaussianprocessregression.predict(x, return_std=True) is used to predict the model.
import numpy as num
target = 0.7 + num.sin(5 * x)
range = num.random.RandomState(1)
target += range.normal(0, 0.5, size=target.shape)
return target.squeeze()
x = num.linspace(0, 7, num=30).reshape(-1, 1)
import matplotlib.pyplot as plot

Gaussianregression= plot.ylabel("y")
range = num.random.RandomState(0)
x_train = range.uniform(0, 5, size=20).reshape(-1, 1)
plot.plot(x, y, label="Expected signal")
plot.scatter(
x=x_train[:, 0],
y=y_train,
color="red",
alpha=0.4,
label="Observations",
)
plot.legend()
plot.xlabel("x")
Gaussianregression = plot.ylabel("y")
from sklearn.gaussian_process import GaussianProcessRegressor
from sklearn.gaussian_process.kernels import RBF, WhiteKernel

kernel = 1.0 * RBF(length_scale=1e1, length_scale_bounds=(1e-2, 1e3)) + WhiteKernel(
noise_level=1, noise_level_bounds=(1e-5, 1e1)
)

gaussianprocessregression = GaussianProcessRegressor(kernel=kernel, alpha=0.0)
gaussianprocessregression.fit(x_train, y_train)
y_mean, y_std = gaussianprocessregression.predict(x, return_std=True)

Output:

After running the above code, we get the following output in which we can see that the Gaussian regressor example is plotted on the screen.

## Scikit learn Gaussian Naive Bayes

In this section, we will learn about how scikit learn Gaussian Naive Bayes works in python.

• Gaussian Naive Bayes is defined as the process which supports continuous value characteristics.
• It creates a simple model and fits this model by simply finding the mean and standard deviation of the points.

Code:

In the following code, we will import some libraries from which to create a Gaussian Naive Bayes classifier.

• n_samples = 50000 is used to create n samples.
• n_bins = 5 the use of 5 bins for the calibration curve.
• x, y = make_blobs(n_samples=n_samples, centers=centers, shuffle=False, random_state=42) is used to generate the 5 blobs and the half of the blobs are positive and half are negative.
• sample_weight = np.random.RandomState(42).rand(y.shape[0]) is used to generate the random samples.
• x_train, x_test, y_train, y_test, sw_train, sw_test = train_test_split(x, y, sample_weight, test_size=0.9, random_state=42 ) is used to split the dataset into train data and test data.
• classifier.fit(x_train, y_train) it defines that the gaussian does not support sample weights.
• classifier_isotonic = CalibratedClassifierCV(classifier, cv=2, method=”isotonic”) is used to define gaussian naive bayes with isotonic calibration.
• classifier_sigmoid = CalibratedClassifierCV(classifier, cv=2, method=”sigmoid”) is used to define as gaussian naive bayes with sigmoid calibration.
• plot.figure() is used to plot the figure on the screen.
• plot.scatter() is used to plot the scatter plot.
• plot.title(“Data”) is used to give thetitle to the graph.
import numpy as num
import matplotlib.pyplot as plot
from matplotlib import cm

from sklearn.datasets import make_blobs
from sklearn.naive_bayes import GaussianNB
from sklearn.metrics import brier_score_loss
from sklearn.calibration import CalibratedClassifierCV
from sklearn.model_selection import train_test_split

n_samples = 50000
n_bins = 5

centers = [(-7, -7), (0, 0), (7, 7)]
x, y = make_blobs(n_samples=n_samples, centers=centers, shuffle=False, random_state=42)

y[: n_samples // 2] = 0
y[n_samples // 2 :] = 1
sample_weight = np.random.RandomState(42).rand(y.shape[0])

x_train, x_test, y_train, y_test, sw_train, sw_test = train_test_split(
x, y, sample_weight, test_size=0.9, random_state=42
)

classifier = GaussianNB()
classifier.fit(x_train, y_train)
probability_pos_classifier = classifier.predict_proba(x_test)[:, 1]

classifier_isotonic = CalibratedClassifierCV(classifier, cv=2, method="isotonic")
classifier_isotonic.fit(x_train, y_train, sample_weight=sw_train)
probability_pos_isotonic = classifier_isotonic.predict_proba(X_test)[:, 1]

classifier_sigmoid = CalibratedClassifierCV(classifier, cv=2, method="sigmoid")
classifier_sigmoid.fit(x_train, y_train, sample_weight=sw_train)
probability_pos_sigmoid = classifier_sigmoid.predict_proba(x_test)[:, 1]

print("Brier Score Losses: (the smaller the better)")

classifier_score = brier_score_loss(y_test, probability_pos_classifier, sample_weight=sw_test)
print("No Calibration: %1.3f" % clf_score)

classifier_isotonic_score = brier_score_loss(y_test, probability_pos_isotonic, sample_weight=sw_test)
print("With Isotonic Calibration: %1.3f" % classifier_isotonic_score)

classifier_sigmoid_score = brier_score_loss(y_test, probability_pos_sigmoid, sample_weight=sw_test)
print("With Sigmoid Calibration: %1.3f" % classifier_sigmoid_score)

plot.figure()
y_unique = num.unique(y)
colors = cm.rainbow(num.linspace(0.0, 1.0, y_unique.size))
for this_y, color in zip(y_unique, colors):
this_x = x_train[y_train == this_y]
this_sw = sw_train[y_train == this_y]
plot.scatter(
this_x[:, 0],
this_x[:, 1],
s=this_sw * 50,
c=color[num.newaxis, :],
alpha=0.5,
edgecolor="y",
label="Class %s" % this_y,
)
plot.legend(loc="best")
plot.title("Data")

plot.figure()
order = num.lexsort((probability_pos_classifier,))
plot.plot(prob_pos_clf[order], "b", label="No Calibration (%1.3f)" % classifier_score)
plot.plot(
prob_pos_isotonic[order],
"g",
linewidth=3,
label="Isotonic Calibration (%1.3f)" % classifier_isotonic_score,
)
plot.plot(
probability_pos_sigmoid[order],
"y",
linewidth=3,
label="Sigmoid Calibration (%1.3f)" % classifier_sigmoid_score,
)
plot.plot(
num.linspace(0, y_test.size, 51)[1::2],
y_test[order].reshape(25, -1).mean(1),
"r",
linewidth=3,
label=r"Empirical",
)
plot.ylim([-0.05, 1.05])
plot.xlabel("Instances Sorted According to Predicted Probability (uncalibrated GNB)")
plot.ylabel("P(y=1)")
plot.legend(loc="upper left")
plot.title("Gaussian naive Bayes probabilities")

plot.show()

Output:

After running the above code, we get the following output in which we can see that the gaussian naive Bayes graph is plotted on the screen.

## Scikit learn Gaussian Kernel

In this section, we will learn about how Scikit learn Gaussian Kernel works in python.

Scikit learn Gaussian kernel is defined as a process in which sigma determines the width of the kernel.

Code:

In the following code, we will import some libraries from which we can calculate the score through the Gaussian kernel.

• kernel = 1.0 * RBF(1.0) is used to calculate the kernel.
• gaussianprocessclassifier = GaussianProcessClassifier(kernel=kernel, random_state=0).fit(x, y) is used to fit the model.
• gaussianprocessclassifier.score(x, y) is used to calculate the score.
from sklearn.gaussian_process import GaussianProcessClassifier
from sklearn.gaussian_process.kernels import RBF
kernel = 1.0 * RBF(1.0)
gaussianprocessclassifier = GaussianProcessClassifier(kernel=kernel,
random_state=0).fit(x, y)
gaussianprocessclassifier.score(x, y)

Output:

After running the above code, we get the following output in which we can see that the Gaussian kernel score is printed on the screen.

## Scikit learn Gaussian classifier

In this section, we will learn about how Scikit learn Gaussian classifier works in python.

Scikit learn Gaussian classifier is defined as a productive approach that makes an effort to model as an input class conditional distribution.

Code:

In the following code, we will import some libraries from which we can predict the probability of the Gaussian classifier.

• gaussianprocessclassifier = GaussianProcessClassifier(kernel=kernel, random_state=0).fit(x, y) is used to fit the classifier model.
• gaussianclassifier.predict_proba(x[:2,:]) is used to predict the probability of classifier.
from sklearn.gaussian_process import GaussianProcessClassifier
from sklearn.gaussian_process.kernels import RBF
kernel = 1.0 * RBF(1.0)
gaussianclassifier = GaussianProcessClassifier(kernel=kernel,
random_state=0).fit(x, y)
gaussianclassifier.score(x, y)
gaussianclassifier.predict_proba(x[:2,:])

Output:

After running the above code, we get the following output in which we can see that the probability of a Gaussian classifier is predicted on the screen.

## Scikit learn Gaussian process classifier

In this section, we will learn about how Scikit learn Gaussian process classifier works in python.

Scikit learn Gaussian process classifier is defined as a Laplace approximation and a productive approach that supports the multiple class classification.

Code:

In the following code, we will import some libraries from which we can make graphs with the help of a Gaussian process classifier.

• X = iris.data[:, :2] is used to take first two features.
• gaussianprocessclassifier_rbf_isotropic=GaussianProcessClassifier(kernel=kernel).fit(X, y) is used to fit the model.
• titles = [“Isotropic RBF”, “Anisotropic RBF”] is used to give the title to the graph.
• Z = classifier.predict_proba(num.c_[xx.ravel(), yy.ravel()]) is used to predict the probability
• plot.imshow(Z, extent=(x_min, x_max, y_min, y_max), origin=”lower”) is used to plot the graph.
• plot.scatter(X[:, 0], X[:, 1], c=num.array([“y”, “c”, “g”])[y], edgecolors=(0, 0, 0)) is used to plot the scatter plot.
• plot.xlabel(“Sepal length”) is used to plot the x label.
• plot.title(“%s,LML:%.3f(titles[i],classifier.log_marginal_likelihood(classifier.kernel_.theta)) ) is used to plot the title on the graph.
import numpy as num
import matplotlib.pyplot as plot
from sklearn import datasets
from sklearn.gaussian_process import GaussianProcessClassifier
from sklearn.gaussian_process.kernels import RBF

X = iris.data[:, :2]
y = num.array(iris.target, dtype=int)

h = 0.04
kernel = 1.0 * RBF([1.0])
gaussianprocessclassifier_rbf_isotropic = GaussianProcessClassifier(kernel=kernel).fit(X, y)
kernel = 1.0 * RBF([1.0, 1.0])
gaussianprocessclassifier_rbf_anisotropic = GaussianProcessClassifier(kernel=kernel).fit(X, y)

x_min, x_max = X[:, 0].min() - 1, X[:, 0].max() + 1
y_min, y_max = X[:, 1].min() - 1, X[:, 1].max() + 1
xx, yy = num.meshgrid(num.arange(x_min, x_max, h), num.arange(y_min, y_max, h))

titles = ["Isotropic RBF", "Anisotropic RBF"]
plot.figure(figsize=(12, 7))
for i, classifier in enumerate((gaussianprocessclassifier_rbf_isotropic, gaussianprocessclassifier_rbf_anisotropic)):

plot.subplot(1, 2, i + 1)

Z = classifier.predict_proba(num.c_[xx.ravel(), yy.ravel()])

Z = Z.reshape((xx.shape[0], xx.shape[1], 3))
plot.imshow(Z, extent=(x_min, x_max, y_min, y_max), origin="lower")

plot.scatter(X[:, 0], X[:, 1], c=num.array(["y", "c", "g"])[y], edgecolors=(0, 0, 0))
plot.xlabel("Sepal length")
plot.ylabel("Sepal width")
plot.xlim(xx.min(), xx.max())
plot.ylim(yy.min(), yy.max())
plot.xticks(())
plot.yticks(())
plot.title(
"%s, LML: %.3f" % (titles[i], classifier.log_marginal_likelihood(classifier.kernel_.theta))
)

plot.tight_layout()
plot.show()

Output:

After running the above code, we get the following output in which we can see that the Gaussian process classifier graph is plotted on the screen.

## Scikit learn Gaussian process RBF Kernel

In this section, we will learn about how Scikit learn Gaussian process RBF kernel works in python.

Scikit learn Gaussian process RBF kernel is defined as a kernel which is a scaler or having the same number of dimensions.

Code:

In the following code, we will import some libraries from which we can make a graph with the help of the RBF kernel.

• x = num.linspace(0, 5, 100) is used to create a linspace.
• y_samples = gpr_model.sample_y(X, n_samples) is used to create a sample model.
• axis.plot() is used to plot the model.
• axis.plot(x, y_mean, color=”blue”, label=”Mean”) is used to plot the axis.
• X_train = range.uniform(0, 5, 10).reshape(-1, 1) is used create the uniform range.
• plot_gpr_samples(gaussianprocessregressor, n_samples=n_samples, axis=axs[0]) is used to plot the samples.
• axs[0].set_title(“Samples from prior distribution”) is used to give the title to the graph.
• axs[1].scatter(X_train[:, 0], y_train, color=”red”, zorder=10, label=”Observations”) is used to plot the scatter plot.
import matplotlib.pyplot as plot
import numpy as num

def plot_gpr_samples(gpr_model, n_samples, axis):

x = num.linspace(0, 5, 100)
X = x.reshape(-1, 1)

y_mean, y_std = gpr_model.predict(X, return_std=True)
y_samples = gpr_model.sample_y(X, n_samples)

for idx, single_prior in enumerate(y_samples.T):
axis.plot(
x,
single_prior,
linestyle="--",
alpha=0.7,
label=f"Sampled function #{idx + 1}",
)
axis.plot(x, y_mean, color="blue", label="Mean")
axis.fill_between(
x,
y_mean - y_std,
y_mean + y_std,
alpha=0.1,
color="blue",
label=r"$\pm$ 1 std. dev.",
)
axis.set_xlabel("x")
axis.set_ylabel("y")
axis.set_ylim([-3, 3])
range = num.random.RandomState(4)
X_train = range.uniform(0, 5, 10).reshape(-1, 1)
y_train = num.sin((X_train[:, 0] - 2.5) ** 2)
n_samples = 5
from sklearn.gaussian_process import GaussianProcessRegressor
from sklearn.gaussian_process.kernels import RBF

kernel = 1.0 * RBF(length_scale=1.0, length_scale_bounds=(1e-1, 10.0))
gaussianprocessregressor = GaussianProcessRegressor(kernel=kernel, random_state=0)

fig, axs = plot.subplots(nrows=2, sharex=True, sharey=True, figsize=(10, 8))

plot_gpr_samples(gaussianprocessregressor, n_samples=n_samples, axis=axs[0])
axs[0].set_title("Samples from prior distribution")

gpr.fit(X_train, y_train)
plot_gpr_samples(gaussianprocessregressor, n_samples=n_samples, axis=axs[1])
axs[1].scatter(X_train[:, 0], y_train, color="red", zorder=10, label="Observations")
axs[1].legend(bbox_to_anchor=(1.05, 1.5), loc="upper left")
axs[1].set_title("Samples from posterior distribution")

plot.tight_layout()

Output:

After running the above code, we get the following output in which we can see that the scikit learn Gaussian process RBF kernel graph is plotted on the screen.

Also, take a look at some more Scikit learn tutorials.

So, in this tutorial we discussed Scikit learn Gaussian and we have also covered different examples related to its implementation. Here is the list of examples that we have covered.

• Scikit learn Gaussian
• Scikit learn Gaussian mixture model
• Scikit learn Gaussian process
• Scikit learn Gaussian regression
• Scikit learn Gaussian regression example
• Scikit learn Gaussian Naive Bayes
• Scikit learn Gaussian Kernel
• Scikit learn Gaussian classifier
• Scikit learn Gaussian process classifier
• Scikit learn Gaussian process RBF Kernel