Mittwoch, 29. Mai 2019

Künstliche Intelligenz SourceCode Python Teil-11

import numpy as np
import matplotlib.pyplot as plt
def bubbleSetNormal(mx,my,number,s):
    x = np.random.normal(0, s, number) + mx
    y = np.random.normal(0, s, number) + my
    return(x,y)
def fourBalls(n1,n2,n3,n4):
    np.random.seed(42)       
    dataset = np.zeros( (n1+n2+n3+n4,2) )   
    (dataset[0:n1,0],dataset[0:n1,1])         = bubbleSetNormal( 2.5, 1.0,n1,0.5)    
    (dataset[n1:n1+n2,0],dataset[n1:n1+n2,1]) = bubbleSetNormal( 2.0,-3.0,n2,0.3)   
    (dataset[n1+n2:n1+n2+n3,0],dataset[n1+n2:n1+n2+n3,1]) = bubbleSetNormal(-2.0, 5.0,n3,0.6)  
    (dataset[n1+n2+n3:n1+n2+n3+n4,0],dataset[n1+n2+n3:n1+n2+n3+n4,1]) = bubbleSetNormal(-4.0,-1.0,n4,0.9)       
    return (dataset)   
plt.close('all')
n1=n2=n3=n4=400
# Testbeispiel 1
XBalls = fourBalls(n1,n2,n3,n4)
fig = plt.figure(1)
ax = fig.add_subplot(1,1,1)
ax.scatter(XBalls[0:n1,0]  ,XBalls[0:n1,1],c='red',s=60,alpha=0.2,marker='*')
ax.scatter(XBalls[n1:n1+n2,0],XBalls[n1:n1+n2,1],c='blue',s=60,alpha=0.2,marker='s')
ax.scatter(XBalls[n1+n2:n1+n2+n3,0],XBalls[n1+n2:n1+n2+n3,1],c='black',s=60,alpha=0.2,marker='<')
ax.scatter(XBalls[n1+n2+n3:n1+n2+n3+n4,0],XBalls[n1+n2+n3:n1+n2+n3+n4,1],c='green',s=60,alpha=0.2,marker='o')
ax.set_xlabel('x')
ax.set_ylabel('y')
ax.grid(True,linestyle='-',color='0.75')
ax.set_aspect('equal', 'datalim')
ax.set_title("Vier gleich grosse Mengen")
n1=n2=100
n3=n4=400
# Testbeispiel 2
XBalls2 = fourBalls(n1,n2,n3,n4)
fig = plt.figure(2)
ax = fig.add_subplot(1,1,1)
ax.scatter(XBalls2[0:n1,0]  ,XBalls2[0:n1,1],c='red',s=60,alpha=0.2,marker='*')
ax.scatter(XBalls2[n1:n1+n2,0],XBalls2[n1:n1+n2,1],c='blue',s=60,alpha=0.2,marker='s')
ax.scatter(XBalls2[n1+n2:n1+n2+n3,0],XBalls2[n1+n2:n1+n2+n3,1],c='black',s=60,alpha=0.2,marker='<')
ax.scatter(XBalls2[n1+n2+n3:n1+n2+n3+n4,0],XBalls2[n1+n2+n3:n1+n2+n3+n4,1],c='green',s=60,alpha=0.2,marker='o')
ax.set_xlabel('x')
ax.set_ylabel('y')
ax.grid(True,linestyle='-',color='0.75')
ax.set_aspect('equal', 'datalim')
ax.set_title("Vier unterschiedlich grosse Mengen")
def mouseShape():
    np.random.seed(42)       
    dataset = np.zeros( (1000,2) )   
    (dataset[0:150,0],dataset[0:150,1])       = bubbleSetNormal(-0.75, 0.75,150,0.15)    
    (dataset[150:300,0],dataset[150:300,1])   = bubbleSetNormal( 0.75, 0.75,150,0.15)   
    (dataset[300:1000,0],dataset[300:1000,1]) = bubbleSetNormal( 0, 0,700,0.29)  
    return (dataset)
# Testbeispiel 3
XBMouse = mouseShape()
fig = plt.figure(3)
ax = fig.add_subplot(1,1,1)
ax.scatter(XBMouse[0:150,0]  ,XBMouse[0:150,1],c='red',s=60,alpha=0.2,marker='*')
ax.scatter(XBMouse[150:300,0],XBMouse[150:300,1],c='blue',s=60,alpha=0.2,marker='s')
ax.scatter(XBMouse[300:1000,0],XBMouse[300:1000,1],c='black',s=60,alpha=0.2,marker='<')
ax.set_xlabel('x')
ax.set_ylabel('y')
ax.grid(True,linestyle='-',color='0.75')
ax.set_aspect('equal', 'datalim')
ax.set_title("Mouse Set")
def twoMoonsProblem( SamplesPerMoon=240, pNoise=2):
    np.random.seed(42)
    tMoon0 = np.linspace(0, np.pi, SamplesPerMoon)
    tMoon1 = np.linspace(0, np.pi, SamplesPerMoon)
    Moon0x = np.cos(tMoon0)
    Moon0y = np.sin(tMoon0)
    Moon1x = 1 - np.cos(tMoon1)
    Moon1y = 0.5 - np.sin(tMoon1)
    X = np.vstack((np.append(Moon0x, Moon1x), np.append(Moon0y, Moon1y))).T
    X = X + pNoise/100*np.random.normal(size=X.shape)
    Y = np.hstack([np.zeros(SamplesPerMoon), np.ones(SamplesPerMoon)])
    return X, Y
# Testbeispiel 4
(XMoons,_) = twoMoonsProblem()
fig = plt.figure(4)
ax = fig.add_subplot(1,1,1)
ax.scatter(XMoons[0:240,0]  ,XMoons[0:240,1],c='red',s=60,alpha=0.2,marker='*')
ax.scatter(XMoons[240:480,0],XMoons[240:480,1],c='black',s=60,alpha=0.2,marker='<')
ax.set_xlabel('x')
ax.set_ylabel('y')
ax.grid(True,linestyle='-',color='0.75')
ax.set_aspect('equal', 'datalim')
ax.set_title("Two Moons Set")
def circels():
    np.random.seed(42)
    phi = np.linspace(0,2*np.pi, 800)
    x1 = 1.5*np.cos(phi)
    y1 = 1.5*np.sin(phi)
    x2 = 0.5*np.cos(phi)
    y2 = 0.5*np.sin(phi)
    X = np.vstack((np.append(x1,x2), np.append(y1,y2))).T
    X = X + 0.1*np.random.normal(size=X.shape)
    return(X)
