草庐IT

random_integer

全部标签

python - 在 C 中模拟 Python `random.random()` 功能

我正在尝试用Python中使用的C创建完全相同的MersenneTwister(MT)。基于Lib/random.py以及阅读thedocs,似乎整个MT都是在_random中实现的,它是在C中实现的:TheunderlyingimplementationinCisbothfastandthreadsafe.通过谷歌搜索“Python_random”,我找到了thispageonGitHub这似乎正是我要找的东西,尽管它似乎不是官方的。我使用了那个源代码并剥离了除MT本身、种子函数和双重创建函数之外的所有内容。我还更改了一些类型,以便整数为32位。首先,这是许可证信息(为了安全起见)/

python - sklearn.linear_model.LogisticRegression 每次都返回不同的系数,尽管设置了 random_state

我正在拟合逻辑回归模型并将随机状态设置为固定值。每次我进行“拟合”时,我都会得到不同的系数,例如:classifier_instance.fit(train_examples_features,train_examples_labels)LogisticRegression(C=1.0,class_weight=None,dual=False,fit_intercept=True,intercept_scaling=1,penalty='l2',random_state=1,tol=0.0001)>>>classifier_instance.raw_coef_array([[0.071

python - np.random.multivariate_normal 的混淆行为

我正在使用numpy从多元正态采样,如下所示。mu=[0,0]cov=np.array([[1,0.5],[0.5,1]]).astype(np.float32)np.random.multivariate_normal(mu,cov)它给了我以下警告。RuntimeWarning:covarianceisnotpositive-semidefinite.矩阵显然是PSD。但是,当我使用np.float64数组时,它工作正常。我需要协方差矩阵为np.float32。我究竟做错了什么? 最佳答案 这是fixed2019年3月。如果您仍

python - TypeError : list indices must be integers, not str,while parsing json

提交请求后,我收到了以下json:{"type":[{"ID":"all","count":1,"references":[{"id":"Boston,MA,02118","text":"Boston,MA,02118","val":"Boston,MA,02118","type":1,"zip":"02118","city":"Boston","state":"MA","lt":"42.3369","lg":"-71.0637","s":""}]}]}我在变量j中捕获了响应并按如下方式加载它,l=json.loads(j)现在我有:>>>type(l)>>>l['type']['re

python - dtype : integer, 但 loc 返回 float

我有一个奇怪的数据集:yearfirmsagesurvival019775649180NaN219785039910NaN3197841313010.731310519794978050NaN6197939035210.774522我将前三列的dtype转换为整数:>>>df.dtypesyearint64firmsint64ageint64survivalfloat64但现在我想根据这里的索引在另一个表中搜索:idx=331otherDf.loc[df.loc[idx,'age']]Traceback(mostrecentcalllast):(...)KeyError:8.0这来自d

python - 使用 python 和 scikit-learn 的 DBSCAN : What exactly are the integer labes returned by make_blobs?

我正在尝试理解由scikit(http://scikit-learn.org/0.13/auto_examples/cluster/plot_dbscan.html)实现的DBSCAN算法的示例。我换了行X,labels_true=make_blobs(n_samples=750,centers=centers,cluster_std=0.4)使用X=my_own_data,因此我可以将自己的数据用于DBSCAN。现在,变量labels_true是make_blobs的第二个返回参数,用于计算结果的一些值,如下所示:print"Homogeneity:%0.3f"%metrics.ho

python 3 : random. 种子 () : where to call it?

我需要确保我程序中的所有随机性都是完全可复制的。我应该在哪里调用random.seed()?我认为它应该在我的main.py模块中,但它导入了碰巧使用随机函数的其他模块。我可以仔细浏览我的导入以查看哪个是第一个执行的,但是当我更改代码结构时,我将不得不记住再次重做此分析。有什么简单安全的解决方法吗? 最佳答案 在主模块的“导入部分”执行代码实际上是安全的,所以如果您不确定导入可能会或可能不会使用随机模块的其他模块,也许绕过您的种子,您当然可以使用像importrandomrandom.seed(seed_value)importso

python - 为什么 Python 在不应该的时候给我 "an integer is required"?

我的Python程序中有一个保存函数,如下所示:defSave(n):print("S3")globalBFglobalWFglobalPBListglobalPWListprint(n)File=open("C:\KingsCapture\Saves\\"+n+"\BF.txt","w")pickle.dump(BF,File)File=open("C:\KingsCapture\Saves\\"+n+"\WF.txt","w")pickle.dump(WF,File)File=open("C:\KingsCapture\Saves\\"+n+"\PBList.txt","w")pi

python - 在 Python 中创建我自己的 "integer"对象

本质上我希望能够做类似的事情:a=Integer(1)a+=1printa当然还有打印数字二作为结果。我需要创建哪些方法才能在我的Integer类中获得此行为?免责声明:我不打算将其用于“真实”,只是好奇。 最佳答案 这是一个简单且不完整的示例。查看方法__sub__、__div__等。classInteger(object):def__init__(self,val=0):self._val=int(val)def__add__(self,val):ifisinstance(val,Integer):returnInteger(s

python - 如何使用 numpy.random.rand 设置生成点的最小距离约束?

我正在尝试生成一个有效的代码来生成一些随机位置向量,然后我用它来计算一对相关函数。我想知道是否有直接的方法来限制放置在我的框中的任意两点之间允许的最小距离。我目前的代码如下:defpointRun(number,dr):"""Computethe3Dpaircorrelationfunctionforarandomdistributionof'number'particlesplacedintoa1.0x1.0x1.0box."""##Createarrayofdistancesoverwhichtocalculate.r=np.arange(0.,1.0+dr,dr)##Genera