IOK所以我希望能够从正常分布中选择只能介于0和1之间的值.在某些情况下,我希望能够基本上只返回一个完全随机的分布,而在其他情况下我想返回值落在高斯的形状.
目前我正在使用以下功能:
def blockedgauss(mu,sigma):
while True:
numb = random.gauss(mu,sigma)
if (numb > 0 and numb < 1):
break
return numb
Run Code Online (Sandbox Code Playgroud)
它从正态分布中选取一个值,如果它超出0到1的范围,则丢弃它,但我觉得必须有更好的方法来做到这一点.
好吧,我有六个可能的数据值,分别是'32','22','12','31','21'和'11'.我将这些存储为字符串.python是否可以对数据进行排序,只需制作六个分区并显示每个分区有多少个分区?或者直方图的输入是否为数字?
所以我有一个2D数据阵列,在同一轴上生成许多时间序列的图.目前,每条线的颜色只是循环,并没有任何意义.
我想以某种方式将每行的颜色映射到其数据的索引 - 因此一组具有低索引的数据显示为红色,然后在高索引处淡化为蓝色.
为了澄清,每条线都应该是相同的颜色,而不是随着时间的推移而消失.差异应该在每一行之间.
谢谢!
在我的代码中,我试图定期创建图形并将图形保存到文件中.代码如下所示:
import pylab as p
def simpledist(speclist,totalbugs,a):
data = [float(spec.pop)/float(totalbugs) for spec in speclist]
p.hist(data)
p.savefig('/Home/s1215235/Documents/python/newfolder/' + str(a) + '.png')
Run Code Online (Sandbox Code Playgroud)
(a是一个柜台)
但是,这样做意味着创建的每个新绘图都会一直覆盖在绘图上.我怎么能让它知道,一旦我保存了这个数字,我想让它开始一个新的数字?
编辑:我认为基本上解决了。
我正在使用scipy.stats中的spearmanr来查找许多不同样本中变量之间的相关性。我大约有2500个变量和36个样本(或“观测值”)
如果我使用全部36个样本计算相关性,则spearmanr可以正常工作。如果仅使用前18个样本,它也可以正常工作。但是,如果我使用后18个样本,则会出现错误并返回nan。
这是错误:
/Home/s1215235/.local/lib/python2.7/site-packages/numpy/lib/function_base.py:1945: RuntimeWarning: invalid value encountered in true_divide
return c / sqrt(multiply.outer(d, d))
/Home/s1215235/.local/lib/python2.7/site-packages/scipy/stats/_distn_infrastructure.py:1718: RuntimeWarning: invalid value encountered in greater
cond1 = (scale > 0) & (x > self.a) & (x < self.b)
/Home/s1215235/.local/lib/python2.7/site-packages/scipy/stats/_distn_infrastructure.py:1718: RuntimeWarning: invalid value encountered in less
cond1 = (scale > 0) & (x > self.a) & (x < self.b)
/Home/s1215235/.local/lib/python2.7/site-packages/scipy/stats/_distn_infrastructure.py:1719: RuntimeWarning: invalid value encountered in less_equal
cond2 = cond0 & (x <= self.a)
Run Code Online (Sandbox Code Playgroud)
这是代码:
populationdata = np.vstack(thing).astype(np.float)
rho, pval = stats.spearmanr(populationdata[:,sampleindexes], axis = …Run Code Online (Sandbox Code Playgroud) 有没有办法做到这一点?hist当我尝试指定base或时,该命令似乎无法识别它basey.
我有一个名为列表的列表列表hab.在这个列表列表中,我存储了一个类的元素,loc这就是为什么我没有使用numpy数组(它不存储数字).
我想通过循环遍历每个元素,用随机选择的'loc'填充每个元素.然而,似乎每当我到达行的末尾时,程序将获取最后一行元素并将其放入该行中的所有其他元素中.这意味着我最终得到的列表如下所示:
3 3 3 3 3
1 1 1 1 1
2 2 2 2 2
2 2 2 2 2
4 4 4 4 4
Run Code Online (Sandbox Code Playgroud)
实际上我希望所有这些数字都是随机的(这是打印出每个loc数字的特定特征,这就是为什么它是数字).
以下是相关的代码:
allspec=[] # a list of species
for i in range(0,initialspec):
allspec.append(species(i)) # make a new species with new index
print 'index is',allspec[i].ind, 'pref is', allspec[i].pref
hab=[[0]*xaxis]*yaxis
respect = randint(0,len(allspec)-1)
for j in range(0,yaxis):
for k in range (0,xaxis):
respect=randint(0,len(allspec)-1)
print 'new species added at ',k,j,' …Run Code Online (Sandbox Code Playgroud)