文章标题

import numpy as np  
import matplotlib.pyplot as plt  
  
from sklearn.datasets import make_blobs  
  
  
def distanceNorm(Norm,D_value):  
    # initialization  
      
  
    # Norm for distance  
    if Norm == '1':  
        counter = np.absolute(D_value);  
        counter = np.sum(counter);  
    elif Norm == '2':  
        counter = np.power(D_value,2);  
        counter = np.sum(counter);  
        counter = np.sqrt(counter);  
    elif Norm == 'Infinity':  
        counter = np.absolute(D_value);  
        counter = np.max(counter);  
    else:  
        raise Exception('We will program this later......');  
  
    return counter;  
  
  
def chi(x):  
    if x < 0:  
        return 1;  
    else:  
        return 0;  
  
  
def fit(features,labels,t,distanceMethod = '2'):  
    # initialization  
    distance = np.zeros((len(labels),len(labels)));  
    distance_sort = list();  
    density = np.zeros(len(labels));  
    distance_higherDensity = np.zeros(len(labels));  
  
  
    # compute distance  
    for index_i in xrange(len(labels)):  
        for index_j in xrange(index_i+1,len(labels)):  
            D_value = features[index_i] - features[index_j];  
            distance[index_i,index_j] = distanceNorm(distanceMethod,D_value);  
            distance_sort.append(distance[index_i,index_j]);  
    distance += distance.T;  
  
    # compute optimal cutoff  
    distance_sort = np.array(distance_sort);  
    cutoff = int(np.round(distance_sort[len(distance_sort) * t]));  
  
    # computer density  
    for index_i in xrange(len(labels)):  
        distance_cutoff_i = distance[index_i] - cutoff;  
        for index_j in xrange(1,len(labels)):  
            density[index_i] += chi(distance_cutoff_i[index_j]);  
  
    # search for the max density  
    Max = np.max(density);  
    MaxIndexList = list();  
    for index_i in xrange(len(labels)):  
        if density[index_i] == Max:  
            MaxIndexList.extend([index_i]);  
  
    # computer distance_higherDensity  
    Min = 0;  
    for index_i in xrange(len(labels)):  
        if index_i in MaxIndexList:  
            distance_higherDensity[index_i] = np.max(distance[index_i]);  
            continue;  
        else:  
            Min = np.max(distance[index_i]);  
        for index_j in xrange(1,len(labels)):  
            if density[index_i] < density[index_j] and distance[index_i,index_j] < Min:  
                Min = distance[index_i,index_j];  
            else:  
                continue;  
        distance_higherDensity[index_i] = Min;  
  
    return density,distance_higherDensity,cutoff; 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值