Python 3.x 使用numpy优化python函数。where和numpy.unique
我使用一个函数来确定资源是否可以再次使用。 这是我正在使用的numpy数组Python 3.x 使用numpy优化python函数。where和numpy.unique,python-3.x,pandas,numpy,numpy-ndarray,Python 3.x,Pandas,Numpy,Numpy Ndarray,我使用一个函数来确定资源是否可以再次使用。 这是我正在使用的numpy数组 'Resource_id', 'Start_date', 'end_date', 'start_time', 'end_time', 'overload' [548, '2019-05-16', '2019-05-16', '08:45:00', '17:40:00',2], [546, '2019-05-16', '2019-05-16', '08:45:00', '17:40:00',2], [5
'Resource_id', 'Start_date', 'end_date', 'start_time', 'end_time', 'overload'
[548, '2019-05-16', '2019-05-16', '08:45:00', '17:40:00',2],
[546, '2019-05-16', '2019-05-16', '08:45:00', '17:40:00',2],
[546, '2019-05-16', '2019-05-16', '08:45:00', '17:40:00',2],
[543, '2019-05-16', '2019-05-16', '08:45:00', '17:40:00',1],
av_resource_np = resource_availability_np[np.where(
(resource_availability_np[:,1] <= '2019-05-16')
& (resource_availability_np[:,2] >= '2019-05-16')
& (resource_availability_np[:,3] <= '17:30:00')
& (resource_availability_np[:,4] >= '08:30:00'))]
'Resource_id' 'overload' 'Count'
548 2 1
546 2 2
543 1 1
重载(>=)
,则我们不能再次使用它
rejected_resources = availability_mat [np.where(availability_mat [:, 2] >= availability_mat [:, 1])]
543
和546
这就是我函数背后的主要思想,这里的问题是它占用了整个程序运行时间的60%以上,我希望您能给我一些关于如何提高效率/速度的建议。多谢各位
完整代码:
def get_available_rooms_on_date_x(date, start_time, end_time, resource_availability_np):
av_resource_np = resource_availability_np[np.where(
(resource_availability_np[:,1] <= date)
& (resource_availability_np[:,2] >= date)
& (resource_availability_np[:,3] <= end_time)
& (resource_availability_np[:,4] >= start_time))]
unique_id, count_nb = np.unique(av_resource_np[:,(0,5)], axis=0, return_counts=True)
availability_mat = np.column_stack((unique_id, count_nb ))
rejected_resources = availability_mat [np.where(availability_mat [:, 2] >= availability_mat [:, 1])]
return rejected_resources
def get_available_rooms_on_date_x(日期、开始时间、结束时间、资源可用性):
av_resource_np=资源可用性[np.where(
(资源可用性=日期)
&(资源可用性\u np[:,3]=开始时间)
unique_id,count_nb=np.unique(av_resource_np[:,(0,5)],axis=0,return_counts=True)
可用性\u mat=np.列\u堆栈((唯一\u id,计数\u nb))
拒绝的资源=可用性材料[np.其中(可用性材料[:,2]>=可用性材料[:,1])]
返回被拒绝的资源
您是否在更大的示例中对此进行了基准测试?对于更大的数据集,这很可能不再是瓶颈。是的,我这样做了,而且在更大的数据集上速度较慢。您能展示一下资源可用性\u np.dtype
是什么吗?@user7138814它的dtype('O')@Mounir,这意味着数组中的值仍然是常规python对象。您需要使用numpy数据类型来获得更高的速度,但随后可能需要对程序进行大量更改。然后,可能最好使用pandas数据帧,甚至是sqlite表。选择一个好的数据结构是一个困难的问题,它的范围比这个函数本身要广得多。但是为了进行实验,可以尝试使用资源可用性\u np.astype('U10')
并查看此函数是否会变得更快。
def get_available_rooms_on_date_x(date, start_time, end_time, resource_availability_np):
av_resource_np = resource_availability_np[np.where(
(resource_availability_np[:,1] <= date)
& (resource_availability_np[:,2] >= date)
& (resource_availability_np[:,3] <= end_time)
& (resource_availability_np[:,4] >= start_time))]
unique_id, count_nb = np.unique(av_resource_np[:,(0,5)], axis=0, return_counts=True)
availability_mat = np.column_stack((unique_id, count_nb ))
rejected_resources = availability_mat [np.where(availability_mat [:, 2] >= availability_mat [:, 1])]
return rejected_resources