Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #Libraries
- from sklearn.mixture import GaussianMixture
- from matplotlib.patches import Ellipse
- #------Just To visualise the results----#
- def draw_ellipse(position, covariance, ax=None, **kwargs):
- """Draw an ellipse with a given position and covariance"""
- ax = ax or plt.gca()
- # Convert covariance to principal axes
- if covariance.shape == (2, 2):
- U, s, Vt = np.linalg.svd(covariance)
- angle = np.degrees(np.arctan2(U[1, 0], U[0, 0]))
- width, height = 2 * np.sqrt(s)
- else:
- angle = 0
- width, height = 2 * np.sqrt(covariance)
- # Draw the Ellipse
- for nsig in range(1, 4):
- ax.add_patch(Ellipse(position, nsig * width, nsig * height,
- angle, **kwargs))
- def plot_gmm(gmm, X, label=True, ax=None):
- ax = ax or plt.gca()
- labels = gmm.fit(X).predict(X)
- if label=='r':
- ax.scatter(X[:, 0], X[:, 1], s=40, zorder=2, c=label,marker='+')
- else:
- ax.scatter(X[:, 0], X[:, 1], s=40, zorder=2,c=label,marker='_')
- ax.axis('equal')
- w_factor = 0.2 / gmm.weights_.max()
- for pos, covar, w in zip(gmm.means_, gmm.covariances_, gmm.weights_):
- draw_ellipse(pos, covar, alpha=w * w_factor)
- #Segragating the positive and negative class
- positive_class = df[df['Class']=='+'][['f1','f2']]
- negative_class = df[df['Class']=='-'][['f1','f2']]
- #Fiting the Gaussian curve
- gmm_positive = GaussianMixture(n_components=1).fit(positive_class)
- gmm_negative = GaussianMixture(n_components=1).fit(negative_class)
- #Visualising the results
- plot_gmm(gmm_positive, np.array(positive_class),label = 'r')
- plot_gmm(gmm_negative, np.array(negative_class),label = 'b')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement