<filename>odc.py
# -*- coding: utf-8 -*-
"""
Created on Thu Jun 08 19:49:40 2017

@author: Pedro
"""

import random
import numpy as np
import matplotlib.pyplot as plt

from functions import euclidianDistance

def updateZ(U, dataset):
   Z = np.zeros((U.shape[1],dataset.shape[1]))
   for i in range(U.shape[1]):
       c = 0
       for j in range(U.shape[0]):
           for l in range(dataset.shape[1]):
               Z[i,l] += U[j,i]*dataset[j,l]
           if U[j, i] == 1:
               c += 1
       if c != 0:
           Z[i] /= c
            
   return Z

def odc(k, p, Z, D, Outliers):
    for i in range(k):
        z = D[random.randint(0,D.shape[0]-1)]
        for j in range(Z.shape[1]):
            Z[i,j] = z[j]

    while True:
        Zant = Z
        
        dist = np.zeros((D.shape[0],k))
        for i in range(D.shape[0]) :
            for j in range(k):
                dist[i,j] = euclidianDistance(D[i],Z[j])
        Md = np.sum(dist)/(k*D.shape[0])
        U_interation = np.zeros((D.shape[0],k))
        for i in range(D.shape[0]):
            if i > D.shape[0] - 1:
                break
            elif np.amin(dist[i,:]) > p*Md:
                Outliers.append(D[i])
                D = np.delete(D,i,axis=0)
                U_interation = np.delete(U_interation,i,axis=0)
                dist = np.delete(dist,i,axis=0)
                i = i - 1
            else:
                U_interation[i,np.argmin(dist[i,:])] = 1                

        Z = updateZ(U_interation, D)
        if np.array_equal(Z, Zant):
            return (Z, U_interation, D, Outliers)
       
def genNewU(X, D, U, k):
    newU = np.zeros((X.shape[0],k+1))
    
    for i in range(np.shape(X)[0]):
        not_outlier = 0
    
        for j in range(np.shape(D)[0]):
            diff_attrib = 0
            
            for a in range(np.shape(D)[1]):
                if(X[i][a] != D[j][a]):
                    diff_attrib = 1
                    break
                    
            if diff_attrib == 0:
                for a in range(np.shape(U)[1]):
                    if(U[j][a] == 1):
                        newU[i][a] = 1
                        break
                
                not_outlier = 1
        
        if not_outlier == 0:
            newU[i][k] = 1
    return newU
<filename>odc.py
# -*- coding: utf-8 -*-
"""
Created on Thu Jun 08 19:49:40 2017

@author: Pedro
"""

import random
import numpy as np
import matplotlib.pyplot as plt

from functions import euclidianDistance

def updateZ(U, dataset):
   Z = np.zeros((U.shape[1],dataset.shape[1]))
   for i in range(U.shape[1]):
       c = 0
       for j in range(U.shape[0]):
           for l in range(dataset.shape[1]):
               Z[i,l] += U[j,i]*dataset[j,l]
           if U[j, i] == 1:
               c += 1
       if c != 0:
           Z[i] /= c
            
   return Z

def odc(k, p, Z, D, Outliers):
    for i in range(k):
        z = D[random.randint(0,D.shape[0]-1)]
        for j in range(Z.shape[1]):
            Z[i,j] = z[j]

    while True:
        Zant = Z
        
        dist = np.zeros((D.shape[0],k))
        for i in range(D.shape[0]) :
            for j in range(k):
                dist[i,j] = euclidianDistance(D[i],Z[j])
        Md = np.sum(dist)/(k*D.shape[0])
        U_interation = np.zeros((D.shape[0],k))
        for i in range(D.shape[0]):
            if i > D.shape[0] - 1:
                break
            elif np.amin(dist[i,:]) > p*Md:
                Outliers.append(D[i])
                D = np.delete(D,i,axis=0)
                U_interation = np.delete(U_interation,i,axis=0)
                dist = np.delete(dist,i,axis=0)
                i = i - 1
            else:
                U_interation[i,np.argmin(dist[i,:])] = 1                

        Z = updateZ(U_interation, D)
        if np.array_equal(Z, Zant):
            return (Z, U_interation, D, Outliers)
       
def genNewU(X, D, U, k):
    newU = np.zeros((X.shape[0],k+1))
    
    for i in range(np.shape(X)[0]):
        not_outlier = 0
    
        for j in range(np.shape(D)[0]):
            diff_attrib = 0
            
            for a in range(np.shape(D)[1]):
                if(X[i][a] != D[j][a]):
                    diff_attrib = 1
                    break
                    
            if diff_attrib == 0:
                for a in range(np.shape(U)[1]):
                    if(U[j][a] == 1):
                        newU[i][a] = 1
                        break
                
                not_outlier = 1
        
        if not_outlier == 0:
            newU[i][k] = 1
    return newU
# -*- coding: utf-8 -*- Created on Thu Jun 08 19:49:40 2017  @author: Pedro