天天看点

python 实现多变量核密度评估函数

</pre>问题描述:如果知道一个用户以前经常购物的地点,现在给定一个新的购物地点,如果只考虑距离因素,如何计算用户会选择该购物地点。<p></p><p></p><p>解决该问题可以通过 </p><h1 id="firstHeading" class="firstHeading"  style="background:none; font-weight:normal; margin:0px 0px 0.25em; overflow:visible; padding:0px; border-bottom:1px solid rgb(170,170,170); font-size:1.8em; line-height:1.3; font-family:"Linux Libertine",Georgia,Times,serif">Multivariate kernel density estimation 方法</h1><div><img src="https://img-blog.csdn.net/20160924213931658?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" /></div><div><img src="" alt="" /></div><div><span style="white-space:pre"></span>其中 x 和 xi 都是 地理位置,可以是二维,也可以是三维。其中xi表示已知的购物地理位置,X表示新的购物地理位置,函数计算出来的值就表示新的购物地址对用户的吸引程度。</div><div><span style="white-space:pre"></span>其中KH函数是一个核函数,可以做如下选择:</div><div><span style="white-space:pre"><img src="" alt="" /> <img src="https://img-blog.csdn.net/20160924214054691?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" /></span></div><div>       其中K() 是一个核函数,为了计算方便,可以选择<span style="color: rgb(37, 37, 37); font-family: sans-serif; font-size: 14px;">standard </span><a target=_blank target="_blank" href="https://en.wikipedia.org/wiki/Multivariate_normal_distribution" target="_blank" rel="external nofollow"  title="Multivariate normal distribution" style="text-decoration:none; color:rgb(11,0,128); font-family:sans-serif; font-size:14px">multivariate normal</a><span style="color:rgb(37,37,37); font-family:sans-serif; font-size:14px"> kernel :</span></div><div><span style="color:rgb(37,37,37); font-family:sans-serif; font-size:14px"><img src="https://img-blog.csdn.net/20160924214130786?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" /></span></div><div><span style="color:rgb(37,37,37); font-family:sans-serif; font-size:14px"><span style="white-space:pre"><img src="" alt="" /></span></span></div><div><span style="font-family:sans-serif; color:#252525"><span style="font-size:14px; white-space:pre">  在此就讲完了,下面是python的实现:</span></span></div><div><span style="font-family:sans-serif; color:#252525"><span style="font-size:14px; white-space:pre"><span style="white-space:pre"></span></span></span></div><div><span style="white-space:pre"></span><pre name="code" class="python"># -*- coding:utf-8 -*-
__author__ = 'Administrator'

import numpy as np
from scipy import stats
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

if __name__ == "__main__":
    mu = np.array([1, 10, 20])  # 一维数组
    sigma = np.matrix([[20, 10, 10], [10, 25, 1], [10, 1, 50]])   # 二维数组 协方差矩阵,该矩阵必须是对称而且是半正定的
    data = np.random.multivariate_normal(mu, sigma, 1000)   # 产生样本点,第一个参数表示样本每维的均值,第二维表示维度之间的协方差,第三个表示产生样本的个数
    values = data.T
    kde = stats.gaussian_kde(values)  # 构造多变量核密度评估函数
    density = kde(values)   # 给定一个样本点,计算该样本点的密度

    # 可视化展现
    fig, ax = plt.subplots(subplot_kw=dict(projection='3d'))
    x, y, z = values
    ax.scatter(x, y, z, c=density)
    plt.show()

    print data