Fastest Way to generate 1,000,000+ random numbers in python

I am currently writing an app in python that needs to generate large amount of random numbers, FAST. Currently I have a scheme going that uses numpy to generate all of the numbers in a giant batch (about ~500,000 at a time). While this seems to be faster than python's implementation. I still need it to go faster. Any ideas? I'm open to writing it in C and embedding it in the program

在Python中生成1,000,000个以上随机数的最快方法

我目前正在Python中编写一个应用程序,需要生成大量的随机数,FAST。 目前我有一个计划使用numpy来生成大批量的所有数字(一次约500,000个)。 虽然这似乎比python的实现更快。 我仍然需要它走得更快。 有任何想法吗? 我愿意用C编写它,并将它嵌入到程序中或者做它需要的东西。 随机数的约束: 一组7个数字都可以有不同的界限: 例如:[0-X1,0-X2,0-X3,0-X4,0-X5,0-X6,0-X7] 目前我正在生成一个7个数字的列表,其

MemoryError in python with chunked uploading

I am writing server side for chunked uploading files. So sometimes i got MemoryError and i can`t understand what i am doing wrong. Here my python code(with framework pyramid): @view_config(route_name='fileupload',renderer='../upload.mako') def upload_file(request): session = request.session if 'authorized' in session and session['authorized'] is False: return HTTPForbidden()

在分块上传的python中的MemoryError

我正在编写分块上传文件的服务器端。 所以有时我得到了MemoryError,我不明白我做错了什么。 这里我的Python代码(与框架金字塔): @view_config(route_name='fileupload',renderer='../upload.mako') def upload_file(request): session = request.session if 'authorized' in session and session['authorized'] is False: return HTTPForbidden() try: filename = request.params.get('filena

How references to variables are resolved in Python

This message is aa bit long with many examples, but I hope it will help me and others to better grasp the full story of variables and attribute lookup in Python 2.7. I am using the terms of PEP 227 (http://www.python.org/dev/peps/pep-0227/) for code blocks (such as modules, class definition, function definitions, etc.) and variable bindings (such as assignments, argument declarations, class and

如何在Python中解析对变量的引用

这个消息有很多例子,但我希望它能帮助我和其他人更好地理解Python 2.7中变量和属性查找的全部内容。 我使用PEP 227(http://www.python.org/dev/peps/pep-0227/)的术语来描述代码块(例如模块,类定义,函数定义等)和变量绑定(比如作为赋值,参数声明,类和函数声明,for循环等) 我使用变量来表示可以不带点的名称,以及需要使用对象名称限定的名称的属性(例如对象obj的属性x的obj.x)。 Python中有三个范围用于所有

Differences between class block and function block in python

Code 1 x = 0 class Foo: print(x) x = 1 print(x) print(x) Result 1 0 1 0 Code 2 x = 0 def foo(): print(x) x = 1 print(x) foo() Result 2 UnboundLocalError: local variable 'x' referenced before assignment. Why can x reference objects from two namespaces in class block ? I don't understand why Code 1 not throw an UnboundLocalError . Inconsistency between fu

python中的类块和功能块之间的区别

代码1 x = 0 class Foo: print(x) x = 1 print(x) print(x) 结果1 0 1 0 代码2 x = 0 def foo(): print(x) x = 1 print(x) foo() 结果2 UnboundLocalError: local variable 'x' referenced before assignment. 为什么x可以在class block引用两个名称空间中的对象? 我不明白为什么Code 1不会抛出UnboundLocalError 。 函数和类之间的不一致打扰了我。 更新: 几次阅读Python文档后

PyPy: Severe performance penalty when using None in a list with integers

Because an algorithm I want to implement uses indices 1..n and because it's very error prone to shift every index by one, I decided to get smart and inserted a dummy element in the beginning of every list, so I can use original formulas from the paper. For the sake of shortness, consider this toy example: def calc(N): nums=[0]+range(1,N+1) return sum(nums[1:]) #skip first element

PyPy:在整数列表中使用None时性能会受到严重影响

因为我想要实现的算法使用索引1..n并且由于很容易将每个索引移动一位,所以我决定变得聪明,并在每个列表的开头插入一个虚拟元素,这样我就可以使用原始公式该文件。 为了简洁起见,请考虑这个玩具的例子: def calc(N): nums=[0]+range(1,N+1) return sum(nums[1:]) #skip first element 然而,我担心,我的结果是虚假的,因为我可以在某处意外地访问第0个元素,而不会意识到这一点。 所以我变得更聪明,并且使用N

Initializing Cython objects with existing C Objects

C++ Model Say I have the following C++ data structures I wish to expose to Python. #include <memory> #include <vector> struct mystruct { int a, b, c, d, e, f, g, h, i, j, k, l, m; }; typedef std::vector<std::shared_ptr<mystruct>> mystruct_list; Boost Python I can wrap these fairly effectively using boost::python with the following code, easily allowing me to us

用现有C对象初始化Cython对象

C ++模型 假设我有以下希望公开给Python的C ++数据结构。 #include <memory> #include <vector> struct mystruct { int a, b, c, d, e, f, g, h, i, j, k, l, m; }; typedef std::vector<std::shared_ptr<mystruct>> mystruct_list; 提升Python 我可以使用下面的代码使用boost :: python相当有效地包装这些,很容易让我使用现有的mystruct(复制shared_ptr)而不是重新创建一个现有的对象。

Fast matrix inversion without a package

Assume that I have a square matrix M . Assume that I would like to invert the matrix M . I am trying to use the the fractions mpq class within gmpy2 as members of my matrix M . If you are not familiar with these fractions, they are functionally similar to python's built-in package fractions . The only problem is, there are no packages that will invert my matrix unless I take them out of

没有包装的快速矩阵反演

假设我有一个方形矩阵M 假设我想反转矩阵M 我正在尝试使用gmpy2中的分数mpq类作为我的矩阵M成员。 如果你不熟悉这些分数,它们在功能上与Python的内置fractions相似。 唯一的问题是,没有任何软件包会颠倒我的矩阵,除非我将它们从分数形式中提取出来。 我需要分数形式的数字和答案。 所以我将不得不编写自己的函数来反转M 有一些我可以编程的已知算法,例如高斯消元法。 然而,表现是一个问题,所以我的问题如下:

Correct way to write

I'm experimenting with OOP python and I wasn't sure about the __repr__ function inheritance. Since the parent class function looked like this: def __repr__(self): '''Returns representation of the object''' return("{}('{}')".format("Class name", self._param)) I wanted to know if it is better to use a generic approach (which could be suitable for children classes as well) like th

正确的写作方式

我正在尝试使用OOP python,并且我不确定__repr__函数的继承。 由于父类功能如下所示: def __repr__(self): '''Returns representation of the object''' return("{}('{}')".format("Class name", self._param)) 我想知道是否最好使用通用方法(也可以适用于儿童课程),如下所示: def __repr__(self): '''Returns representation of the object''' return("{}('{}')".format(self.__class__.__name__, sel

Seaborn regplot using datetime64 as the x axis

I have a dataframe looks like this: date score 2017-06-04 90 2017-06-03 80 2017-06-02 70 When I tried this: sns.regplot(x=date, y=score, data=df) I got an error: TypeError: reduction operation 'mean' not allowed for this dtype The dtype for date is datetime64[ns] , and int64 for the score column. How can I covert the date column so that regplot will work? Seaborn does

Seaborn regplot使用datetime64作为x轴

我有一个像这样的数据框: date score 2017-06-04 90 2017-06-03 80 2017-06-02 70 当我尝试这个: sns.regplot(x=date, y=score, data=df) 我遇到了一个错误: TypeError: reduction operation 'mean' not allowed for this dtype date的dtype是datetime64[ns] , int64是score列。 我如何隐藏date栏以便regplot可以工作? Seaborn不支持regplot日期时间,但这是一个丑陋的黑客: df = df.sort_va

Python: How to get data from linked brushes in mlpd3, Bokeh, Plotly?

Using the code below I can get a 2x2 graph with 4 plots. With brushes, I can select some data points. The question I have is how do get the selected data points as a JSON array or cvs. This code uses mlpd3, but bokeh can do similar selections with brushes.. But there is no example of selecting the data points. I am trying to get selected data as object to continue processing with python. It

Python:如何从mlpd3,Bokeh,Plotly中链接的笔刷中获取数据?

使用下面的代码,我可以得到一个2x2图形和4个图表。 使用画笔,我可以选择一些数据点。 我的问题是如何将选定的数据点作为JSON数组或CVS。 此代码使用mlpd3,但散景可以使用画笔进行类似选择。但是没有选择数据点的示例。 我试图将选定的数据作为对象继续使用python进行处理。 在单元中看到数据会很高兴。 %matplotlib inline import numpy as np import matplotlib.pyplot as plt import matplotlib as mat import mpld3