Auv*_*vee 1 python datetime datetime-format python-3.x python-datetime
我在 sklearn 随机森林分类器的帮助下编写了这些代码,用于在某些数据集上进行预测和测试准确性。
代码
a = datetime.datetime.now()
y_pred=clf.predict(X_test)
b = datetime.datetime.now()
rf_time = b-a
rf_ac = metrics.accuracy_score(y_test, y_pred)
print(type(rf_time))
print('Random Forest Time Duration - ',b-a)
print("Random Forest Accuracy:",metrics.accuracy_score(y_test, y_pred))
Run Code Online (Sandbox Code Playgroud)
输出
<class 'datetime.timedelta'>
Random Forest Time Duration - 0:00:00.011969
Random Forest Accuracy: 0.6324761904761905
Run Code Online (Sandbox Code Playgroud)
在这里我得到了rf_time哪种类型datetime.timedelta。现在,我怎样才能将其转换datetime.timedelta为整数。如何从 中获取 int 值0:00:00.011969?
获取 timedelta 的通用形式UNIT是:
delta / timedelta(UNIT=1)\nRun Code Online (Sandbox Code Playgroud)\n与, , , , ,UNIT之一。dayshoursminutessecondsmicrosecondsmilliseconds
注意。这是一个除法,在 python 中总是给出一个浮点数,因此您可能需要手动转换/显示int。
几秒钟后,就有了timedelta.total_seconds()。
例子:
\ntimedelta(days=1) / timedelta(hours=1) # gives 24.0\nRun Code Online (Sandbox Code Playgroud)\n对于上面的问题,OP可以使用printtimedelta这个语句作为\xc2\xb5s中的数字。
print(\n \'Random Forest Time Duration (in \xc2\xb5s) -\',\n rf_time / datetime.timedelta(microseconds=1)\n)\nRun Code Online (Sandbox Code Playgroud)\n