【问题标题】:Python: How to install utils.metrics module?Python:如何安装 utils.metrics 模块?
【发布时间】:2021-07-21 06:58:19
【问题描述】:

我正在尝试运行一些时间序列深度学习算法,它们需要一些模块。但是,当我运行以下代码时,它给了我一个错误:

import pickle
import warnings
from math import sqrt

import lightgbm as lgb
import matplotlib as mpl
import numpy as np
import pandas as pd  # Basic library for all of our dataset operations
import pmdarima as pm
import tensorflow as tf
import xgboost as xgb
from bayes_opt import BayesianOptimization
from fbprophet import Prophet
from gluonts.dataset.common import ListDataset
from gluonts.evaluation.backtest import make_evaluation_predictions
from gluonts.model.deepar import DeepAREstimator
from gluonts.mx.trainer import Trainer
from matplotlib import pyplot as plt
from sklearn import linear_model, svm
from sklearn.ensemble import RandomForestRegressor
from sklearn.metrics import make_scorer, mean_squared_error
from sklearn.model_selection import GridSearchCV, cross_val_score
from sklearn.neighbors import KNeighborsRegressor
from sklearn.preprocessing import StandardScaler
from statsmodels.tsa.ar_model import AR
from statsmodels.tsa.arima_model import ARIMA, ARMA
from statsmodels.tsa.holtwinters import ExponentialSmoothing, SimpleExpSmoothing
from statsmodels.tsa.statespace.sarimax import SARIMAX
from tqdm import tqdm

from utils.metrics import evaluate

这是它显示的错误:

---------------------------------------------------------------------------
ModuleNotFoundError                       Traceback (most recent call last)
<ipython-input-1-b033063c2c35> in <module>
 29 from tqdm import tqdm
 30 
---> 31 from utils.metrics import evaluate

ModuleNotFoundError: No module named 'utils.metrics'

我尝试了很多天来找到 '''utils.metrics''',但我没有找到任何东西。如果您有任何解决此问题的想法,请帮助我。提前谢谢你。

编辑:这是包含代码的文件夹的样子。

【问题讨论】:

  • 你正在运行的脚本文件必须有一个utils.py文件,它们应该在同一个文件夹中
  • @Shar 谢谢,我去看看文件看看。

标签: python python-3.x jupyter-notebook


【解决方案1】:

在这里,您可以看到一个示例,说明如何构建文件夹才能获得评估功能:

https://www.programcreek.com/python/?code=jakc4103%2FDFQ%2FDFQ-master%2Futils%2Fsegmentation%2Futils.py#

如您所见,utils 文件夹中有一个metrics.py 文件,其中包含Evaluator 类。

这是一个文件夹示例:

main_folder
|_utils                 #it is a folder
  |_metrics.py
|_code1.py
|_code2.py

code1.pycode2.py 只是您可以在主文件夹中拥有的 py 代码名称的示例。

编辑

代码应该在metrics.py:

import numpy as np


class Evaluator(object):
    def __init__(self, num_class):
        self.num_class = num_class
        self.confusion_matrix = np.zeros((self.num_class,)*2)

    def Pixel_Accuracy(self):
        Acc = np.diag(self.confusion_matrix).sum() / self.confusion_matrix.sum()
        return Acc

    def Pixel_Accuracy_Class(self):
        Acc = np.diag(self.confusion_matrix) / self.confusion_matrix.sum(axis=1)
        Acc = np.nanmean(Acc)
        return Acc

    def Mean_Intersection_over_Union(self):
        MIoU = np.diag(self.confusion_matrix) / (
                    np.sum(self.confusion_matrix, axis=1) + np.sum(self.confusion_matrix, axis=0) -
                    np.diag(self.confusion_matrix))
        MIoU = np.nanmean(MIoU)
        return MIoU

    def Frequency_Weighted_Intersection_over_Union(self):
        freq = np.sum(self.confusion_matrix, axis=1) / np.sum(self.confusion_matrix)
        iu = np.diag(self.confusion_matrix) / (
                    np.sum(self.confusion_matrix, axis=1) + np.sum(self.confusion_matrix, axis=0) -
                    np.diag(self.confusion_matrix))

        FWIoU = (freq[freq > 0] * iu[freq > 0]).sum()
        return FWIoU

    def _generate_matrix(self, gt_image, pre_image):
        mask = (gt_image >= 0) & (gt_image < self.num_class)
        label = self.num_class * gt_image[mask].astype('int') + pre_image[mask]
        count = np.bincount(label, minlength=self.num_class**2)
        confusion_matrix = count.reshape(self.num_class, self.num_class)
        return confusion_matrix

    def add_batch(self, gt_image, pre_image):
        assert gt_image.shape == pre_image.shape
        self.confusion_matrix += self._generate_matrix(gt_image, pre_image)

    def reset(self):
        self.confusion_matrix = np.zeros((self.num_class,) * 2)

【讨论】:

  • 感谢您的回复。在您链接中的示例中,该文件夹已经构建。但是就我而言,虽然我从 github 下载了包含所有文件的文件夹,但我不知道如何构建它。我在 jupyter 中运行 ipynb,它仍然给我同样的错误。
  • 我会尝试在我的回答中给你一个文件夹示例
  • 如果它仍然不适合您,请给我们您的实际文件夹的捕获,以便提供更具体的支持。
  • 感谢您的帮助。我添加了我的文件夹的图片。我正在尝试运行 02-Forecasting_models.ipynb。
  • 您是否在utils 文件夹中插入了文件metrics.py?如果您这样做了,您是否插入了我在答案中包含的代码?
【解决方案2】:

如果您正在使用本教程 (https://github.com/jiwidi/time-series-forecasting-with-python) 中的 Python 代码,则必须将其 metrics.py 文档保存到您的 utils 文件夹中。

https://github.com/jiwidi/time-series-forecasting-with-python/tree/master/utils

【讨论】:

    猜你喜欢
    • 2018-11-17
    • 1970-01-01
    • 2015-09-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-03
    • 2016-09-22
    • 1970-01-01
    相关资源
    最近更新 更多