我想知道如何键入提示 matplotlib-subplots 的轴对象的“最佳”方法。
跑步
from matplotlib import pyplot as plt
f, ax = plt.subplots()
print(type(ax))
Run Code Online (Sandbox Code Playgroud)
返回
<class 'matplotlib.axes._subplots.AxesSubplot'>
Run Code Online (Sandbox Code Playgroud)
和跑步
from matplotlib import axes
print(type(axes._subplots))
print(type(axes._subplots.AxesSubplot))
Run Code Online (Sandbox Code Playgroud)
产量
<class 'module'>
AttributeError: module 'matplotlib.axes._subplots' has no attribute 'AxesSubplots'
Run Code Online (Sandbox Code Playgroud)
到目前为止,有效的类型提示解决方案如下:
def multi_rocker(
axy: type(plt.subplots()[1]),
y_trues: np.ndarray,
y_preds: np.ndarray,
):
"""
One-Vs-All ROC-curve:
"""
fpr = dict()
tpr = dict()
roc_auc = dict()
n_classes = y_trues.shape[1]
wanted = list(range(n_classes))
for i,x in enumerate(wanted):
fpr[i], tpr[i], _ = roc_curve(y_trues[:, i], y_preds[:, i])
roc_auc[i] = round(auc(fpr[i], tpr[i]),2) …Run Code Online (Sandbox Code Playgroud) 当前示例显示在三层循环内执行 N=10 个独立操作,但不幸的是,英特尔编译器自动向量化在循环级别计算成本,当在最内层成功时,它拒绝考虑两个“外部”的向量化。 ' 循环。
一种解决方案是仅使用一个循环来表达相同的内容,其中允许但可能不鼓励使用“if-else”条件。
索引涵盖了所有可能组合的一个子集,如果有人知道它的正式名称那就太好了。
下面是 L=5(即 10 种可能的组合)的最小工作示例。生产代码大量使用了 L=6 的情况,其中有 20 个操作被执行约 100k 次(即对于长度为 6 的不同数组)。
std::cout注意:最内层循环的自动矢量化已经过测试,没有在每次迭代中使用打印:-)
注意:出于好奇,预先计算元素的(当前)策略(仅内循环矢量化,英特尔报告显示“估计潜在加速:1.8”)而不是仅在需要时动态计算它们,使该部分占 12%慢点。
#include <iostream>
int vectorizable_function(int &A, int &B, int &C) { return A + B - C; }
void populate_combinations(int *container, int L, int *data){
// Intel vectorization report provides feedback
//
//
int counter=0;
for (int i=0; i<L-2; i++){
for (int j=i+1; j<L-1; j++){
for (int k=j+1; k<L; k++){
// std::cout << "i,j,k are " << i …Run Code Online (Sandbox Code Playgroud)