在32x32图像中使用eli5置换重要性

2024-10-02 08:20:39 发布

您现在位置:Python中文网/ 问答频道 /正文

我已经从我的培训32x32图像数据集中生成了一个keras模型(python)。现在,我创建了一个包含100个验证图像的numpy dataX:

print(dataX.shape)
(100, 32, 32, 1) 

因此,我们可以注意到,有100张来自大小为32x32和1个通道的图像。 当我运行以下代码时:

predictions = model.predict(dataX)
y_pred=predictions.argmax(axis=1).astype(int)

结果是一个(100,)形状的y_pred:我的模型正在工作,dataX具有正确的形状

因此,我想在dataX数据中使用python eli5的排列重要性。大概是这样的:

from eli5.sklearn import PermutationImportance
perm = PermutationImportance(my_model, random_state = 1).fit(dataX, y_true)

(y_true是dataX的真实标签) 但我有一个问题,因为似乎排列的重要性是期望(100,特征数)数据(而不是100,32,32,1)。可能是一个(1001024)矩阵

所以我尝试创建一个类,它可以在拟合、预测之前转换数据形状。类似这样(考虑到图像大小=32):

class Model_PI:
    def __init__(self, model):
          self.model=model

    def predict(self,X,y):
        X_=X.reshape(X.shape[0],image_size,image_size,1)
        return self.model.predict(X_,y)

    def fit(self,X,y):
        X_=X.reshape(X.shape[0],image_size,image_size,1)
        return self.model.fit(X_,y)

    def score(self, X, y, sample_weight=None):
        from sklearn.metrics import accuracy_score
        return accuracy_score(y, self.predict(X,y), sample_weight=sample_weight)

my_model=Model_PI(model) 

我用我的_模型而不是模型。但是当我尝试的时候

perm = PermutationImportance(my_model, random_state = 1).fit(dX, y_pred)

我收到以下错误消息:

TypeError                                 Traceback (most recent call last)
<ipython-input-73-2095af31af97> in <module>
----> 1 perm = PermutationImportance(my_model, random_state = 1).fit(dX, y_pred)

~/anaconda3/envs/explicabilidade/lib/python3.6/site-packages/eli5/sklearn/permutation_importance.py in fit(self, X, y, groups, **fit_params)
    200             si = self._cv_scores_importances(X, y, groups=groups, **fit_params)
    201         else:
--> 202             si = self._non_cv_scores_importances(X, y)
    203         scores, results = si
    204         self.scores_ = np.array(scores)

~/anaconda3/envs/explicabilidade/lib/python3.6/site-packages/eli5/sklearn/permutation_importance.py in _non_cv_scores_importances(self, X, y)
    224     def _non_cv_scores_importances(self, X, y):
    225         score_func = partial(self.scorer_, self.wrapped_estimator_)
--> 226         base_score, importances = self._get_score_importances(score_func, X, y)
    227         return [base_score] * len(importances), importances
    228 

~/anaconda3/envs/explicabilidade/lib/python3.6/site-packages/eli5/sklearn/permutation_importance.py in _get_score_importances(self, score_func, X, y)
    229     def _get_score_importances(self, score_func, X, y):
    230         return get_score_importances(score_func, X, y, n_iter=self.n_iter,
--> 231                                      random_state=self.rng_)
    232 
    233     @property

~/anaconda3/envs/explicabilidade/lib/python3.6/site-packages/eli5/permutation_importance.py in get_score_importances(score_func, X, y, n_iter, columns_to_shuffle, random_state)
     84     """
     85     rng = check_random_state(random_state)
---> 86     base_score = score_func(X, y)
     87     scores_decreases = []
     88     for i in range(n_iter):

~/anaconda3/envs/explicabilidade/lib/python3.6/site-packages/sklearn/metrics/_scorer.py in _passthrough_scorer(estimator, *args, **kwargs)
    369 def _passthrough_scorer(estimator, *args, **kwargs):
    370     """Function that wraps estimator.score"""
--> 371     return estimator.score(*args, **kwargs)
    372 
    373 

<ipython-input-69-eeb6ee36656d> in score(self, X, y, sample_weight)
     13     def score(self, X, y, sample_weight=None):
     14         from sklearn.metrics import accuracy_score
---> 15         return accuracy_score(y, self.predict(X,y), sample_weight=sample_weight)
     16 
     17 my_model=Model_PI(model)

<ipython-input-69-eeb6ee36656d> in predict(self, X, y)
      5     def predict(self,X,y):
      6         X_=X.reshape(X.shape[0],image_size,image_size,1)
----> 7         return self.model.predict(X_,y)
      8 
      9     def fit(self,X,y):

~/anaconda3/envs/explicabilidade/lib/python3.6/site-packages/keras/engine/training.py in predict(self, x, batch_size, verbose, steps)
   1167                                             batch_size=batch_size,
   1168                                             verbose=verbose,
-> 1169                                             steps=steps)
   1170 
   1171     def train_on_batch(self, x, y,

~/anaconda3/envs/explicabilidade/lib/python3.6/site-packages/keras/engine/training_arrays.py in predict_loop(model, f, ins, batch_size, verbose, steps)
    280         # Sample-based predictions.
    281         outs = []
--> 282         batches = make_batches(num_samples, batch_size)
    283         index_array = np.arange(num_samples)
    284         for batch_index, (batch_start, batch_end) in enumerate(batches):

~/anaconda3/envs/explicabilidade/lib/python3.6/site-packages/keras/engine/training_utils.py in make_batches(size, batch_size)
    369     num_batches = (size + batch_size - 1) // batch_size  # round up
    370     return [(i * batch_size, min(size, (i + 1) * batch_size))
--> 371             for i in range(num_batches)]
    372 
    373 

TypeError: only integer scalar arrays can be converted to a scalar index

顺便说一句,我创建了score方法,因为当我试图运行上述代码时,它是一个错误。但现在我被卡住了。 有什么想法吗?或者,是否有更好的方法使用图像(100,32,32,1大小的数据而不是1001024大小的数据)进行许可重要训练

提前谢谢


Tags: inselfsizemodelreturnlibdefbatch
1条回答
网友
1楼 · 发布于 2024-10-02 08:20:39

经过一些测试后,下面是运行良好的类代码:

class Model_PI:
    def __init__(self, model,X):
        self.model=model

    def predict(self,X):
        X_=X.reshape(X.shape[0],image_size,image_size,1)
        return self.model.predict(X_).argmax(axis=1).astype(int)

    def fit(self,X,y):
        X_=X.reshape(X.shape[0],image_size,image_size,1)
        return self.model.fit(X_,y)

    def score(self, X, y, sample_weight=None):
        result=(y==self.predict(X)).sum()/y.shape[0]
        return result


my_model=Model_PI(model,dataX)

谢谢大家

相关问题 更多 >

    热门问题