Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

k means em algorithm program in python

from sklearn.cluster import KMeans
from sklearn.mixture import GaussianMixture
import sklearn.metrics as metrics
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

names = ['Sepal_Length','Sepal_Width','Petal_Length','Petal_Width', 'Class']

dataset = pd.read_csv("8-dataset.csv", names=names)

X = dataset.iloc[:, :-1]  

label = {'Iris-setosa': 0,'Iris-versicolor': 1, 'Iris-virginica': 2} 

y = [label[c] for c in dataset.iloc[:, -1]]

plt.figure(figsize=(14,7))
colormap=np.array(['red','lime','black'])

# REAL PLOT
plt.subplot(1,3,1)
plt.title('Real')
plt.scatter(X.Petal_Length,X.Petal_Width,c=colormap[y])

# K-PLOT
model=KMeans(n_clusters=3, random_state=0).fit(X)
plt.subplot(1,3,2)
plt.title('KMeans')
plt.scatter(X.Petal_Length,X.Petal_Width,c=colormap[model.labels_])

print('The accuracy score of K-Mean: ',metrics.accuracy_score(y, model.labels_))
print('The Confusion matrixof K-Mean:
',metrics.confusion_matrix(y, model.labels_))

# GMM PLOT
gmm=GaussianMixture(n_components=3, random_state=0).fit(X)
y_cluster_gmm=gmm.predict(X)
plt.subplot(1,3,3)
plt.title('GMM Classification')
plt.scatter(X.Petal_Length,X.Petal_Width,c=colormap[y_cluster_gmm])

print('The accuracy score of EM: ',metrics.accuracy_score(y, y_cluster_gmm))
print('The Confusion matrix of EM:
 ',metrics.confusion_matrix(y, y_cluster_gmm))
Comment

PREVIOUS NEXT
Code Example
Python :: Python NumPy asarray Function Example Tuple to an array 
Python :: Python NumPy asmatrix Function Syntax 
Python :: Python NumPy asfortranarray Function Example array to fortanarray 
Python :: Python NumPy asscalar Function Syntax 
Python :: Python NumPy block Function Example by using simple array 
Python :: Python NumPy column_stack Function Example with 2d array 
Python :: how to change text in heatmap matplotlib 
Python :: TemplateDoesNotExist at / 
Python :: Pandas DataFrame 2 
Python :: max index tuple 
Python :: __div__ 
Python :: python model feature importance 
Python :: Open S3 object as string in Python 3 
Python :: pandas use 3 columns for 2d distribution 
Python :: Convertion of an array into binary using NumPy binary_repr 
Python :: python code to scan paper table to excel 
Python :: instance variables python 
Python :: geopandas gdf or df to file 
Python :: pandas impute zero 
Python :: SQL Query results in tkinter 
Python :: print(i) 
Python :: python code sample submission of codeforces 
Python :: cashier program with class python 
Python :: docstring python pycharm 
Python :: cyclic rotation python 
Python :: pandas maxima and minima for given column 
Python :: localizar la fila y columna de un dato pandas 
Python :: Python cut down OS path to certain part 
Python :: sumy library 
Python :: instaed of: newlist = [] for word in wordlist: newlist.append(word.upper()) 
ADD CONTENT
Topic
Content
Source link
Name
9+4 =