python - Windows 10 和 Linux Ubuntu 16.04 在运行时间上的巨大差异,用于 ExtraTreesClassifier 训练和预测 (Python)
问题描述
我ExtraTreesClassifier
用于训练和预测。我在 Windows 10 和 Linux Ubuntu 16.04 上的相同数据集上执行相同的源代码,令人惊讶的是,我在执行时间上得到了巨大的差异。
结果 :
+---------------+------------+----------+---------- +----------+ | Mo 中的数据集 | 赢火车 | 赢得预测 | Ub 火车 | Ub Pred | +---------------+------------+----------+---------- +----------+ | 430 | 104 | 11 | 2420 | 2019 | +---------------+------------+----------+---------- +----------+ | 530 | 122 | 14 | 2948 | 第2162章 +---------------+------------+----------+---------- +----------+ | 699 | 140 | 18 | 3672 | 2500 | +---------------+------------+----------+---------- +----------+
注意:csv文件的加载时间和dataFrame的创建可以忽略不计。
源代码:
import time
import pandas as pd
import datatable as dt
import numpy as np
from sklearn.ensemble import ExtraTreesClassifier
def __init__(self):
self.ExTrCl = ExtraTreesClassifier()
def train_with_dt(self, csv_file_path):
start_0_time = time.time()
data_arn = dt.fread(csv_file_path)
end_time = time.time()
print(" time Read_csv file : ",end_time-start_0_time," s")
data_classe = np.ravel(data_arn[:,"familyId"])
del data_arn[:,"familyId"]
start_time_train = time.time()
self.ExTrCl.fit(data_arn, data_classe)
end_time = time.time()
print(" train only time : ",end_time-start_time_train, " s")
def test_groupe_score_dt(self, test_matrix, list_classes):
start_0_time = time.time()
dt_dftest = dt.Frame(np.array(test_matrix),names=self.list_motifs)
end_time = time.time()
print(" time creatind Fram dt = ",end_time-start_0_time)
result = self.ExTrCl.predict(dt_dftest)
end_time = time.time()
print(" Time pred = ",end_time-start_0_time," s")
使用的操作系统信息和库版本如下表所示。我更新了所有使用过的库。
+----------------------------------------+--------- ----------------------------------+ | 视窗 10 | Ubuntu 16.04 | | Intel i7-8550U CPU @ 1.80Ghz 1.99Ghz | Intel(R) Xeon(R) CPU E5-2660 v4 @ 2.00GHz | | cpu 核心数:4 | cpu 核心数:1 | | 64 位操作系统 | 64 位操作系统 | | RAM 16 转 | 内存 1007 +----------------------------------------+--------- ----------------------------------+ | Python 3.7.7 | Python 3.5.2 | | ------------------ | ------------- | | 生物蟒==1.77 | 生物蟒==1.73 | | 数据表==0.11.0a0+pr2536.12 | 数据表==0.10.1 | | numpy==1.19.0 | numpy==1.18.5 | | 熊猫==1.0.5 | 熊猫==0.24.2 | | pyahocorasick==1.4.0 | pyahocorasick==1.4.0 | | scikit-learn==0.23.1 | scikit-learn==0.22.2.post1 | | scipy==1.5.0 | scipy==1.4.1 | | 后缀树==0.3.0 | 后缀树==0.3.0 | +----------------------------------------+--------- ----------------------------------+
使用 cprofile :
在 6495.451 秒内进行了 1619734 次函数调用(1589052 次原始调用) 排序:内部时间 ncalls tottime percall cumtime percall filename:lineno(function) 4828 6248.349 1.294 6248.349 1.294 {内置方法numpy.core.multiarray.array} 100 130.458 1.305 130.458 1.305 {“sklearn.tree._tree.DepthFirstTreeBuilder”对象的“构建”方法} 1 48.288 48.288 48.288 48.288 {内置方法datatable.lib._datatable.gread} 2 21.834 10.917 25.749 12.874 Main.py:40(get_matrix_nbOcrrs_listStr_AhoCorasick) 2 20.747 10.374 2570.626 1285.313 模型.py:233(test_groupe_score_dt) 4365 6.476 0.001 6.476 0.001 {方法'减少''numpy.ufunc'对象} 1 5.851 5.851 6492.121 6492.121 Main.py:309(主) 6710 3.705 0.001 3.705 0.001 {“列表”对象的“复制”方法} 400 2.548 0.006 2.548 0.006 {“sklearn.tree._tree.Tree”对象的“预测”方法} 1 2.288 2.288 6495.453 6495.453 Main.py:1() 1 1.334 1.334 3889.596 3889.596 模型.py:189(train_with_dt) 400 0.827 0.002 3.628 0.009 _classes.py:880(predict_proba) 4 0.522 0.131 4936.793 1234.198 _forest.py:591(预测) 400 0.354 0.001 3.982 0.010 _forest.py:442(_accumulate_prediction) 376662 0.150 0.000 0.150 0.000 {'ahocorasick.Automaton' 对象的方法'add_word'} 803 0.120 0.000 0.120 0.000 {内置方法 marshal.loads} 2272/2260 0.070 0.000 0.144 0.000 {内置方法 builtins.__build_class__} 1081/1 0.069 0.000 6495.453 6495.453 {内置方法builtins.exec} 143/119 0.064 0.000 0.116 0.001 {内置方法_imp.create_dynamic} 2 0.046 0.023 0.046 0.023 {'ahocorasick.Automaton' 对象的方法'make_automaton'} ...ETC
谢谢您的帮助。
解决方案
我发现导致 Windows 和 Linux 之间执行时间存在巨大差异的问题。
当我对 python 包进行更新时,系统说Requirement already compatible...,所以我认为包已经更新。在@0x5453 的评论之后,我尝试再次更新包,但它不起作用,我在(Python3.4:升级熊猫不起作用)中找到了一个处理更新问题的问题。
解决方案:我在 python 3.5 旁边安装了 python 3.8,并创建了一个虚拟环境,在那里我安装了所需的最新版本的软件包。它大大提高了执行时间。
+---------------+------------+----------+---------- --------+----------------+- | Mo 中的数据集 | 赢火车 | 赢得预测 | Ub 火车 (Py3.5) | Ub Pred (3.5) | +---------------+------------+----------+---------- --------+----------------+- | 430 | 104 | 11 | 2420 (赢 x23) | 2019 (赢 x153)| +---------------+------------+----------+---------- --------+----------------+- | 530 | 122 | 14 | 2948 (赢 x24) | 2162(赢x154)| +---------------+------------+----------+---------- --------+----------------+- | 699 | 140 | 18 | 3672 (赢 x26) | 2500(赢 x204)| +---------------+------------+----------+---------- --------+----------------+- -+------------------+-----------------+ | Ub 火车 (Py3.8) | Ub Pred (Py3.8) | -+------------------+-----------------+ | 353 (赢 x3) | 270 (赢 x24) | -+------------------+-----------------+ | 771(赢 x6)| 646 (赢 x46) | -+------------------+-----------------+ | 901 (赢 x6) | 430 (赢 x23) | -+------------------+-----------------+
现在,windows 和 Linux Python 3.8 之间的剩余差异可以用所用机器的差异来解释。
推荐阅读
- swift - 更改 NSVisualEffectView 的颜色
- angular - protractor 2.5.4 E/launcher - 未创建会话:此版本的 ChromeDriver 仅支持 Chrome 版本 79
- ethereum - 无法将 Dapp 部署到 rinkeby
- laravel - Laravel:为什么 composer update 改变了 AuthenticatesUsers 特征的 username() 函数?
- arrays - 如何在 Vue.js 中收集没有重复的嵌套数据?
- javascript - 为什么 Quill 过滤其内容?
- sql - 从循环中选择具有相似名称但添加了增量编号的表列表
- nvidia - 尝试使用 nvidia 光线跟踪扩展程序跟踪三角形时得到一个立方体
- shell - shell中的大制表符分隔文件操作
- html - 如何将两个引导 col div 与左侧的第三个 col div 对齐?