# Testbeispiel 5   
Xcircels = circels()
fig = plt.figure(5)
ax = fig.add_subplot(1,1,1)
ax.scatter(Xcircels[0:800,0]   ,Xcircels[0:800,1],c='red',s=60,alpha=0.2,marker='*')
ax.scatter(Xcircels[800:1600,0],Xcircels[800:1600,1],c='black',s=60,alpha=0.2,marker='<')
ax.set_xlabel('x')
ax.set_ylabel('y')
ax.grid(True,linestyle='-',color='0.75')
ax.set_aspect('equal', 'datalim')
ax.set_title("Zwei Kreise")
   
np.random.seed(42)
# Testbeispiel 6   
XRauschen = np.random.random( (1000,2) )
fig = plt.figure(6)
ax = fig.add_subplot(1,1,1)
ax.scatter(XRauschen[0:1000,0]   ,XRauschen[0:1000,1],c='red',s=60,alpha=0.2,marker='*')
ax.set_xlabel('x')
ax.set_ylabel('y')
ax.grid(True,linestyle='-',color='0.75')
ax.set_aspect('equal', 'datalim')
ax.set_title("Rauschen")

def kmeans(X, noOfClusters,maxIterations=42,p=2):
    Xmin = X.min(axis=0)
    Xmax = X.max(axis=0)
    newcentres = np.random.rand(noOfClusters,X.shape[1])*(Xmax - Xmin)+Xmin
    oldCentres = np.random.rand(noOfClusters,X.shape[1])*(Xmax - Xmin)+Xmin
    count = 0
   
    while np.sum(np.sum(oldCentres-newcentres))!= 0 and count        count = count + 1
        oldCentres = np.copy(newcentres)
        distances = ( np.sum( np.abs(X-newcentres[0,:])**p,axis=1) )**(1/p)
        for j in range(1,noOfClusters):
            distancesAdd = ( np.sum( np.abs(X-newcentres[j,:])**p,axis=1) )**(1/p)
            distances = np.vstack( (distances,distancesAdd) )
        cluster = distances.argmin(axis=0)

        for j in range(noOfClusters):
            index = np.flatnonzero(cluster == j)   
            if index.shape[0]>0:
                newcentres[j,:] = np.sum(X[index,:],axis=0)/index.shape[0]
            else:
                distances = ( np.sum( (X-newcentres[j,:])**p,axis=1) )**(1/p)
                i = distances.argmin(axis=0)
                newcentres[j,:] = X[i,:]
                cluster[i]=j
    return(newcentres,cluster)
np.random.seed(13456)
(newcentres,cluster) = kmeans(XBalls2,4,p=2)
print(newcentres)
fig = plt.figure(7)
ax = fig.add_subplot(1,1,1)
markers=['*', 's', '<', 'o', 'X', '8', 'p', 'h', 'H', 'D', 'd', 'P']
colorName = ['red','blue','black','green']
for j in range(4):
    index = np.flatnonzero(cluster == j)
    ax.scatter(XBalls2[index,0]  ,XBalls2[index,1],c=colorName[j],s=60,alpha=0.2,marker=markers[j])
ax.set_xlabel('x')
ax.set_ylabel('y')
ax.grid(True,linestyle='-',color='0.75')
ax.set_aspect('equal', 'datalim')
::::::::::::::::::::::::::::::::::::::::::::::::::::::
import numpy as np
from scipy.spatial import KDTree
class DBSCAN:
    def __init__(self, X, p=2, leafSize=30):
        self.p = p
        self.leafSize = leafSize
        self.X = X
        self.kdTree = KDTree(self.X, leafsize=self.leafSize)
    def fit_predict(self,eps=0.5, MinPts=5):
        self.eps = eps
        self.MinPts = MinPts
        C = 0
        self.visited = np.zeros(self.X.shape[0],dtype=bool)
        self.clusters = -10*np.ones(self.X.shape[0],dtype=int)
        for i in range(self.visited.shape[0]):
            if not self.visited[i]:
                self.visited[i] = True
                P = self.X[i,:]
                N = np.array(self.kdTree.query_ball_point(P, self.eps, p=self.p))
                if N.shape[0] < self.MinPts:
                    self.clusters[i] = -1
                else:
                    C = C+ 1
                    self.visited[i] = C
                    self._expandCluster(N, C)
        return self.clusters
   
    def _expandCluster(self,N, C):
        elements = N.shape[0]
        j = 0
        while j < elements:
            i = N[j]
            if not self.visited[i]:
                self.visited[i] = True
                NExpend =np.array(self.kdTree.query_ball_point(self.X[i,:],self.eps,p=self.p))
                if NExpend.shape[0] >= self.MinPts:
                    N = np.hstack( (N,NExpend) )
                    elements = N.shape[0]
            if self.clusters[i]<0: br="">                self.clusters[i] = C
            j = j + 1
           
    def analyseEps(self,MinPts=5):
        (d,_) = self.kdTree.query(self.X,k=MinPts, p=self.p)
        d = np.max(d,axis=1)
        return d
           
if __name__ == '__main__':
   
    def twoMoonsProblem( SamplesPerMoon=240, pNoise=2):
        np.random.seed(42)
        tMoon0 = np.linspace(0, np.pi, SamplesPerMoon)
        tMoon1 = np.linspace(0, np.pi, SamplesPerMoon)
        Moon0x = np.cos(tMoon0)
        Moon0y = np.sin(tMoon0)
        Moon1x = 1 - np.cos(tMoon1)
        Moon1y = 0.5 - np.sin(tMoon1)
        X = np.vstack((np.append(Moon0x, Moon1x), np.append(Moon0y, Moon1y))).T
        X = X + pNoise/100*np.random.normal(size=X.shape)
        Y = np.hstack([np.zeros(SamplesPerMoon), np.ones(SamplesPerMoon)])
        return X, Y
    (XMoons,_) = twoMoonsProblem()
   
    clusterAlg = DBSCAN(XMoons)
   
    import matplotlib.pyplot as plt
    d = clusterAlg.analyseEps()
    fig = plt.figure(1)
    ax = fig.add_subplot(1,1,1)
    ax.hist(d,10,normed=1, facecolor='k', alpha=0.5)
    ax.set_xlabel('Distanz')
    
    c = clusterAlg.fit_predict(eps=0.07,MinPts=5)
   
    index = np.flatnonzero(c == -1)
    print(index.shape[0]/c.shape[0])
   
    markers=['*', 's', '<', 'o', 'X', '^', 'h', 'H', 'D', 'd', 'P']
    colorName = ['red','black','blue','green', 'c', 'm', 'y']  
    fig = plt.figure(2)
    ax = fig.add_subplot(1,1,1)
   
    for i in range(0,c.max()):
        index = np.flatnonzero(c == i+1)
        ax.scatter(XMoons[index,0],XMoons[index,1],c=colorName[i],s=60,alpha=0.2,marker=markers[i])
    ax.set_xlabel('x')
    ax.set_ylabel('y')
    ax.grid(True,linestyle='-',color='0.75')
    ax.set_aspect('equal', 'datalim')
    ax.set_title("Two Moons Set")
   
   
   
   
   
:::::::::::::::::::::::::::::::::::::::::::::
import numpy as np
import matplotlib.pyplot as plt
from dbscan import DBSCAN
def bubbleSetNormal(mx,my,number,s):
    x = np.random.normal(0, s, number) + mx
    y = np.random.normal(0, s, number) + my
    return(x,y)
def fourBalls(n1,n2,n3,n4):
    np.random.seed(42)       
    dataset = np.zeros( (n1+n2+n3+n4,2) )   
    (dataset[0:n1,0],dataset[0:n1,1])     = bubbleSetNormal( 2.5, 1.0,n1,0.5)    
    (dataset[n1:n1+n2,0],dataset[n1:n1+n2,1]) = bubbleSetNormal( 2.0,-3.0,n2,0.3)   
    (dataset[n1+n2:n1+n2+n3,0],dataset[n1+n2:n1+n2+n3,1]) = bubbleSetNormal(-2.0, 5.0,n3,0.6)  
    (dataset[n1+n2+n3:n1+n2+n3+n4,0],dataset[n1+n2+n3:n1+n2+n3+n4,1]) = bubbleSetNormal(-4.0,-1.0,n4,0.9)       
    return (dataset)   
markers=['*', 's', '<', 'o', 'X', '^', 'h', 'H', 'D', 'd', 'P']
colorName = ['red','black','blue','green', 'c', 'm', 'y']   
plt.close('all')
n1=n2=n3=n4=400
# Testbeispiel 1
XBalls = fourBalls(n1,n2,n3,n4)
clusterAlg = DBSCAN(XBalls)
d = clusterAlg.analyseEps(MinPts=10)
fig = plt.figure(1)
ax = fig.add_subplot(1,1,1)
ax.hist(d,10,normed=1, facecolor='k', alpha=0.5)
ax.set_xlabel('Distanz')

c = clusterAlg.fit_predict(eps=0.4,MinPts=10)
index = np.flatnonzero(c == -1)
print(index.shape[0]/c.shape[0])
fig = plt.figure(2)
ax = fig.add_subplot(1,1,1)
for i in range(0,c.max()):
    index = np.flatnonzero(c == i+1)
    ax.scatter(XBalls[index,0],XBalls[index,1],c=colorName[i],s=60,alpha=0.2,marker=markers[i])
ax.set_title('eps=0.4, MinPts=10 und 3.8% Rauschen')
# --------------------------------------------------------------
n1=n2=100
n3=n4=400
# Testbeispiel 2
XBalls2 = fourBalls(n1,n2,n3,n4)

clusterAlg = DBSCAN(XBalls2)
d = clusterAlg.analyseEps(MinPts=10)
fig = plt.figure(3)
ax = fig.add_subplot(1,1,1)
ax.hist(d,5,normed=1, facecolor='k', alpha=0.5)
ax.set_xlabel('Distanz')

c = clusterAlg.fit_predict(eps=0.4,MinPts=10)
index = np.flatnonzero(c == -1)
print(index.shape[0]/c.shape[0])
fig = plt.figure(4)
ax = fig.add_subplot(1,1,1)
for i in range(0,c.max()):
    index = np.flatnonzero(c == i+1)
    ax.scatter(XBalls2[index,0],XBalls2[index,1],c=colorName[i],s=60,alpha=0.2,marker=markers[i])
ax.set_title('eps=0.4, MinPts=10 und 6.9% Rauschen')
# --------------------------------------------------------------
def mouseShape():
    np.random.seed(42)       
    dataset = np.zeros( (1000,2) )   
    (dataset[0:150,0],dataset[0:150,1])     = bubbleSetNormal(-0.75, 0.75,150,0.15)    
    (dataset[150:300,0],dataset[150:300,1]) = bubbleSetNormal( 0.75, 0.75,150,0.15)   
    (dataset[300:1000,0],dataset[300:1000,1]) = bubbleSetNormal( 0, 0,700,0.29)  
    return (dataset)
# Testbeispiel 3
XBMouse = mouseShape()
clusterAlg = DBSCAN(XBMouse)
d = clusterAlg.analyseEps(MinPts=10)
fig = plt.figure(5)
ax = fig.add_subplot(1,1,1)
ax.hist(d,5,normed=1, facecolor='k', alpha=0.5)
ax.set_xlabel('Distanz')

c = clusterAlg.fit_predict(eps=0.1,MinPts=10)
index = np.flatnonzero(c == -1)
print(index.shape[0]/c.shape[0])
fig = plt.figure(6)
ax = fig.add_subplot(1,1,1)
for i in range(0,c.max()):
    index = np.flatnonzero(c == i+1)
    ax.scatter(XBMouse[index,0],XBMouse[index,1],c=colorName[i],s=60,alpha=0.2,marker=markers[i])
ax.set_title('eps=0.1, MinPts=10 und 11.5% Rauschen')
# --------------------------------------------------------------
def twoMoonsProblem( SamplesPerMoon=240, pNoise=2):
    np.random.seed(42)
    tMoon0 = np.linspace(0, np.pi, SamplesPerMoon)
    tMoon1 = np.linspace(0, np.pi, SamplesPerMoon)
    Moon0x = np.cos(tMoon0)
    Moon0y = np.sin(tMoon0)
    Moon1x = 1 - np.cos(tMoon1)
    Moon1y = 0.5 - np.sin(tMoon1)
    X = np.vstack((np.append(Moon0x, Moon1x), np.append(Moon0y, Moon1y))).T
    X = X + pNoise/100*np.random.normal(size=X.shape)
    Y = np.hstack([np.zeros(SamplesPerMoon), np.ones(SamplesPerMoon)])
    return X, Y
(XMoons,_) = twoMoonsProblem()
clusterAlg = DBSCAN(XMoons)
d = clusterAlg.analyseEps(MinPts=5)
fig = plt.figure(7)
ax = fig.add_subplot(1,1,1)
ax.hist(d,5,normed=1, facecolor='k', alpha=0.5)
ax.set_xlabel('Distanz')

c = clusterAlg.fit_predict(eps=0.08,MinPts=5)
index = np.flatnonzero(c == -1)
print(index.shape[0]/c.shape[0])
fig = plt.figure(8)
ax = fig.add_subplot(1,1,1)
for i in range(0,c.max()):
    index = np.flatnonzero(c == i+1)
    ax.scatter(XMoons[index,0],XMoons[index,1],c=colorName[i],s=60,alpha=0.2,marker=markers[i])
