我希望得到一些帮助/指导,以找出一组范围出现在最小/最大值数组中的次数。
import numpy as np; import pandas as pd
np.random.seed(42)
df = pd.DataFrame(np.random.rand(1000, 2),
columns=["Min", "Max"]) \
.mul(6).query("Min <= Max").reset_index(drop=True)
lb = np.arange(1, 5, 0.1)
ub = np.arange(1, 5, 0.1)
a = df[['Min','Max']].to_numpy()
factor = 1
arry = []
字符串
我的第一个尝试是将min/max转换为numpy数组,并迭代所有数组以生成输出。
factor = 1
arry = []
for row in a:
for x in lb:
for y in ub:
lbExists = 0
rangeExists = 0
if y <= x:
continue
if x >= row[0] and x <= row[1]:
lbExists = 1
if x >= row[0] and y <= row[1]:
rangeExists = 1
project = 0
if lbExists == 1 and rangeExists == 0:
project -= factor
if lbExists == 1 and rangeExists == 1:
project = (y - x) * factor
arry.append([round(x, 2), round(y, 2), row[0], row[1], lbExists, rangeExists , round(project,2)])
型
输出(截断):
[[1.0, 1.1, 1.34, 5.8, 0, 0, 0],
[1.0, 1.2, 1.34, 5.8, 0, 0, 0],
[1.0, 1.3, 1.34, 5.8, 0, 0, 0],
[1.0, 1.4, 1.34, 5.8, 0, 0, 0],
[1.0, 1.5, 1.34, 5.8, 0, 0, 0],
[1.0, 1.6, 1.34, 5.8, 0, 0, 0],
[1.0, 1.7, 1.34, 5.8, 0, 0, 0],
[1.0, 1.8, 1.34, 5.8, 0, 0, 0],
[1.0, 1.9, 1.34, 5.8, 0, 0, 0],
[1.0, 2.0, 1.34, 5.8, 0, 0, 0],
...
型
然后,我想做一些分析,找出每个范围在集合中出现的次数。
有没有更好的方法来做到这一点?
1条答案
按热度按时间3duebb1j1#
一种更快的计算方法是使用各种
numpy
方法。字符串
我在整个代码中添加了注解,但请务必询问是否有任何需要澄清的地方。
时间上的差异(大约快100倍):
型
并确认这些是相同的:
型