如何在曲线下选取点? [英] How to pick points under the curve?

查看:180
本文介绍了如何在曲线下选取点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要做的是制作一个高斯函数图。然后在一个说y = [0,1]的空间中的任意位置选择随机数(因为其归一化)& x = [0,200]。然后,我希望它忽略曲线上方的所有值,而只将值保留在曲线下方。

What I'm trying to do is make a gaussian function graph. then pick random numbers anywhere in a space say y=[0,1] (because its normalized) & x=[0,200]. Then, I want it to ignore all values above the curve and only keep the values underneath it.

   import numpy
   import random
   import math
   import matplotlib.pyplot as plt
   import matplotlib.mlab as mlab
   from math import sqrt
   from numpy import zeros
   from numpy import numarray

   variance = input("Input variance of the star:")
   mean = input("Input mean of the star:")

   x=numpy.linspace(0,200,1000)
   sigma = sqrt(variance)

   z = max(mlab.normpdf(x,mean,sigma))
   foo = (mlab.normpdf(x,mean,sigma))/z
   plt.plot(x,foo)

   zing = random.random()
   random = random.uniform(0,200)

   import random

   def method2(size):
       ret = set()
       while len(ret) < size:
           ret.add((random.random(), random.uniform(0,200)))
       return ret

   size = input("Input number of simulations:")

   foos = set(foo)
   xx = set(x)

   method = method2(size)

   def undercurve(xx,foos,method):
       Upper = numpy.where(foos<(method))
       Lower = numpy.where(foos[Upper]>(method[Upper]))
       return (xx[Upper])[Lower],(foos[Upper])[Lower]

何时我尝试打印曲线不足,但出现错误:

When I try to print undercurve, I get an error:

    TypeError: 'set' object has no attribute '__getitem__'

,我不知道如何解决。

and I have no idea how to fix it.

大家都知道,我在python和一般编程领域还是一个新手,但是我们非常感谢您的帮助,如果有任何疑问,我会尽力而为

As you can all see, I'm quite new at python and programming in general, but any help is appreciated and if there are any questions I'll do my best to answer them.

推荐答案

您所看到的错误的直接原因可能是此行(应由完整的追溯-将其发布通常非常有用):

The immediate cause of the error you're seeing is presumably this line (which should be identified by the full traceback -- it's generally quite helpful to post that):

Lower = numpy.where(foos[Upper]>(method[Upper]))

因为变量名称方法实际上是集合,由函数 method2 返回。实际上,再考虑一下, foos 也是 set 的集合,因此它可能首先失败了。集不支持使用 the_set [index] 之类的索引;这就是对 __ getitem __ 的抱怨。

because the confusingly-named variable method is actually a set, as returned by your function method2. Actually, on second thought, foos is also a set, so it's probably failing on that first. Sets don't support indexing with something like the_set[index]; that's what the complaint about __getitem__ means.

我不完全确定代码的所有部分是什么打算做像 foos这样的变量名实际上并没有帮助。因此,这就是我可能要做的事情:

I'm not entirely sure what all the parts of your code are intended to do; variable names like "foos" don't really help like that. So here's how I might do what you're trying to do:

# generate sample points
num_pts = 500
sample_xs = np.random.uniform(0, 200, size=num_pts)
sample_ys = np.random.uniform(0, 1, size=num_pts)

# define distribution
mean = 50
sigma = 10

# figure out "normalized" pdf vals at sample points
max_pdf = mlab.normpdf(mean, mean, sigma)
sample_pdf_vals = mlab.normpdf(sample_xs, mean, sigma) / max_pdf

# which ones are under the curve?
under_curve = sample_ys < sample_pdf_vals

# get pdf vals to plot
x = np.linspace(0, 200, 1000)
pdf_vals = mlab.normpdf(x, mean, sigma) / max_pdf

# plot the samples and the curve
colors = np.array(['cyan' if b else 'red' for b in under_curve])
scatter(sample_xs, sample_ys, c=colors)
plot(x, pdf_vals)

当然,您还应该意识到,如果只希望曲线下的点,则相当于(但效率要低得多),只是从正态分布中进行采样,然后为每个样本从0到pdf值均匀地随机选择一个 y

Of course, you should also realize that if you only want the points under the curve, this is equivalent to (but much less efficient than) just sampling from the normal distribution and then randomly selecting a y for each sample uniformly from 0 to the pdf value there:

sample_xs = np.random.normal(mean, sigma, size=num_pts)
max_pdf = mlab.normpdf(mean, mean, sigma)
sample_pdf_vals = mlab.normpdf(sample_xs, mean, sigma) / max_pdf
sample_ys = np.array([np.random.uniform(0, pdf_val) for pdf_val in sample_pdf_vals])

这篇关于如何在曲线下选取点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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