1

I am using EllipticEnvelope, which estimates my dataset's mean and covariance matrix. Now, I want to plot the multivariate normal distribution using a contour plot but I want to add a contour levels parameter based on different levels of standard deviation similar to this post

enter image description here

but I have this plot (note that the dataset is different):

enter image description here

I also read this post and this one but the answer doesn't work for me and I would like to use levels parameter of the contour plot. Here is my code:

import numpy as np
import pandas as pd
import scipy.linalg
import seaborn as sns
import matplotlib.pyplot as plt
from scipy.stats import multivariate_normal
%matplotlib inline

from sklearn.datasets import load_iris
iris = load_iris(as_frame=True)
X = iris.data

from sklearn.covariance import EllipticEnvelope
cov = EllipticEnvelope(random_state=42)
cov.fit(X)

i = 0
j = 1
mean = [cov.location_[i], cov.location_[j]]
covariance = [[cov.covariance_[i, i], cov.covariance_[i, j]], [cov.covariance_[j, i], cov.covariance_[j, j]]]
x_list = X[X.columns[i]].values
y_list = X[X.columns[j]].values
x, y = np.mgrid[x_list.min():x_list.max():.01, y_list.min():y_list.max():.01]
pos = np.dstack((x, y))
rv = multivariate_normal(mean, covariance)
z = rv.pdf(pos)

plt.figure()
plt.contour(x, y, z, cmap='RdYlGn')
plt.scatter(x_list, y_list)
plt.xlabel(X.columns[i])
plt.ylabel(X.columns[j])
plt.show()
8Simon8
  • 141
  • 9

0 Answers0