简体   繁体   English

如何使用csv dictreader,matplotlib和numpy创建概率密度函数图?

[英]How to create probability density function graph using csv dictreader, matplotlib and numpy?

I'm trying to create a simple probability density function(pdf) graph using data from one column of a csv file using csv dictreader, matplotlib and numpy... 我正在尝试使用csv dictreader,matplotlib和numpy使用来自csv文件的一列的数据创建一个简单的概率密度函数(pdf)图...

Is there an easy way to use CSV DictReader combined with numpy arrays? 有没有一种简单的方法可以将CSV DictReader与numpy数组结合使用? Below is code that doesn't work. 下面是不起作用的代码。 The error message is TypeError: len() of unsized object, which I'm guessing is related to the fact that my data is not in numpy array format? 错误消息是TypeError:len()的unsized对象,我猜这与我的数据不是numpy数组格式的事实有关? Also my data has negative and positive numbers. 我的数据也有负数和正数。 Thanks in advance! 提前致谢!

import easygui
import csv
import scipy.stats
from numpy import*
from pylab import*


filename= easygui.fileopenbox(msg='Altitude outlier graph', title='select file',  filetypes=['*.csv'], default='X:\\')
alt_file=open(filename)    

x=[]
for row in csv.DictReader(alt_file):
    x.append(float(row['Dist_90m(nmi)']))

a=scipy.stats.pdf_moments(x)

prob, bins, patches= hist(a, 10,align='left',facecolor='green')

ylabel('probability density function')
show()

The line 这条线

a=scipy.stats.pdf_moments(x)

"Return[s] the Gaussian expanded pdf function given the list of central moments (first one is mean)." “返回[s]高斯扩展的pdf函数给出中心矩的列表(第一个是平均值)。”

That is to say, a is a function, and you must take its value somehow. 也就是说, a是一个函数,你必须以某种方式获取它的值。

So I modified the line: 所以我修改了这一行:

prob, bins, patches= hist([a(i/100.0) for i in xrange(0,100,1)], 10, align='left', facecolor='green')

And produced this graph with my sample data. 并使用我的样本数据生成此图表。

Now my statistics are pretty rusty, and I am not sure if you normally take a pdf over 0-1, but you can figure it out from there. 现在我的统计数据非常生疏,我不确定你是否通常将pdf超过0-1,但你可以从那里弄明白。

If you do need to go over a range of floating points, range and xrange do not produce floats, so one easy way around that is to generate large numbers and divide down; 如果你确实需要超越一系列浮点数, rangexrange不会产生浮点数,那么一个简单的方法是生成大数并分解; hence a(i/100.0) instead of a(i) for i in xrange(0, 1, 0.01) . 因此a(i/100.0)而不是a(i) for i in xrange(0, 1, 0.01)

样品

Thanks for all the help!! 谢谢你的帮助!! The following code produces a graph of the probability density function: I'm still having some issues formating it but I think this is a good start. 下面的代码生成概率密度函数的图表:我仍然遇到一些问题,但我认为这是一个好的开始。

import easygui
import csv
import scipy.stats
import numpy
from pylab import*

filename= easygui.fileopenbox(msg='Altitude outlier graph', title='select file', filetypes=['*.csv'], default='X:\\herring_schools\\')
alt_file=open(filename)    

a=[]
for row in csv.DictReader(alt_file):
    a.append(row['Dist_90m(nmi)'])
y= numpy.array(a, float)    

pdf, bins, patches=hist(y, bins=6, align='left',range=None, normed=True)
ylabel('probability density function')
xlabel('Distance from 90m contour line(nm)')
ylim([0,1])
show()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM