当前位置: 首页>>代码示例>>Python>>正文


Python KernelDensity.sample方法代码示例

本文整理汇总了Python中sklearn.neighbors.kde.KernelDensity.sample方法的典型用法代码示例。如果您正苦于以下问题:Python KernelDensity.sample方法的具体用法?Python KernelDensity.sample怎么用?Python KernelDensity.sample使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在sklearn.neighbors.kde.KernelDensity的用法示例。


在下文中一共展示了KernelDensity.sample方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: find_max_density

# 需要导入模块: from sklearn.neighbors.kde import KernelDensity [as 别名]
# 或者: from sklearn.neighbors.kde.KernelDensity import sample [as 别名]
def find_max_density(point_list):
    point_list, _ = remove_nan(point_list)
    if point_list.shape[0] == 0:
        return [float('nan'),float('nan'),float('nan')]
    kde = KernelDensity(kernel='gaussian', bandwidth=0.2).fit(point_list)
    points = kde.sample(100000)
    prob_list = kde.score_samples(points)
    max_point = points[np.argmax(prob_list)]
    # print "max", max_point
    return max_point
开发者ID:goolygu,项目名称:ros-deep-vision,代码行数:12,代码来源:distribution.py

示例2: train_rlos

# 需要导入模块: from sklearn.neighbors.kde import KernelDensity [as 别名]
# 或者: from sklearn.neighbors.kde.KernelDensity import sample [as 别名]
def train_rlos(data, show_chart=False):
    """Train LOS estimator"""
    """Train patient LOS for triplet (sex, age, sline)"""
    freq = {}
    for row in data:
        sex = int(row["sex"])
        age = fp.split_age(int(row["age"]))
        sline = row["sline"]
        rlos = int(row["rlos"])

        if rlos == 0:
            print "RLOS equals zero for sex %d, age %d, SL %s" % (sex, age, sline)

        tuple = (sex, age, sline)
        freq.setdefault(tuple, [])
        freq[tuple].append(rlos)

    result = {}
    for tuple, train_data in freq.items():
        (sex, age, sline) = tuple
        if len(train_data) < training_threshold:
            print "Too small training set (<%d) for sex %d, age %d, SL %s. Data will be skipped. " % \
                  (training_threshold, sex, age, sline)
            continue

        X = np.array([train_data]).transpose()
        kde = KernelDensity(kernel='tophat', bandwidth=0.5).fit(X)
        kdef = lambda size: [round(l[0]) for l in kde.sample(size).tolist()]
        result[tuple] = kde

        if show_chart:
            # print "Sex=%d, Age=%d, SL=%s" % (sex, age, sline)
            # print_freq(ages)
            samples = kdef(len(train_data)) if len(train_data) < 500 else kdef(500)
            # print_freq(samples)

            # hist for train data
            plt.subplot(211)
            plt.title("RLOS train data for Sex=%d, Age=%d, SL=%s" % (sex, age, sline))
            plt.ylabel('freq')
            plt.xlabel('RLOS')
            plt.hist(train_data)

            # estimated density
            plt.subplot(212)
            plt.title("Estimated density Sex=%d, Age=%d, SL=%s" % (sex, age, sline))
            plt.ylabel('freq')
            plt.xlabel('RLOS')
            plt.hist(samples)

            plt.show()

    return result
开发者ID:andrewshir,项目名称:CollIntel,代码行数:55,代码来源:DensityEstimation.py

示例3: train_admit_count

# 需要导入模块: from sklearn.neighbors.kde import KernelDensity [as 别名]
# 或者: from sklearn.neighbors.kde.KernelDensity import sample [as 别名]
def train_admit_count(data, show_chart=False):
    """Train patient admittance number for triplet (sex, age, sline)"""
    freq = {}
    for row in data:
        sex = int(row["sex"])
        age = fp.split_age(int(row["age"]))
        sline = row["sline"]
        admit = row["admit"]

        tuple = (sex, age, sline)
        freq.setdefault(tuple, {})
        freq[tuple].setdefault(admit, 0)
        freq[tuple][admit] += 1

    result = {}
    for tuple, days in freq.items():
        (sex, age, sline) = tuple
        train_data = days.values()
        if len(train_data) < training_threshold:
            print "Too small training set (<%d) for sex %d, age %d, SL %s. Data will be skipped. " % \
                  (training_threshold, sex, age, sline)
            continue

        X = np.array([train_data]).transpose()
        kde = KernelDensity(kernel='tophat', bandwidth=0.5).fit(X)
        kdef = lambda size: [int(round(l[0])) for l in kde.sample(size).tolist()]
        result[tuple] = kde

        if show_chart:
            # print "Sex=%d, Age=%d, SL=%s" % (sex, age, sline)
            # print_freq(ages)
            samples = kdef(len(train_data)) if len(train_data) < 500 else kdef(500)
            # print_freq(samples)

            # hist for train data
            plt.subplot(211)
            plt.title("Admit count train data for Sex=%d, Age=%d, SL=%s" % (sex, age, sline))
            plt.ylabel('freq')
            plt.xlabel('admittance count')
            plt.hist(train_data)

            # estimated density
            plt.subplot(212)
            plt.title("Estimated density Sex=%d, Age=%d, SL=%s" % (sex, age, sline))
            plt.ylabel('freq')
            plt.xlabel('admittance count')
            plt.hist(samples)

            plt.show()

    return result
