Python 如何拾取曲线下的点?
我想做的是做一个高斯函数图。然后在空间中的任意位置选取随机数,比如y=[0,1](因为它是标准化的)&x=[0200]。然后,我希望它忽略曲线上方的所有值,只保留曲线下方的值Python 如何拾取曲线下的点?,python,dataset,Python,Dataset,我想做的是做一个高斯函数图。然后在空间中的任意位置选取随机数,比如y=[0,1](因为它是标准化的)&x=[0200]。然后,我希望它忽略曲线上方的所有值,只保留曲线下方的值 import numpy import random import math import matplotlib.pyplot as plt import matplotlib.mlab as mlab from math import sqrt from numpy import
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]
我也不知道如何修复它
正如大家所看到的,我对python和编程基本上都是新手,但是非常感谢您的帮助,如果有任何问题,我会尽力回答。很难阅读您的代码。。无论如何,您不能使用
[]
访问集合,也就是说,foos[Upper]
,method[Upper]
等都是非法的。我不明白你为什么要把foo
,x
转换成set。此外,对于由方法2
产生的点,例如(x0,y0),x0很可能不存在于x
中
我对numpy不熟悉,但这就是我将为您指定的目的所做的:
def undercurve(size):
result = []
for i in xrange(size):
x = random()
y = random()
if y < scipy.stats.norm(0, 200).pdf(x): # here's the 'undercurve'
result.append((x, y))
return results
def欠曲线(尺寸):
结果=[]
对于X范围内的i(尺寸):
x=随机()
y=随机()
如果y
很难读懂你的代码。。无论如何,您不能使用[]
访问集合,也就是说,foos[Upper]
,method[Upper]
等都是非法的。我不明白你为什么要把foo
,x
转换成set。此外,对于由方法2
产生的点,例如(x0,y0),x0很可能不存在于x
中
我对numpy不熟悉,但这就是我将为您指定的目的所做的:
def undercurve(size):
result = []
for i in xrange(size):
x = random()
y = random()
if y < scipy.stats.norm(0, 200).pdf(x): # here's the 'undercurve'
result.append((x, y))
return results
def欠曲线(尺寸):
结果=[]
对于X范围内的i(尺寸):
x=随机()
y=随机()
如果y
您看到的错误的直接原因可能是这一行(应该通过完整的回溯来识别——发布这一行通常非常有用):
因为名称混乱的变量方法
实际上是一个集合
,由函数method2
返回。实际上,仔细想想,foos
也是一个set
,所以它可能在第一个方面失败了。集合不支持像集合[index]
这样的索引;这就是投诉\uuu getitem\uuu
的意思
我不完全确定代码的所有部分都打算做什么;像“foos”这样的变量名并没有真正的帮助。下面是我如何做你想做的事情:
# 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)
您看到的错误的直接原因大概是这一行(应该通过完整的回溯来识别——发布这一行通常非常有用): 因为名称混乱的变量
方法
实际上是一个集合
,由函数method2
返回。实际上,仔细想想,foos
也是一个set
,所以它可能在第一个方面失败了。集合不支持像集合[index]
这样的索引;这就是投诉\uuu getitem\uuu
的意思
我不完全确定代码的所有部分都打算做什么;像“foos”这样的变量名并没有真正的帮助。下面是我如何做你想做的事情:
# 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)
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])