ax.set_title('eps=0.08, MinPts=5 und 0.0% Rauschen')
# --------------------------------------------------------------
def circels():
    np.random.seed(42)
    phi = np.linspace(0,2*np.pi, 800)
    x1 = 1.5*np.cos(phi)
    y1 = 1.5*np.sin(phi)
    x2 = 0.5*np.cos(phi)
    y2 = 0.5*np.sin(phi)
    X = np.vstack((np.append(x1,x2), np.append(y1,y2))).T
    X = X + 0.1*np.random.normal(size=X.shape)
    return(X)
# Testbeispiel 5   
Xcircels = circels()
clusterAlg = DBSCAN(Xcircels)
d = clusterAlg.analyseEps(MinPts=10)
fig = plt.figure(9)
ax = fig.add_subplot(1,1,1)
ax.hist(d,5,normed=1, facecolor='k', alpha=0.5)
ax.set_xlabel('Distanz')

c = clusterAlg.fit_predict(eps=0.15,MinPts=10)
index = np.flatnonzero(c == -1)
print(index.shape[0]/c.shape[0])
fig = plt.figure(10)
ax = fig.add_subplot(1,1,1)
for i in range(0,c.max()):
    index = np.flatnonzero(c == i+1)
    ax.scatter(Xcircels[index,0],Xcircels[index,1],c=colorName[i],s=60,alpha=0.2,marker=markers[i])
ax.set_title('eps=0.15, MinPts=10 und 0.6% Rauschen')
# --------------------------------------------------------------
np.random.seed(42)
# Testbeispiel 6   
XRauschen = np.random.random( (1000,2) )
clusterAlg = DBSCAN(XRauschen)
d = clusterAlg.analyseEps(MinPts=10)
fig = plt.figure(11)
ax = fig.add_subplot(1,1,1)
ax.hist(d,5,normed=1, facecolor='k', alpha=0.5)
ax.set_xlabel('Distanz')

c = clusterAlg.fit_predict(eps=0.08,MinPts=10)
index = np.flatnonzero(c == -1)
print(index.shape[0]/c.shape[0])
fig = plt.figure(12)
ax = fig.add_subplot(1,1,1)
for i in range(0,c.max()):
    index = np.flatnonzero(c == i+1)
    ax.scatter(XRauschen[index,0],XRauschen[index,1],c=colorName[i],s=60,alpha=0.2,marker=markers[i])
ax.set_title('eps=0.08, MinPts=10 und 0.0% Rauschen')

::::::::::::::::::::::::::::::::::::::::::::
import numpy as np
class fuzzyCmeans:
    def __init__(self, noOfClusters ,p=2):
        self.noOfClusters = noOfClusters
        self.p = p
        self.fitHistory = []
       
    def _computeDistances(self,X,centres):
        distances = ( np.sum( np.abs(X-centres[0,:])**self.p,axis=1) )**(1/self.p)
        for j in range(1,self.noOfClusters):
            distancesAdd = ( np.sum( np.abs(X-centres[j,:])**self.p,axis=1) )**(1/self.p)
            distances = np.vstack( (distances,distancesAdd) )
        return(distances)
       
    def fit(self, X, maxIterations=42, vareps=10**-3):
        Xmin = X.min(axis=0)
        Xmax = X.max(axis=0)
        newcentres = np.random.rand(self.noOfClusters,X.shape[1])*(Xmax - Xmin)+Xmin
        oldCentres = newcentres + 1
        count = 0
        while np.sum(np.abs(oldCentres-newcentres)) > vareps and count            count = count + 1
            oldCentres = np.copy(newcentres)
            self.fitHistory.append(newcentres.copy())
           
            distances =self._computeDistances(X,newcentres) #*\label{code:fuzzyCmeans:0} 
            d2 = distances**2 #*\label{code:fuzzyCmeans:1}
            d2Sum = np.sum(1/d2,axis=0) #*\label{code:fuzzyCmeans:2}
            W = d2*d2Sum #*\label{code:fuzzyCmeans:3}
            W = 1/W #*\label{code:fuzzyCmeans:4}
            WSum = np.sum(W**2,axis=1) #*\label{code:fuzzyCmeans:5}
            omega = ((W**2).T/WSum).T #*\label{code:fuzzyCmeans:6}
            newcentres = omega@X
       
        self.fitHistory.append(newcentres.copy())
        self.centers = newcentres
        return(newcentres,W.T)
       
    def predict(self,X):
        distances =self._computeDistances(X,self.centres)
        cluster = distances.argmin(axis=0)
        return cluster
   
if __name__ == '__main__':
    def bubbleSetNormal(mx,my,number,s):
        x = np.random.normal(0, s, number) + mx
        y = np.random.normal(0, s, number) + my
        return(x,y)
   
    def mouseShape():
        np.random.seed(42)       
        dataset = np.zeros( (1000,2) )   
        (dataset[0:150,0],dataset[0:150,1])     = bubbleSetNormal(-0.75, 0.75,150,0.15)    
        (dataset[150:300,0],dataset[150:300,1]) = bubbleSetNormal( 0.75, 0.75,150,0.15)   
        (dataset[300:1000,0],dataset[300:1000,1]) = bubbleSetNormal( 0, 0,700,0.29)  
        return (dataset)
    # Testbeispiel 3
    X = mouseShape()
    noOfClusters  = 3
    cAlgo = fuzzyCmeans(noOfClusters)
    newcentres, W = cAlgo.fit(X)
   
    cluster = np.argmax(W,axis=1)
   
    import matplotlib.pyplot as plt
   
    fig = plt.figure()  
    ax = fig.add_subplot(1,1,1)
    ax.scatter(X[:,0]  ,X[:,1],c=W[:,0],s=60, cmap='binary') 
    ax.set_xlabel('x')
    ax.set_ylabel('y')
    ax.grid(True,linestyle='-',color='0.75')
    ax.set_aspect('equal', 'datalim')
   
    fig = plt.figure()  
    ax = fig.add_subplot(1,1,1)
    ax.scatter(X[:,0]  ,X[:,1],c=W[:,1],s=60, cmap='binary') 
    ax.set_xlabel('x')
    ax.set_ylabel('y')
    ax.grid(True,linestyle='-',color='0.75')
    ax.set_aspect('equal', 'datalim')
   
    fig = plt.figure()  
    ax = fig.add_subplot(1,1,1)
    ax.scatter(X[:,0]  ,X[:,1],c=W[:,2],s=60, cmap='binary') 
    ax.set_xlabel('x')
    ax.set_ylabel('y')
    ax.grid(True,linestyle='-',color='0.75')
    ax.set_aspect('equal', 'datalim')
   
   


::::::::::::::::::::::::::::::::::::::::::::::::
import numpy as np
import matplotlib.pyplot as plt
from fuzzyCmeans import fuzzyCmeans
def bubbleSetNormal(mx,my,number,s):
    x = np.random.normal(0, s, number) + mx
    y = np.random.normal(0, s, number) + my
    return(x,y)
def fourBalls(n1,n2,n3,n4):
    np.random.seed(42)       
    dataset = np.zeros( (n1+n2+n3+n4,2) )   
    (dataset[0:n1,0],dataset[0:n1,1])     = bubbleSetNormal( 2.5, 1.0,n1,0.5)    
    (dataset[n1:n1+n2,0],dataset[n1:n1+n2,1]) = bubbleSetNormal( 2.0,-3.0,n2,0.3)   
    (dataset[n1+n2:n1+n2+n3,0],dataset[n1+n2:n1+n2+n3,1]) = bubbleSetNormal(-2.0, 5.0,n3,0.6)  
    (dataset[n1+n2+n3:n1+n2+n3+n4,0],dataset[n1+n2+n3:n1+n2+n3+n4,1]) = bubbleSetNormal(-4.0,-1.0,n4,0.9)       
    return (dataset)   
np.random.seed(42)
markers=['o', 'v', '^', '<', '>', '8', 's', 'p', '*', 'h', 'H', 'D', 'd', 'P', 'X']
colorName = ['k', 'r', 'y', 'b', 'g', 'r', 'c', 'm',  'k', 'w','b', 'g', 'r', 'c', 'm', 'y']  
plt.close('all')
n1=n2=n3=n4=400
# Testbeispiel 1
XBalls = fourBalls(n1,n2,n3,n4)
clusterAlg= fuzzyCmeans(4)
(newcentres, W) = clusterAlg.fit(XBalls)
c = np.argmax(W,axis=1)
fig = plt.figure(1)
ax = fig.add_subplot(1,1,1)
for i in range(0,c.max()+1):
    index = np.flatnonzero(c == i)
    ax.scatter(XBalls[index,0],XBalls[index,1],c=colorName[i],s=60,alpha=0.2,marker=markers[i])
ax.set_title('noOfClusters = 4')
# --------------------------------------------------------------
n1=n2=100
n3=n4=400
# Testbeispiel 2
XBalls2 = fourBalls(n1,n2,n3,n4)
clusterAlg = fuzzyCmeans(4)
(newcentres, W) = clusterAlg.fit(XBalls2)
c = np.argmax(W,axis=1)
fig = plt.figure(4)
ax = fig.add_subplot(1,1,1)
for i in range(0,c.max()+1):
    index = np.flatnonzero(c == i)
    ax.scatter(XBalls2[index,0],XBalls2[index,1],c=colorName[i],s=60,alpha=0.2,marker=markers[i])
ax.set_title('noOfClusters = 4')
# --------------------------------------------------------------
def mouseShape():
    np.random.seed(42)       
    dataset = np.zeros( (1000,2) )   
    (dataset[0:150,0],dataset[0:150,1])     = bubbleSetNormal(-0.75, 0.75,150,0.15)    
    (dataset[150:300,0],dataset[150:300,1]) = bubbleSetNormal( 0.75, 0.75,150,0.15)   
    (dataset[300:1000,0],dataset[300:1000,1]) = bubbleSetNormal( 0, 0,700,0.29)  
    return (dataset)
# Testbeispiel 3
XBMouse = mouseShape()
clusterAlg = fuzzyCmeans(3)
(newcentres, W) = clusterAlg.fit(XBMouse)
c = np.argmax(W,axis=1)
fig = plt.figure(6)
ax = fig.add_subplot(1,1,1)
for i in range(0,c.max()+1):
    index = np.flatnonzero(c == i)
    ax.scatter(XBMouse[index,0],XBMouse[index,1],c=colorName[i],s=60,alpha=0.2,marker=markers[i])
ax.set_title('noOfClusters = 3')
# --------------------------------------------------------------
def twoMoonsProblem( SamplesPerMoon=240, pNoise=2):
    np.random.seed(42)
    tMoon0 = np.linspace(0, np.pi, SamplesPerMoon)
    tMoon1 = np.linspace(0, np.pi, SamplesPerMoon)
    Moon0x = np.cos(tMoon0)
    Moon0y = np.sin(tMoon0)
    Moon1x = 1 - np.cos(tMoon1)
    Moon1y = 0.5 - np.sin(tMoon1)
    X = np.vstack((np.append(Moon0x, Moon1x), np.append(Moon0y, Moon1y))).T
    X = X + pNoise/100*np.random.normal(size=X.shape)
    Y = np.hstack([np.zeros(SamplesPerMoon), np.ones(SamplesPerMoon)])
    return X, Y
(XMoons,_) = twoMoonsProblem()
clusterAlg = fuzzyCmeans(2)
(newcentres, W) = clusterAlg.fit(XMoons)
c = np.argmax(W,axis=1)
fig = plt.figure(8)
ax = fig.add_subplot(1,1,1)
for i in range(0,c.max()+1):
    index = np.flatnonzero(c == i)
    ax.scatter(XMoons[index,0],XMoons[index,1],c=colorName[i],s=60,alpha=0.2,marker=markers[i])
ax.set_title('noOfClusters = 2')
# --------------------------------------------------------------
def circels():
    np.random.seed(42)
    phi = np.linspace(0,2*np.pi, 800)
    x1 = 1.5*np.cos(phi)
    y1 = 1.5*np.sin(phi)
    x2 = 0.5*np.cos(phi)
    y2 = 0.5*np.sin(phi)
    X = np.vstack((np.append(x1,x2), np.append(y1,y2))).T
    X = X + 0.1*np.random.normal(size=X.shape)
    return(X)
# Testbeispiel 5   
Xcircels = circels()
clusterAlg = fuzzyCmeans(2)
(newcentres, W) = clusterAlg.fit(Xcircels)
c = np.argmax(W,axis=1)
fig = plt.figure(10)
ax = fig.add_subplot(1,1,1)
for i in range(0,c.max()+1):
    index = np.flatnonzero(c == i)
    ax.scatter(Xcircels[index,0],Xcircels[index,1],c=colorName[i],s=60,alpha=0.2,marker=markers[i])
ax.set_title('noOfClusters = 2')
# --------------------------------------------------------------
np.random.seed(42)
# Testbeispiel 6   
XRauschen = np.random.random( (1000,2) )
clusterAlg = fuzzyCmeans(2)
(newcentres, W) = clusterAlg.fit(XRauschen)
c = np.argmax(W,axis=1)
fig = plt.figure(12)
ax = fig.add_subplot(1,1,1)
for i in range(0,c.max()+1):
    index = np.flatnonzero(c == i)
    ax.scatter(XRauschen[index,0],XRauschen[index,1],c=colorName[i],s=60,alpha=0.2,marker=markers[i])
ax.set_title('noOfClusters = 2')

:::::::::::::::::::::::::::::::::::::::::::::::::::
import numpy as np
from matplotlib import pyplot as plt
from scipy.cluster import hierarchy
from scipy.cluster.hierarchy import dendrogram, linkage, fcluster
from scipy.spatial.distance import pdist
def bubbleSetNormal(mx,my,number,s):
    x = np.random.normal(0, s, number) + mx
    y = np.random.normal(0, s, number) + my
    return(x,y)
