将二维gaussian_kde输出/网格保存到csv python [英] Save 2-D gaussian_kde output/meshgrid to csv python

查看:284
本文介绍了将二维gaussian_kde输出/网格保存到csv python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是python的新手(具有纯净的.net背景),我正在使用教程可生成二维kde分析.

I am very new to python (with pure .net background) I am using this tutorial to generate 2-d kde analysis.

生成一些随机的二维数据:

Generate some random two-dimensional data:

from scipy import stats
def measure(n):
    "Measurement model, return two coupled measurements."
    m1 = np.random.normal(size=n)
    m2 = np.random.normal(scale=0.5, size=n)
    return m1+m2, m1-m2
m1, m2 = measure(2000)
xmin = m1.min()
xmax = m1.max()
ymin = m2.min()
ymax = m2.max()

对数据执行内核密度估计:

Perform a kernel density estimate on the data:

X, Y = np.mgrid[xmin:xmax:100j, ymin:ymax:100j]
positions = np.vstack([X.ravel(), Y.ravel()])
values = np.vstack([m1, m2])
kernel = stats.gaussian_kde(values)
Z = np.reshape(kernel(positions).T, X.shape)

绘制结果:

import matplotlib.pyplot as plt
fig = plt.figure()
ax = fig.add_subplot(111)
ax.imshow(np.rot90(Z), cmap=plt.cm.gist_earth_r,
          extent=[xmin, xmax, ymin, ymax])
ax.plot(m1, m2, 'k.', markersize=2)
ax.set_xlim([xmin, xmax])
ax.set_ylim([ymin, ymax])
plt.show()

我需要将其输出转换为csv或其他可解析的格式,以便可以在.net应用程序上呈现它.我看着pmeshcolor方法,它可以生成svg,但是对于大型数据集来说变得非常大.

I need its output to a csv or other parse-able format so that I can render it on .net application. I looked at pmeshcolor method it can generate svg but that becomes very huge for large data set.

我需要执行以下操作,以便可以在.net应用程序中对其进行进一步过滤

I need something like following so that I can further filter it on .net application

输入:

x1 y1  
x2 y2  
x3 y3  

...(或者它可以用逗号分隔,基本上我将从形状文件中提取此文件或直接从数据库中获取它,我主要关心的是输出)

... (or it could be comma separated, basically I will be extracting this from shape file or fetching from database directly, my major concern is on output)

预期输出

x1 y1 value_from_kde  
x2 y2 value_from_kde  
x3 y3 value_from_kde  

...其中value_from_kde是从gaussian_kde函数为该特定点输出的,我知道gaussian_kde使用网格来执行此分析,并且如果不可能实现此点对点值,则其输出也是网格,即一个矩形及其相关联值也可以接受

... where value_from_kde is output from gaussian_kde function for that specific point, I understand that gaussian_kde uses a grid to perform this analysis and its output is also grid if this point to point value is not possible, a rectangle with its associated value is also acceptable like

矩形坐标[p1 p2 p3 p4] value_from_kde

rectangle coordinate [p1 p2 p3 p4] value_from_kde

输出不得包含value_from_kde为零的记录.

output shall not have records where value_from_kde is zero.

注意:重要的是要使矩形坐标保持与输入时相同的格式,以便可以使用相同的投影来渲染我,就像我使用x,y以UTM格式输入kde一样,看起来像这样671290.9984 2727340.004

Note: It is important to keep the rectangle coordinates in same formate as it was input so that i can be rendered with the same projection like I am using x,y to input for kde in UTM format that looks like this 671290.9984 2727340.004

推荐答案

似乎您只想将生成的x,y,value输出到文件中以供以后处理(忽略零值).如果是这样,您可能不想改变Z的形状以简化输出过程(这就是我的Z1)

It seems you want to simply output the resulting x,y,value to a file for later processing (omitting zero values). If so you might want to not reshape Z for simplifying the output process (that is my Z1)

fid = open('output.csv','w')
Z1 = (kernel(positions).T, X.shape)
Z = kernel(positions).T
#for currentIndex,elem in enumerate(positions):
for currentIndex,elem in enumerate(Z):
  #if Z1[currentIneex]>0:
  s1 = '%f %f %f\n'%(positions[0][currentIndex], positions[1][currentIndex], Z[currentIndex] )

  fid.write(s1)
fid.close()

我想漏掉您的问题吗?

这篇关于将二维gaussian_kde输出/网格保存到csv python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