开发者ID:andrewshir,项目名称:CollIntel,代码行数:53,代码来源:DensityEstimation.py

示例4: train_age

# 需要导入模块: from sklearn.neighbors.kde import KernelDensity [as 别名]
# 或者: from sklearn.neighbors.kde.KernelDensity import sample [as 别名]
def train_age(data, show_chart=False):
    """Train age estimator for each SL"""
    def print_freq(data):
        freq = {}
        length = float(len(data))
        for x in data:
            xcat = fp.split_age(x)
            freq.setdefault(xcat, 0)
            freq[xcat] += 1
        for x in sorted(freq.keys()):
            print "%d: %.2f" % (x, round(freq[x]/length, 2)),
        print

    sline_ages = {}
    bad_sl = set()
    for row in data:
        sline = row['sline']
        age = int(row["age"])

        if age <= 0:
            bad_sl.add(sline)
            continue

        sline_ages.setdefault(sline, [])
        sline_ages[sline].append(age)

    for sl in bad_sl:
        print "SL=%s has age values equal or less than zero. Values were ignored" % sl


    for sline, ages in sline_ages.items():
        if len(ages) < alert_count:
            print "SL=%s has less(%d) than %d samples and will be excluded" % (sline, len(ages), alert_count)
            del sline_ages[sline]

    result = {}
    for sline,ages in sline_ages.items():
        X = np.array([ages]).transpose()
        kde = KernelDensity(kernel='tophat', bandwidth=1.0).fit(X)
        kdef = lambda size: [round(l[0]) for l in kde.sample(size).tolist()]
        result[sline] = kdef

        if show_chart:

            print "SL=%s" % sline
            print_freq(ages)
            samples = kdef(len(ages)) if len(ages) < 500 else kdef(500)
            print_freq(samples)

            # hist for train data
            plt.subplot(211)
            plt.title("Age train data for SL=%s" %(sline))
            plt.ylabel('freq')
            plt.xlabel('age category')
            plt.hist(ages)

            # estimated density
            plt.subplot(212)
            plt.title("Estimated density %s" % sline)
            plt.ylabel('freq')
            plt.xlabel('age category')
            plt.hist(samples)

            plt.show()
    return result
开发者ID:andrewshir,项目名称:CollIntel,代码行数:67,代码来源:DensityEstimation.py

示例5: max

# 需要导入模块: from sklearn.neighbors.kde import KernelDensity [as 别名]
# 或者: from sklearn.neighbors.kde.KernelDensity import sample [as 别名]
counts = np.array(counts).reshape(-1, 1)
data['day'] = [s[:2] for s in data.Date]
day = np.array(data['day']).reshape(-1, 1)
trans = np.array(data.Amount).reshape(-1, 1)

X1 = np.linspace(0, 120, 1000)[:, np.newaxis]
X2 = np.linspace(0, 32, 1000)[:, np.newaxis]
X3 = np.linspace(min(trans), max(trans)+1, 1000)[:, np.newaxis]
for kernel in ['gaussian', 'tophat']:
    kde1 = KernelDensity(kernel=kernel, bandwidth=5).fit(counts)
    kde2 = KernelDensity(kernel=kernel, bandwidth=5).fit(day)
    kde3 = KernelDensity(kernel=kernel, bandwidth=5).fit(trans) 
    log_dens1 = kde1.score_samples(X1)
    log_dens2 = kde2.score_samples(X2)
    log_dens3 = kde3.score_samples(X3)
    samples = int(kde1.sample(1)[0][0])
    print('There are', samples, 'transactions', '\n')
    num_days = kde2.sample(samples)
    for m in range(len(num_days)):
        num_days[m] = int(round(num_days[m][0]))
        while num_days[m] <= 0 or num_days[m] > 31:
            num_days[m] = int(round(kde2.sample(1)[0][0]))           
    print('The days are:')
    print(num_days, '\n')
    num_trans = kde3.sample(samples)
    print('The transactions are:')
    print(num_trans, '\n')

    #Plotting density of number of transactions in a month
    plt.plot(X1[:, 0], np.exp(log_dens1), '-',
            label="kernel = '{0}'".format(kernel))
开发者ID:ivanezeigbo,项目名称:statistics,代码行数:33,代码来源:assign5.py


注:本文中的sklearn.neighbors.kde.KernelDensity.sample方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。