def fourBalls(n1,n2,n3,n4):
    np.random.seed(42)       
    dataset = np.zeros( (n1+n2+n3+n4,2) )   
    (dataset[0:n1,0],dataset[0:n1,1])     = bubbleSetNormal( 2.5, 1.0,n1,0.5)    
    (dataset[n1:n1+n2,0],dataset[n1:n1+n2,1]) = bubbleSetNormal( 2.0,-3.0,n2,0.3)   
    (dataset[n1+n2:n1+n2+n3,0],dataset[n1+n2:n1+n2+n3,1]) = bubbleSetNormal(-2.0, 5.0,n3,0.6)  
    (dataset[n1+n2+n3:n1+n2+n3+n4,0],dataset[n1+n2+n3:n1+n2+n3+n4,1]) = bubbleSetNormal(-4.0,-1.0,n4,0.9)       
    return (dataset)   
n1=n2=n3=n4=400
# Testbeispiel 1
X = fourBalls(n1,n2,n3,n4)
D = pdist(X, metric='euclidean')
Z = linkage(D, 'single', metric='euclidean')
plt.figure()
dendrogram(Z,truncate_mode='lastp',p=12, link_color_func=lambda k: 'k')
plt.ylabel('Distanz')
plt.gray()
plt.figure()
clusterNo = fcluster(Z, 0.5, criterion='distance')
(Number , counts) = np.unique(clusterNo,return_counts=True)
big4 = np.argsort(counts)[-4:]
denseCluster = np.flatnonzero(clusterNo==(big4[0]+1))
for i in range(1,len(big4)):
    index = np.flatnonzero(clusterNo==(big4[i]+1))
    denseCluster = np.hstack( (denseCluster,index) )   
plt.scatter(X[denseCluster,0],X[denseCluster,1], color='k')
outlierIndex = np.arange(X.shape[0])
outlierIndex = np.delete(outlierIndex, denseCluster)
plt.scatter(X[outlierIndex,0],X[outlierIndex,1], marker='+', c='r')


::::::::::::::::::::::::::::::::::::::::::::::::
import numpy as np
import matplotlib.pyplot as plt
from scipy.cluster.hierarchy import linkage, fcluster
from scipy.spatial.distance import pdist
def bubbleSetNormal(mx,my,number,s):
    x = np.random.normal(0, s, number) + mx
    y = np.random.normal(0, s, number) + my
    return(x,y)
def fourBalls(n1,n2,n3,n4):
    np.random.seed(42)       
    dataset = np.zeros( (n1+n2+n3+n4,2) )   
    (dataset[0:n1,0],dataset[0:n1,1])     = bubbleSetNormal( 2.5, 1.0,n1,0.5)    
    (dataset[n1:n1+n2,0],dataset[n1:n1+n2,1]) = bubbleSetNormal( 2.0,-3.0,n2,0.3)   
    (dataset[n1+n2:n1+n2+n3,0],dataset[n1+n2:n1+n2+n3,1]) = bubbleSetNormal(-2.0, 5.0,n3,0.6)  
    (dataset[n1+n2+n3:n1+n2+n3+n4,0],dataset[n1+n2+n3:n1+n2+n3+n4,1]) = bubbleSetNormal(-4.0,-1.0,n4,0.9)       
    return (dataset)   
myMethod = 'single'
markers=['*', 's', '<', 'o', 'X', '^', 'h', 'H', 'D', 'd', 'P']
colorName = ['red','black','blue','green', 'c', 'm', 'y']  
plt.close('all')
n1=n2=n3=n4=400
# Testbeispiel 1
XBalls = fourBalls(n1,n2,n3,n4)
D = pdist(XBalls, metric='euclidean')
Z = linkage(D, myMethod, metric='euclidean')
c = fcluster(Z,4,criterion='maxclust')
fig = plt.figure(2)
ax = fig.add_subplot(1,1,1)
for i in range(0,c.max()):
    index = np.flatnonzero(c == i+1)
    ax.scatter(XBalls[index,0],XBalls[index,1],c=colorName[i],s=60,alpha=0.2,marker=markers[i])
ax.set_title(myMethod+' mit 4 Clustern')
# --------------------------------------------------------------
n1=n2=100
n3=n4=400
# Testbeispiel 2
XBalls2 = fourBalls(n1,n2,n3,n4)

D = pdist(XBalls2, metric='euclidean')
Z = linkage(D, myMethod, metric='euclidean')
c = fcluster(Z,4,criterion='maxclust')
fig = plt.figure(4)
ax = fig.add_subplot(1,1,1)
for i in range(0,c.max()):
    index = np.flatnonzero(c == i+1)
    ax.scatter(XBalls2[index,0],XBalls2[index,1],c=colorName[i],s=60,alpha=0.2,marker=markers[i])
ax.set_title(myMethod+' mit 4 Clustern')
# --------------------------------------------------------------
def mouseShape():
    np.random.seed(42)       
    dataset = np.zeros( (1000,2) )   
    (dataset[0:150,0],dataset[0:150,1])     = bubbleSetNormal(-0.75, 0.75,150,0.15)    
    (dataset[150:300,0],dataset[150:300,1]) = bubbleSetNormal( 0.75, 0.75,150,0.15)   
    (dataset[300:1000,0],dataset[300:1000,1]) = bubbleSetNormal( 0, 0,700,0.29)  
    return (dataset)
# Testbeispiel 3
XBMouse = mouseShape()
D = pdist(XBMouse, metric='euclidean')
Z = linkage(D, myMethod, metric='euclidean')
c = fcluster(Z,3,criterion='maxclust')

fig = plt.figure(6)
ax = fig.add_subplot(1,1,1)
for i in range(0,c.max()):
    index = np.flatnonzero(c == i+1)
    ax.scatter(XBMouse[index,0],XBMouse[index,1],c=colorName[i],s=60,alpha=0.2,marker=markers[i])
ax.set_title(myMethod+' mit 3 Clustern')
# --------------------------------------------------------------
def twoMoonsProblem( SamplesPerMoon=240, pNoise=2):
    np.random.seed(42)
    tMoon0 = np.linspace(0, np.pi, SamplesPerMoon)
    tMoon1 = np.linspace(0, np.pi, SamplesPerMoon)
    Moon0x = np.cos(tMoon0)
    Moon0y = np.sin(tMoon0)
    Moon1x = 1 - np.cos(tMoon1)
    Moon1y = 0.5 - np.sin(tMoon1)
    X = np.vstack((np.append(Moon0x, Moon1x), np.append(Moon0y, Moon1y))).T
    X = X + pNoise/100*np.random.normal(size=X.shape)
    Y = np.hstack([np.zeros(SamplesPerMoon), np.ones(SamplesPerMoon)])
    return X, Y
