Python:数据离散化 - 等宽及等频

       在处理数据时,我们往往需要将连续性变量进行离散化,最常用的方式便是等宽离散化,等频离散化,在此处我们讨论离散化的概念,只给出在python中的实现以供参考

1. 等宽离散化

   使用pandas中的cut()函数进行划分

import numpy as np
import pandas as pd

# Discretization: Equal Width #
# Datas: Sample * Feature
def Discretization_EqualWidth(K, Datas, FeatureNumber):
    DisDatas = np.zeros_like(Datas)
    for i in range(FeatureNumber):
        DisOneFeature = pd.cut(Datas[:, i], K, labels=range(1, K+1))
        DisDatas[:, i] = DisOneFeature
    return DisDatas

2. 等频离散化

    pandas中有qcut()可以使用,但是边界易出现重复值,如果为了删除重复值设置 duplicates=‘drop’,则易出现于分片个数少于指定个数的问题,因此在此处不使用qcut()

import numpy as np
import pandas as pd

# Discretization: Equal Frequency #
# vector: single feature
def Rank_qcut(vector, K):
    quantile = np.array([float(i) / K for i in range(K + 1)])  # Quantile: K+1 values
    funBounder = lambda x: (quantile >= x).argmax()
    return vector.rank(pct=True).apply(funBounder)

# Discretization: Equal Frequency #
# Datas: Sample * Feature
def Discretization_EqualFrequency(K, Datas, FeatureNumber):
    DisDatas = np.zeros_like(Datas)
    w = [float(i) / K for i in range(K + 1)]
    for i in range(FeatureNumber):
        DisOneFeature = Rank_qcut(pd.Series(Datas[:, i]), K)
        #print(DisOneFeature)
        DisDatas[:, i] = DisOneFeature
    return DisDatas

 

你可能感兴趣的:(Python,Python,数据离散化)