import numpy as np import pandas as pd from pgmpy.estimators import MaximumLikelihoodEstimator from pgmpy.models import BayesianModel from pgmpy.inference import VariableElimination heartDisease = pd.read_csv('heart.csv') heartDisease = heartDisease.replace('?', np.nan) print('Sample instances from the dataset are given below:') print(heartDisease.head()) print('\nAttributes and datatypes:') print(heartDisease.dtypes) model = BayesianModel([ ('age', 'heartdisease'), ('sex', 'heartdisease'), ('exang', 'heartdisease'), ('cp', 'heartdisease'), ('heartdisease', 'restecg'), ('heartdisease', 'chol') ]) print('\nLearning CPD using Maximum Likelihood Estimators...') model.fit(heartDisease, estimator=MaximumLikelihoodEstimator) print('\nInferencing with Bayesian Network:') heartDisease_infer = VariableElimination(model) print('\n1. Probability of HeartDisease given evidence= restecg: 1') q1 = heartDisease_infer.query(variables=['heartdisease'], evidence={'restecg': 1}) print(q1) print('\n2. Probability of HeartDisease given evidence= cp: 2') q2 = heartDisease_infer.query(variables=['heartdisease'], evidence={'cp': 2}) print(q2) import matplotlib.pyplot as plt from sklearn import datasets from sklearn.cluster import KMeans from sklearn.mixture import GaussianMixture import sklearn.metrics as sm import pandas as pd import numpy as np from sklearn import preprocessing iris = datasets.load_iris() X = pd.DataFrame(iris.data, columns=['Sepal_Length', 'Sepal_Width', 'Petal_Length', 'Petal_Width']) y = pd.DataFrame(iris.target, columns=['Targets']) model = KMeans(n_clusters=3, random_state=42) model.fit(X) plt.figure(figsize=(14, 7)) colormap = np.array(['red', 'lime', 'black']) plt.subplot(1, 2, 1) plt.scatter(X.Petal_Length, X.Petal_Width, c=colormap[y.Targets], s=40) plt.title('Real Classification') plt.xlabel('Petal Length') plt.ylabel('Petal Width') plt.subplot(1, 2, 2) plt.scatter(X.Petal_Length, X.Petal_Width, c=colormap[model.labels_], s=40) plt.title('K-Means Classification') plt.xlabel('Petal Length') plt.ylabel('Petal Width') print('The accuracy score of K-Means (Adjusted Rand Index):', sm.adjusted_rand_score(y.Targets, model.labels_)) print('The Confusion matrix of K-Means:\n', sm.confusion_matrix(y.Targets, model.labels_)) scaler = preprocessing.StandardScaler() scaler.fit(X) xsa = scaler.transform(X) xs = pd.DataFrame(xsa, columns=X.columns) gmm = GaussianMixture(n_components=3, random_state=42) gmm.fit(xs) y_gmm = gmm.predict(xs) plt.figure(figsize=(14, 7)) plt.subplot(1, 2, 1) plt.scatter(X.Petal_Length, X.Petal_Width, c=colormap[y.Targets], s=40) plt.title('Real Classification') plt.xlabel('Petal Length') plt.ylabel('Petal Width') plt.subplot(1, 2, 2) plt.scatter(X.Petal_Length, X.Petal_Width, c=colormap[y_gmm], s=40) plt.title('GMM Classification') plt.xlabel('Petal Length') plt.ylabel('Petal Width') print('The accuracy score of GMM (Adjusted Rand Index):', sm.adjusted_rand_score(y.Targets, y_gmm)) print('The Confusion matrix of GMM:\n', sm.confusion_matrix(y.Targets, y_gmm)) plt.show()