(XMoons,_) = twoMoonsProblem()
D = pdist(XMoons, metric='euclidean')
Z = linkage(D, myMethod, metric='euclidean')
c = fcluster(Z,2,criterion='maxclust')
fig = plt.figure(8)
ax = fig.add_subplot(1,1,1)
for i in range(0,c.max()):
    index = np.flatnonzero(c == i+1)
    ax.scatter(XMoons[index,0],XMoons[index,1],c=colorName[i],s=60,alpha=0.2,marker=markers[i])
ax.set_title(myMethod+' mit 2 Clustern')
# --------------------------------------------------------------
def circels():
    np.random.seed(42)
    phi = np.linspace(0,2*np.pi, 800)
    x1 = 1.5*np.cos(phi)
    y1 = 1.5*np.sin(phi)
    x2 = 0.5*np.cos(phi)
    y2 = 0.5*np.sin(phi)
    X = np.vstack((np.append(x1,x2), np.append(y1,y2))).T
    X = X + 0.1*np.random.normal(size=X.shape)
    return(X)
# Testbeispiel 5   
Xcircels = circels()
D = pdist(Xcircels, metric='euclidean')
Z = linkage(D, myMethod, metric='euclidean')
c = fcluster(Z,2,criterion='maxclust')
fig = plt.figure(10)
ax = fig.add_subplot(1,1,1)
for i in range(0,c.max()):
    index = np.flatnonzero(c == i+1)
    ax.scatter(Xcircels[index,0],Xcircels[index,1],c=colorName[i],s=60,alpha=0.2,marker=markers[i])
ax.set_title(myMethod+' mit 2 Clustern')
# --------------------------------------------------------------
np.random.seed(42)
# Testbeispiel 6   
XRauschen = np.random.random( (1000,2) )
Xcircels = circels()
D = pdist(XRauschen, metric='euclidean')
Z = linkage(D, myMethod, metric='euclidean')
c = fcluster(Z,2,criterion='maxclust')
fig = plt.figure(12)
ax = fig.add_subplot(1,1,1)
for i in range(0,c.max()):
    index = np.flatnonzero(c == i+1)
    ax.scatter(XRauschen[index,0],XRauschen[index,1],c=colorName[i],s=60,alpha=0.2,marker=markers[i])
ax.set_title(myMethod+' mit 2 Clustern')

::::::::::::::::::::::::::::::::::::::::::::::::::::::
import numpy as np
class kmeans:
    def __init__(self, noOfClusters ,p=2):
        self.noOfClusters = noOfClusters
        self.p = p
        self.fitHistory = []
       
    def _computeDistances(self,X,centres):
        distances = ( np.sum( np.abs(X-centres[0,:])**self.p,axis=1) )**(1/self.p)
        for j in range(1,self.noOfClusters):
            distancesAdd = ( np.sum( np.abs(X-centres[j,:])**self.p,axis=1) )**(1/self.p)
            distances = np.vstack( (distances,distancesAdd) )
        return(distances)
       
    def fit(self, X, maxIterations=42):
        Xmin = X.min(axis=0)
        Xmax = X.max(axis=0)
        newcentres = np.random.rand(self.noOfClusters,X.shape[1])*(Xmax - Xmin)+Xmin
        oldCentres = newcentres + 1
        count = 0
        while np.sum(np.abs(oldCentres-newcentres))!= 0 and count            count = count + 1
            oldCentres = np.copy(newcentres)
            self.fitHistory.append(newcentres.copy())
            distances =self._computeDistances(X,newcentres)
            cluster = distances.argmin(axis=0)
   
            for j in range(self.noOfClusters):
                index = np.flatnonzero(cluster == j)   
                if index.shape[0]>0:
                    newcentres[j,:] = np.sum(X[index,:],axis=0)/index.shape[0]
                else:
                    distances = ( np.sum( (X-newcentres[j,:])**self.p,axis=1) )**(1/self.p)
                    i = distances.argmin(axis=0)
                    newcentres[j,:] = X[i,:]
                    cluster[i]=j
        self.centers = newcentres
        return(newcentres,cluster)
       
    def predict(self,X):
        distances =self._computeDistances(X,self.centres)
        cluster = distances.argmin(axis=0)
        return cluster
   
if __name__ == '__main__':
    def bubbleSetNormal(mx,my,number,s):
        x = np.random.normal(0, s, number) + mx
        y = np.random.normal(0, s, number) + my
        return(x,y)

    def fourBalls(n1,n2,n3,n4):
        np.random.seed(42)       
        dataset = np.zeros( (n1+n2+n3+n4,2) )   
        (dataset[0:n1,0],dataset[0:n1,1])     = bubbleSetNormal( 2.5, 1.0,n1,0.5)    
        (dataset[n1:n1+n2,0],dataset[n1:n1+n2,1]) = bubbleSetNormal( 2.0,-3.0,n2,0.3)   
        (dataset[n1+n2:n1+n2+n3,0],dataset[n1+n2:n1+n2+n3,1]) = bubbleSetNormal(-2.0, 5.0,n3,0.6)  
        (dataset[n1+n2+n3:n1+n2+n3+n4,0],dataset[n1+n2+n3:n1+n2+n3+n4,1]) = bubbleSetNormal(-4.0,-1.0,n4,0.9)       
        return (dataset)   
   
    n1=n2=n3=n4=400
    # Testbeispiel 1
    X = fourBalls(n1,n2,n3,n4)
   
    noOfClusters  = 4
    cAlgo = kmeans(noOfClusters)
    newcentres,cluster = cAlgo.fit(X)
   
    import matplotlib.pyplot as plt
    fig = plt.figure(7)
    ax = fig.add_subplot(1,1,1)
    markers=['*', 's', '<', 'o', 'X', '8', 'p', 'h', 'H', 'D', 'd', 'P']
    colorName = ['black','red','blue','green']
    for j in range(noOfClusters):
        index = np.flatnonzero(cluster == j)
        ax.scatter(X[index,0]  ,X[index,1],c=colorName[j],s=60,alpha=0.2,marker=markers[j])
    for i in range(len(cAlgo.fitHistory)):
        for j in range(noOfClusters):
            ax.text(cAlgo.fitHistory[i][j,0], cAlgo.fitHistory[i][j,1], str(i), style='italic',
                    bbox={'facecolor':'white', 'alpha':0.7, 'pad':2},color=colorName[j])
            #ax.annotate(str(i),xy=(cAlgo.fitHistory[i][j,0], cAlgo.fitHistory[i][j,1]),color=colorName[j])
            #ax.scatter(cAlgo.fitHistory[i][j,0], cAlgo.fitHistory[i][j,1],c='m',s=60,marker=markers[10+j])
   
    ax.set_xlabel('x')
    ax.set_ylabel('y')
    ax.grid(True,linestyle='-',color='0.75')
    ax.set_aspect('equal', 'datalim')
   

:::::::::::::::::::::::::::::::::::::::::::::::::::
import numpy as np
import matplotlib.pyplot as plt
from kmeans import kmeans
def bubbleSetNormal(mx,my,number,s):
    x = np.random.normal(0, s, number) + mx
    y = np.random.normal(0, s, number) + my
    return(x,y)
def fourBalls(n1,n2,n3,n4):
    np.random.seed(42)       
    dataset = np.zeros( (n1+n2+n3+n4,2) )   
    (dataset[0:n1,0],dataset[0:n1,1])     = bubbleSetNormal( 2.5, 1.0,n1,0.5)    
    (dataset[n1:n1+n2,0],dataset[n1:n1+n2,1]) = bubbleSetNormal( 2.0,-3.0,n2,0.3)   
    (dataset[n1+n2:n1+n2+n3,0],dataset[n1+n2:n1+n2+n3,1]) = bubbleSetNormal(-2.0, 5.0,n3,0.6)  
    (dataset[n1+n2+n3:n1+n2+n3+n4,0],dataset[n1+n2+n3:n1+n2+n3+n4,1]) = bubbleSetNormal(-4.0,-1.0,n4,0.9)       
    return (dataset)   
np.random.seed(42)
markers=['*', 's', '<', 'o', 'X', '8', 'p', 'h', 'H', 'D', 'd', 'P']
colorName = ['red','black','blue','green']  
plt.close('all')
n1=n2=n3=n4=400
# Testbeispiel 1
XBalls = fourBalls(n1,n2,n3,n4)
clusterAlg= kmeans(4)
(_, c) = clusterAlg.fit(XBalls)
fig = plt.figure(1)
ax = fig.add_subplot(1,1,1)
for i in range(0,c.max()+1):
    index = np.flatnonzero(c == i)
    ax.scatter(XBalls[index,0],XBalls[index,1],c=colorName[i],s=60,alpha=0.2,marker=markers[i])
ax.set_title('noOfClusters = 4')
# --------------------------------------------------------------
n1=n2=100
n3=n4=400
# Testbeispiel 2
XBalls2 = fourBalls(n1,n2,n3,n4)
clusterAlg = kmeans(4)
(_, c) = clusterAlg.fit(XBalls2)
fig = plt.figure(4)
ax = fig.add_subplot(1,1,1)
for i in range(0,c.max()+1):
    index = np.flatnonzero(c == i)
    ax.scatter(XBalls2[index,0],XBalls2[index,1],c=colorName[i],s=60,alpha=0.2,marker=markers[i])
ax.set_title('noOfClusters = 4')
# --------------------------------------------------------------
def mouseShape():
    np.random.seed(42)       
    dataset = np.zeros( (1000,2) )   
    (dataset[0:150,0],dataset[0:150,1])     = bubbleSetNormal(-0.75, 0.75,150,0.15)    
    (dataset[150:300,0],dataset[150:300,1]) = bubbleSetNormal( 0.75, 0.75,150,0.15)   
    (dataset[300:1000,0],dataset[300:1000,1]) = bubbleSetNormal( 0, 0,700,0.29)  
    return (dataset)
# Testbeispiel 3
XBMouse = mouseShape()
clusterAlg = kmeans(3)
(_, c) = clusterAlg.fit(XBMouse)
fig = plt.figure(6)
ax = fig.add_subplot(1,1,1)
for i in range(0,c.max()+1):
    index = np.flatnonzero(c == i)
    ax.scatter(XBMouse[index,0],XBMouse[index,1],c=colorName[i],s=60,alpha=0.2,marker=markers[i])
ax.set_title('noOfClusters = 3')
# --------------------------------------------------------------
def twoMoonsProblem( SamplesPerMoon=240, pNoise=2):
    np.random.seed(42)
    tMoon0 = np.linspace(0, np.pi, SamplesPerMoon)
    tMoon1 = np.linspace(0, np.pi, SamplesPerMoon)
    Moon0x = np.cos(tMoon0)
    Moon0y = np.sin(tMoon0)
    Moon1x = 1 - np.cos(tMoon1)
    Moon1y = 0.5 - np.sin(tMoon1)
    X = np.vstack((np.append(Moon0x, Moon1x), np.append(Moon0y, Moon1y))).T
    X = X + pNoise/100*np.random.normal(size=X.shape)
    Y = np.hstack([np.zeros(SamplesPerMoon), np.ones(SamplesPerMoon)])
    return X, Y
(XMoons,_) = twoMoonsProblem()
clusterAlg = kmeans(2)
(_, c) = clusterAlg.fit(XMoons)
fig = plt.figure(8)
ax = fig.add_subplot(1,1,1)
for i in range(0,c.max()+1):
    index = np.flatnonzero(c == i)
    ax.scatter(XMoons[index,0],XMoons[index,1],c=colorName[i],s=60,alpha=0.2,marker=markers[i])
ax.set_title('noOfClusters = 2')
# --------------------------------------------------------------
def circels():
    np.random.seed(42)
    phi = np.linspace(0,2*np.pi, 800)
    x1 = 1.5*np.cos(phi)
    y1 = 1.5*np.sin(phi)
    x2 = 0.5*np.cos(phi)
    y2 = 0.5*np.sin(phi)
    X = np.vstack((np.append(x1,x2), np.append(y1,y2))).T
    X = X + 0.1*np.random.normal(size=X.shape)
    return(X)
# Testbeispiel 5   
Xcircels = circels()
clusterAlg = kmeans(2)
(_, c) = clusterAlg.fit(Xcircels)
fig = plt.figure(10)
ax = fig.add_subplot(1,1,1)
for i in range(0,c.max()+1):
    index = np.flatnonzero(c == i)
    ax.scatter(Xcircels[index,0],Xcircels[index,1],c=colorName[i],s=60,alpha=0.2,marker=markers[i])
ax.set_title('noOfClusters = 2')
# --------------------------------------------------------------
np.random.seed(42)
# Testbeispiel 6   
XRauschen = np.random.random( (1000,2) )
clusterAlg = kmeans(2)
(_, c) = clusterAlg.fit(XRauschen)
fig = plt.figure(12)
ax = fig.add_subplot(1,1,1)
for i in range(0,c.max()+1):
    index = np.flatnonzero(c == i)
    ax.scatter(XRauschen[index,0],XRauschen[index,1],c=colorName[i],s=60,alpha=0.2,marker=markers[i])
ax.set_title('noOfClusters = 2')
:::::::::::::::::::::::::::::::::::::::::::::::::::::


Keine Kommentare:

Kommentar veröffentlichen

Hinweis: Nur ein Mitglied dieses Blogs kann Kommentare posten.